Repository navigation
Description
Summary
thrust::tabulate is significantly slower than equivalent thrust::transform, thrust::for_each_n, cub::DeviceFor::ForEach, and a direct CUDA kernel for a row-wise functor that reads a wide row and writes one output value.
This came up while investigating libcudf row hashing performance. I minimized it to a standalone CCCL + NVBench reproducer that does not depend on cuDF or RMM.
Reproducer
I have a self-contained reproducer with these files:
tabulate_reproducer.cunvbench_main.cuCMakeLists.txtREADME.md
The benchmark compares these variants over the same synthetic row-hash functor and data:
thrust::tabulate(output.begin(), output.end(), row_hasher)thrust::transform(counting_iterator, output.begin(), row_hasher)thrust::for_each_n(counting_iterator, lambda writes output[row])cub::DeviceFor::ForEach(counting_iterator range, lambda writes output[row])- direct CUDA grid-stride kernel
The core functor is:
struct row_hasher {
cuda::std::uint64_t const* columns{};
int num_rows{};
int num_cols{};
hash_type seed{};
__device__ hash_type operator()(int row_index) const
{
auto hash = mix64_to_32(columns[row_index], seed);
for (int column_index = 1; column_index < num_cols; ++column_index) {
auto const value = columns[static_cast<std::size_t>(column_index) * num_rows + row_index];
hash = hash_combine(hash, mix64_to_32(value, seed));
}
return hash;
}
};Full reproducer source:
// tabulate_reproducer.cu
#include <nvbench/nvbench.cuh>
#include <cub/device/device_for.cuh>
#include <cuda/std/cstdint>
#include <cuda/std/limits>
#include <cuda_runtime_api.h>
#include <thrust/count.h>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <thrust/for_each.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/tabulate.h>
#include <thrust/transform.h>
#include <string>
namespace {
using hash_type = cuda::std::uint32_t;
__host__ __device__ hash_type hash_combine(hash_type lhs, hash_type rhs)
{
return lhs ^ (rhs + 0x9e37'79b9u + (lhs << 6) + (lhs >> 2));
}
__host__ __device__ hash_type mix64_to_32(cuda::std::uint64_t key, hash_type seed)
{
key ^= static_cast<cuda::std::uint64_t>(seed) + 0x9e37'79b9'7f4a'7c15ull;
key ^= key >> 33;
key *= 0xff51afd7ed558ccdull;
key ^= key >> 33;
key *= 0xc4ceb9fe1a85ec53ull;
key ^= key >> 33;
return static_cast<hash_type>(key ^ (key >> 32));
}
struct row_hasher {
cuda::std::uint64_t const* columns{};
int num_rows{};
int num_cols{};
hash_type seed{};
__device__ hash_type operator()(int row_index) const
{
auto hash = mix64_to_32(columns[row_index], seed);
for (int column_index = 1; column_index < num_cols; ++column_index) {
auto const value = columns[static_cast<std::size_t>(column_index) * num_rows + row_index];
hash = hash_combine(hash, mix64_to_32(value, seed));
}
return hash;
}
};
__global__ void direct_kernel(row_hasher hasher, hash_type* output)
{
auto row_index = static_cast<int>(threadIdx.x + blockIdx.x * blockDim.x);
auto const stride = static_cast<int>(blockDim.x * gridDim.x);
for (; row_index < hasher.num_rows; row_index += stride) {
output[row_index] = hasher(row_index);
}
}
void check_cuda(cudaError_t status)
{
if (status != cudaSuccess) { throw std::runtime_error(cudaGetErrorString(status)); }
}
void bench_row_hash(nvbench::state& state)
{
auto const num_rows = static_cast<int>(state.get_int64("num_rows"));
auto const num_cols = static_cast<int>(state.get_int64("num_cols"));
auto const variant = state.get_string("variant");
thrust::device_vector<cuda::std::uint64_t> columns(
static_cast<std::size_t>(num_rows) * num_cols);
thrust::device_vector<hash_type> output(num_rows);
auto const init_hasher = [data = thrust::raw_pointer_cast(columns.data()),
num_rows,
num_cols] __device__(int index) {
auto const row = index % num_rows;
auto const col = index / num_rows;
data[index] = (static_cast<cuda::std::uint64_t>(col) << 32) ^
static_cast<cuda::std::uint64_t>(row * 1315423911u + col);
};
thrust::for_each_n(thrust::device,
thrust::make_counting_iterator<int>(0),
static_cast<int>(columns.size()),
init_hasher);
row_hasher const hasher{thrust::raw_pointer_cast(columns.data()), num_rows, num_cols, 0};
auto* output_ptr = thrust::raw_pointer_cast(output.data());
state.add_global_memory_reads<nvbench::int64_t>(static_cast<int64_t>(num_rows) * num_cols);
state.add_global_memory_writes<nvbench::uint32_t>(num_rows);
state.exec(nvbench::exec_tag::sync, [&](nvbench::launch& launch) {
auto const stream = launch.get_stream().get_stream();
if (variant == "tabulate") {
thrust::tabulate(thrust::cuda::par.on(stream), output.begin(), output.end(), hasher);
} else if (variant == "transform") {
thrust::transform(thrust::cuda::par.on(stream),
thrust::make_counting_iterator<int>(0),
thrust::make_counting_iterator(num_rows),
output.begin(),
hasher);
} else if (variant == "for_each") {
thrust::for_each_n(thrust::cuda::par.on(stream),
thrust::make_counting_iterator<int>(0),
num_rows,
[hasher, output_ptr] __device__(int row_index) {
output_ptr[row_index] = hasher(row_index);
});
} else if (variant == "cub_for") {
check_cuda(cub::DeviceFor::ForEach(
thrust::make_counting_iterator<int>(0),
thrust::make_counting_iterator(num_rows),
[hasher, output_ptr] __device__(int row_index) { output_ptr[row_index] = hasher(row_index); },
stream));
} else if (variant == "direct_kernel") {
auto constexpr block_size = 256;
auto const grid_size = (num_rows + block_size - 1) / block_size;
direct_kernel<<<grid_size, block_size, 0, stream>>>(hasher, output_ptr);
check_cuda(cudaPeekAtLastError());
} else {
state.skip("unknown variant");
}
});
}
NVBENCH_BENCH(bench_row_hash)
.set_name("row_hash")
.add_int64_axis("num_rows", {1048576, 16777216})
.add_int64_axis("num_cols", {8, 32, 128, 256})
.add_string_axis("variant", {"tabulate", "transform", "for_each", "cub_for", "direct_kernel"});
} // namespace// nvbench_main.cu
#include <nvbench/main.cuh>
NVBENCH_MAINObserved results
Hardware: NVIDIA GB10
CUDA: 13.2
CCCL: from current RAPIDS/cuDF build dependency checkout
NVBench: v0.1.0 main:v26.02.00a-1118-gf239649f8d-dirty
Command:
./build/tabulate_reproducer \
--benchmark row_hash \
--axis num_rows=1048576 \
--axis num_cols=[32,128,256] \
--axis variant=[tabulate,transform,for_each,cub_for,direct_kernel] \
--min-samples 5 \
--min-time 0.05 \
--timeout 20 \
--devices 0Results:
| rows | cols | tabulate | transform | for_each | cub_for | direct_kernel |
|---|---|---|---|---|---|---|
| 1,048,576 | 32 | 4.188 ms | 1.264 ms | 1.165 ms | 1.201 ms | 1.209 ms |
| 1,048,576 | 128 | 17.430 ms | 4.411 ms | 4.510 ms | 4.507 ms | 4.502 ms |
| 1,048,576 | 256 | 36.005 ms | 9.255 ms | 9.042 ms | 9.061 ms | 8.960 ms |
thrust::tabulate is consistently about 3.5-4x slower than the equivalent variants.
Expected behavior
I would expect thrust::tabulate(output.begin(), output.end(), f) to be in the same performance family as thrust::transform(counting_iterator, output.begin(), f), thrust::for_each_n(counting_iterator, ...), and cub::DeviceFor::ForEach for this workload.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn Review
Activity
bdice commentedon May 19, 2026
I dug into this further and isolated the cause.
CUDA
thrust::tabulatecurrently does this inthrust/thrust/system/cuda/detail/tabulate.h:cuda_cub::transform_n( policy, ::cuda::counting_iterator<size_type>{}, count, first, ::cuda::proclaim_copyable_arguments(tabulate_op));The slowdown is caused by
cuda::proclaim_copyable_arguments(tabulate_op), not bytransform_nitself or bycuda::counting_iterator.I added these variants to the standalone reproducer:
transform_n_ptrdiff:thrust::transform_nwiththrust::counting_iterator<output_difference_type>transform_n_cuda_counting:thrust::transform_nwithcuda::counting_iterator<output_difference_type>transform_n_proclaim: same astransform_n_ptrdiff, but wraps the functor incuda::proclaim_copyable_argumentstransform_n_cuda_proclaim: same astransform_n_cuda_counting, but wraps the functor incuda::proclaim_copyable_argumentsOn current
mainatfcd4ff4ea1:So adding
proclaim_copyable_argumentsto otherwise-fasttransform_nreproduces the slowtabulatebehavior exactly.I also tested this minimal local patch:
With that patch,
tabulateis back in the same performance family as the other variants:It seems like
proclaim_copyable_argumentsis used intabulatebecause generated counting values do not need stable source addresses. However, the CUB non-stable-address transform path selected byproclaim_copyable_argumentsis the slow path for this functor, so this regresses performance substantially.bdice commentedon May 19, 2026
It seems the implementation of
thrust::tabulatechanged in #6012.bernhardmgruber commentedon May 19, 2026
Thx for reporting this. My guess is that
thrust::tabulatewill try hard to use the vectorized transform kernel, which uses aggressive unrolling. That's usually beneficial for the workloads we benchmark. It seems this is not the case for your functor.I can look into this in a few weeks. My best guess is that you will be very happy when we release the tuning API for DeviceTransform, so you can configure the backend implementation ;)
bernhardmgruber commentedon May 19, 2026
I would like to extract the provided benchmark and add it as a new thrust benchmark for tabulate.
bdice commentedon May 19, 2026
Here are some additional benchmarks I ran on DGX Spark, trying to model cudf's usages of
thrust::tabulate. For now I will implementcudf::detail::tabulatethat mirrors thethrust::tabulateAPI but with a CUB for-each implementation.I also ran a basic segment size benchmark:
Results on current CCCL
main:for_each_nis faster here, modestly.bdice commentedon May 26, 2026
We're avoiding
thrust::tabulatefor now in NVIDIA/cudf#22632. I did not see significant slowdown in other instances ofthrust::tabulate, the row hasher is the main culprit.Use CUB bulk for row hashing (#22632)
lrpierce commentedon Aug 20, 2026
I have also seen a similar issue with tabulate performance compared to thrust::transform in our library code requiring us to remove usage of tabulate. We have found that as the output data width increases the relative performance between the two deteriorates.
Benchmark data using an A100
Benchmark Reproducer:
bernhardmgruber commentedon Sep 21, 2026
The current implementation of
thrust::tabulateshould use the filling code path in the vectorized kernel. For sm80, it tries to fill 8 items per thread, for sm90+ it tries 16 items per thread. in both cases, it tries to vectorize using 8-byte stores.If the elements get big, that's a lot of stores per single thread.
thrust::transformfalls back to a default code path where we use 2 items per thread. I assume that's the cause of the difference we are seeing here.Going forward, we would like to expose a tuning API for Thrust as well, so you will be able to customize this in the future.