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/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 74fe1ed..336590d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,11 +1,21 @@ # 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) + + 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; +}