- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandqueue.lua
More file actions
Latest commit
303 lines (280 loc) · 6.67 KB
/
Copy pathcommandqueue.lua
File metadata and controls
303 lines (280 loc) · 6.67 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
localassert=require'ext.assert'
localtable=require'ext.table'
localffi=require'ffi'
localcl=require'cl'
localclassert=require'cl.assert'
localclassertparam=require'cl.assertparam'
localGCWrapper=require'cl.gcwrapper'
localGetInfo=require'cl.getinfo'
localcl_mem_array=ffi.typeof'cl_mem[?]'
localcl_event_array=ffi.typeof'cl_event[?]'
localdefaultEvent
localCommandQueue=GetInfo(GCWrapper{
ctype=ffi.typeof'cl_command_queue',
retain=function(self) returncl.clRetainCommandQueue(self.id) end,
release=function(self) returncl.clReleaseCommandQueue(self.id) end,
}):subclass()
--[[
args
context
device
properties
--]]
functionCommandQueue:init(args)
assert(args, "expected args")
self.id=classertparam('clCreateCommandQueue',
assert.index(args, 'context').id,
assert.index(args, 'device').id,
args.propertiesor0)
end
--[[
args:
buffer
block
offset (optional) default 0
size
ptr
event (optional) (cl.event)
--]]
functionCommandQueue:enqueueReadBuffer(args)
defaultEvent=defaultEventorrequire'cl.event'()
classert(cl.clEnqueueReadBuffer(
self.id,
assert.index(args, 'buffer').id,
args.block,
args.offsetor0,
assert.index(args, 'size'),
assert.index(args, 'ptr'),
0,
nil,
args.eventandargs.event.ptrordefaultEvent.ptr))
end
--[[
args:
buffer
block
offset (optional) default 0
size
ptr
--]]
functionCommandQueue:enqueueWriteBuffer(args)
defaultEvent=defaultEventorrequire'cl.event'()
classert(cl.clEnqueueWriteBuffer(
self.id,
assert.index(args, 'buffer').id,
args.block,
args.offsetor0,
assert.index(args, 'size'),
assert.index(args, 'ptr'),
0,
nil,
args.eventandargs.event.ptrordefaultEvent.ptr))
end
--[[
args:
buffer
pattern (optional)
patternSize (optional)
offset (optional)
size (bytes)
event (optional) cl.event
--]]
localdefaultPattern=ffi.new('int32_t[128/4]', 0) -- TODO query largest vector possible on the hardware
functionCommandQueue:enqueueFillBuffer(args)
defaultEvent=defaultEventorrequire'cl.event'()
localsize=assert.index(args, 'size')
localpattern=args.pattern
localpatternSize=args.patternSize
-- https://www.khronos.org/registry/OpenCL/sdk/1.2/docs/man/xhtml/clEnqueueFillBuffer.html
-- "CL_INVALID_VALUE if offset and size are not a multiple of pattern_size"
-- so how come on my AMD OpenCL version 3423.0 when this happens the whole program aborts?
ifnotpatternthen
pattern=defaultPattern
ifbit.band(size, 0x7f) ==0then
patternSize=128
elseifbit.band(size, 0x3f) ==0then
patternSize=64
elseifbit.band(size, 0x1f) ==0then
patternSize=32
elseifbit.band(size, 0xf) ==0then
patternSize=16
elseifbit.band(size, 0x7) ==0then
patternSize=8
elseifbit.band(size, 0x3) ==0then
patternSize=4
elseifbit.band(size, 0x1) ==0then
patternSize=2
else
patternSize=1
end
end
classert(cl.clEnqueueFillBuffer(
self.id,
assert.index(args, 'buffer').id,
pattern,
patternSize,
args.offsetor0,
size,
0,
nil,
args.eventandargs.event.ptr
-- or nil
--[[
https://github.com/thenumbernine/HydrodynamicsGPU/blob/master/src/Solver/Solver.cpp
line 24:
if you don't pass that &event pointer on my AMD Radeon then it writes garbage
CL_DEVICE_NAME: AMD Radeon R9 M370X Compute Engine
CL_DEVICE_VENDOR: AMD
CL_DEVICE_VERSION: OpenCL 1.2
CL_DRIVER_VERSION: 1.2 (Jan 11 2016 18:56:15)
...and here we are in 2021...
--]]
ordefaultEvent.ptr
))
end
--[[
args:
src
dst
srcOffset (optional)
dstOffset (optional)
size
--]]
functionCommandQueue:enqueueCopyBuffer(args)
defaultEvent=defaultEventorrequire'cl.event'()
classert(cl.clEnqueueCopyBuffer(
self.id,
assert.index(args, 'src').id,
assert.index(args, 'dst').id,
args.srcOffsetor0,
args.dstOffsetor0,
assert.index(args, 'size'),
0,
nil,
args.eventandargs.event.ptrordefaultEvent.ptr))
end
--[[
args:
kernel
dim (optional) if not provided then offset, globalSize, or localSize must be a table?
offset (optional)
globalSize
localSize
--]]
localoffset=ffi.new('size_t[3]')
localglobalSize=ffi.new('size_t[3]')
locallocalSize=ffi.new('size_t[3]')
localfunctionfillParam(dim, src, dst)
iftype(src) =='number' then
ifnotdimthen
dim=1
else
assert.eq(dim, 1)
end
dst[0] =src
elseiftype(src) =='table' then
ifnotdimthen
dim=#src
else
assert.len(src, dim)
end
fori=1,dimdo
dst[i-1] =src[i]
end
elseiftype(src) =='cdata' then
assert(dim)
fori=0,dim-1do
dst[i] =src[i]
end
end
returndim
end
--[[
args:
dim
offset
globalSize
localSize
kernel (optional) cl.kernel
wait (optional) table of cl.event
event (optional) cl.event
--]]
functionCommandQueue:enqueueNDRangeKernel(args)
defaultEvent=defaultEventorrequire'cl.event'()
localdim=args.dim
dim=fillParam(dim, args.offset, offset)
dim=fillParam(dim, args.globalSize, globalSize)
dim=fillParam(dim, args.localSize, localSize)
localnumWait=args.waitand#args.waitor0
localwait
ifnumWait>0then
wait=cl_event_array(numWait)
fori=1,numWaitdo
wait[i-1] =args.wait[i].id
end
end
classert(cl.clEnqueueNDRangeKernel(
self.id,
assert.index(args, 'kernel').id,
dim,
offset,
globalSize,
localSize,
numWait,
wait,
args.eventandargs.event.ptrordefaultEvent.ptr))
-- hmm should I even use 'id' if ptr[0] will be holding the same information?
ifargs.eventthen
args.event.id=args.event.ptr[0]
end
end
--[[
args:
objs
--]]
functionCommandQueue:enqueueAcquireGLObjects(args)
defaultEvent=defaultEventorrequire'cl.event'()
localobjs=assert.index(args, 'objs')
localids=table.mapi(objs, function(obj) returnobj.idend)
classert(cl.clEnqueueAcquireGLObjects(
self.id,
#objs,
cl_mem_array(#ids, ids),
0,
nil,
args.eventandargs.event.ptrordefaultEvent.ptr))
end
--[[
args:
objs
--]]
functionCommandQueue:enqueueReleaseGLObjects(args)
defaultEvent=defaultEventorrequire'cl.event'()
localobjs=assert.index(args, 'objs')
localids=table.mapi(objs, function(obj) returnobj.idend)
classert(cl.clEnqueueReleaseGLObjects(
self.id,
#objs,
cl_mem_array(#ids, ids),
0,
nil,
args.eventandargs.event.ptrordefaultEvent.ptr))
end
functionCommandQueue:flush()
cl.clFlush(self.id)
end
functionCommandQueue:finish()
cl.clFinish(self.id)
end
CommandQueue.getInfo=CommandQueue:makeGetter{
getter=cl.clGetCommandQueueInfo,
vars= {
{name='CL_QUEUE_CONTEXT', type=''},
{name='CL_QUEUE_DEVICE', type=''},
{name='CL_QUEUE_REFERENCE_COUNT', type=''},
{name='CL_QUEUE_PROPERTIES', type=''},
{name='CL_QUEUE_SIZE', type=''},
{name='CL_QUEUE_DEVICE_DEFAULT', type=''},
},
}
returnCommandQueue