Skip to content

Commit 87680a5

Browse files
committed
do not rely on defaults for optimization ranges in unit tests as GPU dependent; and make them fully deterministic
1 parent 42c37e1 commit 87680a5

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

src/memopt.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -672,19 +672,21 @@ function remove_single_point_optvars(optvars, optranges_arg, offsets, offsets_by
672672
return tuple((A for A in optvars if !(length(keys(offsets[A]))==1 && length(keys(offsets_by_z[A]))==1) || (!isnothing(optranges_arg) && A keys(optranges_arg)))...)
673673
end
674674

675+
function score_centered_nonloop_offsets(candidate::Tuple, offsets::Tuple)
676+
return sum(sum((candidate[i] - other[i])^2 for i in eachindex(candidate)) for other in offsets)
677+
end
678+
679+
function select_offsets_with_max_span(offsets::AbstractDict)
680+
loopspan_max = maximum(nonloop_offsets -> length(keys(offsets[nonloop_offsets])), keys(offsets))
681+
offsets_with_max_span = Tuple(Tuple(nonloop_offsets) for nonloop_offsets in keys(offsets) if length(keys(offsets[nonloop_offsets])) == loopspan_max)
682+
return argmin(nonloop_offsets -> (score_centered_nonloop_offsets(nonloop_offsets, offsets_with_max_span), nonloop_offsets), offsets_with_max_span)
683+
end
684+
675685
function define_optranges(optranges_arg, optvars, offsets, int_type, package)
676686
compute_capability = get_compute_capability(package)
677687
optranges = Dict()
678688
for A in optvars
679-
loopspan_max = 0
680-
offsets_with_max_span = ()
681-
for nonloop_offsets in keys(offsets[A])
682-
loopspan = length(keys(offsets[A][nonloop_offsets]))
683-
if loopspan > loopspan_max
684-
loopspan_max = loopspan
685-
offsets_with_max_span = nonloop_offsets
686-
end
687-
end
689+
offsets_with_max_span = select_offsets_with_max_span(offsets[A])
688690
fullrange = typemin(int_type):typemax(int_type)
689691
pointrange_x = offsets_with_max_span[1]:offsets_with_max_span[1]
690692
pointrange_y = (length(offsets_with_max_span) > 1) ? (offsets_with_max_span[2]:offsets_with_max_span[2]) : fullrange

test/test_parallel.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,13 @@ eval(:(
14381438
end;
14391439
@static if $package != $PKG_KERNELABSTRACTIONS
14401440
@testset "memopt" begin
1441+
fullrange = typemin(Int64):typemax(Int64)
1442+
tie_offsets = Dict(:B => Dict((0, 0) => Dict(0 => 1), (1, 1) => Dict(0 => 1), (1, 2) => Dict(0 => 1)))
1443+
tied_optranges = ParallelStencil.define_optranges(nothing, (:B,), tie_offsets, Int64, ParallelStencil.PKG_THREADS)
1444+
@test tied_optranges[:B] == (1:1, 1:1, fullrange)
1445+
symmetric_tie_offsets = Dict(:B => Dict((0, 0) => Dict(0 => 1), (0, 2) => Dict(0 => 1), (2, 0) => Dict(0 => 1), (2, 2) => Dict(0 => 1)))
1446+
symmetric_tie_optranges = ParallelStencil.define_optranges(nothing, (:B,), symmetric_tie_offsets, Int64, ParallelStencil.PKG_THREADS)
1447+
@test symmetric_tie_optranges[:B] == (0:0, 0:0, fullrange)
14411448
@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)
14421449
A[ix, iy, iz] = 2.0 * B[ix, iy, iz]
14431450
return
@@ -1478,24 +1485,17 @@ eval(:(
14781485
@test metadata_z.shmem_spans == (B = (0, 0),)
14791486
@test metadata_z.stencilranges == (B = (0:0, 0:0, -1:1),)
14801487
@test metadata_z.use_any_shmem == false
1481-
@parallel_indices (ix, iy, iz) memopt=true loopsize=3 optvars=B function metadata_memopt_fullstencil_probe!(A, B, D)
1488+
@parallel_indices (ix, iy, iz) memopt=true loopsize=3 optvars=B optranges=(B=(typemin(Int64):typemax(Int64), typemin(Int64):typemax(Int64), typemin(Int64):typemax(Int64)),) function metadata_memopt_fullstencil_probe!(A, B, D)
14821489
A[ix, iy, iz] = B[ix-1, iy-1, iz-1] + B[ix, iy, iz] + B[ix+1, iy+1, iz+1] + D[ix, iy, iz, 1]
14831490
return
14841491
end
14851492
metadata_full = @metadata metadata_memopt_fullstencil_probe!(A, B, D)
1486-
@static if @isgpu($package)
1487-
@test metadata_full.shmem_optvars == (:B,)
1488-
@test metadata_full.shmem_optvars isa NTuple{1,Symbol}
1489-
@test metadata_full.shmem_spans == (B = (2, 2),)
1490-
@test metadata_full.stencilranges == (B = (-1:1, -1:1, -1:1),)
1491-
@test metadata_full.use_any_shmem == true
1492-
else
1493-
@test metadata_full.shmem_optvars == ()
1494-
@test metadata_full.shmem_optvars isa NTuple{0,Symbol}
1495-
@test metadata_full.shmem_spans == (B = (0, 0),)
1496-
@test metadata_full.stencilranges == (B = (0:0, 0:0, 0:0),)
1497-
@test metadata_full.use_any_shmem == false
1498-
end
1493+
@test metadata_full.optranges[:B] == (fullrange, fullrange, fullrange)
1494+
@test metadata_full.shmem_optvars == (:B,)
1495+
@test metadata_full.shmem_optvars isa NTuple{1,Symbol}
1496+
@test metadata_full.shmem_spans == (B = (2, 2),)
1497+
@test metadata_full.stencilranges == (B = (-1:1, -1:1, -1:1),)
1498+
@test metadata_full.use_any_shmem == true
14991499
@parallel_indices (ix, iy) ndims=2 memopt=true loopdim=2 loopsize=5 optvars=B optranges=(B=(0:0,-1:1,0:0),) function metadata_memopt_2d_probe!(A, B)
15001500
if (1 < iy < size(A, 2))
15011501
A[ix, iy] = B[ix, iy - 1] + B[ix, iy] + B[ix, iy + 1]

0 commit comments

Comments
 (0)