Docsv0.5.1Ask AIGet started

Kernels One function, every GPU

A kernel is a function that runs on the GPU. Mark it with kernel and Emma compiles it for CUDA, ROCm or Vulkan.

01

Your first kernel

This kernel adds two vectors. Every thread handles one element, and id.x tells it which one.

vectorAdd.emma
1
2
3
kernel func vectorAdd(a: Float[], b: Float[], c: Float[]) {
    c[id.x] = a[id.x] + b[id.x]
}
02

Async work inside a kernel

A kernel can start work in the background and wait only when it needs the result.

allocateAndFill.emma
1
2
3
4
5
6
7
8
9
kernel func allocateAndFill(with constant: Float) {
    // The allocation will begin in the background
    let mem = gpu.alloc(1024)

    // do other stuff

    // If the allocation is not complete, pause here until it is
    await gpu.fill(await mem, constant)
}
03

Go lower with builder functions

When Emma does not have the op you need, a builder function emits MLIR at compile time.

gpuBarrier.emma
1
2
3
builder func gpuBarrier() {
    builder.emitOp("gpu.barrier")
}
output.mlir
1
2
3
func.func @gpuBarrier() {
    gpu.barrier
}