Skip to content

[Caspar] Fix FlushSumShared warp reduction and zero-initialize reduction accumulators - #1

Open
sarlinpe wants to merge 1 commit into
colmap-patchesfrom
caspar-fix-flush-sum-shared
Open

sarlinpe wants to merge 1 commit into
colmap-patchesfrom
caspar-fix-flush-sum-shared

Conversation

@sarlinpe

@sarlinpe sarlinpe commented Dec 4, 2026

Copy link
Copy Markdown
Member

Mirrors symforce-org#472, rebased onto upstream main at acdedd0b.

Changes relative to the upstream PR: its three commits are squashed into one. The explicit warp-shuffle reduction (LabeledWarpSum) is only used under Clang CUDA; nvcc keeps the cg::labeled_partition + cg::reduce path, as requested in the upstream review. The zero-initialization is unconditional.


Summary

Follows up on symforce-org#469 (a768308e) with two related fixes in symforce/caspar/source/runtime/memops.cuh when compiling Caspar kernels with Clang CUDA:

  1. Zero-initialize StorageT tot = 0; in FlushSumShared, FlushSumBlock, and FlushSumBlockAdd:
    When a thread is inactive (idx.argsort == 0xffff or !valid), tot was previously uninitialized across __syncthreads() barriers, triggering -Wsometimes-uninitialized under Clang and creating an undef/poison phi node in LLVM IR.
  2. Replace cg::labeled_partition + cg::reduce in FlushSumShared with an explicit warp-shuffle reduction:
    cg::labeled_partition(cg::coalesced_threads(), unique) was called outside the if (idx.argsort != 0xffff) guard while cg::reduce(group, ...) was called inside it. For float and double, cg::reduce over a coalesced_group falls back to cg::details::inclusive_scan_non_contiguous, which runs a warp-shuffle loop (__shfl_sync(mask, ...)) whose trip count (groupSize) varies per partition within the same warp. Under Clang CUDA, this divergent shuffle loop produces corrupted reductions when multiple factors in a warp share a target node. Replacing it with a uniform warp-shuffle reduction over __activemask() ensures all active threads execute __shfl_sync in lockstep.
  3. Add a test case in caspar_solver_cuda_test.cc:
    Existing solver tests used kNumNodes = 4096 (an exact multiple of the 1024-thread block size, leaving no inactive lanes) with a 1-to-1 factor-to-node mapping (indices[i] = i, so all partitions in FlushSumShared had size 1). Added a test case with shared nodes (kNumSharedNodes = 64) and a partial block (kNumFactors = 1500).

…ion accumulators

In FlushSumShared, cg::labeled_partition(cg::coalesced_threads(), unique)
followed by cg::reduce inside the divergent (idx.argsort != 0xffff) branch
produces incorrect warp reductions when compiled with Clang CUDA, where
floating-point coalesced_group reductions fall back to
inclusive_scan_non_contiguous with per-group variable shuffle loop trip
counts.

Under Clang, replace it with an explicit uniform warp-shuffle reduction over
__activemask() (LabeledWarpSum). nvcc keeps the cg::labeled_partition +
cg::reduce path, since the explicit reduction regresses performance there.

Zero-initialize tot in FlushSumShared, FlushSumBlock, and FlushSumBlockAdd
unconditionally (following up on a768308): it was uninitialized across
__syncthreads() in inactive lanes.

Also add a test case in caspar_solver_cuda_test covering shared nodes and
partial blocks.

Signed-off-by: Paul-Edouard Sarlin <15985472+sarlinpe@users.noreply.github.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