opencl

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package opencl provides cross-platform GPU acceleration using OpenCL.

This package implements GPU-accelerated vector similarity search using OpenCL, which provides cross-platform support for AMD, Intel, and NVIDIA GPUs.

Requirements

For AMD GPUs on Linux:

For AMD GPUs on Windows:

  • AMD Adrenalin drivers with OpenCL support

For Intel GPUs:

  • Intel oneAPI or Intel OpenCL runtime

For NVIDIA GPUs (alternative to CUDA):

  • NVIDIA drivers with OpenCL support

Build Tags

This package is only compiled when the "opencl" build tag is present:

go build -tags opencl

Environment Variables

Linux (AMD ROCm):

export LD_LIBRARY_PATH=/opt/rocm/opencl/lib:$LD_LIBRARY_PATH

macOS (via Homebrew):

Note: macOS deprecated OpenCL in favor of Metal. Use Metal backend instead.

Windows:

OpenCL drivers are typically included with GPU drivers.

Architecture

The OpenCL backend uses:

  • OpenCL 1.2 or later for compute operations
  • Work groups optimized for GPU wavefront/warp sizes
  • Coalesced memory access patterns for maximum bandwidth
  • Local memory for reduction operations (top-k)

Performance Considerations

OpenCL performance varies by vendor and driver:

  • AMD GPUs: Excellent performance with ROCm drivers
  • Intel GPUs: Good for integrated graphics, limited VRAM
  • NVIDIA: Prefer CUDA for best performance on NVIDIA hardware

Vector operations are parallelized across compute units:

  • Cosine similarity: One work item per vector element
  • Top-K search: Parallel reduction with local memory
  • Normalization: Per-vector parallelization

Example

Basic usage:

device, err := opencl.NewDevice(0) // First OpenCL device
if err != nil {
    log.Fatal(err)
}
defer device.Release()

// Create buffer with embeddings
buffer, err := device.NewBuffer(embeddings)
if err != nil {
    log.Fatal(err)
}
defer buffer.Release()

// Search for similar vectors
results, err := device.Search(buffer, query, numVectors, dimensions, topK, normalized)
if err != nil {
    log.Fatal(err)
}

for _, r := range results {
    fmt.Printf("Index: %d, Score: %.4f\n", r.Index, r.Score)
}

Package opencl provides cross-platform GPU acceleration using OpenCL. This is a stub implementation for systems without OpenCL support.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpenCLNotAvailable = errors.New("opencl: OpenCL is not available (build without opencl tag)")
	ErrDeviceCreation     = errors.New("opencl: failed to create OpenCL device")
	ErrBufferCreation     = errors.New("opencl: failed to create buffer")
	ErrKernelExecution    = errors.New("opencl: kernel execution failed")
	ErrInvalidBuffer      = errors.New("opencl: invalid buffer")
)

Errors

Functions

func DeviceCount

func DeviceCount() int

DeviceCount returns 0 on systems without OpenCL.

func IsAvailable

func IsAvailable() bool

IsAvailable returns false on systems without OpenCL.

Types

type Buffer

type Buffer struct{}

Buffer represents an OpenCL 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 an OpenCL GPU device (stub).

func NewDevice

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

NewDevice returns an error on systems without OpenCL.

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) (*Buffer, error)

NewBuffer returns an error.

func (*Device) NewEmptyBuffer

func (d *Device) NewEmptyBuffer(count uint64) (*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.

func (*Device) Vendor

func (d *Device) Vendor() string

Vendor returns empty string.

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