cuda

package
v1.0.45 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cuda provides NVIDIA GPU acceleration using CUDA. This is a stub implementation for systems without CUDA support. It includes runtime detection via nvidia-smi for informational purposes.

Package cuda provides NVIDIA GPU acceleration for vector operations using CUDA.

This package requires:

  • NVIDIA GPU with CUDA Compute Capability 3.5+
  • CUDA Toolkit 11.0+ installed
  • cuBLAS library available

The package provides GPU-accelerated:

  • Vector normalization
  • Cosine similarity computation
  • Top-K selection

Build Requirements:

On Linux:

On Windows:

  • Install CUDA Toolkit from NVIDIA
  • Visual Studio with C++ build tools
  • CUDA_PATH environment variable set

Build tags:

  • Build with: go build -tags cuda
  • Without CUDA: builds with stub implementations

Example usage:

if cuda.IsAvailable() {
    device, err := cuda.NewDevice(0)
    if err != nil {
        log.Fatal(err)
    }
    defer device.Release()

    // Create buffer with embeddings
    buf, _ := device.NewBuffer(embeddings, cuda.MemoryDevice)

    // Perform similarity search
    results, _ := device.Search(buf, query, n, dims, k)
}

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCUDANotAvailable = errors.New("cuda: CUDA is not available (build without cuda tag or unsupported platform)")
	ErrDeviceCreation   = errors.New("cuda: failed to create CUDA device")
	ErrBufferCreation   = errors.New("cuda: failed to create buffer")
	ErrKernelExecution  = errors.New("cuda: kernel execution failed")
	ErrInvalidBuffer    = errors.New("cuda: invalid buffer")
)

Errors

Functions

func DeviceCount

func DeviceCount() int

DeviceCount returns 1 if GPU detected, 0 otherwise. Note: In stub mode, device can't actually be used for CUDA operations.

func GPUMemoryMB

func GPUMemoryMB() int

GPUMemoryMB returns the detected GPU memory in MB, or 0 if none.

func GPUName

func GPUName() string

GPUName returns the detected GPU name, or empty string if none.

func HasGPUHardware

func HasGPUHardware() bool

HasGPUHardware returns true if NVIDIA GPU hardware is detected. This is separate from CUDA availability - GPU may be present but CUDA not usable.

func IsAvailable

func IsAvailable() bool

IsAvailable checks if CUDA/GPU is available. In stub mode, we detect GPU via nvidia-smi but can't use it for acceleration. This allows informative logging about GPU presence.

func IsCUDACapable

func IsCUDACapable() bool

IsCUDACapable returns false in stub mode - GPU is detected but CUDA ops unavailable.

Types

type Buffer

type Buffer struct{}

Buffer represents a CUDA memory buffer (stub).

func (*Buffer) ReadFloat32

func (b *Buffer) ReadFloat32(count int) []float32

ReadFloat32 returns nil.

func (*Buffer) Release

func (b *Buffer) Release()

Release is a no-op stub.

func (*Buffer) Size

func (b *Buffer) Size() uint64

Size returns 0.

type Device

type Device struct{}

Device represents a CUDA GPU device (stub).

func NewDevice

func NewDevice(deviceID int) (*Device, error)

NewDevice returns an error on systems without CUDA. Even with GPU detected, CUDA operations require the cuda build tag.

func (*Device) ComputeCapability

func (d *Device) ComputeCapability() (int, int)

ComputeCapability returns 0, 0.

func (*Device) CosineSimilarity

func (d *Device) CosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error

CosineSimilarity returns an error.

func (*Device) ID

func (d *Device) ID() int

ID returns 0.

func (*Device) MemoryBytes

func (d *Device) MemoryBytes() uint64

MemoryBytes returns 0.

func (*Device) MemoryMB

func (d *Device) MemoryMB() int

MemoryMB returns 0.

func (*Device) Name

func (d *Device) Name() string

Name returns empty string.

func (*Device) NewBuffer

func (d *Device) NewBuffer(data []float32, memType MemoryType) (*Buffer, error)

NewBuffer returns an error.

func (*Device) NewEmptyBuffer

func (d *Device) NewEmptyBuffer(count uint64, memType MemoryType) (*Buffer, error)

NewEmptyBuffer returns an error.

func (*Device) NormalizeVectors

func (d *Device) NormalizeVectors(vectors *Buffer, n, dimensions uint32) error

NormalizeVectors returns an error.

func (*Device) Release

func (d *Device) Release()

Release is a no-op stub.

func (*Device) Search

func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, normalized bool) ([]SearchResult, error)

Search returns an error.

func (*Device) TopK

func (d *Device) TopK(scores *Buffer, n, k uint32) ([]uint32, []float32, error)

TopK returns an error.

type MemoryType

type MemoryType int

MemoryType defines how buffer memory is managed.

const (
	MemoryDevice MemoryType = 0
	MemoryPinned MemoryType = 1
)

type SearchResult

type SearchResult struct {
	Index uint32
	Score float32
}

SearchResult holds a similarity search result.

Jump to

Keyboard shortcuts

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