Skip to content

Commit 9c25ebc

Browse files
committed
Move to CartesianIndices alltogether.
1 parent f94a11b commit 9c25ebc

File tree

4 files changed

+10
-48
lines changed

4 files changed

+10
-48
lines changed

src/device/indexing.jl

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,6 @@ macro linearidx(A, ctxsym = :ctx)
5353
end
5454
end
5555

56-
57-
# Base functions that are sadly not fit for the the GPU yet (they only work for Int64)
58-
Base.@pure @inline function gpu_ind2sub(A::AbstractArray, ind::T) where T
59-
_ind2sub(size(A), ind - T(1))
60-
end
61-
Base.@pure @inline function gpu_ind2sub(dims::NTuple{N}, ind::T) where {N, T}
62-
_ind2sub(NTuple{N, T}(dims), ind - T(1))
63-
end
64-
Base.@pure @inline _ind2sub(::Tuple{}, ind::T) where {T} = (ind + T(1),)
65-
Base.@pure @inline function _ind2sub(indslast::NTuple{1}, ind::T) where T
66-
((ind + T(1)),)
67-
end
68-
Base.@pure @inline function _ind2sub(inds, ind::T) where T
69-
r1 = inds[1]
70-
indnext = div(ind, r1)
71-
f = T(1); l = r1
72-
(ind-l*indnext+f, _ind2sub(Base.tail(inds), indnext)...)
73-
end
74-
75-
Base.@pure function gpu_sub2ind(dims::NTuple{N}, I::NTuple{N2, T}) where {N, N2, T}
76-
Base.@_inline_meta
77-
_sub2ind(NTuple{N, T}(dims), T(1), T(1), I...)
78-
end
79-
_sub2ind(x, L, ind) = ind
80-
function _sub2ind(::Tuple{}, L, ind, i::T, I::T...) where T
81-
Base.@_inline_meta
82-
ind + (i - T(1)) * L
83-
end
84-
function _sub2ind(inds, L, ind, i::IT, I::IT...) where IT
85-
Base.@_inline_meta
86-
r1 = inds[1]
87-
_sub2ind(Base.tail(inds), L * r1, ind + (i - IT(1)) * L, I...)
88-
end
89-
9056
"""
9157
cartesianidx(A, ctxsym = :ctx)
9258

src/host/abstractarray.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,12 @@ end
139139
Base.copyto!(dest::AbstractGPUArray, src::AbstractGPUArray) =
140140
copyto!(dest, CartesianIndices(dest), src, CartesianIndices(src))
141141

142-
function copy_kernel!(ctx::AbstractKernelContext, dest, dest_offsets, src, src_offsets, shape, shape_dest, shape_source, length)
142+
function copy_kernel!(ctx::AbstractKernelContext, dest, dest_offsets, src, src_offsets, shape, length)
143143
i = linear_index(ctx)
144144
if i <= length
145145
# TODO can this be done faster and smarter?
146-
idx = gpu_ind2sub(shape, i)
147-
dest_idx = gpu_sub2ind(shape_dest, idx .+ dest_offsets)
148-
src_idx = gpu_sub2ind(shape_source, idx .+ src_offsets)
149-
@inbounds dest[dest_idx] = src[src_idx]
146+
idx = CartesianIndices(shape)[i]
147+
@inbounds dest[idx + dest_offsets] = src[idx + src_offsets]
150148
end
151149
return
152150
end
@@ -159,10 +157,10 @@ function Base.copyto!(dest::AbstractGPUArray{T, N}, destcrange::CartesianIndices
159157
end
160158
len = length(destcrange)
161159

162-
dest_offsets = first.(destcrange.indices) .- 1
163-
src_offsets = first.(srccrange.indices) .- 1
160+
dest_offsets = first(destcrange) - one(CartesianIndex{N})
161+
src_offsets = first(srccrange) - one(CartesianIndex{N})
164162
gpu_call(copy_kernel!,
165-
dest, dest_offsets, src, src_offsets, shape, size(dest), size(src), len;
163+
dest, dest_offsets, src, src_offsets, shape, len;
166164
total_threads=len)
167165
dest
168166
end

src/host/indexing.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ to_index(a, x::Base.LogicalIndex) = error("Logical indexing not implemented")
8484
@generated function index_kernel(ctx::AbstractKernelContext, dest::AbstractArray, src::AbstractArray, idims, Is)
8585
N = length(Is.parameters)
8686
quote
87-
i = linear_index(ctx)
88-
i > length(dest) && return
89-
is = gpu_ind2sub(idims, i)
87+
i = @linearidx dest
88+
is = CartesianIndices(idims)[i]
9089
@nexprs $N i -> @inbounds I_i = Is[i][is[i]]
9190
@inbounds dest[i] = @ncall $N getindex src i -> I_i
9291
return
@@ -112,7 +111,7 @@ end
112111
quote
113112
i = linear_index(ctx)
114113
i > len && return
115-
is = gpu_ind2sub(idims, i)
114+
is = CartesianIndices(idims)[i]
116115
@inbounds setindex!(dest, bgetindex(src, i), $(idx...))
117116
return
118117
end

test/testsuite/base.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
function cartesian_iter(state, res, A, Asize)
22
for i in CartesianIndices(Asize)
3-
idx = GPUArrays.gpu_sub2ind(Asize, i.I)
4-
res[idx] = A[idx]
3+
res[i] = A[i]
54
end
65
return
76
end

0 commit comments

Comments
 (0)