-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathindexing.jl
63 lines (46 loc) · 1.69 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
# indexing
## utilities
using Base.Cartesian
## logical indexing
# we cannot use Base.LogicalIndex, which does not support indexing but requires iteration.
# TODO: it should still be possible to use the same technique;
# Base.LogicalIndex basically contains the same as our `findall` here does.
Base.to_index(::CuArray, I::AbstractArray{Bool}) = findall(I)
if VERSION >= v"1.11.0-DEV.1157"
Base.to_indices(A::CuArray, I::Tuple{AbstractArray{Bool}}) = (Base.to_index(A, I[1]),)
else
Base.to_indices(A::CuArray, inds,
I::Tuple{Union{Array{Bool,N}, BitArray{N}}}) where {N} =
(Base.to_index(A, I[1]),)
end
## find*
function Base.findall(bools::AnyCuArray{Bool})
I = keytype(bools)
indices = cumsum(reshape(bools, prod(size(bools))))
n = isempty(indices) ? 0 : @allowscalar indices[end]
ys = CuArray{I}(undef, n)
if n > 0
function kernel(ys::CuDeviceArray, bools, indices)
i = threadIdx().x + (blockIdx().x - 1i32) * blockDim().x
@inbounds if i <= length(bools) && bools[i]
i′ = CartesianIndices(bools)[i]
b = indices[i] # new position
ys[b] = i′
end
return
end
kernel = @cuda name="findall" launch=false kernel(ys, bools, indices)
config = launch_configuration(kernel.fun)
threads = min(length(indices), config.threads)
blocks = cld(length(indices), threads)
kernel(ys, bools, indices; threads, blocks)
end
unsafe_free!(indices)
return ys
end
function Base.findall(f::Function, A::AnyCuArray)
bools = map(f, A)
ys = findall(bools)
unsafe_free!(bools)
return ys
end