decuda 0.1.0

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"

// Richer fixture exercising atomics, shared memory, syncthreads, warp
// intrinsics, 2D launches, and a constant-memory global.
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

#define N 1024

/* constant */ float kCoefficients[8];

// TODO(decuda): rewrite as rust-gpu kernel fn
 void increment_atomic(int* counter, int delta) {
    int prev = atomicAdd(counter, delta);
    if (thread_idx == 0) {
        atomicCAS(counter, prev, prev + 1);
    }
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void warp_scan(const int* in, int* out) {
    /* shared -> rust-gpu group_memory */ int buf[32];
    int lane = lane_id;
    int wid = thread_idx / 32;
    buf[lane] = in[thread_idx];
    /* syncwarp: only lane=0 of warp at once */;
    if (lane == 0) {
        int s = 0;
        for (int i = 0; i < 32; ++i) s += buf[i];
        out[wid] = s;
    }
    group.sync();
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void matmul(const float* a, const float* b, float* c,
                       int m, int n, int k) {
    int row = block_idx * block_dim + thread_idx;
    int col = block_idx * block_dim + thread_idx;
    if (row < m && col < n) {
        float s = kCoefficients[0] * a[row * k] * b[col];
        c[row * n + col] = s;
    }
}

void launch_examples(int* counter, float* a, float* b, float* c) {
    dim3 grid(N / 32, N / 16);
    dim3 block(32, 16);
    { /* decuda cust launch */ let _kernel = modules.get_function("increment_atomic"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(counter, 1) ); } };
    { /* decuda cust launch */ let _kernel = modules.get_function("warp_scan"); unsafe { let _ = launch!( _kernel<<<1 as grid_size, 32 as block_size, 0 as usize, default>>>(counter, a) ); } };
    { /* decuda cust launch */ let _kernel = modules.get_function("matmul"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(a, b, c, N, N, N) ); } };
}