Search code examples
openclpyopencl

Append OpenCL result to list / Reduce solution room


I have an OpenCL Kernel with multiple work items. Let's assume for discussion, that I have a 2-D Workspace with x*y elements working on an equally sized, but sparce, array of input elements. Few of these input elements produce a result, that I want to keep, most don't. I want to enqueue another kernel, that only takes the kept results as an input.

Is it possible in OpenCL to append results to some kind of list to pass them as input to another Kernel or is there a better idea to reduce the volume of the solution space? Furthermore: Is this even a good question to ask with the programming model of OpenCL in mind?


Solution

  • What I would do if the amount of result data is a small percentage (ie: 0-10%) is use local atomics and global atomics, with a global counter.

    Data interface between kernel 1 <----> Kernel 2:

    int counter                  //used by atomics to know where to write
    data_type results[counter];  //used to store the results
    

    Kernel1:

    • Create a kernel function that does the operation on the data
    • Work items that do produce a result:
      • Save the result to local memory, and ensure no data races occur using local atomics in a local counter.
    • Use the work item 0 to save all the local results back to global memory using global atomics.

    Kernel2:

    • Work items lower than "counter" do work, the others just return.