Skip to content

Commit

Permalink
Merge pull request #145 from omlins/examples-update
Browse files Browse the repository at this point in the history
Enable triggering extension without explicit import
  • Loading branch information
omlins committed Mar 14, 2024
2 parents fd7a162 + 5b830ef commit d2008e0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/ParallelKernel/Exceptions.jl
@@ -1,12 +1,13 @@
module Exceptions
export @ModuleInternalError, @MethodPluginError, @IncoherentCallError, @NotInitializedError, @NotLoadedError, @IncoherentArgumentError, @KeywordArgumentError, @ArgumentEvaluationError, @ArgumentError
export ModuleInternalError, MethodPluginError, IncoherentCallError, NotInitializedError, NotLoadedError, IncoherentArgumentError, KeywordArgumentError, ArgumentEvaluationError
export @ModuleInternalError, @MethodPluginError, @IncoherentCallError, @NotInitializedError, @NotLoadedError, @NotInstalledError, @IncoherentArgumentError, @KeywordArgumentError, @ArgumentEvaluationError, @ArgumentError
export ModuleInternalError, MethodPluginError, IncoherentCallError, NotInitializedError, NotLoadedError, NotInstalledError, IncoherentArgumentError, KeywordArgumentError, ArgumentEvaluationError

macro ModuleInternalError(msg) esc(:(throw(ModuleInternalError($msg)))) end
macro MethodPluginError(msg) esc(:(throw(MethodPluginError($msg)))) end
macro IncoherentCallError(msg) esc(:(throw(IncoherentCallError($msg)))) end
macro NotInitializedError(msg) esc(:(throw(NotInitializedError($msg)))) end
macro NotLoadedError(msg) esc(:(throw(NotLoadedError($msg)))) end
macro NotInstalledError(msg) esc(:(throw(NotInstalledError($msg)))) end
macro IncoherentArgumentError(msg) esc(:(throw(IncoherentArgumentError($msg)))) end
macro KeywordArgumentError(msg) esc(:(throw(KeywordArgumentError($msg)))) end
macro ArgumentEvaluationError(msg) esc(:(throw(ArgumentEvaluationError($msg)))) end
Expand Down Expand Up @@ -37,6 +38,11 @@ struct NotLoadedError <: Exception
end
Base.showerror(io::IO, e::NotLoadedError) = print(io, "NotLoadedError: ", e.msg)

struct NotInstalledError <: Exception
msg::String
end
Base.showerror(io::IO, e::NotInstalledError) = print(io, "NotInstalledError: ", e.msg)

struct IncoherentArgumentError <: Exception
msg::String
end
Expand Down
6 changes: 6 additions & 0 deletions src/ParallelKernel/init_parallel_kernel.jl
Expand Up @@ -27,17 +27,22 @@ end

function init_parallel_kernel(caller::Module, package::Symbol, numbertype::DataType, inbounds::Bool; datadoc_call=:())
if package == PKG_CUDA
if (!CUDA_IS_INSTALLED) @NotInstalledError("CUDA was selected as package for parallelization, but CUDA.jl is not installed. CUDA functionality is provided with an extension of ParallelStencil and CUDA.jl needs therefore to be installed independently.") end
indextype = INT_CUDA
data_module = Data_cuda(numbertype, indextype)
data_module_shared = Data_shared(numbertype, indextype)
pkg_import_cmd = :(import CUDA)
elseif package == PKG_AMDGPU
if (!AMDGPU_IS_INSTALLED) @NotInstalledError("AMDGPU was selected as package for parallelization, but AMDGPU.jl is not installed. AMDGPU functionality is provided with an extension of ParallelStencil and AMDGPU.jl needs therefore to be installed independently.") end
indextype = INT_AMDGPU
data_module = Data_amdgpu(numbertype, indextype)
data_module_shared = Data_shared(numbertype, indextype)
pkg_import_cmd = :(import AMDGPU)
elseif package == PKG_THREADS
indextype = INT_THREADS
data_module = Data_threads(numbertype, indextype)
data_module_shared = Data_shared(numbertype, indextype)
pkg_import_cmd = :()
end
ad_init_cmd = :(ParallelStencil.ParallelKernel.AD.init_AD(ParallelStencil.ParallelKernel.PKG_THREADS))
if !isdefined(caller, :Data) || (@eval(caller, isa(Data, Module)) && length(symbols(caller, :Data)) == 1) # Only if the module Data does not exist in the caller or is empty, create it.
Expand All @@ -46,6 +51,7 @@ function init_parallel_kernel(caller::Module, package::Symbol, numbertype::DataT
else datadoc_call = :(@doc ParallelStencil.ParallelKernel.DATA_DOC Data)
end
end
@eval(caller, $pkg_import_cmd)
@eval(caller, $data_module)
@eval(caller.Data, $data_module_shared)
@eval(caller, $datadoc_call)
Expand Down
2 changes: 2 additions & 0 deletions src/ParallelKernel/shared.jl
Expand Up @@ -10,6 +10,8 @@ gensym_world(tag::String, generator::Module) = gensym(string(tag, GENSYM_SEPARAT
gensym_world(tag::Symbol, generator::Module) = gensym(string(tag, GENSYM_SEPARATOR, generator))
gensym_world(tag::Expr, generator::Module) = gensym(string(tag, GENSYM_SEPARATOR, generator))

const CUDA_IS_INSTALLED = (Base.find_package("CUDA")!==nothing)
const AMDGPU_IS_INSTALLED = (Base.find_package("AMDGPU")!==nothing)
const PKG_CUDA = :CUDA
const PKG_AMDGPU = :AMDGPU
const PKG_THREADS = :Threads
Expand Down
2 changes: 1 addition & 1 deletion src/ParallelStencil.jl
Expand Up @@ -42,7 +42,7 @@ https://github.com/omlins/ParallelStencil.jl
- [`Data`](@ref)
!! note "Activation of GPU support"
The support for GPU (CUDA or AMDGPU) is activated by importing the corresponding module (CUDA or AMDGPU) before importing ParallelStencil (the corresponding extension will be loaded).
The support for GPU (CUDA or AMDGPU) is provided with extensions and requires therefore an explicit installation of the corresponding packages (CUDA.jl or AMDGPU.jl). Note that it is not required to import explicitly the corresponding module (CUDA or AMDGPU); this is automatically done by [`@init_parallel_stencil`](@ref).
To see a description of a macro or module type `?<macroname>` (including the `@`) or `?<modulename>`, respectively.
"""
Expand Down

0 comments on commit d2008e0

Please sign in to comment.