-
Notifications
You must be signed in to change notification settings - Fork 2
/
AtomixCUDAExt.jl
58 lines (51 loc) · 1.43 KB
/
AtomixCUDAExt.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
# TODO: respect ordering
module AtomixCUDAExt
using Atomix: Atomix, IndexableRef
using CUDA: CUDA, CuDeviceArray
const CuIndexableRef{Indexable<:CuDeviceArray} = IndexableRef{Indexable}
function Atomix.get(ref::CuIndexableRef, order)
error("not implemented")
end
function Atomix.set!(ref::CuIndexableRef, v, order)
error("not implemented")
end
@inline function Atomix.replace!(
ref::CuIndexableRef,
expected,
desired,
success_ordering,
failure_ordering,
)
ptr = Atomix.pointer(ref)
expected = convert(eltype(ref), expected)
desired = convert(eltype(ref), desired)
begin
old = CUDA.atomic_cas!(ptr, expected, desired)
end
return (; old = old, success = old === expected)
end
@inline function Atomix.modify!(ref::CuIndexableRef, op::OP, x, order) where {OP}
x = convert(eltype(ref), x)
ptr = Atomix.pointer(ref)
begin
old = if op === (+)
CUDA.atomic_add!(ptr, x)
elseif op === (-)
CUDA.atomic_sub!(ptr, x)
elseif op === (&)
CUDA.atomic_and!(ptr, x)
elseif op === (|)
CUDA.atomic_or!(ptr, x)
elseif op === xor
CUDA.atomic_xor!(ptr, x)
elseif op === min
CUDA.atomic_min!(ptr, x)
elseif op === max
CUDA.atomic_max!(ptr, x)
else
error("not implemented")
end
end
return old => op(old, x)
end
end # module AtomixCUDAExt