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.
// OpenCL device code is the bulk of this file. The host-side calls
// (`cudaXxx`) have been rewritten to OpenCL equivalents inline; the
// surrounding host program still needs a cl_context + cl_queue, not
// included here. Look for TODO(decuda) markers for items requiring
// manual attention.

// 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
#include <CL/cl.h> /* was: cuda_runtime.h */

__kernel void saxpy(float a, float* x, float* y, int n) {
    int i = get_group_id(0) * get_local_size(0) + get_local_id(0);
    if (i < n) {
        y[i] = a * x[i] + y[i];
    }
}

__kernel void sum_reduce(const float* in, float* out, int n) {
    __local float buf[32];
    int tid = get_local_id(0);
    buf[tid] = (tid < n) ? in[tid] : 0.0f;
    barrier(CLK_LOCAL_MEM_FENCE);
    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);
    clEnqueueNDRangeKernel(queue, saxpy_kernel, 1, NULL, (size_t[1]){grid}, (size_t[1]){block}, 0, NULL, NULL) /* args: 2.0f, dx, dy, N */;

    float* out = nullptr;
    cudaMalloc((void**)&out, sizeof(float));
    clEnqueueNDRangeKernel(queue, sum_reduce_kernel, 1, NULL, (size_t[1]){1}, (size_t[1]){32}, 0, NULL, NULL) /* args: dx, out, N */;

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