ptxcubin

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ptxcubin compiles PTX to a CUBIN for one physical target.

Compile uses nvJitLink in process when its library loads, and runs ptxas otherwise:

res, err := ptxcubin.Compile(ctx, src, ptxcubin.Options{})
if err != nil {
	return err
}
mod, err := cuda.LoadModule(dev, res.CUBIN)

The fallback covers only an nvJitLink library that is missing or will not load. When nvJitLink loads and rejects the PTX, Compile returns that error and does not try ptxas, so a diagnostic is never replaced by a second toolchain's opinion. Set Options.Tool to use one tool and no other.

The target is Options.Arch or, when that is empty, the module's .target. A virtual target such as compute_90 has no CUBIN, and Compile refuses it.

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Example

This example builds a kernel, compiles it, and runs it on device 0. It needs nvJitLink or ptxas and an sm_121a device.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/tmc/lib/nvidia/x/compile/ptxcubin"
	"github.com/tmc/lib/nvidia/x/cuda"
	"github.com/tmc/lib/nvidia/x/isa/ptx/ptxbuild"
)

// vectorAdd builds c[i] = a[i] + b[i] for i < n.
func vectorAdd() *ptxbuild.Module {
	m := ptxbuild.NewModule("8.8", "sm_121a")
	f := m.Entry("vadd")
	var bases [3]*ptxbuild.Ptr
	for i, name := range []string{"a", "b", "c"} {
		bases[i] = f.GlobalPtr(f.Param(ptxbuild.U64, name))
	}
	n := f.Reg(ptxbuild.U32)
	f.Inst("ld.param.u32", n, ptxbuild.Addr(f.Param(ptxbuild.U32, "n"), 0))
	i := f.GlobalThreadIndex()
	done := f.Label("done")
	p := f.Reg(ptxbuild.Pred)
	f.Inst("setp.ge.u32", p, i, n)
	f.If(p).Inst("bra", done)
	off := f.Reg(ptxbuild.U64)
	f.Inst("mul.wide.u32", off, i, ptxbuild.Imm(4))
	a, b, c := f.Reg(ptxbuild.F32), f.Reg(ptxbuild.F32), f.Reg(ptxbuild.F32)
	f.Inst("ld.global.f32", a, f.Index(bases[0], off).Addr(0))
	f.Inst("ld.global.f32", b, f.Index(bases[1], off).Addr(0))
	f.Inst("add.rn.f32", c, a, b)
	f.Inst("st.global.f32", f.Index(bases[2], off).Addr(0), c)
	f.Mark(done)
	f.Inst("ret")
	return m
}

func main() {
	mod, err := vectorAdd().Module()
	if err != nil {
		log.Fatal(err)
	}
	res, err := ptxcubin.Compile(context.Background(), []byte(mod.String()), ptxcubin.Options{})
	if err != nil {
		log.Fatal(err)
	}

	dev, err := cuda.NewDevice(0)
	if err != nil {
		log.Fatal(err)
	}
	defer dev.Close()
	m, err := cuda.LoadModule(dev, res.CUBIN)
	if err != nil {
		log.Fatal(err)
	}
	defer m.Close()
	k, err := m.Function("vadd")
	if err != nil {
		log.Fatal(err)
	}

	const n = 1000
	x, y := make([]float32, n), make([]float32, n)
	for i := range x {
		x[i], y[i] = float32(i), float32(2*i)
	}
	var bufs [3]cuda.Slice[float32]
	for i := range bufs {
		if bufs[i], err = cuda.Alloc[float32](dev, n); err != nil {
			log.Fatal(err)
		}
		defer bufs[i].Close()
	}
	if err := bufs[0].CopyFromHost(x); err != nil {
		log.Fatal(err)
	}
	if err := bufs[1].CopyFromHost(y); err != nil {
		log.Fatal(err)
	}
	if err := k.Grid((n+255)/256, 1, 1).Block(256, 1, 1).Launch(bufs[0], bufs[1], bufs[2], uint32(n)); err != nil {
		log.Fatal(err)
	}
	z := make([]float32, n)
	if err := bufs[2].CopyToHost(z); err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Tool, z[1], z[n-1])
}

Index

Examples

Constants

View Source
const (
	NVJitLink = "nvjitlink"
	PTXAS     = "ptxas"
)

Tools that Compile can run.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Arch is the physical target, such as sm_90 or sm_121a.
	// Empty means the module's .target. Both tools refuse a target
	// older than the module's .target.
	Arch string

	// Tool is NVJitLink or PTXAS to use only that tool.
	// Empty means nvJitLink if its library loads, otherwise ptxas.
	Tool string

	// PTXAS is the ptxas executable. Empty searches the CUDA toolkit
	// installs and then PATH.
	PTXAS string

	// MaxRegisters limits registers per thread. Zero means no limit.
	MaxRegisters int

	// LineInfo records PTX line information in the CUBIN.
	LineInfo bool
}

Options control one compilation. The zero value compiles for the module's .target with whichever tool is available.

type Result

type Result struct {
	CUBIN   []byte
	Arch    string // the target compiled for
	Tool    string // NVJitLink or PTXAS
	Path    string // the library or executable that ran
	Version string // the tool's CUDA version, such as "13.4"; empty if unknown
	Log     string // the tool's informational output
}

A Result is a compiled CUBIN and how it was made.

func Compile

func Compile(ctx context.Context, ptx []byte, opt Options) (*Result, error)

Compile compiles ptx to a CUBIN as described in the package doc.

Example (VirtualTarget)

A virtual target has no CUBIN; Options.Arch names the physical one.

package main

import (
	"context"
	"fmt"

	"github.com/tmc/lib/nvidia/x/compile/ptxcubin"
)

func main() {
	src := []byte(".version 8.8\n.target compute_90\n.address_size 64\n")
	_, err := ptxcubin.Compile(context.Background(), src, ptxcubin.Options{})
	fmt.Println(err)
}
Output:
ptxcubin: compute_90 is not a physical target; set Options.Arch

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL