Write a cuda kernel for radix sort
- User
Write a cuda kernel for radix sort
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Write a cuda kernel for radix sort
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a CUDA kernel implementation for radix sort:
#include <cuda_runtime.h>
// Number of bits to sort per pass
#define BITS_PER_PASS 4
#define NUM_BINS (1 << BITS_PER_PASS)
// Kernel to count elements per bin
__global__ void countBins(unsigned int* d_input, unsigned int* d_histograms,
int n, int bit_shift) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
// Shared memory for local histograms
__shared__ unsigned int local_hist[NUM_BINS];
// Initialize local histogram
if (threadIdx.x < NUM_BINS) {
local_hist[threadIdx.x] = 0;
}
__syncthreads();
// Count elements
for (int i = tid; i < n; i += stride) {
unsigned int val = d_input[i];
unsigned int bin = (val >> bit_shift) & (NUM_BINS - 1);
atomicAdd(&local_hist[bin], 1);
}
__syncthreads();
// Write local histogram to global memory
if (threadIdx.x < NUM_BINS) {
atomicAdd(&d_histograms[blockIdx.x * NUM_BINS + threadIdx.x], local_hist[threadIdx.x]);
}
}
// Kernel to scan histograms (exclusive prefix sum)
__global__ void scanHistograms(unsigned int* d_histograms, unsigned int* d_scanned, int num_blocks) {
int tid = threadIdx.x;
int bin = blockIdx.x;
// Shared memory for scan
extern __shared__ unsigned int temp[];
// Load data
if (tid < num_blocks) {
temp[tid] = d_histograms[bin + tid * NUM_BINS];
} else {
temp[tid] = 0;
}
__syncthreads();
// Parallel scan (Blelloch algorithm)
for (int stride = 1; stride < num_blocks; stride *= 2) {
int index = (tid + 1) * 2 * stride - 1;
if (index < num_blocks) {
temp[index] += temp[index - stride];
}
__syncthreads();
}
// Down-sweep phase
if (tid == 0) {
temp[num_blocks - 1] = 0;
}
__syncthreads();
for (int stride = num_blocks / 2; stride > 0; stride /= 2) {
int index = (tid + 1) * 2 * stride - 1;
if (index < num_blocks) {
unsigned int temp_val = temp[index];
temp[index] += temp[index - stride];
temp[index - stride] = temp_val;
}
__syncthreads();
}
// Write results back
if (tid < num_blocks) {
d_scanned[bin + tid * NUM_BINS] = temp[tid];
}
}
// Kernel to reorder elements based on scanned histograms
__global__ void reorderElements(unsigned int* d_input, unsigned int* d_output,
unsigned int* d_histograms, unsigned int* d_scanned,
int n, int bit_shift) {
int tid = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
// Shared memory for local counters
__shared__ unsigned int local_offsets[NUM_BINS];
// Initialize local offsets
if (threadIdx.x < NUM_BINS) {
local_offsets[threadIdx.x] = d_scanned[blockIdx.x * NUM_BINS + threadIdx.x];
}
__syncthreads();
// Reorder elements
for (int i = tid; i < n; i += stride) {
unsigned int val = d_input[i];
unsigned int bin = (val >> bit_shift) & (NUM_BINS - 1);
unsigned int dest = atomicAdd(&local_offsets[bin], 1);
d_output[dest] = val;
}
}
// Host function to perform radix sort
void radixSort(unsigned int* d_input, unsigned int* d_output, int n) {
const int num_bits = sizeof(unsigned int) * 8;
const int num_passes = num_bits / BITS_PER_PASS;
// Determine grid and block sizes
int block_size = 256;
int grid_size = (n + block_size - 1) / block_size;
grid_size = min(grid_size, 1024);