decuda 0.1.1

CUDA to HIP, SYCL, OpenCL, and Rust GPU migration tool — automatic source-code translator for porting CUDA C++ kernels to AMD ROCm HIP, Intel oneAPI SYCL, Khronos OpenCL, and Rust GPU (cust / rust-gpu)
Documentation
// Generated by decuda.
// Host-side runtime calls are translated to the `cust` crate. Kernels
// are emitted as `TODO(decuda)` blocks: rust-gpu translation requires
// the kernel to be authored as a Rust fn. See `examples/` for a
// scaffolded SPIR-V kernel module you can flesh out.
//
// Add to your Cargo.toml:
//   [dependencies]
//   cust = "0.3"

// Complex fixture: shared-memory histogram with atomic updates, grid-stride
// loops, warp-level reduction, and constant-memory lookup table.
//
// Exercises:
//   - __global__ / __device__ / __shared__ / __constant__ qualifiers
//   - atomicAdd, atomicMin, atomicMax
//   - __syncthreads, __syncwarp
//   - threadIdx/blockIdx/blockDim/gridDim with .x field access
//   - warpSize, __laneid
//   - cudaMalloc / cudaFree / cudaMemcpy / cudaMemset
//   - cuda_runtime.h header
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

#define NBINS 256

/* constant */ int c_binMax[1];

/* device function */ #[inline(always)] int clamp_bin(int v) {
    if (v < 0) return 0;
    if (v > c_binMax[0]) return c_binMax[0];
    return v;
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void histogram(const int* data, int n, int* bins) {
    /* shared -> rust-gpu group_memory */ int local_bins[NBINS];
    int tid = thread_idx;
    int bid = block_idx * block_dim + tid;

    // Initialize shared bins.
    for (int i = tid; i < NBINS; i += block_dim) {
        local_bins[i] = 0;
    }
    group.sync();

    // Grid-stride loop: each thread atomically updates its local bin.
    for (int i = bid; i < n; i += grid_dim * block_dim) {
        int b = clamp_bin(data[i]);
        atomicAdd(&local_bins[b], 1);
    }
    group.sync();

    // Reduce shared bins into global bins with atomic ops.
    for (int i = tid; i < NBINS; i += block_dim) {
        if (local_bins[i] > 0) {
            atomicAdd(&bins[i], local_bins[i]);
            atomicMin(&bins[i], c_binMax[0]);
            atomicMax(&bins[i], 0);
        }
    }
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void warp_reduce(const int* in, int* out) {
    int lane = lane_id();
    int v = in[thread_idx];
    // Naive warp shuffle reduction (preserved verbatim by decuda).
    for (int offset = WARP_SIZE / 2; offset > 0; offset /= 2) {
        int t = /* TODO: __shfl_sync */(0xFFFFFFFFu, v, lane - offset);
        v += t;
    }
    /* syncwarp: only lane=0 of warp at once */;
    if (lane == 0) {
        out[block_idx] = v;
    }
}

int main(void) {
    const int N = 1 << 20;
    int* d_data = nullptr;
    int* d_bins = nullptr;
    int* d_out = nullptr;

    cudaMalloc((void**)&d_data, N * sizeof(int));
    cudaMalloc((void**)&d_bins, NBINS * sizeof(int));
    cudaMalloc((void**)&d_out, 1024 * sizeof(int));

    cudaMemset(d_bins, 0, NBINS * sizeof(int));
    cudaMemcpy(d_data, d_data, N * sizeof(int), cudaMemcpyDeviceToDevice);

    int maxbin = NBINS - 1;
    cudaMemcpyToSymbol(c_binMax, &maxbin, sizeof(int));

    dim3 grid(N / 256);
    dim3 block(256);
    { /* decuda cust launch */ let _kernel = modules.get_function("histogram"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_data, N, d_bins) ); } };
    { /* decuda cust launch */ let _kernel = modules.get_function("warp_reduce"); unsafe { let _ = launch!( _kernel<<<N / 32 as grid_size, 32 as block_size, 0 as usize, default>>>(d_data, d_out) ); } };

    cudaDeviceSynchronize();
    cudaFree(d_data);
    cudaFree(d_bins);
    cudaFree(d_out);
    return 0;
}