Skip to content

Commit

Permalink
Add Preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
michel2323 committed Apr 5, 2024
1 parent 31207e1 commit d5e8545
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ uuid = "0979c8fe-16a4-4796-9b82-89a9f10403ea"
authors = ["pedrovalerolara <valerolarap@ornl.gov>", "williamfgc <williamfgc@yahoo.com>"]
version = "0.0.1"

[deps]
Preferences = "21216c6a-2e73-6563-6e65-726566657250"

[weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand All @@ -14,6 +17,7 @@ JACCCUDA = ["CUDA"]
JACCONEAPI = ["oneAPI"]

[compat]
Preferences = "1.4.0"
julia = "1.9.0"

[extras]
Expand Down
5 changes: 4 additions & 1 deletion ext/JACCAMDGPU/JACCAMDGPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ function reduce_kernel_amdgpu_MN((M, N), red, ret)
end

function __init__()
const JACC.default_backend = ROCBackend()
if JACC.JACCPreferences.backend == "amdgpu"
const JACC.default_backend = ROCBackend()
@info "Set default backend to $(JACC.default_backend)"
end
end

end # module JACCAMDGPU
5 changes: 4 additions & 1 deletion ext/JACCCUDA/JACCCUDA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ function reduce_kernel_cuda_MN((M, N), red, ret)
end

function __init__()
const JACC.default_backend = CUDABackend()
if JACC.JACCPreferences.backend == "cuda"
const JACC.default_backend = CUDABackend()
@info "Set default backend to $(JACC.default_backend)"
end
end

end # module JACCCUDA
5 changes: 4 additions & 1 deletion ext/JACCONEAPI/JACCONEAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ function reduce_kernel_oneapi_MN((M, N), red, ret)
end

function __init__()
const JACC.default_backend = oneAPIBackend()
if JACC.JACCPreferences.backend == "oneapi"
const JACC.default_backend = oneAPIBackend()
@info "Set default backend to $(JACC.default_backend)"
end
end

end # module JACCONEAPI
6 changes: 5 additions & 1 deletion src/JACC.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module JACC

# module to set back end preferences
include("JACCPreferences.jl")
include("helper.jl")

export parallel_for, parallel_reduce, ThreadsBackend, print_default_backend
Expand Down Expand Up @@ -69,7 +70,10 @@ function parallel_reduce(::ThreadsBackend, (M, N)::Tuple{I,I}, f::F, x...) where
end

function __init__()
const JACC.default_backend = ThreadsBackend()
if JACCPreferences.backend == "threads"
const JACC.default_backend = ThreadsBackend()
@info "Set default backend to $(JACC.default_backend)"
end
end

function print_default_backend()
Expand Down
22 changes: 22 additions & 0 deletions src/JACCPreferences.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

module JACCPreferences

using Preferences

# taken from https://github.com/JuliaPackaging/Preferences.jl
function set_backend(new_backend::String)

new_backend_lc = lowercase(new_backend)
if !(new_backend_lc in ("threads", "cuda", "amdgpu", "oneapi"))
throw(ArgumentError("Invalid backend: \"$(new_backend)\""))
end

# Set it in our runtime values, as well as saving it to disk
@set_preferences!("backend" => new_backend_lc)
@info("New backend set; restart your Julia session for this change to take effect!")
end

const backend = @load_preference("backend", "threads")


end # module JACCPreferences
2 changes: 1 addition & 1 deletion test/tests_threads_perf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ using Test
for i in 1:ntimes
@time JACC.parallel_for(ThreadsBackend(), N, axpy, alpha, x_JACC, y_JACC)
end
end
end

0 comments on commit d5e8545

Please sign in to comment.