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 ¶
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 ¶
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.
func TextBytes ¶
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
// 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.
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"`
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 ¶
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.
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.