std/cuda

March 13, 2026 ยท View on GitHub

The std/cuda module provides helper functions and types for CUDA interoperability, simplifying memory management, synchronization, and device queries.

Note

This module requires compiling with the --cuda flag.

Usage

import "std/cuda.zc"

fn main() {
    let dev_ptr = cuda_alloc<float>(1024);
    defer cuda_free(dev_ptr);
    
    cuda_sync();
}

Struct Definition

struct CudaDeviceProp {
    name: String;
    total_global_mem: usize;
    multi_processor_count: int;
    major: int;
    minor: int;
    max_threads_per_block: int;
    warp_size: int;
}

Methods

Memory Management

MethodSignatureDescription
cuda_alloccuda_alloc<T>(n: usize) -> T*Allocates device memory for n elements of type T.
cuda_freecuda_free(ptr: void*)Frees device memory.
cuda_copy_to_devicecuda_copy_to_device(dst: void*, src: void*, bytes: usize)Copies data from host (CPU) to device (GPU).
cuda_copy_to_hostcuda_copy_to_host(dst: void*, src: void*, bytes: usize)Copies data from device (GPU) to host (CPU).
cuda_copy_devicecuda_copy_device(dst: void*, src: void*, bytes: usize)Copies data from device to device.
cuda_zerocuda_zero(ptr: void*, bytes: usize)Sets device memory to zero.

Synchronization

MethodSignatureDescription
cuda_synccuda_sync()Synchronizes the device (blocks until all previous CUDA calls complete).

Device Information

MethodSignatureDescription
cuda_device_countcuda_device_count() -> intReturns the number of available CUDA devices.
cuda_set_devicecuda_set_device(id: int)Sets the active CUDA device.
cuda_device_propertiescuda_device_properties(device_id: int) -> CudaDevicePropReturns properties of the specified device.

Device Functions (Kernel Only)

These functions are marked @device and should only be called from within a kernel (@global) or device functions.

FunctionSignatureDescription
thread_idthread_id() -> intGlobal thread index.
block_idblock_id() -> intBlock index (blockIdx.x).
local_idlocal_id() -> intLocal thread index (threadIdx.x).
block_sizeblock_size() -> intBlock dimension (blockDim.x).
grid_sizegrid_size() -> intGrid dimension (gridDim.x).