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: asynchronous pipeline using CUDA streams and events.
//
// Exercises:
//   - cudaStreamCreate / cudaStreamDestroy / cudaStreamSynchronize
//   - cudaEventCreate / cudaEventRecord / cudaEventSynchronize / cudaEventDestroy
//   - cudaMemcpyAsync (async memcpy on a stream)
//   - Multiple kernels launched on different streams
//   - Launch with shared-memory size argument: kernel<<<grid, block, smem, stream>>>
//   - __global__ kernels with __syncthreads
//   - cuda_runtime.h header
#include <hip/hip_runtime.h> /* was: cuda_runtime.h */

__global__ void scale(float a, float* x, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        x[i] *= a;
    }
}

__global__ void add(const float* x, const float* y, float* out, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        out[i] = x[i] + y[i];
    }
    __syncthreads();
}

int main(void) {
    const int N = 1 << 16;
    float *dx = nullptr, *dy = nullptr, *dz = nullptr;

    hipMalloc((void**)&dx, N * sizeof(float));
    hipMalloc((void**)&dy, N * sizeof(float));
    hipMalloc((void**)&dz, N * sizeof(float));

    hipStream_t() s1, s2;
    hipStreamCreate(&s1);
    hipStreamCreate(&s2);

    hipEvent_t() e1, e2;
    hipEventCreate(&e1);
    hipEventCreate(&e2);

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

    // Async memcpy on stream s1.
    hipMemcpyAsync(dx, dy, N * sizeof(float), cudaMemcpyDeviceToDevice, s1);

    // Launch with explicit shared-memory size and stream.
    hipLaunchKernelGGL(scale, dim3(grid), dim3(block), 0, s1, 2.0f, dx, N);
    hipEventRecord(e1, s1);

    // Second stream waits on event e1 via host-side synchronize.
    hipEventSynchronize(e1);
    hipLaunchKernelGGL(add, dim3(grid), dim3(block), 128, s2, dx, dy, dz, N);
    hipEventRecord(e2, s2);

    hipStreamSynchronize(s1);
    hipStreamSynchronize(s2);

    hipEventDestroy(e1);
    hipEventDestroy(e2);
    hipStreamDestroy(s1);
    hipStreamDestroy(s2);

    hipFree(dx);
    hipFree(dy);
    hipFree(dz);
    return 0;
}