$darkmode
A high-performance general-purpose compute library
/home/abuild/rpmbuild/BUILD/arrayfire-full-v3.9.0/docs/pages/interop_opencl.md
Go to the documentation of this file.
1 Interoperability with OpenCL {#interop_opencl}
2 ========
3 
4 Although ArrayFire is quite extensive, there remain many cases in which you
5 may want to write custom kernels in OpenCL or [CUDA](\ref interop_cuda).
6 For example, you may wish to add ArrayFire to an existing code base to increase
7 your productivity, or you may need to supplement ArrayFire's functionality
8 with your own custom implementation of specific algorithms.
9 
10 ArrayFire manages its own context, queue, memory, and creates custom IDs
11 for devices. As such, most of the interoperability functions focus on reducing
12 potential synchronization conflicts between ArrayFire and OpenCL.
13 
14 # Basics
15 
16 It is fairly straightforward to interface ArrayFire with your own custom OpenCL
17 code. ArrayFire provides several functions to ease this process including:
18 
19 | Function | Purpose |
20 |-----------------------|-----------------------------------------------------|
21 | af::array(...) | Construct an ArrayFire array from cl_mem references or cl::Buffer objects |
22 | af::array.device() | Obtain a pointer to the cl_mem reference (implies `lock()`) |
23 | af::array.lock() | Removes ArrayFire's control of a cl_mem buffer |
24 | af::array.unlock() | Restores ArrayFire's control over a cl_mem buffer |
25 | afcl::getPlatform() | Get ArrayFire's current cl_platform |
26 | af::getDevice() | Get the current ArrayFire Device ID |
27 | afcl::getDeviceId() | Get ArrayFire's current cl_device_id |
28 | af::setDevice() | Set ArrayFire's device from an ArrayFire device ID |
29 | afcl::setDeviceId() | Set ArrayFire's device from a cl_device_id |
30 | afcl::setDevice() | Set ArrayFire's device from a cl_device_id and cl_context |
31 | afcl::getContext() | Get ArrayFire's current cl_context |
32 | afcl::getQueue() | Get ArrayFire's current cl_command_queue |
33 | afcl::getDeviceType() | Get the current afcl_device_type |
34 
35 Additionally, the OpenCL backend permits the programmer to add and remove custom
36 devices from the ArrayFire device manager. These permit you to attach ArrayFire
37 directly to the OpenCL queue used by other portions of your application.
38 
39 | Function | Purpose |
40 |-----------------------|---------------------------------------------------|
41 | afcl::addDevice() | Add a new device to ArrayFire's device manager |
42 | afcl::deleteDevice() | Remove a device from ArrayFire's device manager |
43 
44 Below we provide two worked examples on how ArrayFire can be integrated
45 into new and existing projects.
46 
47 # Adding custom OpenCL kernels to an existing ArrayFire application
48 
49 By default, ArrayFire manages its own context, queue, memory, and creates custom
50 IDs for devices. Thus there is some bookkeeping that needs to be done to
51 integrate your custom OpenCL kernel.
52 
53 If your kernels can share operate in the same queue as ArrayFire, you should:
54 
55 1. Add an include for `af/opencl.h` to your project
56 2. Obtain the OpenCL context, device, and queue used by ArrayFire
57 3. Obtain cl_mem references to af::array objects
58 4. Load, build, and use your kernels
59 5. Return control of af::array memory to ArrayFire
60 
61 Note, ArrayFire uses an in-order queue, thus when ArrayFire and your kernels
62 are operating in the same queue, there is no need to perform any
63 synchronization operations.
64 
65 This process is best illustrated with a fully worked example:
66 
67 \snippet test/interop_opencl_custom_kernel_snippet.cpp interop_opencl_custom_kernel_snippet
68 
69 If your kernels needs to operate in their own OpenCL queue, the process is
70 essentially identical, except you need to instruct ArrayFire to complete
71 its computations using the af::sync() function prior to launching your
72 own kernel and ensure your kernels are complete using `clFinish`
73 (or similar) commands prior to returning control of the memory to ArrayFire:
74 
75 1. Add an include for `af/opencl.h` to your project
76 2. Obtain the OpenCL context, device, and queue used by ArrayFire
77 3. Obtain cl_mem references to af::array objects
78 4. Instruct ArrayFire to finish operations using af::sync()
79 5. Load, build, and use your kernels
80 6. Instruct OpenCL to finish operations using clFinish() or similar commands.
81 5. Return control of af::array memory to ArrayFire
82 
83 # Adding ArrayFire to an existing OpenCL application
84 
85 Adding ArrayFire to an existing OpenCL application is slightly more involved
86 and can be somewhat tricky due to several optimizations we implement. The
87 most important are as follows:
88 
89 * ArrayFire assumes control of all memory provided to it.
90 * ArrayFire does not (in general) support in-place memory transactions.
91 
92 We will discuss the implications of these items below. To add ArrayFire
93 to existing code you need to:
94 
95 1. Add includes
96 2. Instruct OpenCL to complete its operations using clFinish (or similar)
97 3. Instruct ArrayFire to use the user-created OpenCL Context
98 4. Create ArrayFire arrays from OpenCL memory objects
99 5. Perform ArrayFire operations on the Arrays
100 6. Instruct ArrayFire to finish operations using af::sync()
101 7. Obtain cl_mem references for important memory
102 8. Continue your OpenCL application
103 
104 To create the af::array objects, you should use one of the following
105 constructors:
106 
107 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
108 // 1D - 3D af::array constructors
109 static af::array array (dim_t dim0, cl_mem buf, af::dtype type, bool retain=false)
110 static af::array array (dim_t dim0, dim_t dim1, cl_mem buf, af::dtype type, bool retain=false)
111 static af::array array (dim_t dim0, dim_t dim1, dim_t dim2, cl_mem buf, af::dtype type, bool retain=false)
112 static af::array array (dim_t dim0, dim_t dim1, dim_t dim2, dim_t dim3, cl_mem buf, af::dtype type, bool retain=false)
113 
114 // af::array constructor using a dim4 object
115 static af::array array (af::dim4 idims, cl_mem buf, af::dtype type, bool retain=false)
116 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117 
118 *NOTE*: With all of these constructors, ArrayFire's memory manager automatically
119 assumes responsibility for any memory provided to it. If you are creating
120 an array from a `cl::Buffer`, you should specify `retain=true` to ensure your
121 memory is not deallocated if your `cl::Buffer` were to go out of scope.
122 We use this technique in the example below.
123 If you do not wish for ArrayFire to manage your memory, you may call the
124 `array::unlock()` function and manage the memory yourself; however, if you do
125 so, please be cautious not to call `clReleaseMemObj` on a `cl_mem` when
126 ArrayFire might be using it!
127 
128 The eight steps above are best illustrated using a fully-worked example. Below we
129 use the OpenCL C++ API and omit error checking to keep the code readable.
130 
131 \snippet test/interop_opencl_external_context_snippet.cpp interop_opencl_external_context_snippet
132 
133 # Using multiple devices
134 
135 If you are using ArrayFire and OpenCL with multiple devices be sure to use
136 `afcl::addDevice` to add your custom context + device + queue to ArrayFire's
137 device manager. This will let you switch ArrayFire devices using your current
138 `cl_device_id` and `cl_context`.