lane

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: 3 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 lane holds the resources of one execution lane on a CUDA device: a stream, a tracked device memory arena, and the per-lane resources, such as modules and matrix plans, built for that stream.

A lane lets a second step run beside the first, for example on another green context's stream, while sharing state such as weights that the lane does not own. New takes ownership of the stream and runs an init function that builds the lane's resources, registering each one's release with Lane.Defer. If init fails, New releases everything init made and closes the stream, so a caller never cleans up a half-built lane. Lane.Close closes the stream, then runs the deferred releases in reverse order, then closes the arena.

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 lane with its own module and activations on a new stream. It needs a CUDA device, so it has no output to check.

package main

import (
	"fmt"
	"log"

	"github.com/tmc/lib/nvidia/x/cuda"
	"github.com/tmc/lib/nvidia/x/cuda/lane"
)

// This example builds a lane with its own module and activations on a new
// stream. It needs a CUDA device, so it has no output to check.
func main() {
	dev, err := cuda.NewDevice(0)
	if err != nil {
		log.Fatal(err)
	}
	defer dev.Close()
	stream, err := dev.NewStream()
	if err != nil {
		log.Fatal(err)
	}
	var x cuda.Slice[float32]
	l, err := lane.New(stream, nil, func(l *lane.Lane) error {
		mod, err := cuda.LoadModule(dev, ptx)
		if err != nil {
			return fmt.Errorf("load kernels: %w", err)
		}
		l.Defer(mod.Close)
		x = cuda.AllocIn[float32](l.Arena(), "x", 1024)
		return nil
	})
	if err != nil {
		log.Fatal(err) // New has closed stream.
	}
	defer l.Close()
	fmt.Println(x.Len())
}

var ptx []byte

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lane

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

A Lane owns a stream, an arena that allocates on the stream's device, and the releases registered with Lane.Defer.

func New

func New(stream *cuda.Stream, tracker *cuda.Tracker, init func(*Lane) error) (*Lane, error)

New returns a lane on stream whose arena's allocations are accounted by tracker, which may be nil. It takes ownership of stream, then calls init, which builds the lane's resources. If init returns an error or an allocation from the arena failed, New closes the lane, stream included, and returns the error.

func (*Lane) Arena

func (l *Lane) Arena() *cuda.Arena

Arena returns the lane's arena. Close frees its allocations.

func (*Lane) Close

func (l *Lane) Close() error

Close closes the lane's stream, which waits for the work on it, runs the releases registered with Defer in reverse order, and closes the arena. It runs every step whatever errors earlier steps report, and returns them joined. Closing a closed lane does nothing.

func (*Lane) Defer

func (l *Lane) Defer(release func() error)

Defer registers release to run when the lane closes, after the stream has closed and before the arena is closed. Releases run in the reverse order of their registration, so a resource registered after the ones it uses is released before them. A release must not use the lane's stream.

func (*Lane) Stream

func (l *Lane) Stream() *cuda.Stream

Stream returns the lane's stream.

Jump to

Keyboard shortcuts

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