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