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: unified/managed memory patterns.
//
// Exercises:
//   - cudaMallocManaged (in DB -> hipMallocManaged for HIP; flagged for SYCL/Rust/OpenCL)
//   - __managed__ qualifier (in DB -> per-target rewrite)
//   - cudaHostAlloc (in DB -> renamed for HIP)
//   - cudaMallocHost (in DB -> renamed for HIP)
//   - cudaMemcpy (in DB -> renamed for HIP)
//   - cudaDeviceSynchronize (in DB -> hipDeviceSynchronize for HIP; flagged for SYCL/Rust/OpenCL)
//   - __global__ and __device__ qualifiers
//   - threadIdx.x, blockIdx.x, blockDim.x
//   - cuda_runtime.h header
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

// __managed__ memory: accessible from both host and device without explicit
// cudaMemcpy. decuda rewrites __managed__ per target and flags the
// managed-memory runtime APIs for manual review.
/* managed -> unified memory */ int managed_counter = 0;

/* device function */ #[inline(always)] int atomic_increment(int* addr) {
    return atomicAdd(addr, 1);
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void increment_kernel(int* counter, int n) {
    int i = block_idx * block_dim + thread_idx;
    if (i < n) {
        atomic_increment(counter);
    }
}

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

int main(void) {
    const int N = 1 << 16;

    // Managed memory: no explicit cudaMemcpy needed between host and device.
    // cudaMallocManaged is in the DB for HIP (-> hipMallocManaged);
    // for SYCL/Rust/OpenCL it is preserved verbatim and flagged.
    float* managed_data = nullptr;
    cudaMallocManaged((void**)&managed_data, N * sizeof(float));

    // Initialize on the host (managed memory is directly accessible).
    for (int i = 0; i < N; i++) {
        managed_data[i] = (float)i;
    }

    // Pinned host memory for comparison.
    float* pinned_data = nullptr;
    cudaMallocHost((void**)&pinned_data, N * sizeof(float));

    dim3 grid(N / 256);
    dim3 block(256);

    // Launch on managed memory — no cudaMemcpy required.
    { /* decuda cust launch */ let _kernel = modules.get_function("scale_kernel"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(managed_data, 2.0f, N) ); } };
    { /* decuda cust launch */ let _kernel = modules.get_function("increment_kernel"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(&managed_counter, N) ); } };

    // cudaDeviceSynchronize is in the DB for HIP (-> hipDeviceSynchronize);
    // for SYCL/Rust/OpenCL it is preserved verbatim and flagged.
    cudaDeviceSynchronize();

    // Read back managed memory directly on the host.
    printf("counter = %d, data[0] = %f\n", managed_counter, managed_data[0]);

    // Copy to pinned memory for explicit transfer path.
    cudaMemcpy(pinned_data, managed_data, N * sizeof(float), cudaMemcpyDeviceToHost);

    cudaFree(managed_data);
    cudaFreeHost(pinned_data);
    return 0;
}