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"

// Advanced fixture: parallel reduction with warp shuffles and tree reduction.
//
// Exercises:
//   - Warp-level shuffle: __shfl_sync (flagged for manual review)
//   - atomicAdd for cross-block final reduction
//   - __syncthreads for block-level barrier
//   - warpSize, __laneid builtins
//   - Grid-stride loop pattern
//   - threadIdx.x, blockIdx.x, blockDim.x, gridDim.x
//   - cudaMalloc, cudaFree, cudaMemcpy
//   - Multiple kernel launches (reduce_block, reduce_final)
//   - __global__ and __device__ qualifiers
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

#define WARP 32
#define BLOCK 256

/* device function */ #[inline(always)] int warp_reduce(int v) {
    // Warp shuffle reduction — __shfl_sync is NOT auto-translated by decuda;
    // it is preserved verbatim and flagged in the migration report.
    for (int offset = WARP / 2; offset > 0; offset /= 2) {
        v += /* TODO: __shfl_sync */(0xFFFFFFFFu, v, lane_id() - offset);
    }
    return v;
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void reduce_block(const int* in, int* partial, int n) {
    /* shared -> rust-gpu group_memory */ int shared[BLOCK / WARP];
    int tid = thread_idx;
    int gid = block_idx * block_dim + tid;

    // Grid-stride load + per-thread sum.
    int v = 0;
    for (int i = gid; i < n; i += grid_dim * block_dim) {
        v += in[i];
    }

    // In-warp reduction via shuffles.
    v = warp_reduce(v);
    group.sync();

    // First lane of each warp writes to shared memory.
    int lane = tid % WARP;
    int warp = tid / WARP;
    if (lane == 0) {
        shared[warp] = v;
    }
    group.sync();

    // Final reduction across warps (first warp does the work).
    if (warp == 0) {
        v = (tid < BLOCK / WARP) ? shared[lane] : 0;
        v = warp_reduce(v);
        if (lane == 0) {
            atomicAdd(partial, v);
        }
    }
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void reduce_final(int* partial) {
    // Single-thread kernel to read back the final sum.
    if (thread_idx == 0 && block_idx == 0) {
        int result = *partial;
        // Do nothing — the host reads `partial` back.
        (void)result;
    }
}

int main(void) {
    const int N = 1 << 22;
    int* d_in = nullptr;
    int* d_partial = nullptr;

    cudaMalloc((void**)&d_in, N * sizeof(int));
    cudaMalloc((void**)&d_partial, sizeof(int));
    cudaMemset(d_partial, 0, sizeof(int));

    dim3 grid(N / BLOCK);
    dim3 block(BLOCK);
    { /* decuda cust launch */ let _kernel = modules.get_function("reduce_block"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_in, d_partial, N) ); } };
    { /* decuda cust launch */ let _kernel = modules.get_function("reduce_final"); unsafe { let _ = launch!( _kernel<<<1 as grid_size, 1 as block_size, 0 as usize, default>>>(d_partial) ); } };

    int result = 0;
    cudaMemcpy(&result, d_partial, sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree(d_in);
    cudaFree(d_partial);
    return 0;
}