Skip to content

Fix bar.red (__syncthreads_count/and/or) parsing and results - #155

Open
shatalov-alex wants to merge 4 commits into
accel-sim:devfrom
shatalov-alex:fix-bar-red
Open

shatalov-alex wants to merge 4 commits into
accel-sim:devfrom
shatalov-alex:fix-bar-red

Conversation

@shatalov-alex

Copy link
Copy Markdown

Four fixes to bar.red (__syncthreads_count, __syncthreads_and, __syncthreads_or), one commit each. Before them, a kernel using __syncthreads_count aborted in the PTX parser; once it parsed, the results were wrong in both simulation modes.

Commit Before
Accept the .popc option of bar.red bar.red.popc.u32 hits the assertion for unhandled instruction options in the ptx_instruction constructor (.and and .or were handled, .popc was not)
Start each bar.red reduction at 0 in the timing model reduction_storage is zeroed only when the core is created, so each reduction on a barrier builds on the previous ones in that CTA slot
Deliver bar.red results in functional simulation with PTX_SIM_MODE_FUNC=1 the destination is never written: only the timing model's broadcast_barrier_reduction runs the callbacks
Give bar.red.and and bar.red.or the right predicate the result is written into the .pred destination as a plain 0/1, but predicate registers hold a zero flag (as setp_impl writes them), so it is inverted; .and also accumulates with &= into storage that starts at 0

Testing

Results of the reproducer below for block 0, after each commit (dev aborts in the parser). Each commit builds on its own with the Makefile flow on aarch64 (CUDA 12.6.3, gcc 13.2), with #153's Makefile change applied, which aarch64 needs. clang-format 16.0.6 reports no changes.

After PTX_SIM_MODE_FUNC=1 timing model
dev parser assertion parser assertion
.popc 0 1 1 1 1 0 10 1 1 0 0 4
timing reset 0 1 1 1 1 0 10 1 1 0 1 3
functional delivery 10 1 1 0 1 3 10 1 1 0 1 3
and/or predicate 10 1 0 1 0 3 10 1 0 1 0 3

Expected (CUDA semantics): 10 1 0 1 0 3. Block 1 gives the same. I found the .popc problems running the rasterizer of Microsoft TRELLIS.2 (o-voxel), and the .and/.or one while testing these fixes. I have not built or run them on x86, and I could not run the short-tests regressions (self-hosted runner).

This PR is independent of #154 (it applies to dev on its own) and merges cleanly with it.

Reproducer (nvcc -O2 -cudart shared -gencode arch=compute_75,code=compute_75, SM75_RTX2060 config)
#include <cstdio>
__global__ void k(int *out) {
  int t = threadIdx.x;
  int c = __syncthreads_count(t < 10);
  int a1 = __syncthreads_and(t < 1000);  // all true -> 1
  int a0 = __syncthreads_and(t < 10);    // some false -> 0
  int o1 = __syncthreads_or(t == 5);     // one true -> nonzero
  int o0 = __syncthreads_or(t > 1000);   // none -> 0
  int c2 = __syncthreads_count(t < 3);
  if (t == 0) {
    int *o = out + blockIdx.x * 6;
    o[0] = c; o[1] = a1; o[2] = a0; o[3] = o1 != 0; o[4] = o0; o[5] = c2;
  }
}
int main() {
  int *d, h[12];
  cudaMalloc(&d, sizeof h);
  k<<<2, 64>>>(d);
  cudaMemcpy(h, d, sizeof h, cudaMemcpyDeviceToHost);
  for (int b = 0; b < 2; b++)
    printf("block %d: %d %d %d %d %d %d (want 10 1 0 1 0 3)\n", b, h[b * 6], h[b * 6 + 1],
           h[b * 6 + 2], h[b * 6 + 3], h[b * 6 + 4], h[b * 6 + 5]);
}

bar.red.popc.u32, which nvcc emits for __syncthreads_count, stopped the
PTX parser at its assertion for unhandled instruction options: the
grammar accepts .popc as an atomic_operation_spec, but the option
switch in the ptx_instruction constructor handled only .and and .or of
the three bar.red operators. Store it as the operation, like them.
reduction_storage was zeroed once, when the core was created, and never
again: every bar.red on a barrier added to the counts of all the earlier
ones in the same CTA slot, so a second __syncthreads_count in a kernel
returned the sum of both counts (and a CTA that later reused the slot
started from the last one).

Reset the barrier's storage once broadcast_barrier_reduction has given
every waiting thread its result, before any warp can reach the next
reduction.
With PTX_SIM_MODE_FUNC=1 the destination of bar.red was never written:
the timing model delivers the result through
shader_core_ctx::broadcast_barrier_reduction when the barrier releases,
and functionalCoreSim had no counterpart, so __syncthreads_count
returned whatever the register held (0 in practice).

Keep each warp's bar.red instruction while it waits, and when every
live warp has reached the barrier, run its callbacks and reset the
barrier's storage, as the timing model does.
Both wrote the reduction as a plain 0 or 1 into their .pred destination.
GPGPU-Sim's predicate registers hold a zero flag (setp_impl writes 1 for
false), so every result was inverted. .and also accumulated with &= into
storage that starts at 0, so it was always 0 before the inversion:
__syncthreads_and returned true and __syncthreads_or returned the
opposite of the vote.

Count the false votes for .and (true when there are none), turn the
count into the operator's result in bar_callback, and write predicate
destinations in the zero-flag form.
shatalov-alex added a commit to shatalov-alex/qpel that referenced this pull request Nov 13, 2026
…mitted upstream (accel-sim/gpgpu-sim_distribution#155); self-test checks __syncthreads_and/or; image qpel11
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