Skip to content

Make GPU- and system-scope memory barriers wait for store acks - #152

Open
karthikbhata97 wants to merge 1 commit into
accel-sim:devfrom
karthikbhata97:membar-wait-for-store-acks
Open

karthikbhata97 wants to merge 1 commit into
accel-sim:devfrom
karthikbhata97:membar-wait-for-store-acks

Conversation

@karthikbhata97

@karthikbhata97 karthikbhata97 commented Nov 4, 2026 •

Copy link
Copy Markdown

Problem

shader_core_ctx::warp_waiting_at_mem_barrier() (src/gpgpu-sim/shader.cc) releases a warp from a memory barrier as soon as m_scoreboard->pendingWrites(warp_id) is false:

bool shader_core_ctx::warp_waiting_at_mem_barrier(unsigned warp_id) {
  if (!m_warp[warp_id]->get_membar()) return false;
  if (!m_scoreboard->pendingWrites(warp_id)) {
    m_warp[warp_id]->clear_membar();
    ...
      // Based on Nvidia Doc, at MEM barrier, we have to
      //(1) wait for all pending writes till they are acked

The scoreboard reserves only instruction output registers (Scoreboard::reserveRegisters). A store has no output register, so a fence never waits for the warp's stores to be acknowledged, even though the comment above says it must. The per-warp store counter already exists: shd_warp_t::m_stores_outstanding / stores_done() is incremented when the LDST unit sends a store request and decremented in shader_core_ctx::store_ack(). Today it is checked only when the warp exits.

As a result, membar.cta, membar.gl and membar.sys all cost the same, and GPU- and system-scope fences are modeled as too cheap. Functional results are not affected, because the functional model executes in order. The same code is in gpgpu-sim/gpgpu-sim_distribution master.

Fix

  • inst_t gets a memory-barrier scope (membar_scope: MEMBAR_SCOPE_{UNKNOWN,CTA,GPU,SYS}), next to the existing fence-proxy fields.

  • PTX mode: ptx_instruction::set_opcode_and_latency() sets the scope from the membar level (.cta / .gl / .sys).

  • issue_warp() passes the scope to the warp: set_membar(next_inst->get_membar_scope()).

  • warp_waiting_at_mem_barrier(): a barrier that is not CTA-scope keeps the warp waiting until two things hold:

    • every instruction the warp issued up to and including the barrier has left the pipeline (num_issued_inst_in_pipeline() == 0);
    • the warp has no unacknowledged stores (stores_done()).

    After that, the existing register-write check and the L1 flush apply as before. CTA-scope barriers keep today's behavior.

Why the pipeline condition: inc_store_req() runs only when the LDST unit sends the store's requests. A store issued one or two cycles before the fence is usually still in the operand collector or the LDST pipe when the fence is checked, so stores_done() alone misses it. In the microbenchmark below, stores_done() alone captures less than half of the effect (QV100: 7357 cycles, compared with 8640 for the full fix). The fence itself goes down the MEM pipe, so the condition also covers the fence's own trip through the LDST unit. This cannot block forever: every in-flight instruction completes without the warp issuing anything else, and a warp already needs stores_done() before it can exit.

Unknown scope waits (conservative). A barrier whose scope is not set (MEMBAR_SCOPE_UNKNOWN) is treated like a GPU-scope one. For an unknown fence, waiting can only make timing too pessimistic, while not waiting would silently repeat the bug. Trace-driven MEMBARs are in this case until the trace front end sets the scope. That is a companion PR in accel-sim-framework: accel-sim/accel-sim-framework#580. Until it is merged, trace-driven MEMBAR.CTA also waits for store acks.

Tests

Built on Ubuntu with CUDA 12.4 (the Makefile build for PTX mode; the accel-sim-framework dev CMake build, which also compiles this tree, for trace mode). No new compiler warnings. format-code.sh (clang-format 16.0.6) leaves the tree unchanged.

Microbenchmark, PTX mode. 80 CTAs x 256 threads, each doing 16 iterations of a global store followed by a fence. The table shows gpu_tot_sim_cycle, before -> after:

config no fence __threadfence_block() (membar.cta) __threadfence() (membar.gl) __threadfence_system() (membar.sys)
SM7_QV100 6348 -> 6348 6417 -> 6417 6417 -> 8640 (+34.6%) 6417 -> 8640
SM7_TITANV 2535 -> 2535 2595 -> 2595 2595 -> 3876 (+49.4%) 2595 -> 3876
SM75_RTX2060 8646 -> 8646 8730 -> 8730 8730 -> 10862 (+24.4%) 8730 -> 10862
SM86_RTX3070 7446 -> 7446 7462 -> 7462 7462 -> 9588 (+28.5%) 7462 -> 9588
SM80_A100 6410 -> 6410 6486 -> 6486 6486 -> 9800 (+51.1%) 6486 -> 9800
  • Before the fix, all three fence scopes cost the same.
  • With the fix, the no-fence and membar.cta runs give the same statistics output as before, line for line, apart from wall-clock rates and host addresses. This holds on all five configs.
  • The results are correct in every run.

threadFenceReduction (cuda-samples v12.4, --n=65536, SM7_QV100, PTX mode): gpu_tot_sim_cycle 855536 -> 873736 (+2.1%). The instruction count is the same and the GPU result matches the CPU result. This sample issues only one __threadfence() per block, so the effect is small.

rodinia_2.0-ft, PTX mode (SM7_QV100, the 10 apps and arguments of define-all-apps.yml, gpu-app-collection built with CUDA 12.4). None of these apps has a membar.

  • gpu_tot_sim_cycle, instruction counts and all other statistics are identical in every app, with one exception: per-stream *_stats_breakdown lines differ in bfs, lud, nw, srad_v2 and streamcluster.
  • Those same differences appear with a control build that only adds the unused m_membar_scope field to inst_t, with no logic change. Against that control build, this PR's statistics are identical in all 10 apps.
  • So the breakdown lines depend on memory layout, and this is pre-existing. The likely cause is that per-stream stats are keyed by the stream ID, which in PTX mode is a host pointer (kernel_stream_id changes from build to build).

Trace mode (accel-sim-framework dev built against this branch):

  • rodinia_2.0-ft V100 traces (the CI set) on SM7_QV100: the output is identical in all 10 apps.
  • The microbenchmark, traced with NVBit on an L40S as an sm_86 binary (MEMBAR.SC.CTA / MEMBAR.SC.GPU / MEMBAR.SC.SYS), on SM86_RTX3070:
    • no fence: 7415 -> 7415;
    • MEMBAR.SC.GPU and MEMBAR.SC.SYS: 7422 -> 9467 (+27.6%);
    • MEMBAR.SC.CTA with this PR alone: 7391 -> 9424 (the conservative unknown-scope wait);
    • MEMBAR.SC.CTA with this PR plus the companion PR: 7391, identical output to before.

Measured effect on real workloads. On a research fork of Accel-Sim, a prototype of this change (GPU-scope MEMBARs in SASS traces wait for store acks) changed the simulated time of fence-heavy kernels by up to 24%.

🤖 Generated with Claude Code

shader_core_ctx::warp_waiting_at_mem_barrier() released a warp from a
memory barrier as soon as the scoreboard had no pending register writes
for it. The scoreboard tracks only instruction output registers, and a
store has none, so a membar never waited for the warp's stores to be
acknowledged, although the comment in that function says a memory barrier
must "wait for all pending writes till they are acked". The per-warp store
counter shd_warp_t::m_stores_outstanding (stores_done()) already exists
but was consulted only at warp exit. As a result membar.cta, membar.gl and
membar.sys all cost the same, and GPU- and system-scope fences were modeled
as too cheap.

Record the scope of a memory barrier on inst_t (membar_scope:
MEMBAR_SCOPE_{UNKNOWN,CTA,GPU,SYS}), set it from the PTX membar level in
ptx_instruction::set_opcode_and_latency(), and pass it to the warp at
issue. A barrier that is not CTA-scope now also waits until every
instruction the warp issued up to and including the barrier has left the
pipeline (so their memory requests have been sent and counted) and until
stores_done(). CTA-scope barriers keep the old behavior. A barrier whose
scope is unknown waits for the stores too: that is never too optimistic,
and it is what trace-driven MEMBARs get until the trace front end sets the
scope from the SASS opcode.

Functional results are unchanged; only the timing of fences changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant