Skip to content

Fix Data module type replacement in interactive session #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 2, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ParallelStencil_MetalExt = "Metal"
[compat]
AMDGPU = "0.6, 0.7, 0.8, 0.9, 1"
CUDA = "3.12, 4, 5"
CellArrays = "0.3"
CellArrays = "0.3.2"
Enzyme = "0.12, 0.13"
MacroTools = "0.5"
Metal = "1.2"
Expand Down
2 changes: 1 addition & 1 deletion src/ParallelKernel/CUDAExt/allocators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ParallelStencil.ParallelKernel.fill_cuda(::Type{T}, blocklength, args...) where

ParallelStencil.ParallelKernel.zeros_cuda(::Type{T}, blocklength, args...) where {T<:Union{SArray,FieldArray}} = (check_datatype_cuda(T); fill_cuda(T, blocklength, 0, args...))
ParallelStencil.ParallelKernel.ones_cuda(::Type{T}, blocklength, args...) where {T<:Union{SArray,FieldArray}} = (check_datatype_cuda(T); fill_cuda(T, blocklength, 1, args...))
ParallelStencil.ParallelKernel.rand_cuda(::Type{T}, ::Val{B}, dims) where {T<:Union{SArray,FieldArray}, B} = (check_datatype_cuda(T, Bool, Enum); blocklen = (B == 0) ? prod(dims) : B; CellArray{T,length(dims),B, CUDA.CuArray{eltype(T),3}}(CUDA.rand(eltype(T), blocklen, prod(size(T)), ceil(Int,prod(dims)/(blocklen))), dims))
ParallelStencil.ParallelKernel.rand_cuda(::Type{T}, ::Val{B}, dims) where {T<:Union{SArray,FieldArray}, B} = (check_datatype_cuda(T, Bool, Enum); blocklen = (B == 0) ? prod(dims) : B; CuCellArray{T,length(dims),B, eltype(T)}(CUDA.rand(eltype(T), blocklen, prod(size(T)), ceil(Int,prod(dims)/(blocklen))), dims))
ParallelStencil.ParallelKernel.rand_cuda(::Type{T}, blocklength, dims...) where {T<:Union{SArray,FieldArray}} = rand_cuda(T, blocklength, dims)
ParallelStencil.ParallelKernel.falses_cuda(::Type{T}, blocklength, args...) where {T<:Union{SArray,FieldArray}} = fill_cuda(T, blocklength, false, args...)
ParallelStencil.ParallelKernel.trues_cuda(::Type{T}, blocklength, args...) where {T<:Union{SArray,FieldArray}} = fill_cuda(T, blocklength, true, args...)
Expand Down
4 changes: 2 additions & 2 deletions src/ParallelKernel/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ function simplify_conditions(caller::Module, expr::Expr)
elseif (@capture(cond, a_ <= ixyz_ - c_ <= b_) && ixyz in INDICES) cond = :($a + $c <= $ixyz <= $b + $c)
end
if @capture(cond, a_ < x_ < b_) || @capture(cond, a_ < x_ <= b_) || @capture(cond, a_ <= x_ < b_) || @capture(cond, a_ <= x_ <= b_)
a_val = eval_try(caller, a)
b_val = eval_try(caller, b)
a_val = eval_try(caller, a; when_interactive=false)
b_val = eval_try(caller, b; when_interactive=false)
if !isnothing(a_val) cond = substitute(cond, a, :($a_val), inQuoteNode=true) end
if !isnothing(b_val) cond = substitute(cond, b, :($b_val), inQuoteNode=true) end
end
Expand Down
8 changes: 4 additions & 4 deletions src/ParallelKernel/shared.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ function eval_arg(caller::Module, arg)
end
end

function eval_try(caller::Module, expr)
if isinteractive() # NOTE: this is required to avoid that this function returns non-constant values in interactive sessions.
function eval_try(caller::Module, expr; when_interactive::Bool=true)
if !when_interactive && isinteractive() # NOTE: this is required to avoid that this function returns non-constant values in interactive sessions, when not appropriate (e.g. in for optimization)
return nothing
else
try
Expand Down Expand Up @@ -562,9 +562,9 @@ end


function interpolate(sym::Symbol, vals::NTuple, block::Expr)
return quote
return flatten(unblock(quote
$((substitute(block, sym, val; inQuoteNode=true, inString=true) for val in vals)...)
end
end))
end

interpolate(sym::Symbol, vals_expr::Expr, block::Expr) = interpolate(sym, (extract_tuple(vals_expr)...,), block)
Expand Down
44 changes: 22 additions & 22 deletions test/ParallelKernel/test_parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ eval(:(
@test Array(Ā) ≈ Ā_ref
@test Array(B̄) ≈ B̄_ref
end
end
end;
@testset "@parallel_indices" begin
@testset "inbounds" begin
expansion = @prettystring(1, @parallel_indices (ix) inbounds=true f(A) = (2*A; return))
Expand All @@ -147,27 +147,27 @@ eval(:(
@test !occursin("Base.@inbounds begin", expansion)
expansion = @prettystring(1, @parallel_indices (ix) f(A) = (2*A; return))
@test !occursin("Base.@inbounds begin", expansion)
end
end;
@testset "addition of range arguments" begin
expansion = @gorgeousstring(1, @parallel_indices (ix,iy) f(a::T, b::T) where T <: Union{Array{Float32}, Array{Float64}} = (println("a=$a, b=$b)"); return))
@test occursin("f(a::T, b::T, ranges::Tuple{UnitRange, UnitRange, UnitRange}, rangelength_x::Int64, rangelength_y::Int64, rangelength_z::Int64", expansion)
end
$(interpolate(:__T__, ARRAYTYPES, :(
end;
@testset "Data.T to Data.Device.T" $(interpolate(:__T__, ARRAYTYPES, :(
@testset "Data.__T__ to Data.Device.__T__" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::Data.__T__, B::Data.__T__, c::T) where T <: Integer = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::Data.Device.__T__, B::Data.Device.__T__,", expansion)
end
end
)))
$(interpolate(:__T__, FIELDTYPES, :(
end;
)));
@testset "Data.Fields.T to Data.Fields.Device.T" $(interpolate(:__T__, FIELDTYPES, :(
@testset "Data.Fields.__T__ to Data.Fields.Device.__T__" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::Data.Fields.__T__, B::Data.Fields.__T__, c::T) where T <: Integer = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::Data.Fields.Device.__T__, B::Data.Fields.Device.__T__,", expansion)
end
end
)))
end;
)));
# NOTE: the following GPU tests fail, because the Fields module cannot be imported.
# @testset "Fields.Field to Data.Fields.Device.Field" begin
# @static if @isgpu($package)
Expand All @@ -183,22 +183,22 @@ eval(:(
# @test occursin("f(A::Data.Fields.Device.Field, B::Data.Fields.Device.Field,", expansion)
# end
# end
$(interpolate(:__T__, ARRAYTYPES, :(
@testset "TData.T to TData.Device.T" $(interpolate(:__T__, ARRAYTYPES, :(
@testset "TData.__T__ to TData.Device.__T__" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::TData.__T__, B::TData.__T__, c::T) where T <: Integer = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::TData.Device.__T__, B::TData.Device.__T__,", expansion)
end
end
)))
$(interpolate(:__T__, FIELDTYPES, :(
end;
)));
@testset "TData.Fields.T to TData.Fields.Device.T" $(interpolate(:__T__, FIELDTYPES, :(
@testset "TData.Fields.__T__ to TData.Fields.Device.__T__" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::TData.Fields.__T__, B::TData.Fields.__T__, c::T) where T <: Integer = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::TData.Fields.Device.__T__, B::TData.Fields.Device.__T__,", expansion)
end
end
)))
end;
)));
# NOTE: the following GPU tests fail, because the Fields module cannot be imported.
# @testset "Fields.Field to TData.Fields.Device.Field" begin
# @static if @isgpu($package)
Expand All @@ -214,14 +214,14 @@ eval(:(
# @test occursin("f(A::TData.Fields.Device.Field, B::TData.Fields.Device.Field,", expansion)
# end
# end
$(interpolate(:__T__, ARRAYTYPES, :(
@testset "Nested Data.T to Data.Device.T" $(interpolate(:__T__, ARRAYTYPES, :(
@testset "Nested Data.__T__ to Data.Device.__T__" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::NamedTuple{T1, NTuple{T2,T3}} where {T1,T2} where T3 <: Data.__T__, c::T) where T <: Integer = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::((NamedTuple{T1, NTuple{T2, T3}} where {T1, T2}) where T3 <: Data.Device.__T__),", expansion)
end
end
)))
end;
)));
@testset "@parallel_indices (1D)" begin
A = @zeros(4)
@parallel_indices (ix) function write_indices!(A)
Expand Down Expand Up @@ -422,22 +422,22 @@ eval(:(
@require !@is_initialized()
@init_parallel_kernel(package = $package)
@require @is_initialized
$(interpolate(:__T__, ARRAYTYPES, :(
@testset "Data.T{T2} to Data.Device.T{T2}" $(interpolate(:__T__, ARRAYTYPES, :(
@testset "Data.__T__{T2} to Data.Device.__T__{T2}" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::Data.__T__{T2}, B::Data.__T__{T2}, c<:Integer) where T2 <: Union{Float32, Float64} = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::Data.Device.__T__{T2}, B::Data.Device.__T__{T2},", expansion)
end
end;
)))
$(interpolate(:__T__, FIELDTYPES, :(
)));
@testset "Data.Fields.T{T2} to Data.Fields.Device.T{T2}" $(interpolate(:__T__, FIELDTYPES, :(
@testset "Data.Fields.__T__{T2} to Data.Fields.Device.__T__{T2}" begin
@static if @isgpu($package)
expansion = @prettystring(1, @parallel_indices (ix,iy) f(A::Data.Fields.__T__{T2}, B::Data.Fields.__T__{T2}, c<:Integer) where T2 <: Union{Float32, Float64} = (A[ix,iy] = B[ix,iy]^c; return))
@test occursin("f(A::Data.Fields.Device.__T__{T2}, B::Data.Fields.Device.__T__{T2},", expansion)
end
end;
)))
)));
@reset_parallel_kernel()
end;
@testset "5. Exceptions" begin
Expand Down
Loading