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"

// Example CUDA source for decuda. This file is the input used to generate
// the sibling .hip.cpp / .sycl.cpp / .rs / .cl example outputs.
//
// Regenerate the outputs with:
//   cargo run -- migrate -i examples/saxpy.cu -o examples/out --target all
//
// It exercises the four main CUDA constructs decuda handles:
//   1. __global__ kernel qualifiers and definitions
//   2. Kernel launch syntax: kernel<<<grid, block>>>(...)
//   3. Built-in variables: threadIdx, blockIdx, blockDim
//   4. Runtime API calls: cudaMalloc, cudaMemcpy, cudaFree
//   5. Shared memory and synchronization
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

// TODO(decuda): rewrite as rust-gpu kernel fn
 void saxpy(float a, float* x, float* y, int n) {
    int i = block_idx * block_dim + thread_idx;
    if (i < n) {
        y[i] = a * x[i] + y[i];
    }
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void sum_reduce(const float* in, float* out, int n) {
    /* shared -> rust-gpu group_memory */ float buf[32];
    int tid = thread_idx;
    buf[tid] = (tid < n) ? in[tid] : 0.0f;
    group.sync();
    if (tid == 0) {
        float s = 0.0f;
        for (int i = 0; i < 32; ++i) s += buf[i];
        *out = s;
    }
}

int main(void) {
    const int N = 1024;
    float *dx = nullptr, *dy = nullptr;
    cudaMalloc((void**)&dx, N * sizeof(float));
    cudaMalloc((void**)&dy, N * sizeof(float));

    dim3 grid(N / 256);
    dim3 block(256);
    { /* decuda cust launch */ let _kernel = modules.get_function("saxpy"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(2.0f, dx, dy, N) ); } };

    float* out = nullptr;
    cudaMalloc((void**)&out, sizeof(float));
    { /* decuda cust launch */ let _kernel = modules.get_function("sum_reduce"); unsafe { let _ = launch!( _kernel<<<1 as grid_size, 32 as block_size, 0 as usize, default>>>(dx, out, N) ); } };

    cudaDeviceSynchronize();
    cudaFree(dx);
    cudaFree(dy);
    cudaFree(out);
    return 0;
}