- https://karthikhegde.blogspot.com/2013/09/hope-you-liked-previous-introductory.html
- http://fisica.cab.cnea.gov.ar/gpgpu/images/charlas/lab-slides.pdf
- OpenCL object diagram:

- header:
import pyopencl as cl
- choose the platform that support the OpenCL you want, for example, platform '0' here in default (since I have only one laptop connected):
platforms=cl.get_platforms()[0]
it would be necessary to add some code to check the support for the necessary platform extension:
>>> platforms.extensions
'cl_APPLE_SetMemObjectDestructor cl_APPLE_ContextLoggingFunctions cl_APPLE_clut cl_APPLE_query_kernel_names cl_APPLE_gl_sharing cl_khr_gl_event'
- show the devices (accelerator) on the platform:
device0=platforms.get_devices()
this will print all the devices on the platform, for example, the laptop I am using has the following devices:
[<pyopencl.Device 'Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz' on 'Apple' at 0xffffffff>, <pyopencl.Device 'Intel(R) HD Graphics 630' on 'Apple' at 0x1024500>, <pyopencl.Device 'AMD Radeon Pro 560 Compute Engine' on 'Apple' at 0x1021c00>]
you can specify which device to use to run the process (only one can used at one time??):
device0=platforms.get_devices()[0]
- since you have chosen the proper platform and the device available on the platform, now you can create context on the device:
ctx=cl.create_some_context() #there is also a method called cl.Context([device])
ctx=cl.Context(device=None|[dev1,dev2], dev_type=Non)
# dev_type: DEFAULT, ALL, CPU, GPU
if you do not have specify the platform and/or the devices, the system will ask you to do so, for example, you may see:
>>> cl.create_some_context()
Choose platform:
[0] <pyopencl.Platform 'Apple' at 0x7fff0000>
Choice [0]:0
Choose device(s):
[0] <pyopencl.Device 'Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz' on 'Apple' at 0xffffffff>
[1] <pyopencl.Device 'Intel(R) HD Graphics 630' on 'Apple' at 0x1024500>
[2] <pyopencl.Device 'AMD Radeon Pro 560 Compute Engine' on 'Apple' at 0x1021c00>
Choice, comma-separated [0]:2
Set the environment variable PYOPENCL_CTX='0:2' to avoid being asked again.
<pyopencl.Context at 0x7fe40c4d69d0 on <pyopencl.Device 'AMD Radeon Pro 560 Compute Engine' on 'Apple' at 0x1021c00>>
set the environment as suggested to avoid doing this again:
import os
os.environ['PYOPENCL_CTX']='0:2'
- create a queue for the context:
queue=cl.CommandQueue(ctx,device=None,properties=None|[(prop,value),...]) #FIFO?
cl.command queue properties...
- memory objects: Buffers (Chunk of DEVICE memory)
buf=cl.Buffer(ctx,mem_flags,size,hostbuf=None)
- Programs and Kernels
prg=cl.Program(ctx,src)
# functions with __kernel attribute can be invoked from host
# prg.build(options="",devices=None)
# kernel=prg.kernel_name
the program objects are specified as:
kernel(queue,(Gx,Gy,Gz),(Sx,Sy,Sz),arg, ..., wait_for=None)