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.
// SYCL kernels replace CUDA kernels with parallel_for lambdas; this
// output is a *starting point* and almost always requires manual
// follow-up. Look for the `TODO(decuda)` markers in this file.

#include <sycl/sycl.hpp>

// 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 <sycl/sycl.hpp> /* was: cuda_runtime.h */

// TODO(decuda): rewrite as SYCL kernel lambda
 void saxpy(float a, float* x, float* y, int n) {
    int i = item.get_group(0) * item.get_local_range() + item.get_local_id();
    if (i < n) {
        y[i] = a * x[i] + y[i];
    }
}

// TODO(decuda): rewrite as SYCL kernel lambda
 void sum_reduce(const float* in, float* out, int n) {
    __shared__ float buf[32];
    int tid = item.get_local_id();
    buf[tid] = (tid < n) ? in[tid] : 0.0f;
    item.barrier(sycl::access::fence_space::global_space);
    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 SYCL launch: queue.submit([&](sycl::handler& h) { h.parallel_for(sycl::range<3>{grid}, [=](sycl::item<3> it) { /* kernel `saxpy` body with thread indices from it.get_*() */ }); }); smem=none stream=default args=2.0f, dx, dy, N */ };

    float* out = nullptr;
    cudaMalloc((void**)&out, sizeof(float));
    { /* decuda SYCL launch: queue.submit([&](sycl::handler& h) { h.parallel_for(sycl::range<3>{1}, [=](sycl::item<3> it) { /* kernel `sum_reduce` body with thread indices from it.get_*() */ }); }); smem=none stream=default args=dx, out, N */ };

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