driver

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 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 driver provides Pure-Go bindings to NVIDIA's CUDA Driver API (libcuda.so.1 / nvcuda.dll) loaded dynamically via internal/dynlib.

Index

Constants

View Source
const (
	MemHostAllocPortable      uint32 = 0x01
	MemHostAllocDeviceMap     uint32 = 0x02
	MemHostAllocWriteCombined uint32 = 0x04
)

MemHostAllocFlags corresponds to CU_MEMHOSTALLOC_* flags.

Variables

View Source
var ErrDriverUnavailable = stderrors.New("CUDA driver unavailable")

ErrDriverUnavailable indicates that no usable CUDA driver library or CUDA-capable GPU is present.

Functions

This section is empty.

Types

type API

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

API holds dynamically bound CUDA Driver API function pointers.

func Load

func Load() (*API, error)

Load lazily opens the CUDA Driver library, binds core Driver API symbols, and calls cuInit(0). Subsequent calls return the cached *API instance.

func (*API) CtxGetCurrent

func (a *API) CtxGetCurrent() (Context, error)

CtxGetCurrent returns the CUDA context currently bound to the calling OS thread.

func (*API) CtxSetCurrent

func (a *API) CtxSetCurrent(ctx Context) error

CtxSetCurrent binds ctx to the calling OS thread.

func (*API) CtxSynchronize

func (a *API) CtxSynchronize() error

CtxSynchronize blocks until all work on the current context completes.

func (*API) DeviceAttribute

func (a *API) DeviceAttribute(dev Device, attr DeviceAttribute) (int, error)

DeviceAttribute queries a single CUdevice_attribute integer value.

func (*API) DeviceCount

func (a *API) DeviceCount() (int, error)

DeviceCount returns the number of CUDA-capable devices.

func (*API) DeviceGet

func (a *API) DeviceGet(ordinal int) (Device, error)

DeviceGet returns the CUdevice handle for the given ordinal.

func (*API) DeviceGetDefaultMemPool

func (a *API) DeviceGetDefaultMemPool(dev Device) (MemPool, error)

DeviceGetDefaultMemPool returns the default CUDA memory pool for dev.

func (*API) DriverVersion

func (a *API) DriverVersion() int

DriverVersion returns the raw CUDA driver version integer (e.g., 13030 for CUDA 13.3).

func (*API) DriverVersionString

func (a *API) DriverVersionString() string

DriverVersionString returns the formatted CUDA driver version (e.g., "13.3").

func (*API) EventCreate

func (a *API) EventCreate(flags EventFlags) (Event, error)

EventCreate creates a CUDA event in the current context.

func (*API) EventDestroy

func (a *API) EventDestroy(e Event) error

EventDestroy destroys a CUDA event.

func (*API) EventElapsedTime

func (a *API) EventElapsedTime(start, end Event) (float32, error)

EventElapsedTime returns the elapsed time in milliseconds between start and end events.

func (*API) EventRecord

func (a *API) EventRecord(e Event, s Stream) error

EventRecord records event in stream.

func (*API) EventSynchronize

func (a *API) EventSynchronize(e Event) error

EventSynchronize blocks until event completes.

func (*API) GraphDestroy added in v0.1.1

func (a *API) GraphDestroy(graph Graph) error

GraphDestroy releases a captured Graph template (independent of any instantiated GraphExec).

func (*API) GraphExecDestroy added in v0.1.1

func (a *API) GraphExecDestroy(graphExec GraphExec) error

GraphExecDestroy releases an instantiated GraphExec.

func (*API) GraphExecUpdate added in v0.1.1

func (a *API) GraphExecUpdate(graphExec GraphExec, graph Graph) (bool, error)

GraphExecUpdate attempts to update an instantiated GraphExec in-place from a topologically identical Graph. Returns updated=true on success; updated=false (with nil error) when cuGraphExecUpdate_v2 is unavailable or reports a non-updatable change so the caller can fall back to destroying and re-instantiating the GraphExec.

func (*API) GraphInstantiate added in v0.1.1

func (a *API) GraphInstantiate(graph Graph, flags uint64) (GraphExec, error)

GraphInstantiate instantiates a captured Graph into a launchable GraphExec.

func (*API) GraphLaunch added in v0.1.1

func (a *API) GraphLaunch(graphExec GraphExec, stream Stream) error

GraphLaunch submits an instantiated GraphExec onto stream.

func (*API) HasGraphAPI added in v0.1.1

func (a *API) HasGraphAPI() bool

HasGraphAPI reports whether CUDA Graph stream capture and replay symbols are available.

func (*API) HasGraphExecUpdate added in v0.1.1

func (a *API) HasGraphExecUpdate() bool

HasGraphExecUpdate reports whether cuGraphExecUpdate_v2 is available.

func (*API) HasStreamOrderedAlloc

func (a *API) HasStreamOrderedAlloc() bool

HasStreamOrderedAlloc reports whether cuMemAllocAsync / cuMemFreeAsync symbols were bound.

func (*API) LaunchKernel

func (a *API) LaunchKernel(
	fn Function,
	gridX, gridY, gridZ uint32,
	blockX, blockY, blockZ uint32,
	sharedMemBytes uint32,
	stream Stream,
	kernelParams unsafe.Pointer,
) error

LaunchKernel launches a CUDA kernel function on stream with the given grid/block dimensions and kernelParams (void** pointer array, e.g., from arena.KernelArgs.ParamsPtr()).

func (*API) LibraryPath

func (a *API) LibraryPath() string

LibraryPath returns the dynamic library name/path that was opened (e.g., "libcuda.so.1").

func (*API) MemAlloc

func (a *API) MemAlloc(byteSize uintptr) (DevicePtr, error)

MemAlloc allocates byteSize bytes of linear device memory in the current context.

func (*API) MemAllocAsync

func (a *API) MemAllocAsync(byteSize uintptr, s Stream) (DevicePtr, error)

MemAllocAsync allocates byteSize bytes of stream-ordered VRAM on stream s.

func (*API) MemFree

func (a *API) MemFree(ptr DevicePtr) error

MemFree frees device memory previously allocated by MemAlloc.

func (*API) MemFreeAsync

func (a *API) MemFreeAsync(ptr DevicePtr, s Stream) error

MemFreeAsync asynchronously frees stream-ordered VRAM ptr on stream s.

func (*API) MemFreeHost

func (a *API) MemFreeHost(ptr unsafe.Pointer) error

MemFreeHost frees page-locked host memory allocated by MemHostAlloc.

func (*API) MemGetInfo

func (a *API) MemGetInfo() (freeBytes, totalBytes uint64, err error)

MemGetInfo returns free and total VRAM in bytes for the current context.

func (*API) MemHostAlloc

func (a *API) MemHostAlloc(byteSize uintptr, flags uint32) (unsafe.Pointer, error)

MemHostAlloc allocates page-locked (pinned) host memory.

func (*API) MemPoolSetReleaseThreshold

func (a *API) MemPoolSetReleaseThreshold(pool MemPool, thresholdBytes uint64) error

MemPoolSetReleaseThreshold configures CU_MEMPOOL_ATTR_RELEASE_THRESHOLD on pool.

func (*API) MemPoolTrimTo

func (a *API) MemPoolTrimTo(pool MemPool, minBytesToKeep uintptr) error

MemPoolTrimTo releases unused memory from pool back to the OS down to minBytesToKeep.

func (*API) MemcpyDtoD

func (a *API) MemcpyDtoD(dst, src DevicePtr, byteSize uintptr) error

MemcpyDtoD synchronously copies byteSize bytes between device allocations.

func (*API) MemcpyDtoDAsync

func (a *API) MemcpyDtoDAsync(dst, src DevicePtr, byteSize uintptr, s Stream) error

MemcpyDtoDAsync asynchronously copies byteSize bytes from device src to device dst on stream s.

func (*API) MemcpyDtoH

func (a *API) MemcpyDtoH(dst unsafe.Pointer, src DevicePtr, byteSize uintptr) error

MemcpyDtoH synchronously copies byteSize bytes from device src to host dst.

func (*API) MemcpyDtoHAsync

func (a *API) MemcpyDtoHAsync(dst unsafe.Pointer, src DevicePtr, byteSize uintptr, s Stream) error

MemcpyDtoHAsync asynchronously copies byteSize bytes from device src to host dst on stream s.

func (*API) MemcpyHtoD

func (a *API) MemcpyHtoD(dst DevicePtr, src unsafe.Pointer, byteSize uintptr) error

MemcpyHtoD synchronously copies byteSize bytes from host src to device dst.

func (*API) MemcpyHtoDAsync

func (a *API) MemcpyHtoDAsync(dst DevicePtr, src unsafe.Pointer, byteSize uintptr, s Stream) error

MemcpyHtoDAsync asynchronously copies byteSize bytes from host src to device dst on stream s.

func (*API) MemsetD8Async

func (a *API) MemsetD8Async(dst DevicePtr, value byte, count uintptr, s Stream) error

MemsetD8Async asynchronously fills count bytes at device dst with value on stream s.

func (*API) ModuleGetFunction

func (a *API) ModuleGetFunction(mod Module, name string) (Function, error)

ModuleGetFunction retrieves a kernel function handle by symbol name from mod.

func (*API) ModuleLoadData

func (a *API) ModuleLoadData(image []byte) (Module, error)

ModuleLoadData loads a PTX string or cubin/fatbin binary into the current CUDA context. If loading fails, it retries with cuModuleLoadDataEx to capture detailed NVIDIA JIT error logs.

func (*API) ModuleUnload

func (a *API) ModuleUnload(mod Module) error

ModuleUnload unloads a previously loaded CUDA module.

func (*API) PrimaryCtxRelease

func (a *API) PrimaryCtxRelease(dev Device) error

PrimaryCtxRelease releases the primary context on dev.

func (*API) PrimaryCtxRetain

func (a *API) PrimaryCtxRetain(dev Device) (Context, error)

PrimaryCtxRetain retains the primary context on dev.

func (*API) QueryDeviceInfo

func (a *API) QueryDeviceInfo(ordinal int) (DeviceInfo, error)

QueryDeviceInfo returns hardware properties for the specified device ordinal.

func (*API) StreamBeginCapture added in v0.1.1

func (a *API) StreamBeginCapture(stream Stream, mode StreamCaptureMode) error

StreamBeginCapture begins capturing asynchronous GPU work submitted to stream into a CUDA Graph.

func (*API) StreamCreate

func (a *API) StreamCreate(flags StreamFlags) (Stream, error)

StreamCreate creates a CUDA stream in the current context.

func (*API) StreamDestroy

func (a *API) StreamDestroy(s Stream) error

StreamDestroy destroys a CUDA stream.

func (*API) StreamEndCapture added in v0.1.1

func (a *API) StreamEndCapture(stream Stream) (Graph, error)

StreamEndCapture terminates stream capture and returns the captured Graph.

func (*API) StreamIsCapturing added in v0.1.1

func (a *API) StreamIsCapturing(stream Stream) (StreamCaptureStatus, error)

StreamIsCapturing queries whether stream is currently capturing into a graph.

func (*API) StreamSynchronize

func (a *API) StreamSynchronize(s Stream) error

StreamSynchronize waits until all queued operations in stream have completed.

func (*API) StreamWaitEvent

func (a *API) StreamWaitEvent(s Stream, e Event) error

StreamWaitEvent makes all future work submitted to s wait for e to complete.

type CUDAError

type CUDAError struct {
	Op          string
	Code        Result
	Name        string
	Description string
}

CUDAError wraps a non-zero CUresult with human-readable name and description.

func (*CUDAError) Error

func (e *CUDAError) Error() string

type Context

type Context uintptr

Context corresponds to CUcontext (opaque pointer).

type Device

type Device int32

