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:
- ROCm (Radeon Open Compute): https://rocm.docs.amd.com/
- Or AMD GPU drivers with OpenCL support
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 ¶
- Variables
- func DeviceCount() int
- func IsAvailable() bool
- type Buffer
- type Device
- func (d *Device) CosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
- func (d *Device) ID() int
- func (d *Device) MemoryBytes() uint64
- func (d *Device) MemoryMB() int
- func (d *Device) Name() string
- func (d *Device) NewBuffer(data []float32) (*Buffer, error)
- func (d *Device) NewEmptyBuffer(count uint64) (*Buffer, error)
- func (d *Device) NormalizeVectors(vectors *Buffer, n, dimensions uint32) error
- func (d *Device) Release()
- func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, ...) ([]SearchResult, error)
- func (d *Device) TopK(scores *Buffer, n, k uint32) ([]uint32, []float32, error)
- func (d *Device) Vendor() string
- type SearchResult
Constants ¶
This section is empty.
Variables ¶
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 ¶
Types ¶
type Buffer ¶
type Buffer struct{}
Buffer represents an OpenCL memory buffer (stub).
func (*Buffer) ReadFloat32 ¶
ReadFloat32 returns nil.
type Device ¶
type Device struct{}
Device represents an OpenCL GPU device (stub).
func (*Device) CosineSimilarity ¶
func (d *Device) CosineSimilarity(embeddings, query, scores *Buffer, n, dimensions uint32, normalized bool) error
CosineSimilarity returns an error.
func (*Device) NewEmptyBuffer ¶
NewEmptyBuffer returns an error.
func (*Device) NormalizeVectors ¶
NormalizeVectors returns an error.
func (*Device) Search ¶
func (d *Device) Search(embeddings *Buffer, query []float32, n, dimensions uint32, k int, normalized bool) ([]SearchResult, error)
Search returns an error.
type SearchResult ¶
SearchResult holds a similarity search result.