I’m starting doing some development in OpenCL and one of my first objectives concerns the porting of a library with a lot of data to OpenCL.
This particular library consists in its native C form of a large number (about 20MB in memory) of arrays of arrays whose values are strictly constant, as well as a couple of functions that allow the user to extract (and sometimes perform some basic operations on) values from these arrays.
I have all these arrays together in a huge C source code file (about 1M lines) that I compile together with the code of the functions into the library.
My question now is: is it somehow possible to compile this huge file, with the necessary memory qualifiers, into OpenCL kernels that use the datasets and the associated functions?
Again, these arrays are constant and will not be changed during execution.
Thanks in advance for all advice!
Tom
OpenCL supports constant memory, which is exactly what you’re looking for. It works similarly to global memory, though exact placement can vary depending on implementation, and it lets the compiler optimize things differently since the memory is guaranteed not to be modified during the kernel’s execution.
You’d create this buffer, marking it read-only and copying from the host-accessible data. Then you just pass it into the kernels normally:
Presumably you would want to create the buffer once per context and re-use it (like you would with the kernel) to avoid excessive re-work.
Also, your data is large enough that some devices might not have enough constant memory available, in which case you can fall back to using global memory and marking it read-only. (This would mean two versions of your kernel, one with either type of parameter.) Check
clGetDeviceInfoforCL_DEVICE_MAX_CONSTANT_BUFFER_SIZEand decide at runtime.