Pidoku

Your First Kernel, Called from Go

Module 5 Lesson 5.c 1h 30m Difficulty 3/5

Prerequisites 5.b

Optional for readers who do not use Go: the five steps in this lesson are identical from Python (PyTorch, CuPy), Rust or C++. Read it for the steps and the cost model; skip the cgo details if they do not apply to you.


Before you start: words you need#

WordPlain meaning
Host programThe CPU-side program that drives the GPU. Here it is written in Go.
cgoGo’s built-in bridge for calling C functions.
Shared library (.so)Compiled code that another program loads at run time.
SAXPY“Single-precision a·x plus y”: y = a·x + y for every element. The classic first GPU kernel.
extern "C"Tells the C++ compiler to export a function under a plain C name, so other languages can call it.

1. What is it?#

A real GPU program has two halves. The kernel is written in CUDA C and compiled for the GPU. The host program — ours is in Go — allocates GPU memory, copies data in, launches the kernel, and copies results out. Go reaches the CUDA half through cgo, Go’s built-in bridge to C.

Every GPU program, in any language, performs the same five steps you are about to write: allocate, copy in, launch, copy out, free.


2. Why does it exist?#

Lesson 5.b wrote a kernel and its host code in one C++ file. Real systems usually split them: the kernels live in a library, and a service written in another language drives them. Seeing that split once — across a language boundary — makes clear what crosses PCIe, what runs where, and why inference servers are designed to keep data on the GPU.


3. Simple analogy#

Sending work to a GPU is like sending documents to a print shop across town. You package the originals (copy in), give instructions (launch), wait, collect the prints (copy out), and pay the courier each way. For one page, the courier costs more than the printing. Print shops pay off when you send a large job, or leave the originals there and order many runs.


4. Tiny example#

sequenceDiagram
  participant Go as Go program (host)
  participant C as CUDA C wrapper
  participant GPU as GPU (device)
  Go->>C: saxpy(a, x, y, n) via cgo
  C->>GPU: 1. allocate device memory
  C->>GPU: 2. copy x and y to the device
  C->>GPU: 3. launch kernel on the grid
  Note over GPU: thousands of threads run
  C->>GPU: 4. copy result back
  GPU-->>C: bytes
  C->>GPU: 5. free device memory
  C-->>Go: return

The kernel (CUDA C)#

CUDA C++
// saxpy.cu
#include <cuda_runtime.h>

// __global__ marks a function that runs on the GPU and is launched from the host.
__global__ void saxpy_kernel(int n, float a, const float *x, float *y) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;   // which element am I?
    if (i >= n) return;                              // bounds check
    y[i] = a * x[i] + y[i];
}

// A plain C function Go can call. It performs the five steps.
extern "C" int saxpy(int n, float a, const float *host_x, float *host_y) {
    float *x, *y;
    size_t bytes = n * sizeof(float);

    if (cudaMalloc(&x, bytes) != cudaSuccess) return 1;         // 1. allocate
    if (cudaMalloc(&y, bytes) != cudaSuccess) return 1;

    cudaMemcpy(x, host_x, bytes, cudaMemcpyHostToDevice);       // 2. copy in
    cudaMemcpy(y, host_y, bytes, cudaMemcpyHostToDevice);

    int block = 256;
    int grid  = (n + block - 1) / block;
    saxpy_kernel<<<grid, block>>>(n, a, x, y);                  // 3. launch

    cudaMemcpy(host_y, y, bytes, cudaMemcpyDeviceToHost);       // 4. copy out (waits for the kernel)

    cudaFree(x); cudaFree(y);                                   // 5. free
    return cudaGetLastError() == cudaSuccess ? 0 : 2;
}

The only unusual syntax is <<<grid, block>>>: it is how CUDA says “launch this function on a grid of this many blocks of this many threads”.

Compile it into a shared library with NVIDIA’s compiler:

Shell
nvcc -O2 -shared -fPIC -o libsaxpy.so saxpy.cu

The host program (Go)#

Go
// main.go — build with: CGO_ENABLED=1 go build && LD_LIBRARY_PATH=. ./first-kernel
package main

/*
#cgo LDFLAGS: -L. -lsaxpy
int saxpy(int n, float a, const float *x, float *y);
*/
import "C"

import (
	"fmt"
	"unsafe"
)

func main() {
	const n = 1 << 20
	x, y := make([]float32, n), make([]float32, n)
	for i := range x {
		x[i], y[i] = float32(i), 1
	}

	// Hand C a pointer to each slice's backing array. cgo keeps the slices
	// alive and in place for the duration of the call.
	rc := C.saxpy(C.int(n), C.float(2),
		(*C.float)(unsafe.Pointer(&x[0])),
		(*C.float)(unsafe.Pointer(&y[0])))
	if rc != 0 {
		panic(fmt.Sprint("saxpy failed: ", rc))
	}
	fmt.Println(y[0], y[1], y[n-1]) // 1 3 2.097151e+06
}

5. Technical explanation#

Three things to notice#

  1. The kernel is tiny; the plumbing is not. Three lines of real work, fifteen of memory management. That ratio is normal.
  2. This program is slow on purpose. It copies 8 MB over PCIe, does a trivial amount of arithmetic, and copies it back. The copies take far longer than the kernel. A CPU would win. GPUs pay off when data stays on the device across many kernels.
  3. cgo has a cost of its own. Each Go→C call costs on the order of 50–100 nanoseconds and pins the calling goroutine to an OS thread while it runs. That is nothing compared with a kernel launch, but it means you make few, large calls — never one cgo call per element.

