Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/parallel.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import .ParallelKernel: get_name, set_name, get_body, set_body!, add_return, remove_return, extract_kwargs, split_parallel_args, extract_tuple, substitute, literaltypes, push_to_signature!, add_loop, add_threadids, promote_maxsize
import .ParallelKernel: get_name, get_body, set_body!, add_return, remove_return, extract_kwargs, split_parallel_args, extract_tuple, substitute, literaltypes, push_to_signature!, add_loop, add_threadids, promote_maxsize

# NOTE: @parallel and @parallel_indices and @parallel_async do not appear in the following as they are extended and therefore re-defined here in parallel.jl
@doc replace(ParallelKernel.SYNCHRONIZE_DOC, "@init_parallel_kernel" => "@init_parallel_stencil") macro synchronize(args...) check_initialized(__module__); esc(:(ParallelStencil.ParallelKernel.@synchronize($(args...)))); end
Expand Down Expand Up @@ -411,15 +411,15 @@ function parallel_indices(source::LineNumberNode, caller::Module, args::Union{Sy
memopt = haskey(kwargs, :memopt) ? kwargs.memopt : get_memopt(caller)
if memopt
quote
$metadata_function
Core.@__doc__ $(parallel_indices_memopt(metadata_module, metadata_function, is_parallel_kernel, caller, package, posargs..., kernelarg; kwargs...)) #TODO: the package and numbertype will have to be passed here further once supported as kwargs (currently removed from call: package, numbertype, )
$metadata_function
end
else
kwargs_expr = (:(inbounds=$inbounds), :(padding=$padding))
kernel = ParallelKernel.parallel_indices(caller, posargs..., kwargs_expr..., kernelarg; package=package)
quote
$metadata_function
Core.@__doc__ $kernel
$metadata_function
end
end
end
Expand Down Expand Up @@ -551,16 +551,16 @@ function parallel_kernel(metadata_module::Module, metadata_function::Expr, calle
if memopt
expanded_kernel = macroexpand(caller, kernel)
quote
$metadata_function
Core.@__doc__ $(parallel_indices_memopt(metadata_module, metadata_function, is_parallel_kernel, caller, package, get_indices_expr(ndims), expanded_kernel; kwargs...)) #TODO: the package and numbertype will have to be passed here further once supported as kwargs (currently removed from call: package, numbertype, )
$metadata_function
end
else
if package == PKG_KERNELABSTRACTIONS
kernel = :(ParallelStencil.ParallelKernel.@ka_kernel $kernel)
end
return quote
$metadata_function
Core.@__doc__ $kernel
$metadata_function
end # TODO: later could be here called parallel_indices instead of adding the threadids etc above.
end
end
Expand Down Expand Up @@ -853,28 +853,20 @@ function create_module(hostmodule::Module, modulename::Symbol; do_baremodule=tru
end
end

# Kernel metadata is stored as an additional method of the kernel function itself, `kernel(::KernelMetadata, args...)`, returning the metadata module of the kernel method that `kernel(args...)` calls. It is thus found by dispatch, whatever the callee expression is (e.g., `f`, `Mod.f`, `s.f` or a function argument; see issue #234) and in whatever scope the kernel is defined. The metadata method must be defined after the kernel, as KernelAbstractions defines the kernel constructors only if the kernel name is not yet defined.
struct KernelMetadata end

function create_metadata_function(kernel::Expr, metadata_module::Module) # NOTE: unlike the creation of the module above, the creation of the matter data function has to happen every time: if we redefine the same function we to have to redefine the meta data...
metadata_function = deepcopy(kernel)
kernelname = get_name(kernel)
functionname = get_meta_function(kernelname)
metadata_function = set_name(metadata_function, functionname)
set_body!(metadata_function, quote
metadata_function = splitdef(deepcopy(kernel))
pushfirst!(metadata_function[:args], :(::ParallelStencil.KernelMetadata))
metadata_function[:body] = quote
return $metadata_module
end)
return :(@inline $metadata_function)
end

function create_metadata_call(configcall::Expr)
metadata_call = deepcopy(configcall)
kernelname = metadata_call.args[1]
if kernelname isa Expr && kernelname.head === :.
metadata_call.args[1] = Expr(:., kernelname.args[1], QuoteNode(get_meta_function(kernelname.args[2].value)))
else
metadata_call.args[1] = get_meta_function(kernelname)
end
return metadata_call
return :(@inline $(combinedef(metadata_function)))
end

create_metadata_call(configcall::Expr) = :($(configcall.args[1])(ParallelStencil.KernelMetadata(), $(configcall.args[2:end]...)))

function store_metadata(metadata_module::Module, caller::Module, nb_parallel_indices::Integer; memopt::Union{Nothing,Bool}=nothing, double_buffer_args::Union{Nothing,Tuple{Vararg{Int}}}=nothing, double_buffering_opt::Union{Nothing,Bool}=nothing)
nonconst_metadata = get_nonconst_metadata(caller)
if nonconst_metadata
Expand Down Expand Up @@ -924,7 +916,6 @@ end

get_kernelid(kernelname, file, line) = Symbol("$(kernelname)_$(file)_$(line)")
get_kernelid(kernel::Expr, file, line) = Symbol("$(get_kernelid(get_name(kernel), file, line))_$(hash(string(kernel)))")
get_meta_function(kernelname) = Symbol("$(META_FUNCTION_PREFIX)$(GENSYM_SEPARATOR)$(kernelname)")


## FUNCTIONS TO DEAL WITH ON-THE-FLY ASSIGNMENTS
Expand Down
3 changes: 1 addition & 2 deletions src/shared.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MacroTools: @capture, postwalk, splitdef, splitarg # NOTE: inexpr_walk used instead of MacroTools.inexpr
import MacroTools: @capture, postwalk, splitdef, combinedef, splitarg # NOTE: inexpr_walk used instead of MacroTools.inexpr
import .ParallelKernel: eval_arg, split_args, split_kwargs, extract_posargs_init, extract_kernel_args, insert_device_types, is_kernel, is_call, gensym_world, isgpu, iscpu, @isgpu, @iscpu, substitute, substitute_in_kernel, in_signature, inexpr_walk, adjust_signatures, handle_indices_and_literals, add_inbounds, cast, @ranges, @rangelengths, @return_value, @return_nothing, @firstindex, @lastindex, is_access, find_vars, dotted_expr_to_symbol, handle_padding, handle_inverses, increment_arg, quote_expr, get_compute_capability
import .ParallelKernel: PKG_CUDA, PKG_AMDGPU, PKG_METAL, PKG_KERNELABSTRACTIONS, PKG_THREADS, PKG_POLYESTER, PKG_NONE, NUMBERTYPE_NONE, INBOUNDS_DEFAULT, PADDING_DEFAULT, SUPPORTED_NUMBERTYPES, SUPPORTED_PACKAGES, ERRMSG_UNSUPPORTED_PACKAGE, INT_CUDA, INT_AMDGPU, INT_METAL, INT_KERNELABSTRACTIONS, INT_POLYESTER, INT_THREADS, INDICES, INDICES_INN, INDICES_DIR, INDICES_DIR_FUNCTIONS_SYMS, PKNumber, RANGES_VARNAME, RANGES_TYPE, RANGELENGTH_XYZ_TYPE, RANGELENGTHS_VARNAMES, THREADIDS_VARNAMES, GENSYM_SEPARATOR, AD_SUPPORTED_ANNOTATIONS, ARRAYTYPES, FIELDTYPES, SCALARTYPES
import .ParallelKernel: @require, @symbols, symbols, longnameof, @prettyexpand, @prettystring, prettystring, @gorgeousexpand, @gorgeousstring, gorgeousstring, interpolate, @get_hardware
Expand Down Expand Up @@ -43,7 +43,6 @@ const OPTRANGES_LENGTH_DEFAULT_OLD = Inf
const FULLRANGE_THRESHOLD = 1
const NOEXPR = :(begin end)
const MOD_METADATA_PS = gensym_world("__metadata_PS__", @__MODULE__) # # TODO: name mangling should be used here later, or if there is any sense to leave it like that then at check whether it's available must be done before creating it
const META_FUNCTION_PREFIX = string(gensym_world("META", @__MODULE__))


## FUNCTIONS TO DEAL WITH KERNEL DEFINITIONS
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ end
excludedfiles = [ "test_excluded.jl", "test_incremental_compilation.jl", "test_revise.jl"]; # TODO: test_incremental_compilation has to be deactivated until Polyester support released

const STDERR_SUPPRESSION_RULES = (
(name="metadata method overwrite warnings", start=r"^WARNING: Method definition .*###META.* overwritten.*$", stop=nothing),
(name="metadata method overwrite warnings", start=r"^WARNING: Method definition .*\(ParallelStencil\.KernelMetadata, .* overwritten.*$", stop=nothing),
(name="[T]Data module replacement warnings", start=r"^WARNING: replacing module [T]?Data\.$", stop=nothing),
(name="Metal OS support warnings", start=r"^┌ Error: Metal\.jl is only supported on (macOS|Apple Silicon)$", stop=r"^└ @ Metal .*$"),
)
Expand Down
20 changes: 19 additions & 1 deletion test/test_parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ eval(:(
@all(Pt) = @all(A) + 1.0
return
end)
@test occursin("##META", s2)
@test occursin("function step_db2!(::ParallelStencil.KernelMetadata, Pt::Field2B, A::Data.Number", s2)
@test occursin("onthefly", s2)
end;
@testset "rewrite with partial-update 2B field" begin
Expand Down Expand Up @@ -1631,6 +1631,7 @@ eval(:(
A = @zeros(4, 5, 6)
B = @ones(4, 5, 6)
D = @ones(4, 5, 6, 2)
@test hasmethod(metadata_probe!, Tuple{ParallelStencil.KernelMetadata, typeof(A), typeof(B), typeof(D)})
metadata = @metadata metadata_probe!(A, B, D)
metadata_symbols = sort(setdiff(names(metadata; all=true), names(metadata)))
@test metadata isa Module
Expand All @@ -1640,6 +1641,22 @@ eval(:(
@test metadata.nb_parallel_indices == 3
@test all(Array(A) .== 0)
end;
@testset "kernel not called by its name (issue #234)" begin
@parallel_indices (ix, iy, iz) function metadata_indirect_probe!(A, B)
A[ix, iy, iz] = 2.0 * B[ix, iy, iz]
return
end
A = @zeros(4, 5, 6)
B = @ones(4, 5, 6)
s = (kernel=metadata_indirect_probe!, A=A, B=B)
launch!(kernel, A, B) = @parallel kernel(A, B)
@test (@metadata s.kernel(A, B)).nb_parallel_indices == 3
@parallel s.kernel(s.A, s.B)
@test all(Array(A) .== 2.0)
A .= 0
launch!(metadata_indirect_probe!, A, B)
@test all(Array(A) .== 2.0)
end;
@static if $package != $PKG_KERNELABSTRACTIONS
@testset "memopt" begin
@parallel_indices (ix, iy, iz) memopt=true loopsize=3 optvars=B optranges=(B=(0:0,0:0,0:0),) function metadata_memopt_probe!(A, B, D)
Expand All @@ -1649,6 +1666,7 @@ eval(:(
A = @zeros(4, 5, 6)
B = @ones(4, 5, 6)
D = @ones(4, 5, 6, 2)
@test hasmethod(metadata_memopt_probe!, Tuple{ParallelStencil.KernelMetadata, typeof(A), typeof(B), typeof(D)})
metadata = @metadata metadata_memopt_probe!(A, B, D)
metadata_symbols = sort(setdiff(names(metadata; all=true), names(metadata)))
@test metadata isa Module
Expand Down
Loading