hip

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package hip provides low-level bindings for the AMD HIP runtime API using purego dlopen. No build tags required; use hip.Available() to check runtime availability.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Available

func Available() bool

Available returns true if HIP runtime is loadable on this machine. The result is cached after the first call.

func Free

func Free(devPtr unsafe.Pointer) error

Free releases device memory previously allocated with Malloc.

func GetDeviceCount

func GetDeviceCount() (int, error)

GetDeviceCount returns the number of HIP-capable devices.

func Malloc

func Malloc(size int) (unsafe.Pointer, error)

Malloc allocates size bytes on the HIP device and returns a device pointer.

func Memcpy

func Memcpy(dst, src unsafe.Pointer, count int, kind MemcpyKind) error

Memcpy copies count bytes between host and device memory.

func MemcpyAsync

func MemcpyAsync(dst, src unsafe.Pointer, count int, kind MemcpyKind, stream *Stream) error

MemcpyAsync copies count bytes asynchronously on the given stream.

func MemcpyPeer

func MemcpyPeer(dst unsafe.Pointer, dstDevice int, src unsafe.Pointer, srcDevice int, count int) error

MemcpyPeer copies count bytes between devices using peer-to-peer transfer.

func SetDevice

func SetDevice(deviceID int) error

SetDevice sets the current HIP device.

Types

type HIPLib

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

HIPLib holds dlopen handles and resolved function pointers for HIP runtime functions. All function pointers are resolved at Open() time via dlsym. Calls go through cuda.Ccall which uses the platform-specific zero-CGo mechanism.

func Lib

func Lib() *HIPLib

Lib returns the global HIPLib instance, or nil if HIP is not available.

func Open

func Open() (*HIPLib, error)

Open loads libamdhip64 via dlopen and resolves all HIP runtime function pointers via dlsym. Returns an error if HIP is not available (library not found or symbols missing).

type MemPool

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

MemPool is a per-device, size-bucketed free-list allocator for HIP device memory. It caches freed allocations by (deviceID, byteSize) for reuse, avoiding the overhead of hipMalloc/hipFree on every operation and preventing cross-device pointer reuse in multi-GPU setups.

func NewMemPool

func NewMemPool() *MemPool

NewMemPool creates a new empty memory pool.

func (*MemPool) Alloc

func (p *MemPool) Alloc(deviceID, byteSize int) (unsafe.Pointer, error)

Alloc returns a device pointer of the given byte size on the specified device. If a cached allocation of the exact (deviceID, byteSize) exists, it is reused. Otherwise SetDevice is called and a fresh hipMalloc is performed.

func (*MemPool) Drain

func (p *MemPool) Drain() error

Drain releases all cached device memory back to HIP. Iterates all devices, calling SetDevice before freeing each device's pointers. Returns the first error encountered, but attempts to free all pointers.

func (*MemPool) Free

func (p *MemPool) Free(deviceID int, ptr unsafe.Pointer, byteSize int)

Free returns a device pointer to the pool for later reuse. The caller must provide the same deviceID and byteSize that were used to allocate.

func (*MemPool) Stats

func (p *MemPool) Stats() (allocations int, totalBytes int)

Stats returns the number of cached allocations and total cached bytes across all devices.

type MemcpyKind

type MemcpyKind int

MemcpyKind specifies the direction of a memory copy.

const (
	// MemcpyHostToDevice copies from host to device.
	MemcpyHostToDevice MemcpyKind = 1
	// MemcpyDeviceToHost copies from device to host.
	MemcpyDeviceToHost MemcpyKind = 2
	// MemcpyDeviceToDevice copies from device to device.
	MemcpyDeviceToDevice MemcpyKind = 3
)

type Stream

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

Stream wraps a hipStream_t handle for asynchronous kernel execution.

func CreateStream

func CreateStream() (*Stream, error)

CreateStream creates a new HIP stream.

func StreamFromPtr

func StreamFromPtr(ptr unsafe.Pointer) *Stream

StreamFromPtr wraps an existing hipStream_t handle as a Stream.

func (*Stream) Destroy

func (s *Stream) Destroy() error

Destroy releases the HIP stream.

func (*Stream) Ptr

func (s *Stream) Ptr() unsafe.Pointer

Ptr returns the underlying hipStream_t as an unsafe.Pointer.

func (*Stream) Synchronize

func (s *Stream) Synchronize() error

Synchronize blocks until all work on this stream completes.

Directories

Path Synopsis
Package kernels provides Go wrappers for custom HIP kernels via purego dlopen.
Package kernels provides Go wrappers for custom HIP kernels via purego dlopen.

Jump to

Keyboard shortcuts

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