1
0
mirror of https://github.com/fumiama/gozel.git synced 2026-06-27 14:20:28 +08:00

feat(ze): add event support & vadd demo & refactor (#5)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
fumiama
2026-03-28 18:00:12 +08:00
committed by GitHub
parent 163549e271
commit b821801ecd
134 changed files with 481 additions and 49 deletions

View File

@@ -2,24 +2,23 @@
package ze
import (
"math"
"runtime"
"unsafe"
"github.com/fumiama/gozel"
"github.com/fumiama/gozel/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 DeviceHandle) (
func (h ContextHandle) CommandQueueCreate(hDevice DeviceHandle, mode gozel.ZeCommandQueueMode) (
CommandQueueHandle, error,
) {
var q gozel.ZeCommandQueueHandle
_, err := gozel.ZeCommandQueueCreate(gozel.ZeContextHandle(h), gozel.ZeDeviceHandle(hDevice), &gozel.ZeCommandQueueDesc{
Stype: gozel.ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
Mode: gozel.ZE_COMMAND_QUEUE_MODE_DEFAULT,
Mode: mode,
Priority: gozel.ZE_COMMAND_QUEUE_PRIORITY_NORMAL,
}, &q)
return CommandQueueHandle(q), err
@@ -36,8 +35,8 @@ func (h CommandQueueHandle) ExecuteCommandLists(hCommandList ...CommandListHandl
}
// 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)
func (h CommandQueueHandle) Synchronize(timeout uint64) error {
_, err := gozel.ZeCommandQueueSynchronize(gozel.ZeCommandQueueHandle(h), timeout)
return err
}
@@ -61,11 +60,30 @@ func (h ContextHandle) CommandListCreate(hDevice DeviceHandle) (
return CommandListHandle(cl), err
}
// CommandListCreateImmediate creates a command list on the given device, also creates an implicit command queue.
func (h ContextHandle) CommandListCreateImmediate(hDevice DeviceHandle, mode gozel.ZeCommandQueueMode) (
CommandListHandle, error,
) {
var cl gozel.ZeCommandListHandle
_, err := gozel.ZeCommandListCreateImmediate(gozel.ZeContextHandle(h), gozel.ZeDeviceHandle(hDevice), &gozel.ZeCommandQueueDesc{
Stype: gozel.ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC,
Mode: mode,
Priority: gozel.ZE_COMMAND_QUEUE_PRIORITY_NORMAL,
}, &cl)
return CommandListHandle(cl), err
}
// AppendLaunchKernel appends a kernel launch command to the command list.
func (h CommandListHandle) AppendLaunchKernel(
hKernel KernelHandle, pLaunchFuncArgs *gozel.ZeGroupCount,
hSignalEvent EventHandle, waitEvents ...EventHandle,
) error {
_, err := gozel.ZeCommandListAppendLaunchKernel(gozel.ZeCommandListHandle(h), gozel.ZeKernelHandle(hKernel), pLaunchFuncArgs, 0, 0, nil)
_, err := gozel.ZeCommandListAppendLaunchKernel(
gozel.ZeCommandListHandle(h), gozel.ZeKernelHandle(hKernel),
pLaunchFuncArgs, gozel.ZeEventHandle(hSignalEvent), uint32(len(waitEvents)),
(*gozel.ZeEventHandle)(unsafe.SliceData(waitEvents)),
)
runtime.KeepAlive(waitEvents)
return err
}
@@ -73,9 +91,15 @@ func (h CommandListHandle) AppendLaunchKernel(
func (h CommandListHandle) AppendLaunchKernelWithArguments(
hKernel KernelHandle, groupCounts *gozel.ZeGroupCount,
groupSizes *gozel.ZeGroupSize, pArguments *unsafe.Pointer,
hSignalEvent EventHandle, waitEvents ...EventHandle,
) error {
_, err := gozel.ZeCommandListAppendLaunchKernelWithArguments(
gozel.ZeCommandListHandle(h), gozel.ZeKernelHandle(hKernel), groupCounts, groupSizes, pArguments, nil, 0, 0, nil)
gozel.ZeCommandListHandle(h), gozel.ZeKernelHandle(hKernel),
groupCounts, groupSizes, pArguments,
nil, gozel.ZeEventHandle(hSignalEvent), uint32(len(waitEvents)),
(*gozel.ZeEventHandle)(unsafe.SliceData(waitEvents)),
)
runtime.KeepAlive(waitEvents)
return err
}
@@ -88,8 +112,14 @@ func (h CommandListHandle) Close() error {
// 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,
hSignalEvent EventHandle, waitEvents ...EventHandle,
) error {
_, err := gozel.ZeCommandListAppendMemoryCopy(gozel.ZeCommandListHandle(h), dstptr, srcptr, size, 0, 0, nil)
_, err := gozel.ZeCommandListAppendMemoryCopy(
gozel.ZeCommandListHandle(h), dstptr, srcptr, size,
gozel.ZeEventHandle(hSignalEvent), uint32(len(waitEvents)),
(*gozel.ZeEventHandle)(unsafe.SliceData(waitEvents)),
)
runtime.KeepAlive(waitEvents)
return err
}
@@ -100,7 +130,21 @@ func (h CommandListHandle) Destroy() error {
}
// AppendBarrier appends an execution barrier to the command list.
func (h CommandListHandle) AppendBarrier() error {
_, err := gozel.ZeCommandListAppendBarrier(gozel.ZeCommandListHandle(h), 0, 0, nil)
func (h CommandListHandle) AppendBarrier(
hSignalEvent EventHandle, waitEvents ...EventHandle,
) error {
_, err := gozel.ZeCommandListAppendBarrier(
gozel.ZeCommandListHandle(h),
gozel.ZeEventHandle(hSignalEvent), uint32(len(waitEvents)),
(*gozel.ZeEventHandle)(unsafe.SliceData(waitEvents)),
)
runtime.KeepAlive(waitEvents)
return err
}
// HostSynchronize Synchronizes an immediate command list by waiting on the host for the
// completion of all commands previously submitted to it.
func (h CommandListHandle) HostSynchronize(timeout uint64) error {
_, err := gozel.ZeCommandListHostSynchronize(gozel.ZeCommandListHandle(h), timeout)
return err
}

View File

@@ -1,6 +1,6 @@
package ze
import "github.com/fumiama/gozel"
import "github.com/fumiama/gozel/gozel"
// ContextHandle is a handle to a Level Zero context.
type ContextHandle gozel.ZeContextHandle

View File

@@ -1,6 +1,6 @@
package ze
import "github.com/fumiama/gozel"
import "github.com/fumiama/gozel/gozel"
// DeviceHandle is a handle to a Level Zero driver's device object.
type DeviceHandle gozel.ZeDeviceHandle

60
ze/event.go Normal file
View File

@@ -0,0 +1,60 @@
package ze
import (
"runtime"
"unsafe"
"github.com/fumiama/gozel/gozel"
)
// EventPoolHandle (ze_event_pool_handle_t) Handle of driver's event pool object
type EventPoolHandle gozel.ZeEventPoolHandle
// EventPoolCreate Creates a pool of events on the context.
func (h ContextHandle) EventPoolCreate(
evcount uint32, devices ...DeviceHandle,
) (eph EventPoolHandle, err error) {
_, err = gozel.ZeEventPoolCreate(gozel.ZeContextHandle(h), &gozel.ZeEventPoolDesc{
Stype: gozel.ZE_STRUCTURE_TYPE_EVENT_POOL_DESC,
Flags: gozel.ZE_EVENT_POOL_FLAG_HOST_VISIBLE,
Count: evcount,
}, uint32(len(devices)), (*gozel.ZeDeviceHandle)(unsafe.SliceData(devices)),
(*gozel.ZeEventPoolHandle)(&eph),
)
runtime.KeepAlive(devices)
return
}
// Destroy Deletes an event pool object.
func (h EventPoolHandle) Destroy() error {
_, err := gozel.ZeEventPoolDestroy(gozel.ZeEventPoolHandle(h))
return err
}
// EventHandle (ze_event_handle_t) Handle of driver's event object
type EventHandle gozel.ZeEventHandle
// EventCreate Creates an event from the pool.
func (h EventPoolHandle) EventCreate(
index uint32, signal, wait gozel.ZeEventScopeFlags,
) (eh EventHandle, err error) {
_, err = gozel.ZeEventCreate(gozel.ZeEventPoolHandle(h), &gozel.ZeEventDesc{
Stype: gozel.ZE_STRUCTURE_TYPE_EVENT_DESC,
Index: index,
Signal: signal,
Wait: wait,
}, (*gozel.ZeEventHandle)(&eh))
return
}
// HostSynchronize The current host thread waits on an event to be signaled.
func (h EventHandle) HostSynchronize(timeout uint64) error {
_, err := gozel.ZeEventHostSynchronize(gozel.ZeEventHandle(h), timeout)
return err
}
// Destroy Deletes an event object.
func (h EventHandle) Destroy() error {
_, err := gozel.ZeEventDestroy(gozel.ZeEventHandle(h))
return err
}

View File

@@ -3,7 +3,7 @@ package ze
import (
"unsafe"
"github.com/fumiama/gozel"
"github.com/fumiama/gozel/gozel"
)
// DriverHandle is a handle to a Level Zero driver instance.

View File

@@ -1,10 +1,11 @@
package ze
import (
"reflect"
"runtime"
"unsafe"
"github.com/fumiama/gozel"
"github.com/fumiama/gozel/gozel"
)
// KernelHandle is a handle to a Level Zero kernel.
@@ -23,8 +24,15 @@ func (h ModuleHandle) KernelCreate(kernelName string) (KernelHandle, error) {
}
// 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)
func (h KernelHandle) SetArgumentValue(argIndex uint32, arg any) error {
_, err := gozel.ZeKernelSetArgumentValue(
gozel.ZeKernelHandle(h), argIndex, reflect.TypeOf(arg).Size(),
*(*unsafe.Pointer)(
unsafe.Add(unsafe.Pointer(&arg),
unsafe.Sizeof(uintptr(0))),
),
)
runtime.KeepAlive(arg)
return err
}

View File

@@ -3,7 +3,7 @@ package ze
import (
"unsafe"
"github.com/fumiama/gozel"
"github.com/fumiama/gozel/gozel"
)
// MemAllocDevice allocates device memory on the given device with the specified size and alignment.

View File

@@ -5,7 +5,7 @@ import (
"runtime"
"strings"
"github.com/fumiama/gozel"
"github.com/fumiama/gozel/gozel"
)
// ModuleHandle is a handle to a Level Zero module.