Other ways to reach a GPU from Go#

ApproachWhat it isWhen to use
cgo + your own CUDA CWhat you just didCustom kernels, full control
cgo + a library (cuBLAS, ONNX Runtime, llama.cpp)Call existing optimized codeAlmost always the practical answer
Driver API without cgo (purego-style bindings)Load libcuda at run time, no C compiler in the buildSimpler builds and cross-compiling; kernels shipped precompiled
A separate inference server (vLLM, Triton, Ollama) over HTTP/gRPCThe GPU lives in another processProduction serving; the usual choice for Go services
go-nvmlManagement only: read state, no computeMonitoring and scheduling (5.a)

Most production Go code never launches a kernel. It talks to a server that does. Knowing what that server is doing underneath is what this material is for.


6. Under the hood#

What actually happens in step 4: cudaMemcpy from device to host waits until all earlier work on the default stream — including the kernel — has finished, then copies. That hidden wait is why the example does not call cudaDeviceSynchronize(). Lesson 5.d follows the launch and copy path in detail, and 5.e shows how to overlap copies with work.

On the Go side, a cgo call switches the goroutine onto an OS thread and out of Go’s scheduler for the duration of the call. Go’s garbage collector must not move or free x and y while C uses them; passing &x[0] directly in the call is the documented safe pattern (cgo pins the memory for the call).

No GPU? A pretend device#

If you cannot run the CUDA version, this simulator has a separate “device memory” and forces you through the same five steps:

Go
// device.go — a pretend GPU that enforces the five-step discipline.
package main

import "fmt"

type Device struct {
	mem    map[int][]float32 // device allocations, by handle
	next   int
	copies int // how many times data crossed the "PCIe link"
}

func NewDevice() *Device { return &Device{mem: map[int][]float32{}} }

func (d *Device) Malloc(n int) int              { d.next++; d.mem[d.next] = make([]float32, n); return d.next }
func (d *Device) Free(h int)                    { delete(d.mem, h) }
func (d *Device) CopyIn(h int, host []float32)  { copy(d.mem[h], host); d.copies++ }
func (d *Device) CopyOut(host []float32, h int) { copy(host, d.mem[h]); d.copies++ }

// Launch runs kernel for every index. The kernel can only see device memory.
func (d *Device) Launch(n int, kernel func(i int, mem map[int][]float32)) {
	for i := 0; i < n; i++ {
		kernel(i, d.mem)
	}
}

func main() {
	const n = 8
	hostX, hostY := make([]float32, n), make([]float32, n)
	for i := range hostX {
		hostX[i], hostY[i] = float32(i), 1
	}

	dev := NewDevice()
	x, y := dev.Malloc(n), dev.Malloc(n) // 1. allocate
	dev.CopyIn(x, hostX)                 // 2. copy in
	dev.CopyIn(y, hostY)
	dev.Launch(n, func(i int, m map[int][]float32) { // 3. launch
		m[y][i] = 2*m[x][i] + m[y][i]
	})
	dev.CopyOut(hostY, y) // 4. copy out
	dev.Free(x)           // 5. free
	dev.Free(y)

	fmt.Println(hostY, "link crossings:", dev.copies)
}

Output:

[1 3 5 7 9 11 13 15] link crossings: 3

7. Performance implications#

saxpy on 2^20 floats (8 MB in, 4 MB out):
  PCIe copies at ~25–50 GB/s achieved   ≈ 0.25–0.5 ms
  the kernel at ~3 TB/s                  ≈ 0.004 ms

The copies cost about a hundred times the kernel. A GPU pays off only when data stays on the device across many kernels — exactly what an inference engine does: weights are copied once at start-up, and per request only a few kilobytes of token IDs cross PCIe.


8. Production implications#

  • Structure GPU code as upload once, run many times, download only results.
  • Keep cgo (or any foreign-function) calls few and large.
  • In practice, Go services talk to an inference server over HTTP or gRPC rather than linking CUDA code; the table above lists the options.

9. Common mistakes#

  1. Copying inputs and outputs on every call when the data could stay on the GPU.
  2. One foreign-function call per element.
  3. Ignoring return codes from cudaMalloc and cudaMemcpy; errors surface later in confusing places (5.b).
  4. Freeing device memory while a kernel may still be using it (5.e).

10. Hands-on exercise#

  1. (GPU) Build and run the CUDA version. Time the whole saxpy call for n = 2^10, 2^20 and 2^26. Then time a plain Go loop doing the same. Where does the GPU start to win, if at all?
  2. (GPU) Split saxpy into upload, run and download functions so run can be called 100 times without copying. Time 100 runs. This is how real programs are structured.
  3. (No GPU) In device.go, run the kernel 100 times. Count link crossings when you copy in and out every time versus once. Which design would you ship?

11. Interview questions#

  1. What are the five steps of every GPU program?
  2. Why is the SAXPY example slower on a GPU than on a CPU?
  3. Why should foreign-function calls be few and large?
  4. How does an inference engine avoid paying PCIe costs on every request?

12. Further reading#

Checked on 11 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in