budget

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: 23 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 budget checks CUDA compilation resource budgets (registers, stack frames, spill loads/stores, and shared memory) across target SM architectures and enforces the repository-wide kernel census.

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

Example
package main

import (
	"context"
	"fmt"
	"os"
	"strings"

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

func main() {
	src, err := os.ReadFile("testdata/controls.cu")
	if err != nil {
		panic(err)
	}
	toolkit, err := os.ReadFile("testdata/census/toolkit.txt")
	if err != nil {
		panic(err)
	}
	// This toolchain replays the census recorded in testdata. On a host
	// with CUDA, leave NVCC and Toolkit empty to compile with its nvcc.
	tc := &budget.Toolchain{
		NVCC:    "/nonexistent/nvcc",
		Toolkit: strings.TrimSpace(string(toolkit)),
		Cache:   "testdata/census",
	}
	tu := &budget.TU{
		Source: src,
		Arch:   "sm_121a",
		Flags:  []string{"-std=c++17", "-O3"},
		Budgets: map[string]budget.Budget{
			"fits":  {Registers: 32, Opcodes: []string{"FFMA"}},
			"spill": {Registers: 64},
		},
	}
	_, err = budget.CheckTU(context.Background(), tc, tu)
	fmt.Println(err)
}
Output:
spill: 488 bytes stack frame, budget 0
spill: 1016 bytes spill stores, budget 0
spill: 1100 bytes spill loads, budget 0

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoToolchain = errors.New("cudacompilecheck: no nvcc")

ErrNoToolchain is returned when a compilation is not in the cache and no nvcc can be found to run it.

Functions

func SharedLimits

func SharedLimits(arch string) (static, optin int, err error)

SharedLimits returns the shared memory one block of a kernel compiled for arch may use: at most static bytes declared statically, and at most optin bytes in all, static plus dynamic, once the kernel opts in. The values come from the cudadevicetable profile of arch's architecture.

Example
package main

import (
	"fmt"

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

func main() {
	for _, arch := range []string{"sm_90a", "sm_121a"} {
		static, optin, err := budget.SharedLimits(arch)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s: %d bytes static, %d bytes with opt-in\n", arch, static, optin)
	}
}
Output:
sm_90a: 49152 bytes static, 232448 bytes with opt-in
sm_121a: 49152 bytes static, 101376 bytes with opt-in

func TextBytes

func TextBytes(cubin []byte) (map[string]int, error)

TextBytes returns the text bytes of each function in a cubin, keyed by the function's name: the size of its .text.<name> section, which holds its machine code. A callee the compiler did not inline may sit inside its caller's section, where it is counted with the caller, or in a section of its own. Constant banks and other sections are not counted.

Types

type Budget

type Budget struct {
	Registers   int // per thread; entry functions only
	StackFrame  int // bytes per thread
	SpillStores int // bytes
	SpillLoads  int // bytes
	SharedMem   int // static bytes per block; entry functions only

	// DynamicSharedMem is the dynamic shared memory, in bytes per block,
	// that the kernel's launches request; entry functions only. Check
	// refuses an entry whose static plus dynamic shared memory exceeds the
	// target's opt-in limit per block, and one whose static shared memory
	// alone exceeds the static limit, whatever SharedMem allows: nvcc and
	// ptxas accept both, and the launch is what fails.
	DynamicSharedMem int

	// Opcodes are SASS mnemonics an entry function's code section must
	// contain, matched as [cudasass.Disassembly.AssertOpcodes] matches
	// them. The section includes the device functions the entry calls, so
	// a device function's opcodes are checked through a Probe that calls
	// it; a device function's budget may not name any.
	Opcodes []string
}

A Budget bounds one function's resources. Every field is a maximum, and a zero field is a maximum of zero: Budget{Registers: 64} admits no stack, no spills and no static shared memory. BudgetOf gives the budget a function exactly meets, for recording a golden.

func BudgetOf

func BudgetOf(f *ptx.Function) Budget

BudgetOf returns the budget f exactly meets, without opcodes.

type KernelFact

type KernelFact struct {
	Unit              string   `json:"unit"`
	Entry             string   `json:"entry"`
	Arch              string   `json:"arch"`
	Launcher          string   `json:"launcher"`
	Block             int      `json:"block"`
	SharedMem         int      `json:"shared_mem"`
	DeclaredSharedMem int      `json:"declared_shared_mem"`
	ExternShared      *bool    `json:"-"`
	DynamicSharedMem  *int     `json:"-"`
	PTXAS             []string `json:"-"`
	SourceSHA256      string   `json:"-"`
	SASSSHA256        string   `json:"-"`
	CompileError      string   `json:"-"`
}

KernelFact links an inventoried entry and launcher to recorded compiler facts. Each row describes one target. A nil ExternShared or DynamicSharedMem means the census records no value; it does not mean false or zero. PTXAS describes the whole translation unit. No cubin or SASS listing is included.

func KernelFacts

func KernelFacts() ([]KernelFact, error)

KernelFacts returns the generated inventory joined to the recorded census. It reads embedded data and requires neither a toolkit nor a device. The returned slices and pointers belong to the caller.

Example
facts, err := KernelFacts()
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(len(facts) > 0)
Output:
true

type Probe

type Probe struct {
	Name   string
	Params string
	Body   string
}

A Probe is a generated entry function:

extern "C" __global__ void Name(Params) { Body }

type Report

type Report struct {
	Result *Result
	PTXAS  *ptx.Report
	SASS   *isasass.Disassembly
}

A Report is a checked compilation.

func Check

func Check(tu *TU, res *Result) (*Report, error)

Check parses res and checks it against tu's budgets. It returns the parsed report with an error joining every violation.

func CheckTU

func CheckTU(ctx context.Context, tc *Toolchain, tu *TU) (*Report, error)

CheckTU compiles tu with tc and checks it, as Check does.

type Result

type Result struct {
	Toolkit string         `json:"toolkit"` // nvcc's release line
	Arch    string         `json:"arch"`
	Flags   []string       `json:"flags,omitempty"`
	Source  string         `json:"source_sha256"`        // of the compiled text, probes included
	PTXAS   []byte         `json:"-"`                    // nvcc and ptxas -v output
	SASS    []byte         `json:"-"`                    // nvdisasm -c output
	Text    map[string]int `json:"text_bytes,omitempty"` // machine code bytes per function (TextBytes)
	Elapsed time.Duration  `json:"elapsed_ns"`           // nvcc wall-clock time

	// Cached reports whether the result was read from the cache.
	Cached bool `json:"-"`
}

A Result is the output of compiling a TU.

type TU

type TU struct {
	Source []byte
	Arch   string   // exact target, such as "sm_121a"
	Flags  []string // extra nvcc flags, such as "-std=c++20"

	// Probes are entry functions appended to Source, each calling device
	// code that no kernel in Source instantiates or calls, so that ptxas
	// reports its resources and its SASS can be checked.
	Probes []Probe

	// Budgets bounds each function by mangled name, or by Probe name.
	// Every entry function the compiler reports must have a budget, and
	// every budget must name a function the compiler reported.
	Budgets map[string]Budget

	// CompileTime bounds the compiler's wall-clock time; zero is no bound.
	CompileTime time.Duration
}

A TU is a CUDA translation unit to compile for one target.

type Toolchain

type Toolchain struct {
	// NVCC is the nvcc to run. Empty means a toolkit install or PATH, as
	// cudatool.LookTool resolves it. ptxas and nvdisasm are taken from
	// the same directory.
	NVCC string

	// Toolkit identifies the toolkit, as nvcc --version's release line.
	// Empty means ask nvcc. A Toolchain with no nvcc must set it to read
	// a cache.
	Toolkit string

	// Cache is the census cache directory; empty means no cache.
	Cache string
}

A Toolchain compiles TUs with nvcc, ptxas and nvdisasm, through a census cache.

The cache is a directory of JSON files named by a digest of the toolkit, the target, the flags and the compiled text. A recorded cache in testdata lets a host without CUDA check budgets against real compiler output; a cache miss there is an error wrapping ErrNoToolchain, not a skip.

func (*Toolchain) Compile

func (tc *Toolchain) Compile(ctx context.Context, tu *TU) (*Result, error)

Compile compiles tu to a cubin, recording ptxas -v output and the SASS, or reads the result from the cache.

Jump to

Keyboard shortcuts

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