1

I want to pass a vector of octrees to OpenCL. Basic structure of the C++ classes/structs

struct Element
{
    //child nodes
    unsigned int c[8];
    //actual value
    Voxel vxl;
};
class Octree
{
    vector<Element> elements;
}

I cannot simply pass a vector<Octree> to OpenCL, nor can I use an approach like this (from: How to pass and access C++ vectors to OpenCL kernel?):

cl_mem buffer_b = clCreateBuffer(
    context, // OpenCL context
    CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, // Only read access from kernel,
                                             // copy data from host
    sizeof(cl_double) * b.size(), // Buffer size in bytes
    &b[0], // Pointer to data to copy
    &errorcode);

My approach was putting all Elements into a single arrray and have an array of indices to know which Elements belong to which Octree. This, however is really slow and also not a pretty solution.

vector<Element> allElements;
vector<unsigned int> indices;
for (int i = 0; i < length(); i++)
{
    indices.push_back(allElements.size());
    for (int j = 0; j < this->at(i).length(); j++)
    {
        allElements.push_back(*this->at(i)[j]);
    }
}

This code takes several seconds (depending on how many Octrees and Elements there are, of course) to execute.

So, how would I pass a structure like this to OpenCL? There must be a way, right?

Community
  • 1
  • 1
l'arbre
  • 719
  • 2
  • 10
  • 29
  • 1
    a `.reserve` would probably help, but if you have _that_ many elements, you probably want to store them in the correct format to begin with. – Lightness Races in Orbit Aug 01 '16 at 09:44
  • It would be helpful if you could show how you pass your final data to OpenCL. Do you only use `&allElements[0]` or do you some other additional processing on the data? Because if you only use `&allElements[0]` then you could allocate an empty buffer with the final size and write each cell of your octree at once to your buffer using [clEnqueueWriteBuffer](https://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clEnqueueWriteBuffer.html). This still might not have the best performance, but you could give it a try. – t.niese Aug 01 '16 at 19:52

0 Answers0