mirror of
https://github.com/fumiama/gozel.git
synced 2026-06-17 15:54:27 +08:00
feat(example): impl. vadd
This commit is contained in:
91
ze/command.go
Normal file
91
ze/command.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package ze
|
||||
|
||||
import (
|
||||
"math"
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/gozel"
|
||||
)
|
||||
|
||||
// CommandQueueHandle is a handle to a Level Zero command queue.
|
||||
type CommandQueueHandle gozel.ZeCommandQueueHandle
|
||||
|
||||
// CommandQueueCreate creates a command queue on the given device with default mode and normal priority.
|
||||
func (h ContextHandle) CommandQueueCreate(hDevice gozel.ZeDeviceHandle) (
|
||||
CommandQueueHandle, error,
|
||||
) {
|
||||
var q gozel.ZeCommandQueueHandle
|
||||
_, err := gozel.ZeCommandQueueCreate(gozel.ZeContextHandle(h), hDevice, &gozel.ZeCommandQueueDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
|
||||
Mode: gozel.ZE_COMMAND_QUEUE_MODE_DEFAULT,
|
||||
Priority: gozel.ZE_COMMAND_QUEUE_PRIORITY_NORMAL,
|
||||
}, &q)
|
||||
return CommandQueueHandle(q), err
|
||||
}
|
||||
|
||||
// ExecuteCommandLists submits the command list for execution on the command queue.
|
||||
func (h CommandQueueHandle) ExecuteCommandLists(hCommandList CommandListHandle) error {
|
||||
cl := gozel.ZeCommandListHandle(hCommandList)
|
||||
_, err := gozel.ZeCommandQueueExecuteCommandLists(gozel.ZeCommandQueueHandle(h), 1, &cl, 0)
|
||||
return err
|
||||
}
|
||||
|
||||
// Synchronize blocks the host until all commands in the command queue have completed.
|
||||
func (h CommandQueueHandle) Synchronize() error {
|
||||
_, err := gozel.ZeCommandQueueSynchronize(gozel.ZeCommandQueueHandle(h), math.MaxUint64)
|
||||
return err
|
||||
}
|
||||
|
||||
// Destroy destroys the command queue and releases its resources.
|
||||
func (h CommandQueueHandle) Destroy() error {
|
||||
_, err := gozel.ZeCommandQueueDestroy(gozel.ZeCommandQueueHandle(h))
|
||||
return err
|
||||
}
|
||||
|
||||
// CommandListHandle is a handle to a Level Zero command list.
|
||||
type CommandListHandle gozel.ZeCommandListHandle
|
||||
|
||||
// CommandListCreate creates a command list on the given device.
|
||||
func (h ContextHandle) CommandListCreate(hDevice gozel.ZeDeviceHandle) (
|
||||
CommandListHandle, error,
|
||||
) {
|
||||
var cl gozel.ZeCommandListHandle
|
||||
_, err := gozel.ZeCommandListCreate(gozel.ZeContextHandle(h), hDevice, &gozel.ZeCommandListDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC,
|
||||
}, &cl)
|
||||
return CommandListHandle(cl), err
|
||||
}
|
||||
|
||||
// AppendLaunchKernel appends a kernel launch command to the command list.
|
||||
func (h CommandListHandle) AppendLaunchKernel(
|
||||
hKernel KernelHandle, pLaunchFuncArgs *gozel.ZeGroupCount,
|
||||
) error {
|
||||
_, err := gozel.ZeCommandListAppendLaunchKernel(gozel.ZeCommandListHandle(h), gozel.ZeKernelHandle(hKernel), pLaunchFuncArgs, 0, 0, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// Close closes the command list, making it ready for execution.
|
||||
func (h CommandListHandle) Close() error {
|
||||
_, err := gozel.ZeCommandListClose(gozel.ZeCommandListHandle(h))
|
||||
return err
|
||||
}
|
||||
|
||||
// AppendMemoryCopy appends a memory copy command from srcptr to dstptr of the given size.
|
||||
func (h CommandListHandle) AppendMemoryCopy(
|
||||
dstptr unsafe.Pointer, srcptr unsafe.Pointer, size uintptr,
|
||||
) error {
|
||||
_, err := gozel.ZeCommandListAppendMemoryCopy(gozel.ZeCommandListHandle(h), dstptr, srcptr, size, 0, 0, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
// Destroy destroys the command list and releases its resources.
|
||||
func (h CommandListHandle) Destroy() error {
|
||||
_, err := gozel.ZeCommandListDestroy(gozel.ZeCommandListHandle(h))
|
||||
return err
|
||||
}
|
||||
|
||||
// AppendBarrier appends an execution barrier to the command list.
|
||||
func (h CommandListHandle) AppendBarrier() error {
|
||||
_, err := gozel.ZeCommandListAppendBarrier(gozel.ZeCommandListHandle(h), 0, 0, nil)
|
||||
return err
|
||||
}
|
||||
21
ze/context.go
Normal file
21
ze/context.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package ze
|
||||
|
||||
import "github.com/fumiama/gozel"
|
||||
|
||||
// ContextHandle is a handle to a Level Zero context.
|
||||
type ContextHandle gozel.ZeContextHandle
|
||||
|
||||
// ContextCreate creates a new context for the driver.
|
||||
func (h DriverHandle) ContextCreate() (ContextHandle, error) {
|
||||
var ctx gozel.ZeContextHandle
|
||||
_, err := gozel.ZeContextCreate(gozel.ZeDriverHandle(h), &gozel.ZeContextDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_CONTEXT_DESC,
|
||||
}, &ctx)
|
||||
return ContextHandle(ctx), err
|
||||
}
|
||||
|
||||
// Destroy destroys the context and releases its resources.
|
||||
func (h ContextHandle) Destroy() error {
|
||||
_, err := gozel.ZeContextDestroy(gozel.ZeContextHandle(h))
|
||||
return err
|
||||
}
|
||||
21
ze/device.go
Normal file
21
ze/device.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package ze
|
||||
|
||||
import "github.com/fumiama/gozel"
|
||||
|
||||
// DeviceGet retrieves all devices within the driver.
|
||||
func (h DriverHandle) DeviceGet() ([]gozel.ZeDeviceHandle, error) {
|
||||
var count uint32
|
||||
_, err := gozel.ZeDeviceGet(gozel.ZeDriverHandle(h), &count, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
devices := make([]gozel.ZeDeviceHandle, count)
|
||||
_, err = gozel.ZeDeviceGet(gozel.ZeDriverHandle(h), &count, &devices[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
15
ze/init.go
15
ze/init.go
@@ -1,10 +1,15 @@
|
||||
package ze
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/gozel"
|
||||
)
|
||||
|
||||
func initDrivers(flags gozel.ZeInitDriverTypeFlags) ([]gozel.ZeDriverHandle, error) {
|
||||
// DriverHandle is a handle to a Level Zero driver instance.
|
||||
type DriverHandle gozel.ZeDriverHandle
|
||||
|
||||
func initDrivers(flags gozel.ZeInitDriverTypeFlags) ([]DriverHandle, error) {
|
||||
var count uint32
|
||||
desc := &gozel.ZeInitDriverTypeDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC,
|
||||
@@ -17,8 +22,8 @@ func initDrivers(flags gozel.ZeInitDriverTypeFlags) ([]gozel.ZeDriverHandle, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
handles := make([]gozel.ZeDriverHandle, count)
|
||||
_, err = gozel.ZeInitDrivers(&count, &handles[0], desc)
|
||||
handles := make([]DriverHandle, count)
|
||||
_, err = gozel.ZeInitDrivers(&count, (*gozel.ZeDriverHandle)(unsafe.Pointer(&handles[0])), desc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -28,13 +33,13 @@ func initDrivers(flags gozel.ZeInitDriverTypeFlags) ([]gozel.ZeDriverHandle, err
|
||||
// InitGPUDrivers calls zeInitDrivers with ZE_INIT_DRIVER_TYPE_FLAG_GPU from ze_loader.dll.
|
||||
// On success pCount contains the number of drivers and phDrivers (if non-nil)
|
||||
// is filled with driver handles.
|
||||
func InitGPUDrivers() ([]gozel.ZeDriverHandle, error) {
|
||||
func InitGPUDrivers() ([]DriverHandle, error) {
|
||||
return initDrivers(gozel.ZE_INIT_DRIVER_TYPE_FLAG_GPU)
|
||||
}
|
||||
|
||||
// InitNPUDrivers calls zeInitDrivers with ZE_INIT_DRIVER_TYPE_FLAG_NPU from ze_loader.dll.
|
||||
// On success pCount contains the number of drivers and phDrivers (if non-nil)
|
||||
// is filled with driver handles.
|
||||
func InitNPUDrivers() ([]gozel.ZeDriverHandle, error) {
|
||||
func InitNPUDrivers() ([]DriverHandle, error) {
|
||||
return initDrivers(gozel.ZE_INIT_DRIVER_TYPE_FLAG_NPU)
|
||||
}
|
||||
|
||||
41
ze/kernel.go
Normal file
41
ze/kernel.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package ze
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/gozel"
|
||||
)
|
||||
|
||||
// KernelHandle is a handle to a Level Zero kernel.
|
||||
type KernelHandle gozel.ZeKernelHandle
|
||||
|
||||
// KernelCreate creates a kernel from the module by the given function name.
|
||||
func (h ModuleHandle) KernelCreate(kernelName string) (KernelHandle, error) {
|
||||
b := []byte(kernelName + "\x00")
|
||||
var k gozel.ZeKernelHandle
|
||||
_, err := gozel.ZeKernelCreate(gozel.ZeModuleHandle(h), &gozel.ZeKernelDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_KERNEL_DESC,
|
||||
Pkernelname: &b[0],
|
||||
}, &k)
|
||||
runtime.KeepAlive(b)
|
||||
return KernelHandle(k), err
|
||||
}
|
||||
|
||||
// SetArgumentValue sets the value of a kernel argument at the given index.
|
||||
func (h KernelHandle) SetArgumentValue(argIndex uint32, argSize uintptr, pArgValue unsafe.Pointer) error {
|
||||
_, err := gozel.ZeKernelSetArgumentValue(gozel.ZeKernelHandle(h), argIndex, argSize, pArgValue)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetGroupSize sets the thread group size for the kernel.
|
||||
func (h KernelHandle) SetGroupSize(groupSizeX uint32, groupSizeY uint32, groupSizeZ uint32) error {
|
||||
_, err := gozel.ZeKernelSetGroupSize(gozel.ZeKernelHandle(h), groupSizeX, groupSizeY, groupSizeZ)
|
||||
return err
|
||||
}
|
||||
|
||||
// Destroy destroys the kernel and releases its resources.
|
||||
func (h KernelHandle) Destroy() error {
|
||||
_, err := gozel.ZeKernelDestroy(gozel.ZeKernelHandle(h))
|
||||
return err
|
||||
}
|
||||
35
ze/mem.go
Normal file
35
ze/mem.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package ze
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/fumiama/gozel"
|
||||
)
|
||||
|
||||
// MemAllocDevice allocates device memory on the given device with the specified size and alignment.
|
||||
func (h ContextHandle) MemAllocDevice(hDevice gozel.ZeDeviceHandle, size uintptr, alignment uintptr) (
|
||||
unsafe.Pointer, error,
|
||||
) {
|
||||
var p unsafe.Pointer
|
||||
_, err := gozel.ZeMemAllocDevice(gozel.ZeContextHandle(h), &gozel.ZeDeviceMemAllocDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC,
|
||||
}, size, alignment, hDevice, &p)
|
||||
return p, err
|
||||
}
|
||||
|
||||
// MemAllocHost allocates host memory with the specified size and alignment.
|
||||
func (h ContextHandle) MemAllocHost(size uintptr, alignment uintptr) (
|
||||
unsafe.Pointer, error,
|
||||
) {
|
||||
var p unsafe.Pointer
|
||||
_, err := gozel.ZeMemAllocHost(gozel.ZeContextHandle(h), &gozel.ZeHostMemAllocDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC,
|
||||
}, size, alignment, &p)
|
||||
return p, err
|
||||
}
|
||||
|
||||
// MemFree frees memory previously allocated with MemAllocDevice or MemAllocHost.
|
||||
func (h ContextHandle) MemFree(ptr unsafe.Pointer) error {
|
||||
_, err := gozel.ZeMemFree(gozel.ZeContextHandle(h), ptr)
|
||||
return err
|
||||
}
|
||||
31
ze/module.go
Normal file
31
ze/module.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package ze
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/fumiama/gozel"
|
||||
)
|
||||
|
||||
// ModuleHandle is a handle to a Level Zero module.
|
||||
type ModuleHandle gozel.ZeModuleHandle
|
||||
|
||||
// ModuleCreate creates a module from SPIR-V binary data on the given device.
|
||||
func (h ContextHandle) ModuleCreate(hDevice gozel.ZeDeviceHandle, data []byte) (
|
||||
ModuleHandle, error,
|
||||
) {
|
||||
var m gozel.ZeModuleHandle
|
||||
_, err := gozel.ZeModuleCreate(gozel.ZeContextHandle(h), hDevice, &gozel.ZeModuleDesc{
|
||||
Stype: gozel.ZE_STRUCTURE_TYPE_MODULE_DESC,
|
||||
Format: gozel.ZE_MODULE_FORMAT_IL_SPIRV,
|
||||
Inputsize: uintptr(len(data)),
|
||||
Pinputmodule: &data[0],
|
||||
}, &m, nil)
|
||||
runtime.KeepAlive(data)
|
||||
return ModuleHandle(m), err
|
||||
}
|
||||
|
||||
// Destroy destroys the module and releases its resources.
|
||||
func (h ModuleHandle) Destroy() error {
|
||||
_, err := gozel.ZeModuleDestroy(gozel.ZeModuleHandle(h))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user