decuda 0.1.0

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: parallel reduction with warp shuffles and tree reduction.
//
// Exercises:
//   - Warp-level shuffle: __shfl_sync (flagged for manual review)
//   - atomicAdd for cross-block final reduction
//   - __syncthreads for block-level barrier
//   - warpSize, __laneid builtins
//   - Grid-stride loop pattern
//   - threadIdx.x, blockIdx.x, blockDim.x, gridDim.x
//   - cudaMalloc, cudaFree, cudaMemcpy
//   - Multiple kernel launches (reduce_block, reduce_final)
//   - __global__ and __device__ qualifiers
#include <hip/hip_runtime.h> /* was: cuda_runtime.h */

#define WARP 32
#define BLOCK 256

__device__ __forceinline__ int warp_reduce(int v) {
    // Warp shuffle reduction — __shfl_sync is NOT auto-translated by decuda;
    // it is preserved verbatim and flagged in the migration report.
    for (int offset = WARP / 2; offset > 0; offset /= 2) {
        v += __shfl_sync(0xFFFFFFFFu, v, __laneid() - offset);
    }
    return v;
}

__global__ void reduce_block(const int* in, int* partial, int n) {
    __shared__ int shared[BLOCK / WARP];
    int tid = threadIdx.x;
    int gid = blockIdx.x * blockDim.x + tid;

    // Grid-stride load + per-thread sum.
    int v = 0;
    for (int i = gid; i < n; i += gridDim.x * blockDim.x) {
        v += in[i];
    }

    // In-warp reduction via shuffles.
    v = warp_reduce(v);
    __syncthreads();

    // First lane of each warp writes to shared memory.
    int lane = tid % WARP;
    int warp = tid / WARP;
    if (lane == 0) {
        shared[warp] = v;
    }
    __syncthreads();

    // Final reduction across warps (first warp does the work).
    if (warp == 0) {
        v = (tid < BLOCK / WARP) ? shared[lane] : 0;
        v = warp_reduce(v);
        if (lane == 0) {
            atomicAdd(partial, v);
        }
    }
}

__global__ void reduce_final(int* partial) {
    // Single-thread kernel to read back the final sum.
    if (threadIdx.x == 0 && blockIdx.x == 0) {
        int result = *partial;
        // Do nothing — the host reads `partial` back.
        (void)result;
    }
}

int main(void) {
    const int N = 1 << 22;
    int* d_in = nullptr;
    int* d_partial = nullptr;

    hipMalloc((void**)&d_in, N * sizeof(int));
    hipMalloc((void**)&d_partial, sizeof(int));
    hipMemset(d_partial, 0, sizeof(int));

    dim3 grid(N / BLOCK);
    dim3 block(BLOCK);
    hipLaunchKernelGGL(reduce_block, dim3(grid), dim3(block), 0, 0, d_in, d_partial, N);
    hipLaunchKernelGGL(reduce_final, dim3(1), dim3(1), 0, 0, d_partial);

    int result = 0;
    hipMemcpy(&result, d_partial, sizeof(int), cudaMemcpyDeviceToHost);

    hipFree(d_in);
    hipFree(d_partial);
    return 0;
}