stage

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: 17 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 stage builds and pins CUDA host-glue libraries with the entry ABI int stage(struct stage_ctx *). Context carries named device addresses, a stream and caller-retained CUDA function handles. Every registered native entry receives a pointer; no aggregate is passed by value.

Builder uses cc or nvcc in a caller-owned retained directory. Load compares the library's compiler-evaluated context layout with the actual Go layout. Library pins protect code lifetime only: the caller must also retain device allocations and function generation pins until stream completion. A zero host return establishes submission, not device completion. Failed unload is retryable. Compiler source, output and logs remain for qualification receipts.

Stage libraries are C host code, not Go c-shared libraries with a second Go runtime. Go plugins also cannot be unloaded and require a matching toolchain. Loading C libraries runs arbitrary host constructors and is not isolation. Linux and Darwin are supported; other platforms refuse native loading.

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

Example
ctx, err := NewContext(nil, 0, nil)
fmt.Println(err, ctx.BufferCount, ctx.FunctionCount)
Output:
<nil> 0 0

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPinned = errors.New("stage library is pinned")

ErrPinned means a library still has retained calls or submitted device work. Release the pins only after actual completion, then retry Close if necessary.

Functions

This section is empty.

Types

type Artifact

type Artifact struct {
	// contains filtered or unexported fields
}

Artifact is one compiler result. Its source, generation file and digest are private so Load cannot accept a result with substituted identity metadata. Retain its directory until the library and all pins have closed.

func (*Artifact) Generation

func (a *Artifact) Generation() uint64

Generation identifies this build within the process. Path also has a random generation-directory suffix, preventing reuse after a process restart.

Example
var artifact *Artifact
fmt.Println(artifact.Generation())
Output:
0

func (*Artifact) Path

func (a *Artifact) Path() string

Path returns the unique generation library path.

Example
var artifact *Artifact
fmt.Printf("%q\n", artifact.Path())
Output:
""

func (*Artifact) SHA256

func (a *Artifact) SHA256() string

SHA256 returns the digest of the compiler's shared library bytes. Load verifies the file still matches this digest before and after opening it.

func (*Artifact) SourceDigest

func (a *Artifact) SourceDigest() string

SourceDigest identifies the supplied stage body, independently of compiler output. Build does not claim identity for external headers or loaded libraries.

Example
var artifact *Artifact
fmt.Printf("%q\n", artifact.SourceDigest())
Output:
""

type Buffer

type Buffer struct {
	Name    *byte
	Pointer uint64
	Bytes   uint64
}

Buffer identifies one retained device allocation. Name is a NUL-terminated string that remains live through the call. Pointer is a CUDA device address, not host storage. Bytes is its declared accessible extent.

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder compiles libraries in an explicit retained directory. Its zero value is not usable. Compiler names cc or nvcc, or their explicit executable path.

func NewBuilder

func NewBuilder(dir, compiler string) (*Builder, error)

NewBuilder resolves compiler and creates dir. It does not load a library or perform CUDA work. Use a task-owned directory, not a shared temporary root.

Example
_, err := NewBuilder("", "cc")
fmt.Println(err)
Output:
create stage builder: directory and compiler are required

func (*Builder) Build

func (b *Builder) Build(ctx context.Context, source Source) (*Artifact, error)

Build compiles the fixed pointed-to ABI and its compiler layout witness in the same translation unit as Source. It leaves source and output for receipts.

Example
var builder *Builder
_, err := builder.Build(context.Background(), Source{})
fmt.Println(err)
Output:
build stage: context, builder and code are required

type Context

type Context struct {
	Version       uint32
	Size          uint32
	Buffers       *Buffer
	BufferCount   uint64
	Stream        uintptr
	Functions     *Function
	FunctionCount uint64
}

Context is the pointed-to stage_ctx ABI. The caller retains all named storage, stream and function pins until actual GPU completion, including on errors. Stage code may use this storage during Call but must not retain its address.

func NewContext

func NewContext(buffers []Buffer, stream uintptr, functions []Function) (*Context, error)

NewContext creates the fixed versioned descriptor. buffers/functions and their names remain caller-owned; their addresses must not move during Call.

Example
_, err := NewContext(make([]Buffer, 65), 0, nil)
fmt.Println(err)
Output:
stage context exceeds 64 named buffers or functions

type Function

type Function struct {
	Name   *byte
	Handle uintptr
}

Function identifies a CUDA function retained by the caller's generation pin. A stage library does not acquire or release this pin.

type Library

type Library struct {
	// contains filtered or unexported fields
}

Library owns one loaded host library. Close refuses new pins; existing pins remain usable until released. Failed unload retains its handle for retry. Its zero value is closed. A Library must not be copied after use.

func Load

func Load(artifact *Artifact) (*Library, error)

Load verifies output identity and compiler-evaluated ABI before admitting calls. A nonnil result with an error retains an unload owner; Close it before removing the artifact. Loading runs host library constructors, not a sandbox.

Example
_, err := Load(nil)
fmt.Println(err)
Output:
load stage: invalid artifact

func (*Library) Close

func (l *Library) Close() error

Close refuses new pins and unloads only after all pins are released. It does not delete retained compiler artifacts. An error permits an explicit retry.

Example
var library Library
fmt.Println(library.Close())
Output:
<nil>

func (*Library) Pin

func (l *Library) Pin() (*Pin, error)

Pin retains the loaded entry until Release. It performs no CUDA work.

Example
var library Library
_, err := library.Pin()
fmt.Println(err)
Output:
pin stage: library is unavailable

type Pin

type Pin struct {
	// contains filtered or unexported fields
}

Pin retains a host stage generation. It does not itself retain the device allocations, stream or CUDA function pins supplied in Context.

func (*Pin) Call

func (p *Pin) Call(ctx *Context) error

Call invokes int stage(stage_ctx*) synchronously on the host. Success means only that host submission returned zero; it does not establish GPU completion. Keep this pin and every Context dependency until actual stream completion.

Example
var pin *Pin
fmt.Println(pin.Call(nil))
Output:
call stage: nil pin

func (*Pin) Release

func (p *Pin) Release() error

Release drops the library pin. It must follow actual completion even when Call failed. An unload failure leaves the Library available for Close retry.

Example
var pin *Pin
fmt.Println(pin.Release())
Output:
<nil>

type Source

type Source struct {
	Code  string
	Flags []string
	CUDA  bool
}

Source is C or CUDA host glue defining int stage(struct stage_ctx *). Flags are compiler arguments; the builder owns input and output file names. Code and Flags must not be changed during Build.

Jump to

Keyboard shortcuts

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