Device corresponds to CUdevice (an int32 handle).

type DeviceAttribute

type DeviceAttribute int32

DeviceAttribute corresponds to CUdevice_attribute.

const (
	AttrMaxThreadsPerBlock      DeviceAttribute = 1
	AttrMaxSharedMemoryPerBlock DeviceAttribute = 8
	AttrWarpSize                DeviceAttribute = 10
	AttrMultiprocessorCount     DeviceAttribute = 16
	AttrUnifiedAddressing       DeviceAttribute = 41
	AttrComputeCapabilityMajor  DeviceAttribute = 75
	AttrComputeCapabilityMinor  DeviceAttribute = 76
	AttrMemoryPoolsSupported    DeviceAttribute = 115
)

type DeviceContext

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

DeviceContext owns a retained CUDA primary context and its default compute/copy streams for a single GPU ordinal.

Because CUDA Driver API contexts are OS-thread-local while Go goroutines migrate freely across OS threads, all context-bound driver operations must execute inside WithContext.

func RetainDeviceContext

func RetainDeviceContext(api *API, ordinal int) (*DeviceContext, error)

RetainDeviceContext retains the primary CUDA context for ordinal and creates non-blocking compute and copy streams on it.

func (*DeviceContext) API

func (dc *DeviceContext) API() *API

API returns the underlying CUDA Driver API bindings.

func (*DeviceContext) ComputeStream

func (dc *DeviceContext) ComputeStream() Stream

ComputeStream returns the default non-blocking compute stream for this device.

func (*DeviceContext) Context

func (dc *DeviceContext) Context() Context

Context returns the retained primary CUcontext handle.

func (*DeviceContext) CopyStream

func (dc *DeviceContext) CopyStream() Stream

CopyStream returns the default non-blocking async copy stream for this device.

func (*DeviceContext) Info

func (dc *DeviceContext) Info() DeviceInfo

Info returns the queried hardware properties of this device.

func (*DeviceContext) Release

func (dc *DeviceContext) Release() error

Release destroys the device's streams and releases its retained primary context. Release is goroutine-safe and idempotent. Callers must ensure all in-flight work using this DeviceContext has finished before calling Release.

func (*DeviceContext) WithContext

func (dc *DeviceContext) WithContext(fn func() error) error

WithContext locks the calling goroutine to its OS thread, binds the primary CUDA context as current on that thread, invokes fn, and unlocks the OS thread on return.

Callers should group a sequence of driver calls (such as a full graph execution) inside a single WithContext call to amortize the OS-thread lock and cuCtxSetCurrent cost.

type DeviceInfo

type DeviceInfo struct {
	Ordinal              int
	Handle               Device
	Name                 string
	TotalMemBytes        uint64
	ComputeMajor         int
	ComputeMinor         int
	MultiprocessorCount  int
	MaxThreadsPerBlock   int
	MaxSharedMemPerBlock int
	WarpSize             int
	UnifiedAddressing    bool
	MemoryPoolsSupported bool
}

DeviceInfo holds queried hardware properties of a CUDA GPU.

func (DeviceInfo) ComputeCapability

func (d DeviceInfo) ComputeCapability() int

ComputeCapability returns the combined SM capability integer (e.g., 75 for sm_75, 120 for sm_120).

func (DeviceInfo) String

func (d DeviceInfo) String() string

String returns a concise human-readable description of the device.

func (DeviceInfo) SupportsDP4A

func (d DeviceInfo) SupportsDP4A() bool

SupportsDP4A reports whether the GPU supports native Int8 DP4A dot-product instructions (Pascal sm_61+).

func (DeviceInfo) SupportsNativeBF16

func (d DeviceInfo) SupportsNativeBF16() bool

SupportsNativeBF16 reports whether the GPU has native BFloat16 hardware instructions (Ampere sm_80+).

func (DeviceInfo) SupportsNativeFP16

func (d DeviceInfo) SupportsNativeFP16() bool

SupportsNativeFP16 reports whether the GPU has native Float16 compute units (Volta sm_70+ / Turing sm_75+).

