Home > Net >  How to store a result of an OpenCL kernel without taking it to a host to reuse it in another kernel?
How to store a result of an OpenCL kernel without taking it to a host to reuse it in another kernel?

Time:09-11

I have two kernels in my app:

__kernel void
set_decomposition_key(
    __global const double* values_ref,
    __global const double* decomposition_results_ref,
    __global double* decomposition_keys)
{ /* Some code, results are put into decomposition_keys */ }

__kernel void
decompose(
    __global const double* values,
    __global const double* decomposition_keys,
    __global double* decomposition_results)
{ /* Some code, results are put into decomposition_results */ }

set_decomposition_key() executes once when the app's starting, it puts its calculation results into the decomposition_keys value. Then decompose(), the main function, comes into play: it executes in a loop performing some operations in real time. It uses results of the first function stored in decomposition_keys as its second parameter. So the thing's that I want to keep this variable in an OpenCL device's memory without taking it to a host and then setting again for the second kernel every loop.

But I don't know how to do this, my attempts to find a working solution by myself failed. So I'll appreciate any help here. Thank you in advance.

Note: I know about this answer, suddenly it doesn't work for me.

CodePudding user response:

On the host side, make one cl::Buffer decomposition_keys; object and link it to both cl::Kernel set_decomposition_key, decompose; objects via setArg(...);.

When you call set_decomposition_key, it writes the results into the decomposition_keys array in device memory, and the data stays there unchanged. You can call the kernel decompose over and over again, it only reads decomposition_keys. The array stays in device memory and no copying to host memory is required.

Note that on the host side, cl::Buffer decomposition_keys; must remain in scope for the buffer not to be deleted on the device side. For testing, you can ensure this by setting it as a global C variable.


For an easier start with OpenCL/C , try this OpenCL-Wrapper. It makes buffer handling on the C side so much easier and shorter.

  • Related