From e26657f9f72a6febac3deb0027a10661fb3905a0 Mon Sep 17 00:00:00 2001 From: Jeff Daily Date: Thu, 19 Nov 2026 03:33:48 +0000 Subject: [PATCH 1/2] [ROCm] Sort all 64 bits where builders key on the upper half On ROCm, the spatial-median, SAH and ELH GPU builders return trees whose leaves reference the wrong primitives (some primitives in no leaf, others in two) once a leaf holds more than one primitive and there are more than 1024 primitives, and rebinRadixBuilder can fault with an illegal memory access on clustered input. The radix builders are not affected. Cause: these builders sort packed 64-bit words by bits [32,64), the node ID of a PrimState or the range begin of a RebinRange. rocPRIM's radix sort mis-orders 64-bit keys when end_bit is 64 and begin_bit is not 0, for 1025 to 2^20 keys (ROCm/rocm-libraries#16299), so the node IDs are not grouped and the leaf offsets point into other leaves. Change: on HIP only, the four affected sorts use bits [0,64). That keeps the order of the upper halves and only orders equal upper halves by their lower half (the prim ID with its done bit, or a distinct range begin), which none of the callers depend on. Bits [32,63) would also avoid the rocPRIM defect and sort slightly faster, but they are not correct for the prim builders: invalid prims carry node ID (uint32_t)-1, which under [32,63) gets the same key as node 0x7FFFFFFF, and node IDs at or above 2^31 lose their top bit. The CUDA build is unchanged: the new macro expands to 32 there. tests/builder_leaves.cu builds trees with all six binary builders over 1000 to 200000 uniform, clustered and partly empty boxes with leaf sizes 1, 4 and 8, and checks on the host that every valid primitive is in exactly one reachable leaf, no empty box is referenced, and every box and child lies inside its node. It builds for CUDA and for HIP. This change was prepared with the help of an AI coding agent (Claude). Test Plan: On an AMD GPU (MI355X, gfx950, ROCm 10.2.0): 135/135 checks pass. Without the builder change the same test reports 17 wrong trees before rebinRadixBuilder faults on the clustered 200000-box case. ``` cmake -S . -B build-hip -G Ninja -DCUBQL_USE_HIP=ON -DCMAKE_HIP_ARCHITECTURES=gfx950 -DCMAKE_BUILD_TYPE=Release -DCUBQL_BUILD_TESTS=ON cmake --build build-hip --target cubql_builder_leaves ctest --test-dir build-hip -R builder_leaves --output-on-failure ``` CUDA (nvcc 12.8): the library and both tests compile, and the preprocessed builders are identical before and after the change. ``` cmake -S . -B build-cuda -G Ninja -DCMAKE_CUDA_ARCHITECTURES=80 -DCMAKE_BUILD_TYPE=Release -DCUBQL_BUILD_TESTS=ON cmake --build build-cuda --target cuBQL_cuda_float3 cubql_builder_leaves cubql_morton_sort_range nvcc -std=c++17 -x cu -E -Xcompiler -P -DCUBQL_HAVE_CUDA=1 -DCUBQL_INSTANTIATE_T=float -DCUBQL_INSTANTIATE_D=3 -arch=sm_80 -I. cuBQL/builder/cuda/instantiate_builders.cu ``` --- cuBQL/builder/cuda/builder_common.h | 13 +++ cuBQL/builder/cuda/elh_builder.h | 4 +- cuBQL/builder/cuda/rebinMortonBuilder.h | 4 +- cuBQL/builder/cuda/sah_builder.h | 4 +- cuBQL/builder/cuda/sm_builder.h | 4 +- tests/CMakeLists.txt | 9 ++ tests/builder_leaves.cu | 143 ++++++++++++++++++++++++ 7 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 tests/builder_leaves.cu diff --git a/cuBQL/builder/cuda/builder_common.h b/cuBQL/builder/cuda/builder_common.h index 63a9a33..bd97be4 100644 --- a/cuBQL/builder/cuda/builder_common.h +++ b/cuBQL/builder/cuda/builder_common.h @@ -20,6 +20,19 @@ namespace cub { } #endif +/* begin bit for radix sorts keyed on the upper 32 bits of a 64-bit + word. rocPRIM's radix sort mis-orders 64-bit keys when end_bit is + 64 and begin_bit is not 0 (1025 to 2^20 keys), so on HIP sort all + 64 bits: that keeps the order of the upper halves and only orders + equal upper halves by their lower half, which no caller relies on. + Bits [32,63) would not do: invalid prims carry node ID + (uint32_t)-1, whose top bit must take part in the sort */ +#ifdef __HIPCC__ +# define CUBQL_UPPER_HALF_SORT_BEGIN_BIT 0 +#else +# define CUBQL_UPPER_HALF_SORT_BEGIN_BIT 32 +#endif + namespace cuBQL { namespace gpuBuilder_impl { diff --git a/cuBQL/builder/cuda/elh_builder.h b/cuBQL/builder/cuda/elh_builder.h index 7dc44db..69d9635 100644 --- a/cuBQL/builder/cuda/elh_builder.h +++ b/cuBQL/builder/cuda/elh_builder.h @@ -510,13 +510,13 @@ namespace cuBQL { cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); _ALLOC(d_temp_storage,temp_storage_bytes,s,memResource); rc = cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); rc = rc; CUBQL_CUDA_CALL(StreamSynchronize(s)); _FREE(d_temp_storage,s,memResource); diff --git a/cuBQL/builder/cuda/rebinMortonBuilder.h b/cuBQL/builder/cuda/rebinMortonBuilder.h index 424cec6..ddf3aa9 100644 --- a/cuBQL/builder/cuda/rebinMortonBuilder.h +++ b/cuBQL/builder/cuda/rebinMortonBuilder.h @@ -1069,7 +1069,7 @@ namespace cuBQL { (nullptr,cub_tempMemSize, /*key+values in:*/ d_keysValues_in, /*key+values out:*/ d_keysValues_out, - N,32,64,s); + N,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); // allocate temp mem and output arrays void *d_tempMem = 0; @@ -1080,7 +1080,7 @@ namespace cuBQL { (d_tempMem,cub_tempMemSize, /*key+values in:*/ d_keysValues_in, /*key+values out:*/ d_keysValues_out, - N,32,64,s); + N,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); _FREE(d_tempMem,s,memResource); } diff --git a/cuBQL/builder/cuda/sah_builder.h b/cuBQL/builder/cuda/sah_builder.h index 423d6fb..acedfae 100644 --- a/cuBQL/builder/cuda/sah_builder.h +++ b/cuBQL/builder/cuda/sah_builder.h @@ -508,13 +508,13 @@ namespace cuBQL { cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); _ALLOC(d_temp_storage,temp_storage_bytes,s,memResource); rc = cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); rc = rc; // ignore 'unused' warning CUBQL_CUDA_CALL(StreamSynchronize(s)); _FREE(d_temp_storage,s,memResource); diff --git a/cuBQL/builder/cuda/sm_builder.h b/cuBQL/builder/cuda/sm_builder.h index a0e700b..fb9888c 100644 --- a/cuBQL/builder/cuda/sm_builder.h +++ b/cuBQL/builder/cuda/sm_builder.h @@ -490,13 +490,13 @@ namespace cuBQL { cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); _ALLOC(d_temp_storage,temp_storage_bytes,s,memResource); rc = cub::DeviceRadixSort::SortKeys((void*&)d_temp_storage, temp_storage_bytes, (uint64_t*)primStates, (uint64_t*)sortedPrimStates, - numPrims,32,64,s); + numPrims,CUBQL_UPPER_HALF_SORT_BEGIN_BIT,64,s); rc = rc; _FREE(d_temp_storage,s,memResource); // ================================================================== diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 74fe1ed..6b7f7e4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,3 +9,12 @@ if (CUBQL_HAVE_CUDA) CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON) add_test(NAME cubql_morton_sort_range COMMAND cubql_morton_sort_range) endif() + +if (CUBQL_HAVE_CUDA OR CUBQL_HAVE_HIP) + add_executable(cubql_builder_leaves builder_leaves.cu) + if (CUBQL_USE_HIP) + set_source_files_properties(builder_leaves.cu PROPERTIES LANGUAGE HIP) + endif() + target_link_libraries(cubql_builder_leaves PRIVATE cuBQL_cuda_float3) + add_test(NAME cubql_builder_leaves COMMAND cubql_builder_leaves) +endif() diff --git a/tests/builder_leaves.cu b/tests/builder_leaves.cu new file mode 100644 index 0000000..b0293d2 --- /dev/null +++ b/tests/builder_leaves.cu @@ -0,0 +1,143 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +// Builds BinaryBVHs with every GPU builder and checks each tree on the host: +// every valid prim is referenced by exactly one reachable leaf, no invalid +// (empty-box) prim is referenced, and every prim box and child node lies +// inside its parent's bounds. + +#include "cuBQL/bvh.h" +#include "cuBQL/builder/cuda.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +using box3 = cuBQL::box_t; +using bvh3 = cuBQL::BinaryBVH; + +void check(cudaError_t result) +{ + if (result != cudaSuccess) + throw std::runtime_error(cudaGetErrorString(result)); +} + +bool inside(const box3 &in, const box3 &out) +{ + for (int d=0;d<3;d++) + if (in.lower[d] < out.lower[d] || in.upper[d] > out.upper[d]) return false; + return true; +} + +bool valid(const box3 &b) { return b.lower[0] <= b.upper[0]; } + +struct Errors { size_t missing = 0, duplicated = 0, invalid = 0, containment = 0; }; + +Errors checkTree(const bvh3 &bvh, const std::vector &boxes) +{ + std::vector nodes(bvh.numNodes); + std::vector primIDs(bvh.numPrims); + check(cudaMemcpy(nodes.data(),bvh.nodes,nodes.size()*sizeof(nodes[0]), + cudaMemcpyDeviceToHost)); + check(cudaMemcpy(primIDs.data(),bvh.primIDs,primIDs.size()*sizeof(primIDs[0]), + cudaMemcpyDeviceToHost)); + Errors e; + std::vector seen(boxes.size(),0); + std::vector stack = {0}; + while (!stack.empty()) { + const auto &node = nodes[stack.back()]; + stack.pop_back(); + if (node.admin.count == 0) { + for (int c=0;c<2;c++) { + const uint32_t child = node.admin.offset+c; + if (!inside(nodes[child].bounds,node.bounds)) e.containment++; + stack.push_back(child); + } + continue; + } + for (uint32_t i=0;i= boxes.size() || !valid(boxes[primID])) { e.invalid++; continue; } + seen[primID]++; + if (!inside(boxes[primID],node.bounds)) e.containment++; + } + } + for (size_t i=0;i 1; + } + return e; +} + +enum Scene { UNIFORM, CLUSTERED, WITH_EMPTY }; +const char *sceneName[] = { "uniform", "clustered", "with-empty" }; + +std::vector makeBoxes(size_t n, Scene scene, std::mt19937 &rng) +{ + std::uniform_real_distribution u(0.f,1.f); + std::vector boxes(n); + for (size_t i=0;i", "rebinRadix" + }; + std::mt19937 rng(42); + int failures = 0; + for (Scene scene : {UNIFORM, CLUSTERED, WITH_EMPTY}) + for (size_t n : {1000, 20000, 200000}) + for (int leafSize : {1, 4, 8}) { + const std::vector boxes = makeBoxes(n,scene,rng); + box3 *d_boxes = nullptr; + check(cudaMalloc((void **)&d_boxes,n*sizeof(box3))); + check(cudaMemcpy(d_boxes,boxes.data(),n*sizeof(box3),cudaMemcpyHostToDevice)); + // the radix builders require nonempty boxes + const int numBuilders = scene == WITH_EMPTY ? 3 : 6; + for (int b=0;b(bvh,d_boxes,(uint32_t)n,config); + else cuBQL::cuda::rebinRadixBuilder(bvh,d_boxes,(uint32_t)n,config); + check(cudaDeviceSynchronize()); + const Errors e = checkTree(bvh,boxes); + const bool ok = !(e.missing || e.duplicated || e.invalid || e.containment); + failures += !ok; + printf("%-10s n=%-6zu leaf=%d %-15s %s (missing %zu, duplicated %zu, " + "invalid %zu, containment %zu)\n", + sceneName[scene],n,leafSize,builderName[b],ok ? "ok" : "FAILED", + e.missing,e.duplicated,e.invalid,e.containment); + cuBQL::cuda::free(bvh); + } + check(cudaFree(d_boxes)); + } + printf("%s\n",failures ? "FAILED" : "PASSED"); + return failures ? 1 : 0; +} From 1a2b9bb3d90ae5e6d0513ed217a2437550656158 Mon Sep 17 00:00:00 2001 From: Jeff Daily Date: Thu, 19 Nov 2026 03:45:03 +0000 Subject: [PATCH 2/2] [ROCm] Build and run the Morton sort range test on HIP tests/morton_sort_range.cu covers the radix builder and its 32- and 64-bit Morton key entry points, but it was built only for CUDA, so a HIP build never ran it. It needs four more CUDA runtime names that the HIP block of cuBQL/math/common.h did not map (cudaDeviceProp, cudaGetDeviceProperties, cudaRuntimeGetVersion, cudaDriverGetVersion, used by its benchmark report). With those mapped, the test builds under CUBQL_USE_HIP like builder_leaves.cu, with its source marked LANGUAGE HIP. The CUDA build is unchanged: the new aliases are inside the __HIPCC__ block. This change was prepared with the help of an AI coding agent (Claude). Test Plan: On an AMD GPU (MI355X, gfx950, ROCm 10.2.0): both tests pass; cubql_morton_sort_range reports status=PASS for all 27 cases and query_oracle=PASS, and its --benchmark mode reports validation=PASS. ``` cmake -S . -B build-hip -G Ninja -DCUBQL_USE_HIP=ON -DCMAKE_HIP_ARCHITECTURES=gfx950 -DCMAKE_BUILD_TYPE=Release -DCUBQL_BUILD_TESTS=ON cmake --build build-hip --target cubql_morton_sort_range cubql_builder_leaves ctest --test-dir build-hip --output-on-failure ./build-hip/cubql_morton_sort_range --benchmark ``` CUDA (nvcc 12.8): the library and both tests compile, and the preprocessed instantiate_builders.cu and morton_sort_range.cu are identical to those of the base commit. ``` cmake -S . -B build-cuda -G Ninja -DCMAKE_CUDA_ARCHITECTURES=80 -DCMAKE_BUILD_TYPE=Release -DCUBQL_BUILD_TESTS=ON cmake --build build-cuda --target cuBQL_cuda_float3 cubql_builder_leaves cubql_morton_sort_range ``` --- cuBQL/math/common.h | 4 ++++ tests/CMakeLists.txt | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cuBQL/math/common.h b/cuBQL/math/common.h index db3865f..56e3178 100644 --- a/cuBQL/math/common.h +++ b/cuBQL/math/common.h @@ -37,6 +37,7 @@ using cudaError_t = hipError_t; using cudaStream_t = hipStream_t; using cudaEvent_t = hipEvent_t; using cudaMemPool_t = hipMemPool_t; +using cudaDeviceProp = hipDeviceProp_t; # define cudaSuccess hipSuccess # define cudaMemcpyDeviceToHost hipMemcpyDeviceToHost # define cudaMemcpyHostToDevice hipMemcpyHostToDevice @@ -50,6 +51,9 @@ using cudaMemPool_t = hipMemPool_t; # define cudaGetDevice hipGetDevice # define cudaSetDevice hipSetDevice # define cudaGetDeviceCount hipGetDeviceCount +# define cudaGetDeviceProperties hipGetDeviceProperties +# define cudaRuntimeGetVersion hipRuntimeGetVersion +# define cudaDriverGetVersion hipDriverGetVersion # define cudaMalloc hipMalloc # define cudaMallocHost hipHostMalloc # define cudaFree hipFree diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6b7f7e4..336590d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,16 +1,17 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -if (CUBQL_HAVE_CUDA) +if (CUBQL_HAVE_CUDA OR CUBQL_HAVE_HIP) add_executable(cubql_morton_sort_range morton_sort_range.cu) + if (CUBQL_USE_HIP) + set_source_files_properties(morton_sort_range.cu PROPERTIES LANGUAGE HIP) + endif() target_link_libraries(cubql_morton_sort_range PRIVATE cuBQL) target_compile_definitions(cubql_morton_sort_range PRIVATE CUBQL_HAVE_CUDA=1) set_target_properties(cubql_morton_sort_range PROPERTIES CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON) add_test(NAME cubql_morton_sort_range COMMAND cubql_morton_sort_range) -endif() -if (CUBQL_HAVE_CUDA OR CUBQL_HAVE_HIP) add_executable(cubql_builder_leaves builder_leaves.cu) if (CUBQL_USE_HIP) set_source_files_properties(builder_leaves.cu PROPERTIES LANGUAGE HIP)