This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
indexing.jl
209 lines (148 loc) · 5.27 KB
/
indexing.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# indexing
## utilities
using Base.Cartesian
## logical indexing
Base.getindex(xs::CuArray, bools::AbstractArray{Bool}) = getindex(xs, CuArray(bools))
function Base.getindex(xs::CuArray{T}, bools::CuArray{Bool}) where {T}
bools = reshape(bools, prod(size(bools)))
indices = cumsum(bools) # unique indices for elements that are true
n = @allowscalar indices[end] # number that are true
ys = CuArray{T}(undef, n)
if n > 0
function kernel(ys::CuDeviceArray{T}, xs::CuDeviceArray{T}, bools, indices)
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x
@inbounds if i <= length(xs) && bools[i]
b = indices[i] # new position
ys[b] = xs[i]
end
return
end
function configurator(kernel)
config = launch_configuration(kernel.fun)
threads = min(length(indices), config.threads)
blocks = cld(length(indices), threads)
return (threads=threads, blocks=blocks)
end
@cuda name="logical_getindex" config=configurator kernel(ys, xs, bools, indices)
end
unsafe_free!(indices)
return ys
end
## find*
function Base.findall(bools::CuArray{Bool})
I = keytype(bools)
indices = cumsum(reshape(bools, prod(size(bools))))
n = @allowscalar indices[end]
ys = CuArray{I}(undef, n)
if n > 0
function kernel(ys::CuDeviceArray, bools, indices)
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x
@inbounds if i <= length(bools) && bools[i]
i′ = CartesianIndices(bools)[i]
b = indices[i] # new position
ys[b] = i′
end
return
end
function configurator(kernel)
config = launch_configuration(kernel.fun)
threads = min(length(indices), config.threads)
blocks = cld(length(indices), threads)
return (threads=threads, blocks=blocks)
end
@cuda name="findall" config=configurator kernel(ys, bools, indices)
end
unsafe_free!(indices)
return ys
end
function Base.findall(f::Function, A::CuArray)
bools = map(f, A)
ys = findall(bools)
unsafe_free!(bools)
return ys
end
function Base.findfirst(testf::Function, xs::CuArray)
I = keytype(xs)
y = CuArray([typemax(Int)])
function kernel(y::CuDeviceArray, xs::CuDeviceArray)
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x
@inbounds if i <= length(xs) && testf(xs[i])
CUDAnative.@atomic y[1] = min(y[1], i)
end
return
end
function configurator(kernel)
config = launch_configuration(kernel.fun)
threads = min(length(xs), config.threads)
blocks = cld(length(xs), threads)
return (threads=threads, blocks=blocks)
end
@cuda name="findfirst" config=configurator kernel(y, xs)
first_i = @allowscalar y[1]
return first_i == typemax(Int) ? nothing : keys(xs)[first_i]
end
Base.findfirst(xs::CuArray{Bool}) = findfirst(identity, xs)
function Base.findfirst(vals::CuArray, xs::CuArray)
## find the first matching element
# NOTE: this kernel performs global atomic operations for the sake of simplicity.
# if this turns out to be a bottleneck, we will need to cache in local memory.
# that requires the dimension-under-reduction to be iterated in first order.
# this can be done by splitting the iteration domain eagerly; see the
# accumulate kernel for an example, or git history from before this comment.
indices = fill(typemax(Int), size(vals))
function kernel(xs, vals, indices)
i = (blockIdx().x-1) * blockDim().x + threadIdx().x
R = CartesianIndices(xs)
if i <= length(R)
I = R[i]
Jmax = last(CartesianIndices(vals))
J = min(I, Jmax)
@inbounds if xs[I] == vals[J]
I′ = LinearIndices(xs)[I] # atomic_min only works with integers
J′ = LinearIndices(indices)[J] # FIXME: @atomic doesn't handle array ref with CartesianIndices
CUDAnative.@atomic indices[J′] = min(indices[J′], I′)
end
end
return
end
function configurator(kernel)
config = launch_configuration(kernel.fun)
threads = min(length(xs), config.threads)
blocks = cld(length(xs), threads)
return (threads=threads, blocks=blocks)
end
@cuda config=configurator kernel(xs, vals, indices)
## convert the linear indices to an appropriate type
kt = keytype(xs)
if kt == Int
return indices
else
indices′ = CuArray{kt}(undef, size(indices))
broadcast!(indices′, indices, Ref(keys(xs))) do index, keys
keys[index]
end
return indices′
end
end
function Base.findmin(a::CuArray; dims=:)
if dims == Colon()
m = minimum(a)
i = findfirst(x->x==m, a)
return m,i
else
minima = minimum(a; dims=dims)
i = findfirst(minima, a)
return minima,i
end
end
function Base.findmax(a::CuArray; dims=:)
if dims == Colon()
m = maximum(a)
i = findfirst(x->x==m, a)
return m,i
else
maxima = maximum(a; dims=dims)
i = findfirst(maxima, a)
return maxima,i
end
end