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.

// Advanced fixture: unified/managed memory patterns.
//
// Exercises:
//   - cudaMallocManaged (in DB -> hipMallocManaged for HIP; flagged for SYCL/Rust/OpenCL)
//   - __managed__ qualifier (in DB -> per-target rewrite)
//   - cudaHostAlloc (in DB -> renamed for HIP)
//   - cudaMallocHost (in DB -> renamed for HIP)
//   - cudaMemcpy (in DB -> renamed for HIP)
//   - cudaDeviceSynchronize (in DB -> hipDeviceSynchronize for HIP; flagged for SYCL/Rust/OpenCL)
//   - __global__ and __device__ qualifiers
//   - threadIdx.x, blockIdx.x, blockDim.x
//   - cuda_runtime.h header
#include <hip/hip_runtime.h> /* was: cuda_runtime.h */

// __managed__ memory: accessible from both host and device without explicit
// cudaMemcpy. decuda rewrites __managed__ per target and flags the
// managed-memory runtime APIs for manual review.
__managed__ int managed_counter = 0;

__device__ __forceinline__ int atomic_increment(int* addr) {
    return atomicAdd(addr, 1);
}

__global__ void increment_kernel(int* counter, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        atomic_increment(counter);
    }
}

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

int main(void) {
    const int N = 1 << 16;

    // Managed memory: no explicit cudaMemcpy needed between host and device.
    // cudaMallocManaged is in the DB for HIP (-> hipMallocManaged);
    // for SYCL/Rust/OpenCL it is preserved verbatim and flagged.
    float* managed_data = nullptr;
    hipMallocManaged((void**)&managed_data, N * sizeof(float));

    // Initialize on the host (managed memory is directly accessible).
    for (int i = 0; i < N; i++) {
        managed_data[i] = (float)i;
    }

    // Pinned host memory for comparison.
    float* pinned_data = nullptr;
    hipHostMalloc((void**)&pinned_data, N * sizeof(float));

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

    // Launch on managed memory — no cudaMemcpy required.
    hipLaunchKernelGGL(scale_kernel, dim3(grid), dim3(block), 0, 0, managed_data, 2.0f, N);
    hipLaunchKernelGGL(increment_kernel, dim3(grid), dim3(block), 0, 0, &managed_counter, N);

    // cudaDeviceSynchronize is in the DB for HIP (-> hipDeviceSynchronize);
    // for SYCL/Rust/OpenCL it is preserved verbatim and flagged.
    hipDeviceSynchronize();

    // Read back managed memory directly on the host.
    printf("counter = %d, data[0] = %f\n", managed_counter, managed_data[0]);

    // Copy to pinned memory for explicit transfer path.
    hipMemcpy(pinned_data, managed_data, N * sizeof(float), cudaMemcpyDeviceToHost);

    hipFree(managed_data);
    hipFreeHost(pinned_data);
    return 0;
}