You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
GPU streams: work on the task's default stream is not ordered before @hide_communication / @overlap (wrong results with Metal) #233
@hide_communication and @overlap launch kernels on streams that ParallelStencil manages itself (@get_stream, @get_priority_stream) and synchronize those streams at the end of the block. Work that was issued before the block on the task's default stream (array allocation/initialization, broadcasts, @parallel calls) is never ordered before those launches. The GPU backends only order work within a stream; across the streams of one task there is no implicit ordering.
With Metal.jl (current release, command batching), this gives wrong results: the default queue is a BatchedCommandQueue that submits lazily, so e.g. the zero-fill of A = @zeros(...) is still pending when the block starts, and is executed after the block when the queue is next flushed (e.g. by Array(A)), overwriting the results. On CUDA and AMDGPU the same race exists but is normally hidden by timing (the preceding @parallel calls are synchronous, so the default stream is usually idle).
Reproducer (Apple silicon)
using ParallelStencil
@init_parallel_stencil(Metal, Float32, 3)
@parallel_indices (ix,iy,iz) functionfill_value!(A, value) A[ix,iy,iz] = value; returnend
A =@zeros(4, 3, 2)
B =@zeros(4, 3, 2)
@overlapbegin@parallelfill_value!(A, 1f0)
@parallelfill_value!(B, 2f0)
endall(Array(A) .==1f0), all(Array(B) .==2f0) # (false, false): A and B are all zeros
First seen in the macOS CI job of #232 (test/ParallelKernel/test_overlap.jl and test_hide_communication.jl, Metal): all result checks fail with all-zero arrays.
The GPU expansions of @overlap and @hide_communication now start with @synchronize() (synchronization of the task's default stream/queue) before launching on the ParallelStencil streams. The cost is negligible in a time loop (the default stream is idle after a synchronous @parallel), and it closes the latent race on CUDA/AMDGPU too. The synchronizations at the end of the blocks already order everything that follows.
Longer term
The underlying mismatch is between the backends' task-local stream model (one default stream per task, concurrency through tasks) and ParallelStencil scheduling several streams manually from one task. On Metal, the batched queues also mean that the kernels on the ParallelStencil queues are only submitted at the respective synchronize, so no actual overlap happens there. A redesign would run each overlapped computation in its own task on that task's default stream and order through task synchronization. ImplicitGlobalGrid's update_halo! manages its own streams the same way, so it should be covered by the same design (and probably has the same latent race).
Closing: the wrong results reported here are fixed, and the open points were settled in the stream-ordering work tracked in eth-cscs/ImplicitGlobalGrid.jl#143.
Not covered: overlap on Metal. The batched queues submit the kernels only at the synchronization, so @hide_communication and @overlap give correct results on Metal but do not overlap. This was not looked at further (no hardware); a separate issue can track it if needed.
Activity
luraess commentedon Oct 30, 2026
Closing: the wrong results reported here are fixed, and the open points were settled in the stream-ordering work tracked in eth-cscs/ImplicitGlobalGrid.jl#143.
@hide_communicationand@overlapfirst synchronize the task's default stream. Overlap in@hide_communicationand@overlapagain with CUDA.jl ≥ 6.3 (and AMDGPU.jl) #245 keeps this. The macOS CI job, where the failure was first seen, is green onmain.update_halo!): works the same way by design:update_halo!does not synchronize with work issued before the call; the caller does. This contract is now documented (Document the asynchronous execution of update_halo!, order its local exchange (#141), AMD analog of #145 eth-cscs/ImplicitGlobalGrid.jl#147). Inside@hide_communication, the synchronization above already covers it.update_halo!on GPU arrays does not wait for kernels still running on the task-local stream (CUDA.jl < 6.3) eth-cscs/ImplicitGlobalGrid.jl#141 and Acoustic2D + PML example #143 was to keep the streams managed by ParallelStencil and ImplicitGlobalGrid and to document the contract. The overlap regression with CUDA.jl ≥ 6.3 and AMDGPU.jl was fixed within that design (Overlap in@hide_communicationand@overlapagain with CUDA.jl ≥ 6.3 (and AMDGPU.jl) #245).@hide_communicationand@overlapgive correct results on Metal but do not overlap. This was not looked at further (no hardware); a separate issue can track it if needed.