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();