This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm.go
229 lines (218 loc) · 7.44 KB
/
svm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package cl30
// #include "api.h"
// extern cl_int cl30EnqueueSVMFree(cl_command_queue commandQueue,
// cl_uint svmPointerCount, void *svmPointers,
// uintptr_t *userData,
// cl_uint waitListCount, cl_event const *waitList,
// cl_event *event);
// extern cl_int cl30EnqueueSVMMigrateMem(
// cl_command_queue commandQueue,
// cl_uint svmPointerCount, void *svmPointers,
// size_t *sizes, cl_mem_migration_flags flags,
// cl_uint waitListCount, cl_event const *waitList,
// cl_event *event);
import "C"
import "unsafe"
// SvmMemFlags describe properties of a shared virtual memory (SVM) buffer.
type SvmMemFlags C.cl_mem_flags
// SvmAlloc allocates a shared virtual memory (SVM) buffer that can be shared by the host and all devices in an OpenCL
// context that support shared virtual memory.
//
// For flags, potential values are MemReadWriteFlag, MemWriteOnlyFlag, MemReadOnlyFlag, MemSvmAtomicsFlag,
// MemSvmFineGrainBufferFlag.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clSVMAlloc.html
func SvmAlloc(context Context, flags SvmMemFlags, size int, alignment uint32) (unsafe.Pointer, error) {
ptr := C.clSVMAlloc(
context.handle(),
C.cl_svm_mem_flags(flags),
C.size_t(size),
C.cl_uint(alignment))
if ptr == nil {
return nil, ErrOutOfMemory
}
return ptr, nil
}
// SvmFree frees a shared virtual memory buffer allocated using SvmAlloc().
//
// SvmFree does not wait for previously enqueued commands that may be using ptr to finish before freeing the memory.
// It is the responsibility of the application to make sure that enqueued commands that use ptr have finished before
// freeing the memory.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clSVMFree.html
func SvmFree(context Context, ptr unsafe.Pointer) {
C.clSVMFree(context.handle(), ptr)
}
// EnqueueSvmFree enqueues a command to free shared virtual memory allocated using SvmAlloc() or a shared system
// memory pointer.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSvmFree.html
func EnqueueSvmFree(commandQueue CommandQueue, ptrs []unsafe.Pointer, callback func(CommandQueue, []unsafe.Pointer), waitList []Event, event *Event) error {
var callbackUserData userData
if callback != nil {
var err error
callbackUserData, err = userDataFor(callback)
if err != nil {
return err
}
}
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
ptrAddresses := make([]uintptr, len(ptrs))
for i, ptr := range ptrs {
ptrAddresses[i] = uintptr(ptr)
}
status := C.cl30EnqueueSVMFree(
commandQueue.handle(),
C.cl_uint(len(ptrs)),
unsafe.Pointer(&ptrAddresses[0]),
callbackUserData.ptr,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
//export cl30GoSvmFreeCallback
func cl30GoSvmFreeCallback(commandQueue CommandQueue, svmPointerCount C.cl_uint, svmPointers unsafe.Pointer, userData *C.uintptr_t) {
callbackUserData := userDataFrom(userData)
callback := callbackUserData.Value().(func(CommandQueue, []unsafe.Pointer))
callbackUserData.Delete()
ptrs := unsafe.Slice((*unsafe.Pointer)(svmPointers), int(svmPointerCount))
callback(commandQueue, ptrs)
}
// EnqueueSvmMemcpy enqueues a command to do a memcpy operation.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSVMMemcpy.html
func EnqueueSvmMemcpy(commandQueue CommandQueue, blocking bool, dstPtr unsafe.Pointer, srcPtr unsafe.Pointer, size int,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueSVMMemcpy(
commandQueue.handle(),
C.cl_bool(BoolFrom(blocking)),
dstPtr,
srcPtr,
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueSvmMemFill enqueues a command to fill a region in memory with a pattern of a given pattern size.
//
// The pattern must be a scalar or vector integer or floating-point data type supported by OpenCL.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSVMMemFill.html
func EnqueueSvmMemFill(commandQueue CommandQueue, svmPtr, pattern unsafe.Pointer, patternSize, size int,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueSVMMemFill(
commandQueue.handle(),
svmPtr,
pattern,
C.size_t(patternSize),
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueSvmMap enqueues a command that will allow the host to update a region of an SVM buffer.
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSVMMap.html
func EnqueueSvmMap(commandQueue CommandQueue, blocking bool, flags MemFlags, svmPtr unsafe.Pointer, size int,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueSVMMap(
commandQueue.handle(),
C.cl_bool(BoolFrom(blocking)),
C.cl_map_flags(flags),
svmPtr,
C.size_t(size),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueSvmUnmap enqueues a command to indicate that the host has completed updating the region given by an SVM
// pointer and which was specified in a previous call to EnqueueSvmMap().
//
// Since: 2.0
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSVMUnmap.html
func EnqueueSvmUnmap(commandQueue CommandQueue, svmPtr unsafe.Pointer, waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
status := C.clEnqueueSVMUnmap(
commandQueue.handle(),
svmPtr,
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}
// EnqueueSvmMigrateMem enqueues a command to indicate which device a set of ranges of SVM allocations should be
// associated with.
//
// Since: 2.1
// See also: https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clEnqueueSVMMigrateMem.html
func EnqueueSvmMigrateMem(commandQueue CommandQueue, svmPtrs []unsafe.Pointer, sizes []int, flags MemMigrationFlags,
waitList []Event, event *Event) error {
var rawWaitList unsafe.Pointer
if len(waitList) > 0 {
rawWaitList = unsafe.Pointer(&waitList[0])
}
svmPtrAddresses := make([]uintptr, len(svmPtrs))
for i, svmPtr := range svmPtrs {
svmPtrAddresses[i] = uintptr(svmPtr)
}
var sizesPtr unsafe.Pointer
if len(sizes) > 0 {
sizesPtr = unsafe.Pointer(&sizes[0])
}
status := C.cl30EnqueueSVMMigrateMem(
commandQueue.handle(),
C.cl_uint(len(svmPtrs)),
unsafe.Pointer(&svmPtrAddresses[0]),
(*C.size_t)(sizesPtr),
C.cl_mem_migration_flags(flags),
C.cl_uint(len(waitList)),
(*C.cl_event)(rawWaitList),
(*C.cl_event)(unsafe.Pointer(event)))
if status != C.CL_SUCCESS {
return StatusError(status)
}
return nil
}