|
| 1 | +# TODO: respect ordering |
| 2 | +module AtomixOpenCLExt |
| 3 | + |
| 4 | +using Atomix: Atomix, IndexableRef |
| 5 | +using OpenCL: SPIRVIntrinsics, CLDeviceArray |
| 6 | + |
| 7 | +const CLIndexableRef{Indexable<:CLDeviceArray} = IndexableRef{Indexable} |
| 8 | + |
| 9 | +function Atomix.get(ref::CLIndexableRef, order) |
| 10 | + error("not implemented") |
| 11 | +end |
| 12 | + |
| 13 | +function Atomix.set!(ref::CLIndexableRef, v, order) |
| 14 | + error("not implemented") |
| 15 | +end |
| 16 | + |
| 17 | +@inline function Atomix.replace!( |
| 18 | + ref::CLIndexableRef, |
| 19 | + expected, |
| 20 | + desired, |
| 21 | + success_ordering, |
| 22 | + failure_ordering, |
| 23 | +) |
| 24 | + ptr = Atomix.pointer(ref) |
| 25 | + expected = convert(eltype(ref), expected) |
| 26 | + desired = convert(eltype(ref), desired) |
| 27 | + begin |
| 28 | + old = SPIRVIntrinsics.atomic_cmpxchg!(ptr, expected, desired) |
| 29 | + end |
| 30 | + return (; old = old, success = old === expected) |
| 31 | +end |
| 32 | + |
| 33 | +@inline function Atomix.modify!(ref::CLIndexableRef, op::OP, x, order) where {OP} |
| 34 | + x = convert(eltype(ref), x) |
| 35 | + ptr = Atomix.pointer(ref) |
| 36 | + begin |
| 37 | + old = if op === (+) |
| 38 | + SPIRVIntrinsics.atomic_add!(ptr, x) |
| 39 | + elseif op === (-) |
| 40 | + SPIRVIntrinsics.atomic_sub!(ptr, x) |
| 41 | + elseif op === (&) |
| 42 | + SPIRVIntrinsics.atomic_and!(ptr, x) |
| 43 | + elseif op === (|) |
| 44 | + SPIRVIntrinsics.atomic_or!(ptr, x) |
| 45 | + elseif op === xor |
| 46 | + SPIRVIntrinsics.atomic_xor!(ptr, x) |
| 47 | + elseif op === min |
| 48 | + SPIRVIntrinsics.atomic_min!(ptr, x) |
| 49 | + elseif op === max |
| 50 | + SPIRVIntrinsics.atomic_max!(ptr, x) |
| 51 | + else |
| 52 | + error("not implemented") |
| 53 | + end |
| 54 | + end |
| 55 | + return old => op(old, x) |
| 56 | +end |
| 57 | + |
| 58 | +end # module AtomixOpenCLExt |
| 59 | + |
0 commit comments