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>

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

#define TILE 16

// TODO(decuda): rewrite as SYCL kernel lambda
 void transpose(const float* in, float* out, int width, int height) {
    __shared__ float tile[TILE][TILE];

    int x = item.get_group(0) * item.get_local_range() + item.get_local_id();
    int y = item.get_group(0) * item.get_local_range() + item.get_local_id();

    // Load a tile from global memory into shared memory (coalesced read).
    if (x < width && y < height) {
        tile[item.get_local_id()][item.get_local_id()] = in[y * width + x];
    }
    item.barrier(sycl::access::fence_space::global_space);

    // Write the transposed tile back to global memory.
    int ox = item.get_group(0) * item.get_local_range() + item.get_local_id();
    int oy = item.get_group(0) * item.get_local_range() + item.get_local_id();
    if (ox < height && oy < width) {
        out[oy * height + ox] = tile[item.get_local_id()][item.get_local_id()];
    }
}

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 SYCL launch: queue.submit([&](sycl::handler& h) { h.parallel_for(sycl::range<3>{grid}, [=](sycl::item<3> it) { /* kernel `transpose` body with thread indices from it.get_*() */ }); }); smem=none stream=default args=d_in, d_out, W, H */ };

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