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"

// Complex fixture: tiled matrix transpose using a shared-memory tile.
//
// Exercises:
//   - 2D grid and block (dim3 with two components)
//   - __shared__ tile with __syncthreads barriers
//   - blockIdx.{x,y} and threadIdx.{x,y} and blockDim.{x,y}
//   - __global__ kernel with pointer + dimension args
//   - cudaMalloc / cudaFree / cudaMemcpy
//   - cuda_runtime.h header
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

#define TILE 16

// TODO(decuda): rewrite as rust-gpu kernel fn
 void transpose(const float* in, float* out, int width, int height) {
    /* shared -> rust-gpu group_memory */ float tile[TILE][TILE];

    int x = block_idx * block_dim + thread_idx;
    int y = block_idx * block_dim + thread_idx;

    // Load a tile from global memory into shared memory (coalesced read).
    if (x < width && y < height) {
        tile[thread_idx][thread_idx] = in[y * width + x];
    }
    group.sync();

    // Write the transposed tile back to global memory.
    int ox = block_idx * block_dim + thread_idx;
    int oy = block_idx * block_dim + thread_idx;
    if (ox < height && oy < width) {
        out[oy * height + ox] = tile[thread_idx][thread_idx];
    }
}

int main(void) {
    const int W = 1024;
    const int H = 1024;
    float* d_in = nullptr;
    float* d_out = nullptr;

    cudaMalloc((void**)&d_in, W * H * sizeof(float));
    cudaMalloc((void**)&d_out, W * H * sizeof(float));
    cudaMemcpy(d_in, d_in, W * H * sizeof(float), cudaMemcpyDeviceToDevice);

    dim3 grid(W / TILE, H / TILE);
    dim3 block(TILE, TILE);
    { /* decuda cust launch */ let _kernel = modules.get_function("transpose"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_in, d_out, W, H) ); } };

    cudaDeviceSynchronize();
    cudaFree(d_in);
    cudaFree(d_out);
    return 0;
}