Skip to content

GPU streams: work on the task's default stream is not ordered before @hide_communication / @overlap (wrong results with Metal) #233

Closed

Description

@luraess

Summary

@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) function fill_value!(A, value) A[ix,iy,iz] = value; return end
A = @zeros(4, 3, 2)
B = @zeros(4, 3, 2)
@overlap begin
    @parallel fill_value!(A, 1f0)
    @parallel fill_value!(B, 2f0)
end
all(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.

Interim fix (#232)

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).

Activity

  1. luraess commented on Oct 30, 2026

    @luraess
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions