#cudaz: Zig Cuda Wrapper

1 messages · Page 1 of 1 (latest)

rich flume
#

I've plans to build a Machine learning library in zig and this is the first step, a cuda based library for building GPU powered ML applications.

It can copy data from host to gpu and vice versa, has the capability to compile and run gpu code/kernel both from file and text, dynamically link to cuda and nvrtc library on the system. Below is the sample code that increments each value of zig array by one parallely on GPU.

// Cuda Kernel
const increment_kernel =
    \\extern "C" __global__ void increment(float *out)
    \\{
    \\    int i = blockIdx.x * blockDim.x + threadIdx.x;
    \\    out[i] = out[i] + 1;
    \\}
;

// Initialize GPU
const device = try CuDevice.default();
defer device.free();

// Copy data from host to GPU
const data = [_]f32{ 1.2, 2.8, 0.123 };
const cu_slice = try device.htod_copy(f32, &data);
defer cu_slice.free();

// Compile and load the Kernel
const ptx = try CuCompile.cudaText(increment_kernel, .{}, std.testing.allocator);
defer std.testing.allocator.free(ptx);
const module = try CuDevice.load_ptx_text(ptx);
const function = try module.get_func("increment");

// Run the kernel on the data
try function.run(.{&cu_slice.device_ptr}, CuLaunchConfig{ .block_dim = .{ 3, 1, 1 }, .grid_dim = .{ 1, 1, 1 }, .shared_mem_bytes = 0 });

// Retrieve incremented data back to the system
const incremented_arr = try CuDevice.sync_reclaim(f32, std.testing.allocator, cu_slice);
defer incremented_arr.deinit();

https://github.com/akhildevelops/cudaz

GitHub

A Zig Cuda wrapper. Contribute to akhildevelops/cudaz development by creating an account on GitHub.

meager rampart
abstract pewter
rapid saffron
#

@abstract pewter Why not just OneAPI though? Shouldn't it support all GPUs?

abstract pewter
meager rampart
#

They all claim to support all gpus but when it comes to it support for nvidia is meh and support for the other manufacterers is practically nonexistant

rapid saffron
#

There's also OpenCL

#

That should be supported by everyone

meager rampart
#

Technically maybe

#

In reality its pretty janky

#

Nvidias support is there only for the lulz and amd would rather have it be forgotten

#

At least intel is doing something cool with it

#

(sycl works on top of opencl)