]> gitweb.factorcode.org Git - factor.git/blob - extra/opencl/opencl.factor
core: Add the shuffler words but without primitives.
[factor.git] / extra / opencl / opencl.factor
1 ! Copyright (C) 2010 Erik Charlebois.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data arrays
4 byte-arrays combinators combinators.smart destructors
5 io.encodings.ascii io.encodings.string kernel libc locals make
6 math namespaces opencl.ffi sequences specialized-arrays
7 variants ;
8 IN: opencl
9 SPECIALIZED-ARRAYS: void* char size_t ;
10
11 <PRIVATE
12
13 ERROR: cl-error err ;
14
15 : cl-success ( err -- )
16     dup CL_SUCCESS = [ drop ] [ cl-error ] if ; inline
17
18 : cl-not-null ( err -- )
19     dup f = [ cl-error ] [ drop ] if ; inline
20
21 : info-data-size ( handle name info-quot -- size_t )
22     [ 0 f 0 size_t <ref> ] dip
23     [ call cl-success ] keepd size_t deref ; inline
24
25 : info-data-bytes ( handle name info-quot size -- bytes )
26     swap [ dup <byte-array> f ] dip [ call cl-success ] keepdd ; inline
27
28 : info ( handle name info-quot lift-quot -- value )
29     [ 3dup info-data-size info-data-bytes ] dip call ; inline
30
31 : 2info-data-size ( handle1 handle2 name info-quot -- size_t )
32     [ 0 f 0 size_t <ref> ] dip
33     [ call cl-success ] keepd size_t deref ; inline
34
35 : 2info-data-bytes ( handle1 handle2 name info-quot size -- bytes )
36     swap [ dup <byte-array> f ] dip [ call cl-success ] keepdd ; inline
37
38 : 2info ( handle1 handle2 name info_quot lift_quot -- value )
39     [ 4dup 2info-data-size 2info-data-bytes ] dip call ; inline
40
41 : info-bool ( handle name quot -- ? )
42     [ uint deref CL_TRUE = ] info ; inline
43
44 : info-ulong ( handle name quot -- ulong )
45     [ ulonglong deref ] info ; inline
46
47 : info-int ( handle name quot -- int )
48     [ int deref ] info ; inline
49
50 : info-uint ( handle name quot -- uint )
51     [ uint deref ] info ; inline
52
53 : info-size_t ( handle name quot -- size_t )
54     [ size_t deref ] info ; inline
55
56 : 2info-size_t ( handle1 handle2 name quot -- size_t )
57     [ size_t deref ] 2info ; inline
58
59 : info-string ( handle name quot -- string )
60     [ ascii decode but-last ] info ; inline
61
62 : 2info-string ( handle name quot -- string )
63     [ ascii decode but-last ] 2info ; inline
64
65 : info-size_t-array ( handle name quot -- size_t-array )
66     [ [ length size_t heap-size / ] keep swap size_t <c-direct-array> ] info ; inline
67
68 TUPLE: cl-handle < disposable handle ;
69
70 PRIVATE>
71
72 VARIANT: cl-device-type
73     cl-device-default cl-device-cpu cl-device-gpu cl-device-accelerator ;
74
75 : size_t>cl-device-type ( size_t -- cl-device-type )
76     {
77         { CL_DEVICE_TYPE_DEFAULT     [ cl-device-default     ] }
78         { CL_DEVICE_TYPE_CPU         [ cl-device-cpu         ] }
79         { CL_DEVICE_TYPE_GPU         [ cl-device-gpu         ] }
80         { CL_DEVICE_TYPE_ACCELERATOR [ cl-device-accelerator ] }
81     } case ; inline
82
83 VARIANT: cl-fp-feature
84     cl-denorm cl-inf-and-nan cl-round-to-nearest cl-round-to-zero cl-round-to-inf cl-fma ;
85
86 VARIANT: cl-cache-type
87     cl-no-cache cl-read-only-cache cl-read-write-cache ;
88
89 VARIANT: cl-buffer-access-mode
90     cl-read-access cl-write-access cl-read-write-access ;
91
92 VARIANT: cl-image-channel-order
93     cl-channel-order-r cl-channel-order-a cl-channel-order-rg cl-channel-order-ra
94     cl-channel-order-rga cl-channel-order-rgba cl-channel-order-bgra cl-channel-order-argb
95     cl-channel-order-intensity cl-channel-order-luminance ;
96
97 VARIANT: cl-image-channel-type
98     cl-channel-type-snorm-int8 cl-channel-type-snorm-int16 cl-channel-type-unorm-int8
99     cl-channel-type-unorm-int16 cl-channel-type-unorm-short-565
100     cl-channel-type-unorm-short-555 cl-channel-type-unorm-int-101010
101     cl-channel-type-signed-int8 cl-channel-type-signed-int16 cl-channel-type-signed-int32
102     cl-channel-type-unsigned-int8 cl-channel-type-unsigned-int16
103     cl-channel-type-unsigned-int32 cl-channel-type-half-float cl-channel-type-float ;
104
105 VARIANT: cl-addressing-mode
106     cl-repeat-addressing cl-clamp-to-edge-addressing cl-clamp-addressing cl-no-addressing ;
107
108 VARIANT: cl-filter-mode
109     cl-filter-nearest cl-filter-linear ;
110
111 VARIANT: cl-command-type
112     cl-ndrange-kernel-command cl-task-command cl-native-kernel-command cl-read-buffer-command
113     cl-write-buffer-command cl-copy-buffer-command cl-read-image-command cl-write-image-command
114     cl-copy-image-command cl-copy-buffer-to-image-command cl-copy-image-to-buffer-command
115     cl-map-buffer-command cl-map-image-command cl-unmap-mem-object-command
116     cl-marker-command cl-acquire-gl-objects-command cl-release-gl-objects-command ;
117
118 VARIANT: cl-execution-status
119     cl-queued cl-submitted cl-running cl-complete cl-failure ;
120
121 TUPLE: cl-platform
122     id profile version name vendor extensions devices ;
123
124 TUPLE: cl-device
125     id type vendor-id max-compute-units max-work-item-dimensions
126     max-work-item-sizes max-work-group-size preferred-vector-width-char
127     preferred-vector-width-short preferred-vector-width-int
128     preferred-vector-width-long preferred-vector-width-float
129     preferred-vector-width-double max-clock-frequency address-bits
130     max-mem-alloc-size image-support max-read-image-args max-write-image-args
131     image2d-max-width image2d-max-height image3d-max-width image3d-max-height
132     image3d-max-depth max-samplers max-parameter-size mem-base-addr-align
133     min-data-type-align-size single-fp-config global-mem-cache-type
134     global-mem-cacheline-size global-mem-cache-size global-mem-size
135     max-constant-buffer-size max-constant-args local-mem? local-mem-size
136     error-correction-support profiling-timer-resolution endian-little
137     available compiler-available execute-kernels? execute-native-kernels?
138     out-of-order-exec-available? profiling-available?
139     name vendor driver-version profile version extensions ;
140
141 TUPLE: cl-context < cl-handle ;
142 TUPLE: cl-queue   < cl-handle ;
143 TUPLE: cl-buffer  < cl-handle ;
144 TUPLE: cl-sampler < cl-handle ;
145 TUPLE: cl-program < cl-handle ;
146 TUPLE: cl-kernel  < cl-handle ;
147 TUPLE: cl-event   < cl-handle ;
148
149 M: cl-context dispose* handle>> clReleaseContext      cl-success ;
150 M: cl-queue   dispose* handle>> clReleaseCommandQueue cl-success ;
151 M: cl-buffer  dispose* handle>> clReleaseMemObject    cl-success ;
152 M: cl-sampler dispose* handle>> clReleaseSampler      cl-success ;
153 M: cl-program dispose* handle>> clReleaseProgram      cl-success ;
154 M: cl-kernel  dispose* handle>> clReleaseKernel       cl-success ;
155 M: cl-event   dispose* handle>> clReleaseEvent        cl-success ;
156
157 TUPLE: cl-buffer-ptr
158     { buffer cl-buffer read-only }
159     { offset integer   read-only } ;
160 C: <cl-buffer-ptr> cl-buffer-ptr
161
162 TUPLE: cl-buffer-range
163     { buffer cl-buffer read-only }
164     { offset integer   read-only }
165     { size   integer   read-only } ;
166 C: <cl-buffer-range> cl-buffer-range
167
168 SYMBOLS: cl-current-context cl-current-queue cl-current-device ;
169
170 <PRIVATE
171
172 : (current-cl-context) ( -- cl-context )
173     cl-current-context get ; inline
174
175 : (current-cl-queue) ( -- cl-queue )
176     cl-current-queue get ; inline
177
178 : (current-cl-device) ( -- cl-device )
179     cl-current-device get ; inline
180
181 GENERIC: buffer-access-constant ( buffer-access-mode -- n )
182 M: cl-read-write-access buffer-access-constant drop CL_MEM_READ_WRITE ;
183 M: cl-read-access       buffer-access-constant drop CL_MEM_READ_ONLY ;
184 M: cl-write-access      buffer-access-constant drop CL_MEM_WRITE_ONLY ;
185
186 GENERIC: buffer-map-flags ( buffer-access-mode -- n )
187 M: cl-read-write-access buffer-map-flags drop CL_MAP_READ CL_MAP_WRITE bitor ;
188 M: cl-read-access       buffer-map-flags drop CL_MAP_READ ;
189 M: cl-write-access      buffer-map-flags drop CL_MAP_WRITE ;
190
191 GENERIC: addressing-mode-constant ( addressing-mode -- n )
192 M: cl-repeat-addressing        addressing-mode-constant drop CL_ADDRESS_REPEAT ;
193 M: cl-clamp-to-edge-addressing addressing-mode-constant drop CL_ADDRESS_CLAMP_TO_EDGE ;
194 M: cl-clamp-addressing         addressing-mode-constant drop CL_ADDRESS_CLAMP ;
195 M: cl-no-addressing            addressing-mode-constant drop CL_ADDRESS_NONE ;
196
197 GENERIC: filter-mode-constant ( filter-mode -- n )
198 M: cl-filter-nearest filter-mode-constant drop CL_FILTER_NEAREST ;
199 M: cl-filter-linear  filter-mode-constant drop CL_FILTER_LINEAR ;
200
201 : cl_addressing_mode>addressing-mode ( cl_addressing_mode -- addressing-mode )
202     {
203         { CL_ADDRESS_REPEAT        [ cl-repeat-addressing        ] }
204         { CL_ADDRESS_CLAMP_TO_EDGE [ cl-clamp-to-edge-addressing ] }
205         { CL_ADDRESS_CLAMP         [ cl-clamp-addressing         ] }
206         { CL_ADDRESS_NONE          [ cl-no-addressing            ] }
207     } case ; inline
208
209 : cl_filter_mode>filter-mode ( cl_filter_mode -- filter-mode )
210     {
211         { CL_FILTER_LINEAR  [ cl-filter-linear  ] }
212         { CL_FILTER_NEAREST [ cl-filter-nearest ] }
213     } case ; inline
214
215 : platform-info-string ( handle name -- string )
216     [ clGetPlatformInfo ] info-string ;
217
218 : platform-info ( id -- profile version name vendor extensions )
219     {
220         [ CL_PLATFORM_PROFILE    platform-info-string ]
221         [ CL_PLATFORM_VERSION    platform-info-string ]
222         [ CL_PLATFORM_NAME       platform-info-string ]
223         [ CL_PLATFORM_VENDOR     platform-info-string ]
224         [ CL_PLATFORM_EXTENSIONS platform-info-string ]
225     } cleave ;
226
227 : cl_device_fp_config>flags ( ulong -- sequence )
228     [ {
229         [ CL_FP_DENORM           bitand 0 = [ f ] [ cl-denorm           ] if ]
230         [ CL_FP_INF_NAN          bitand 0 = [ f ] [ cl-inf-and-nan      ] if ]
231         [ CL_FP_ROUND_TO_NEAREST bitand 0 = [ f ] [ cl-round-to-nearest ] if ]
232         [ CL_FP_ROUND_TO_ZERO    bitand 0 = [ f ] [ cl-round-to-zero    ] if ]
233         [ CL_FP_ROUND_TO_INF     bitand 0 = [ f ] [ cl-round-to-inf     ] if ]
234         [ CL_FP_FMA              bitand 0 = [ f ] [ cl-fma              ] if ]
235     } cleave ] { } output>sequence sift ;
236
237 : cl_device_mem_cache_type>cache-type ( uint -- cache-type )
238     {
239         { CL_NONE             [ cl-no-cache         ] }
240         { CL_READ_ONLY_CACHE  [ cl-read-only-cache  ] }
241         { CL_READ_WRITE_CACHE [ cl-read-write-cache ] }
242     } case ; inline
243
244 : device-info-bool ( handle name -- ? )
245     [ clGetDeviceInfo ] info-bool ;
246
247 : device-info-ulong ( handle name -- ulong )
248     [ clGetDeviceInfo ] info-ulong ;
249
250 : device-info-uint ( handle name -- uint )
251     [ clGetDeviceInfo ] info-uint ;
252
253 : device-info-string ( handle name -- string )
254     [ clGetDeviceInfo ] info-string ;
255
256 : device-info-size_t ( handle name -- size_t )
257     [ clGetDeviceInfo ] info-size_t ;
258
259 : device-info-size_t-array ( handle name -- size_t-array )
260     [ clGetDeviceInfo ] info-size_t-array ;
261
262 : device-info ( device-id -- device )
263     dup {
264         [ CL_DEVICE_TYPE                          device-info-size_t size_t>cl-device-type ]
265         [ CL_DEVICE_VENDOR_ID                     device-info-uint         ]
266         [ CL_DEVICE_MAX_COMPUTE_UNITS             device-info-uint         ]
267         [ CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS      device-info-uint         ]
268         [ CL_DEVICE_MAX_WORK_ITEM_SIZES           device-info-size_t-array ]
269         [ CL_DEVICE_MAX_WORK_GROUP_SIZE           device-info-size_t       ]
270         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_CHAR   device-info-uint         ]
271         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_SHORT  device-info-uint         ]
272         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_INT    device-info-uint         ]
273         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_LONG   device-info-uint         ]
274         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_FLOAT  device-info-uint         ]
275         [ CL_DEVICE_PREFERRED_VECTOR_WIDTH_DOUBLE device-info-uint         ]
276         [ CL_DEVICE_MAX_CLOCK_FREQUENCY           device-info-uint         ]
277         [ CL_DEVICE_ADDRESS_BITS                  device-info-uint         ]
278         [ CL_DEVICE_MAX_MEM_ALLOC_SIZE            device-info-ulong        ]
279         [ CL_DEVICE_IMAGE_SUPPORT                 device-info-bool         ]
280         [ CL_DEVICE_MAX_READ_IMAGE_ARGS           device-info-uint         ]
281         [ CL_DEVICE_MAX_WRITE_IMAGE_ARGS          device-info-uint         ]
282         [ CL_DEVICE_IMAGE2D_MAX_WIDTH             device-info-size_t       ]
283         [ CL_DEVICE_IMAGE2D_MAX_HEIGHT            device-info-size_t       ]
284         [ CL_DEVICE_IMAGE3D_MAX_WIDTH             device-info-size_t       ]
285         [ CL_DEVICE_IMAGE3D_MAX_HEIGHT            device-info-size_t       ]
286         [ CL_DEVICE_IMAGE3D_MAX_DEPTH             device-info-size_t       ]
287         [ CL_DEVICE_MAX_SAMPLERS                  device-info-uint         ]
288         [ CL_DEVICE_MAX_PARAMETER_SIZE            device-info-size_t       ]
289         [ CL_DEVICE_MEM_BASE_ADDR_ALIGN           device-info-uint         ]
290         [ CL_DEVICE_MIN_DATA_TYPE_ALIGN_SIZE      device-info-uint         ]
291         [ CL_DEVICE_SINGLE_FP_CONFIG              device-info-ulong cl_device_fp_config>flags           ]
292         [ CL_DEVICE_GLOBAL_MEM_CACHE_TYPE         device-info-uint  cl_device_mem_cache_type>cache-type ]
293         [ CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE     device-info-uint         ]
294         [ CL_DEVICE_GLOBAL_MEM_CACHE_SIZE         device-info-ulong        ]
295         [ CL_DEVICE_GLOBAL_MEM_SIZE               device-info-ulong        ]
296         [ CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE      device-info-ulong        ]
297         [ CL_DEVICE_MAX_CONSTANT_ARGS             device-info-uint         ]
298         [ CL_DEVICE_LOCAL_MEM_TYPE                device-info-uint CL_LOCAL = ]
299         [ CL_DEVICE_LOCAL_MEM_SIZE                device-info-ulong        ]
300         [ CL_DEVICE_ERROR_CORRECTION_SUPPORT      device-info-bool         ]
301         [ CL_DEVICE_PROFILING_TIMER_RESOLUTION    device-info-size_t       ]
302         [ CL_DEVICE_ENDIAN_LITTLE                 device-info-bool         ]
303         [ CL_DEVICE_AVAILABLE                     device-info-bool         ]
304         [ CL_DEVICE_COMPILER_AVAILABLE            device-info-bool         ]
305         [ CL_DEVICE_EXECUTION_CAPABILITIES        device-info-ulong CL_EXEC_KERNEL                         bitand 0 = not ]
306         [ CL_DEVICE_EXECUTION_CAPABILITIES        device-info-ulong CL_EXEC_NATIVE_KERNEL                  bitand 0 = not ]
307         [ CL_DEVICE_QUEUE_PROPERTIES              device-info-ulong CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE bitand 0 = not ]
308         [ CL_DEVICE_QUEUE_PROPERTIES              device-info-ulong CL_QUEUE_PROFILING_ENABLE              bitand 0 = not ]
309         [ CL_DEVICE_NAME                          device-info-string       ]
310         [ CL_DEVICE_VENDOR                        device-info-string       ]
311         [ CL_DRIVER_VERSION                       device-info-string       ]
312         [ CL_DEVICE_PROFILE                       device-info-string       ]
313         [ CL_DEVICE_VERSION                       device-info-string       ]
314         [ CL_DEVICE_EXTENSIONS                    device-info-string       ]
315     } cleave cl-device boa ;
316
317 : platform-devices ( platform-id -- devices )
318     CL_DEVICE_TYPE_ALL [
319         0 f 0 uint <ref> [ clGetDeviceIDs cl-success ] keep uint deref
320     ] [
321         rot dup void* <c-array> [ f clGetDeviceIDs cl-success ] keep
322     ] 2bi ; inline
323
324 : command-queue-info-ulong ( handle name -- ulong )
325     [ clGetCommandQueueInfo ] info-ulong ;
326
327 : sampler-info-bool ( handle name -- ? )
328     [ clGetSamplerInfo ] info-bool ;
329
330 : sampler-info-uint ( handle name -- uint )
331     [ clGetSamplerInfo ] info-uint ;
332
333 : program-build-info-string ( program-handle device-handle name -- string )
334     [ clGetProgramBuildInfo ] 2info-string ;
335
336 : program-build-log ( program-handle device-handle -- string )
337     CL_PROGRAM_BUILD_LOG program-build-info-string ;
338
339 : strings>char*-array ( strings -- char*-array )
340     [
341         ascii encode dup length dup malloc
342         [ cl-not-null ] keep &free [ -rot memcpy ] keep
343     ] void*-array{ } map-as ;
344
345 : (program) ( cl-context sources -- program-handle )
346     [ handle>> ] dip [
347         [ length ]
348         [ strings>char*-array ]
349         [ [ length ] size_t-array{ } map-as ] tri
350         0 int <ref> [ clCreateProgramWithSource ] keep int deref cl-success
351     ] with-destructors ;
352
353 :: (build-program) ( program-handle device options -- program )
354     program-handle 1 device 1array [ id>> ] void*-array{ } map-as
355     options ascii encode 0 suffix f f clBuildProgram
356     {
357         { CL_BUILD_PROGRAM_FAILURE [
358             program-handle device id>> program-build-log program-handle
359             clReleaseProgram cl-success cl-error f ] }
360         { CL_SUCCESS [ cl-program new-disposable program-handle >>handle ] }
361         [ program-handle clReleaseProgram cl-success cl-success f ]
362     } case ;
363
364 : kernel-info-string ( handle name -- string )
365     [ clGetKernelInfo ] info-string ;
366
367 : kernel-info-uint ( handle name -- uint )
368     [ clGetKernelInfo ] info-uint ;
369
370 : kernel-work-group-info-size_t ( handle1 handle2 name -- size_t )
371     [ clGetKernelWorkGroupInfo ] 2info-size_t ;
372
373 : event-info-uint ( handle name -- uint )
374     [ clGetEventInfo ] info-uint ;
375
376 : event-info-int ( handle name -- int )
377     [ clGetEventInfo ] info-int ;
378
379 : cl_command_type>command-type ( cl_command-type -- command-type )
380     {
381         { CL_COMMAND_NDRANGE_KERNEL       [ cl-ndrange-kernel-command       ] }
382         { CL_COMMAND_TASK                 [ cl-task-command                 ] }
383         { CL_COMMAND_NATIVE_KERNEL        [ cl-native-kernel-command        ] }
384         { CL_COMMAND_READ_BUFFER          [ cl-read-buffer-command          ] }
385         { CL_COMMAND_WRITE_BUFFER         [ cl-write-buffer-command         ] }
386         { CL_COMMAND_COPY_BUFFER          [ cl-copy-buffer-command          ] }
387         { CL_COMMAND_READ_IMAGE           [ cl-read-image-command           ] }
388         { CL_COMMAND_WRITE_IMAGE          [ cl-write-image-command          ] }
389         { CL_COMMAND_COPY_IMAGE           [ cl-copy-image-command           ] }
390         { CL_COMMAND_COPY_BUFFER_TO_IMAGE [ cl-copy-buffer-to-image-command ] }
391         { CL_COMMAND_COPY_IMAGE_TO_BUFFER [ cl-copy-image-to-buffer-command ] }
392         { CL_COMMAND_MAP_BUFFER           [ cl-map-buffer-command           ] }
393         { CL_COMMAND_MAP_IMAGE            [ cl-map-image-command            ] }
394         { CL_COMMAND_UNMAP_MEM_OBJECT     [ cl-unmap-mem-object-command     ] }
395         { CL_COMMAND_MARKER               [ cl-marker-command               ] }
396         { CL_COMMAND_ACQUIRE_GL_OBJECTS   [ cl-acquire-gl-objects-command   ] }
397         { CL_COMMAND_RELEASE_GL_OBJECTS   [ cl-release-gl-objects-command   ] }
398     } case ;
399
400 : cl_int>execution-status ( clint -- execution-status )
401     {
402         { CL_QUEUED    [ cl-queued    ] }
403         { CL_SUBMITTED [ cl-submitted ] }
404         { CL_RUNNING   [ cl-running   ] }
405         { CL_COMPLETE  [ cl-complete  ] }
406         [ drop cl-failure ]
407     } case ; inline
408
409 : profiling-info-ulong ( handle name -- ulong )
410     [ clGetEventProfilingInfo ] info-ulong ;
411
412 : bind-kernel-arg-buffer ( kernel index buffer -- )
413     [ handle>> ] [ cl_mem heap-size ] [ handle>> void* deref ] tri*
414     clSetKernelArg cl-success ; inline
415
416 : bind-kernel-arg-data ( kernel index byte-array -- )
417     [ handle>> ] 2dip
418     [ byte-length ] keep clSetKernelArg cl-success ; inline
419
420 GENERIC: bind-kernel-arg ( kernel index data -- )
421 M: cl-buffer  bind-kernel-arg bind-kernel-arg-buffer ;
422 M: byte-array bind-kernel-arg bind-kernel-arg-data ;
423
424 PRIVATE>
425
426 : with-cl-state ( context/f device/f queue/f quot -- )
427     [
428         [
429             [ cl-current-queue   ,, ] when*
430             [ cl-current-device  ,, ] when*
431             [ cl-current-context ,, ] when*
432         ] 3curry H{ } make
433     ] dip with-variables ; inline
434
435 : cl-platforms ( -- platforms )
436     0 f 0 uint <ref> [ clGetPlatformIDs cl-success ] keep uint deref
437     dup void* <c-array> [ f clGetPlatformIDs cl-success ] keep
438     [
439         dup
440         [ platform-info ]
441         [ platform-devices [ device-info ] { } map-as ] bi
442         cl-platform boa
443     ] { } map-as ;
444
445 : <cl-context> ( devices -- cl-context )
446     [ f ] dip
447     [ length ] [ [ id>> ] void*-array{ } map-as ] bi
448     f f 0 int <ref> [ clCreateContext ] keep int deref cl-success
449     cl-context new-disposable swap >>handle ;
450
451 : <cl-queue> ( context device out-of-order? profiling? -- command-queue )
452     [ [ handle>> ] [ id>> ] bi* ] 2dip
453     [ [ CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE ] [ 0 ] if ]
454     [ [ CL_QUEUE_PROFILING_ENABLE ] [ 0 ] if ] bi* bitor
455     0 int <ref> [ clCreateCommandQueue ] keep int deref cl-success
456     cl-queue new-disposable swap >>handle ;
457
458 : cl-out-of-order-execution? ( command-queue -- ? )
459     CL_QUEUE_PROPERTIES command-queue-info-ulong
460     CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE bitand 0 = not ; inline
461
462 : cl-profiling? ( command-queue -- ? )
463     CL_QUEUE_PROPERTIES command-queue-info-ulong
464     CL_QUEUE_PROFILING_ENABLE bitand 0 = not ; inline
465
466 : <cl-buffer> ( buffer-access-mode size initial-data -- buffer )
467     [ (current-cl-context) ] 3dip
468     tuck [
469         [ handle>> ]
470         [ buffer-access-constant ]
471         [ [ CL_MEM_COPY_HOST_PTR ] [ CL_MEM_ALLOC_HOST_PTR ] if ] tri* bitor
472     ] 2dip
473     0 int <ref> [ clCreateBuffer ] keep int deref cl-success
474     cl-buffer new-disposable swap >>handle ;
475
476 : cl-read-buffer ( buffer-range -- byte-array )
477     [ (current-cl-queue) handle>> ] dip
478     [ buffer>> handle>> CL_TRUE ]
479     [ offset>> ]
480     [ size>> dup <byte-array> ] tri
481     [ 0 f f clEnqueueReadBuffer cl-success ] keep ; inline
482
483 : cl-write-buffer ( buffer-range byte-array -- )
484     [
485         [ (current-cl-queue) handle>> ] dip
486         [ buffer>> handle>> CL_TRUE ]
487         [ offset>> ]
488         [ size>> ] tri
489     ] dip 0 f f clEnqueueWriteBuffer cl-success ; inline
490
491 : cl-queue-copy-buffer ( src-buffer-ptr dst-buffer-ptr size dependent-events -- event )
492     [
493         (current-cl-queue)
494         [ handle>> ]
495         [ [ buffer>> handle>> ] [ offset>> ] bi ]
496         [ [ buffer>> handle>> ] [ offset>> ] bi ]
497         tri* swapd
498     ] 2dip [ length ] keep [ f ] [ [ handle>> ] void*-array{ } map-as ] if-empty
499     f void* <ref> [ clEnqueueCopyBuffer cl-success ] keep void* deref cl-event
500     new-disposable swap >>handle ;
501
502 : cl-queue-read-buffer ( buffer-range alien dependent-events -- event )
503     [
504         [ (current-cl-queue) handle>> ] dip
505         [ buffer>> handle>> CL_FALSE ] [ offset>> ] [ size>> ] tri
506     ] 2dip [ length ] keep [ f ] [ [ handle>> ] void*-array{ } map-as ] if-empty
507     f void* <ref> [ clEnqueueReadBuffer cl-success ] keep void* deref cl-event
508     new-disposable swap >>handle ;
509
510 : cl-queue-write-buffer ( buffer-range alien dependent-events -- event )
511     [
512         [ (current-cl-queue) handle>> ] dip
513         [ buffer>> handle>> CL_FALSE ] [ offset>> ] [ size>> ] tri
514     ] 2dip [ length ] keep [ f ] [ [ handle>> ] void*-array{ } map-as ] if-empty
515     f void* <ref> [ clEnqueueWriteBuffer cl-success ] keep void* deref cl-event
516     new-disposable swap >>handle ;
517
518 : <cl-sampler> ( normalized-coords? addressing-mode filter-mode -- sampler )
519     [ (current-cl-context) ] 3dip
520     [ [ CL_TRUE ] [ CL_FALSE ] if ]
521     [ addressing-mode-constant ]
522     [ filter-mode-constant ]
523     tri* 0 int <ref> [ clCreateSampler ] keep int deref cl-success
524     cl-sampler new-disposable swap >>handle ;
525
526 : cl-normalized-coords? ( sampler -- ? )
527     handle>> CL_SAMPLER_NORMALIZED_COORDS sampler-info-bool ; inline
528
529 : sampler>cl-addressing-mode ( sampler -- addressing-mode )
530     handle>> CL_SAMPLER_ADDRESSING_MODE sampler-info-uint cl_addressing_mode>addressing-mode ; inline
531
532 : sampler>cl-filter-mode ( sampler -- filter-mode )
533     handle>> CL_SAMPLER_FILTER_MODE sampler-info-uint cl_filter_mode>filter-mode ; inline
534
535 : <cl-program> ( options strings -- program )
536     [ (current-cl-device) ] 2dip
537     [ (current-cl-context) ] dip
538     (program) -rot (build-program) ;
539
540 : <cl-kernel> ( program kernel-name -- kernel )
541     [ handle>> ] [ ascii encode 0 suffix ] bi*
542     0 int <ref> [ clCreateKernel ] keep int deref cl-success
543     cl-kernel new-disposable swap >>handle ; inline
544
545 : cl-kernel-name ( kernel -- string )
546     handle>> CL_KERNEL_FUNCTION_NAME kernel-info-string ;
547
548 : cl-kernel-arity ( kernel -- arity )
549     handle>> CL_KERNEL_NUM_ARGS kernel-info-uint ;
550
551 : cl-kernel-local-size ( kernel -- size )
552     (current-cl-device) [ handle>> ] bi@ CL_KERNEL_WORK_GROUP_SIZE kernel-work-group-info-size_t ; inline
553
554 :: cl-queue-kernel ( kernel args sizes dependent-events -- event )
555     args [| arg idx | kernel idx arg bind-kernel-arg ] each-index
556     (current-cl-queue) handle>>
557     kernel handle>>
558     sizes [ length f ] [ [ ] size_t-array{ } map-as f ] bi
559     dependent-events [ length ] [ [ f ] [ [ handle>> ] void*-array{ } map-as ] if-empty ] bi
560     f void* <ref> [ clEnqueueNDRangeKernel cl-success ] keep void* deref
561     cl-event new-disposable swap >>handle ;
562
563 : cl-event-type ( event -- command-type )
564     handle>> CL_EVENT_COMMAND_TYPE event-info-uint cl_command_type>command-type ; inline
565
566 : cl-event-status ( event -- execution-status )
567     handle>> CL_EVENT_COMMAND_EXECUTION_STATUS event-info-int cl_int>execution-status ; inline
568
569 : cl-profile-counters ( event -- queued submitted started finished )
570     handle>> {
571         [ CL_PROFILING_COMMAND_QUEUED profiling-info-ulong ]
572         [ CL_PROFILING_COMMAND_SUBMIT profiling-info-ulong ]
573         [ CL_PROFILING_COMMAND_START  profiling-info-ulong ]
574         [ CL_PROFILING_COMMAND_END    profiling-info-ulong ]
575     } cleave ; inline
576
577 : cl-barrier-events ( event/events -- )
578     [ (current-cl-queue) handle>> ] dip
579     dup sequence? [ 1array ] unless
580     [ handle>> ] void*-array{ } map-as [ length ] keep clEnqueueWaitForEvents cl-success ; inline
581
582 : cl-marker ( -- event )
583     (current-cl-queue)
584     f void* <ref> [ clEnqueueMarker cl-success ] keep void* deref cl-event new-disposable
585     swap >>handle ; inline
586
587 : cl-barrier ( -- )
588     (current-cl-queue) clEnqueueBarrier cl-success ; inline
589
590 : cl-flush ( -- )
591     (current-cl-queue) handle>> clFlush cl-success ; inline
592
593 : cl-wait ( event/events -- )
594     dup sequence? [ 1array ] unless
595     [ handle>> ] void*-array{ } map-as [ length ] keep clWaitForEvents cl-success ; inline
596
597 : cl-finish ( -- )
598     (current-cl-queue) handle>> clFinish cl-success ; inline