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.
// Host-side runtime calls are translated to the `cust` crate. Kernels
// are emitted as `TODO(decuda)` blocks: rust-gpu translation requires
// the kernel to be authored as a Rust fn. See `examples/` for a
// scaffolded SPIR-V kernel module you can flesh out.
//
// Add to your Cargo.toml:
//   [dependencies]
//   cust = "0.3"

// Advanced fixture: warp-level primitives and cooperative patterns.
//
// Exercises:
//   - __shfl_sync (NOT a known builtin -> preserved, flagged)
//   - __ballot_sync (NOT a known builtin -> preserved, flagged)
//   - __any_sync (NOT a known builtin -> preserved, flagged)
//   - __all_sync (NOT a known builtin -> preserved, flagged)
//   - __activemask (NOT a known builtin -> preserved, flagged)
//   - __laneid (known builtin -> rewritten per target)
//   - __syncwarp (known builtin -> rewritten per target)
//   - warpSize (known builtin -> rewritten per target)
//   - __syncthreads (known builtin -> rewritten per target)
//   - atomicAdd, atomicMin, atomicMax (known atomics -> preserved/rewritten)
//   - __global__, __device__, __shared__ qualifiers
//   - threadIdx.x, blockIdx.x, blockDim.x, gridDim.x
//   - cuda_runtime.h header
// was: #include cuda_runtime.h  ->  cust::cuda_build_setup() /* TODO: import cust crate */

#define WARP 32

/* device function */ #[inline(always)] int warp_sum(int v) {
    // __shfl_sync is NOT in decuda's builtin table — preserved verbatim.
    for (int offset = WARP / 2; offset > 0; offset /= 2) {
        v += /* TODO: __shfl_sync */(0xFFFFFFFFu, v, lane_id() - offset);
    }
    return v;
}

/* device function */ #[inline(always)] int warp_ballot(int predicate) {
    // __ballot_sync is NOT in decuda's builtin table — preserved verbatim.
    return /* TODO: __ballot_sync */(0xFFFFFFFFu, predicate);
}

/* device function */ #[inline(always)] int warp_any(int predicate) {
    // __any_sync is NOT in decuda's builtin table — preserved verbatim.
    return /* TODO: __any_sync */(0xFFFFFFFFu, predicate);
}

/* device function */ #[inline(always)] int warp_all(int predicate) {
    // __all_sync is NOT in decuda's builtin table — preserved verbatim.
    return /* TODO: __all_sync */(0xFFFFFFFFu, predicate);
}

// TODO(decuda): rewrite as rust-gpu kernel fn
 void warp_demo(const int* in, int* out, int n) {
    /* shared -> rust-gpu group_memory */ int shared[WARP];
    int tid = thread_idx;
    int gid = block_idx * block_dim + tid;
    int lane = lane_id();

    // Load value and compute predicate.
    int v = (gid < n) ? in[gid] : 0;
    int pred = (v > 0) ? 1 : 0;

    // Warp-level vote: how many lanes have pred == 1?
    int ballot = warp_ballot(pred);
    int any_pos = warp_any(pred);
    int all_pos = warp_all(pred);
    int active = /* TODO: __activemask */();

    // Warp-level sum via shuffle.
    v = warp_sum(v);
    /* syncwarp: only lane=0 of warp at once */;

    if (lane == 0) {
        shared[tid / WARP] = v;
        atomicAdd(out, v);
        atomicMin(out + 1, ballot);
        atomicMax(out + 2, active);
    }
    group.sync();

    // Record vote results from lane 0 of the first warp.
    if (tid == 0) {
        atomicAdd(out + 3, any_pos);
        atomicAdd(out + 4, all_pos);
    }
}

int main(void) {
    const int N = 1 << 16;
    int* d_in = nullptr;
    int* d_out = nullptr;

    cudaMalloc((void**)&d_in, N * sizeof(int));
    cudaMalloc((void**)&d_out, 5 * sizeof(int));
    cudaMemset(d_out, 0, 5 * sizeof(int));

    dim3 grid(N / 256);
    dim3 block(256);
    { /* decuda cust launch */ let _kernel = modules.get_function("warp_demo"); unsafe { let _ = launch!( _kernel<<<grid as grid_size, block as block_size, 0 as usize, default>>>(d_in, d_out, N) ); } };

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