type DevicePtr

type DevicePtr uint64

DevicePtr corresponds to 64-bit CUdeviceptr.

type Event

type Event uintptr

Event corresponds to CUevent (opaque pointer).

type EventFlags

type EventFlags uint32

EventFlags corresponds to CUevent_flags.

const (
	EventDefault       EventFlags = 0x0
	EventBlockingSync  EventFlags = 0x1
	EventDisableTiming EventFlags = 0x2
)

type Function

type Function uintptr

Function corresponds to CUfunction (opaque pointer).

type Graph added in v0.1.1

type Graph uintptr

Graph corresponds to CUgraph (opaque pointer).

type GraphExec added in v0.1.1

type GraphExec uintptr

GraphExec corresponds to CUgraphExec (opaque pointer).

type GraphExecUpdateResult added in v0.1.1

type GraphExecUpdateResult uint32

GraphExecUpdateResult corresponds to CUgraphExecUpdateResult.

const (
	GraphExecUpdateSuccess GraphExecUpdateResult = 0
)

type GraphExecUpdateResultInfo added in v0.1.1

type GraphExecUpdateResultInfo struct {
	Result GraphExecUpdateResult

	ErrorNode     uintptr
	ErrorFromNode uintptr
	// contains filtered or unexported fields
}

GraphExecUpdateResultInfo matches CUDA's CUgraphExecUpdateResultInfo (24 bytes on 64-bit).

type MemPool

type MemPool uintptr

MemPool corresponds to CUmemoryPool (opaque pointer).

type MemPoolAttr

type MemPoolAttr uint32

MemPoolAttr corresponds to CUmemPool_attribute.

const (
	// MemPoolAttrReleaseThreshold (CU_MEMPOOL_ATTR_RELEASE_THRESHOLD) controls the amount of
	// reserved memory in bytes that a pool holds onto before trying to release memory back to the OS.
	MemPoolAttrReleaseThreshold MemPoolAttr = 4
)

type Module

type Module uintptr

Module corresponds to CUmodule (opaque pointer).

type Result

type Result int32

Result corresponds to CUDA's CUresult status code.

const (
	Success                 Result = 0
	ErrorInvalidValue       Result = 1
	ErrorOutOfMemory        Result = 2
	ErrorNotInitialized     Result = 3
	ErrorDeinitialized      Result = 4
	ErrorNoDevice           Result = 100
	ErrorInvalidDevice      Result = 101
	ErrorInvalidImage       Result = 200
	ErrorInvalidContext     Result = 201
	ErrorInvalidPTX         Result = 218
	ErrorUnsupportedPTX     Result = 222
	ErrorNotFound           Result = 500
	ErrorNotReady           Result = 600
	ErrorIllegalAddress     Result = 700
	ErrorLaunchFailed       Result = 719
	ErrorInsufficientDriver Result = 35
)

type Stream

type Stream uintptr

Stream corresponds to CUstream (opaque pointer).

type StreamCaptureMode added in v0.1.1

type StreamCaptureMode uint32

StreamCaptureMode corresponds to CUstreamCaptureMode.

const (
	StreamCaptureModeGlobal      StreamCaptureMode = 0
	StreamCaptureModeThreadLocal StreamCaptureMode = 1
	StreamCaptureModeRelaxed     StreamCaptureMode = 2
)

type StreamCaptureStatus added in v0.1.1

type StreamCaptureStatus uint32

StreamCaptureStatus corresponds to CUstreamCaptureStatus.

const (
	StreamCaptureStatusNone        StreamCaptureStatus = 0
	StreamCaptureStatusActive      StreamCaptureStatus = 1
	StreamCaptureStatusInvalidated StreamCaptureStatus = 2
)

type StreamFlags

type StreamFlags uint32

StreamFlags corresponds to CUstream_flags.

const (
	StreamDefault     StreamFlags = 0x0
	StreamNonBlocking StreamFlags = 0x1
)

Jump to

Keyboard shortcuts

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