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. Edit with care.
// HIP is mostly source-compatible with CUDA at the kernel level.
// Compare against the original .cu file for sanity.

// 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 <hip/hip_runtime.h> /* was: cuda_runtime.h */

#define TILE 16

__global__ void transpose(const float* in, float* out, int width, int height) {
    __shared__ float tile[TILE][TILE];

    int x = blockIdx.x * blockDim.x + threadIdx.x;
    int y = blockIdx.y * blockDim.y + threadIdx.y;

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

    // Write the transposed tile back to global memory.
    int ox = blockIdx.y * blockDim.y + threadIdx.x;
    int oy = blockIdx.x * blockDim.x + threadIdx.y;
    if (ox < height && oy < width) {
        out[oy * height + ox] = tile[threadIdx.x][threadIdx.y];
    }
}

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

    hipMalloc((void**)&d_in, W * H * sizeof(float));
    hipMalloc((void**)&d_out, W * H * sizeof(float));
    hipMemcpy(d_in, d_in, W * H * sizeof(float), cudaMemcpyDeviceToDevice);

    dim3 grid(W / TILE, H / TILE);
    dim3 block(TILE, TILE);
    hipLaunchKernelGGL(transpose, dim3(grid), dim3(block), 0, 0, d_in, d_out, W, H);

    hipDeviceSynchronize();
    hipFree(d_in);
    hipFree(d_out);
    return 0;
}