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