Skip to content
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

Add projection mechanism #403

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/nditeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ for block in ndrange
end
```
"""
struct NDRange{N, StaticBlocks, StaticWorkitems, DynamicBlock, DynamicWorkitems}
struct NDRange{N, StaticBlocks, StaticWorkitems, DynamicBlock, DynamicWorkitems, Projection}
blocks::DynamicBlock
workitems::DynamicWorkitems
projection::Projection

function NDRange{N, B, W}() where {N, B, W}
new{N, B, W, Nothing, Nothing}(nothing, nothing)
function NDRange{N, B, W}(projection=identity) where {N, B, W}
new{N, B, W, Nothing, Nothing, typeof(projection)}(nothing, nothing, projection)
end

function NDRange{N, B, W}(blocks, workitems) where {N, B, W}
new{N, B, W, typeof(blocks), typeof(workitems)}(blocks, workitems)
function NDRange{N, B, W}(blocks, workitems, projection=identity) where {N, B, W}
new{N, B, W, typeof(blocks), typeof(workitems), typeof(projection)}(blocks, workitems, projection)
end
end

Expand All @@ -77,7 +78,7 @@ Base.length(range::NDRange) = length(blocks(range))
gidx = groupidx.I[I]
(gidx-1)*stride + idx.I[I]
end
CartesianIndex(nI)
ndrange.projection(CartesianIndex(nI))
end

Base.@propagate_inbounds function expand(ndrange::NDRange, groupidx::Integer, idx::Integer)
Expand Down Expand Up @@ -126,4 +127,19 @@ needs to perform dynamic bounds-checking.
end
end

abstract type IndexProjection end
struct Identity <: IndexProjection end
(::Identity)(idx::CartesianIndex) = idx
const identity = Identity()

struct Offsets{N} <: IndexProjection
offsets::NTuple{N}
end
function (o::Offsets{N})(idx::CartesianIndex{N}) where N
nI = ntuple(Val{N}) do i
idx.I[i] + o.offsets[i]
end
CartesianIndex(nI)
end

end #module
22 changes: 22 additions & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,28 @@ end
synchronize(Backend())
end

@kernel function index_global_offset!(a)
i, j = @index(Global, NTuple)
n, m = size(a)
@inbounds a[i, j] = i + n * j
end

@conditional_testset "Offset iteration space $Backend" skip_tests begin
a = KernelAbstractions.zeros(Backend(), 7, 9)
index_global_offset!(Backend(), (2, 2), size(a) .- 4, (2, 2))(a)
synchronize(Backend())

b = KernelAbstractions.zeros(CPU(), 7, 9)
b .= a

c = [i + 7 * j for i in 1:7, j in 1:9]

@test b[3:5, 3:7] == c[3:5, 3:7]
@test b[1:2, :] == zeros(2, 9)
@test b[6:7, :] == zeros(2, 9)
@test b[:, 1:2] == zeros(7, 2)
@test b[:, 8:9] == zeros(7, 2)
end

@conditional_testset "return statement" skip_tests begin
try
Expand Down