-
Notifications
You must be signed in to change notification settings - Fork 7
Add atomic operations support for Complex numbers #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
albertomercurio
wants to merge
4
commits into
JuliaConcurrent:main
Choose a base branch
from
albertomercurio:add-complex-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9abbe99
Add atomic operations support for Complex numbers
albertomercurio 59a7d44
Add support to ComplexF32 for CUDA
albertomercurio 9dc7ce9
Handle the real and imaginary parts separately
albertomercurio 21df681
Add complex number support for atomic operations in Metal, OpenCL, an…
albertomercurio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,35 +24,96 @@ end | |
| ptr = Atomix.pointer(ref) | ||
| expected = convert(eltype(ref), expected) | ||
| desired = convert(eltype(ref), desired) | ||
| begin | ||
| old = CUDA.atomic_cas!(ptr, expected, desired) | ||
| end | ||
| old = _cuda_atomic_cas!(ptr, expected, desired) | ||
| return (; old = old, success = old === expected) | ||
| end | ||
|
|
||
| # Native CUDA CAS for supported types | ||
| @inline function _cuda_atomic_cas!(ptr::Core.LLVMPtr{T,A}, cmp::T, new::T) where {T,A} | ||
| CUDA.atomic_cas!(ptr, cmp, new) | ||
| end | ||
|
|
||
| # Complex CAS - using separate CAS on real and imaginary components | ||
| # Note: This is NOT fully atomic (components updated separately) | ||
| # but works for both ComplexF32 and ComplexF64 | ||
| @inline function _cuda_atomic_cas!(ptr::Core.LLVMPtr{Complex{T},A}, cmp::Complex{T}, new::Complex{T}) where {T<:Union{Float32,Float64},A} | ||
| # Get pointers to real and imaginary components | ||
| ptr_re = Base.bitcast(Core.LLVMPtr{T,A}, ptr) | ||
| ptr_im = Base.bitcast(Core.LLVMPtr{T,A}, ptr + sizeof(T)) | ||
|
|
||
| # CAS on real part | ||
| old_re = CUDA.atomic_cas!(ptr_re, cmp.re, new.re) | ||
| # CAS on imaginary part | ||
| old_im = CUDA.atomic_cas!(ptr_im, cmp.im, new.im) | ||
|
|
||
| # Return just the old value for consistency with non-Complex CUDA CAS | ||
| # Note: The caller checks success by comparing old === expected | ||
| # This works because if both components match, the Complex values will be equal | ||
| return Complex{T}(old_re, old_im) | ||
| 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 | ||
| old = _cuda_atomic_modify!(ptr, op, x) | ||
| return old => op(old, x) | ||
| end | ||
|
|
||
| # Native CUDA atomic operations for supported types | ||
| @inline function _cuda_atomic_modify!(ptr::Core.LLVMPtr{T,A}, op::OP, x::T) where {T,A,OP} | ||
| 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 | ||
|
|
||
| # Complex atomic operations - separate atomics on real and imaginary parts | ||
| # This works for operations that decompose component-wise (+, -, right) | ||
| # Note: This provides per-component atomicity, not full Complex atomicity | ||
| # (other threads may observe intermediate states, but final result is correct) | ||
| @inline function _cuda_atomic_modify!(ptr::Core.LLVMPtr{Complex{T},A}, op::OP, x::Complex{T}) where {T<:Union{Float32,Float64},A,OP} | ||
|
Comment on lines
+83
to
+87
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you are not gurantueed that a user is only using one kind of atomic operation on a memory location. (e.g. someone doing a mul for good measure). |
||
| # Get pointers to real and imaginary components | ||
| ptr_re = Base.bitcast(Core.LLVMPtr{T,A}, ptr) | ||
| ptr_im = Base.bitcast(Core.LLVMPtr{T,A}, ptr + sizeof(T)) | ||
|
|
||
| if op === (+) | ||
| old_re = CUDA.atomic_add!(ptr_re, x.re) | ||
| old_im = CUDA.atomic_add!(ptr_im, x.im) | ||
| return Complex{T}(old_re, old_im) | ||
| elseif op === (-) | ||
| old_re = CUDA.atomic_sub!(ptr_re, x.re) | ||
| old_im = CUDA.atomic_sub!(ptr_im, x.im) | ||
| return Complex{T}(old_re, old_im) | ||
| else | ||
| # For other operations (like right for swap), use CAS loop | ||
| # Read the old value component by component (not atomic together) | ||
| old_re = CUDA.atomic_add!(ptr_re, zero(T)) # atomic read | ||
| old_im = CUDA.atomic_add!(ptr_im, zero(T)) # atomic read | ||
| old = Complex{T}(old_re, old_im) | ||
|
|
||
| # Compute new value | ||
| new = op(old, x) | ||
|
|
||
| # Try to swap using CAS (will only succeed if value hasn't changed) | ||
| # This is a simplified version - a full CAS loop would be more robust | ||
| _cuda_atomic_cas!(ptr, old, new) | ||
|
|
||
| # Return the old value we read | ||
| return old | ||
| end | ||
| end | ||
|
|
||
| end # module AtomixCUDAExt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof this is a no-go in my opinion. You will thus easily get torn writes.
I think this would need to use 128-byte atomics