Skip to Main Content
Talk Intermediate

Flavours of Zig language

Approved
Session Description

Why ziglang ?

Learning a language as it is is boring and one cannot appreciate it's offerings unless they're applied. I would like to present ziglang and it's powerful features like comptime, build.zig and many more that I applied in building a GPU library.

Ok, what's comptime ?

It's the specific code regions in the source code that should be resolved during comptime. For example, below Fibonacci number at 50th position get's resolved to 12586269025 during compilation and the value is available during runtime.

const std = @import("std");
fn fibo(position: usize) usize {
    var a: usize = 0;
    var b: usize = 1;
    for (0..position) |_| {
        b = a + b;
        a = b - a;
    }
    return a;
}

pub fn main() void {
    const comptime_fib = comptime fibo(50); // calculates during compilation
    std.debug.print("comptime_val:{d}", .{comptime_fib});
}

Assembly output is just 5 lines: https://godbolt.org/z/8553K9Ps7

get_fibo:
        push    rbp
        mov     rbp, rsp
        movabs  rax, 12586269025
        pop     rbp
        ret

I have used this powerful feature in creating Error types for my gpu library during compile time by parsing flat strings.

Here's where https://github.com/akhildevelops/cudaz/blob/cb8b1d11fa87b399ebf59a00b3031d34b64eb4c1/src/error.zig#L10-L12 error types are generated through comptime.

Searching and linking dynamic libraries:

The library should be portable i.e, should work on windows / linux. The major problem is to search and link gpu dynamic library provided by the vendor. In this regard we need a stronger build system like cmake and build.zig is the answer for zig ecosystem. build.zig helps in book keeping activities, pre-comptime checks, generate tree like process flows to generate executables, libraries. Interestingly compilation flow can be written in zig code. The build file for gpu library can be found at https://github.com/akhildevelops/cudaz/blob/main/build.zig

Importing C libraries:

It's easy to integrate C libraries into zig code using compiler builtin functions. They have been extensively used to import C headerfiles https://github.com/akhildevelops/cudaz/blob/main/src/c.zig

There are many other features with simpler syntax that will be shared in the talk in the context of gpu library.

I'll also show an end to end example in running a GPU application through ziglang and the library.

Key Takeaways
  • Ziglang ergonomics

  • GPU basics

  • compile time codegen

References

Session Categories

Story of a FOSS project - from inception to growth
Which track are you applying for?
Main track

Speakers

Akhil Teja G R Sr Machine Learning Engineer

I'm Sr Machine Learning Engineer @DBS, Singapore headquartered international bank.

My work revolves around Machine learning and GenAI. I'm associated with Swecha (tech community based out of Hyderabad) as a regular speaker for talks related to Rustlang and Python.

I contribute to several opensource projects and have few popular projects under my name:

https://github.com/akhildevelops/cudaz

https://github.com/akhildevelops/summarizer

Speaker Links:

Pycon 2024 India: https://www.youtube.com/watch?v=10O34A7lAKs&list=PL6GW05BfqWIe8GJyxf11fldN32IoHSFjv

Talk on Internals of Rust: https://slides.com/akhilg/palette-34e054

Talk on Podcast Summarizer built in Rust: https://slides.com/akhilg/palette

Cuda library in Zig: https://github.com/akhildevelops/cudaz

Akhil Teja G R
https://x.com/akhildevelops

Reviews

Reviews are hidden by the event organisers.