2
votes

I need to pass a vector<vector<string>> to a kernel OpenCL. What is the easiest way of doing it? Passing a char*** gives me an error:

__kernel void vadd(
   __global char*** sets,
   __global int* m,
   __global long* result)
{}

ERROR: clBuildProgram(CL_BUILD_PROGRAM_FAILURE)

1
Can OpenCL make sense of std::vector arguments? Those are not interchangeable with pointers. You must convert, as in map out with data(). Likewise with std::string you'll have to map out c_str(). - tadman
You kinda need to allocate and copy the data to the device. And use designated pointers. You cannot just use std::vector... (In case you use Sycl - it might be working in a different manner). - ALX23z
If the internal vectors are all the same size, just use 2D array emulation and pass in char** instead. - tadman
@tadman what do you mean for 2D array emulation? - pinotto
Instead of x[y][z] you do x[y * w + z] where w is the "width" of each sub-element. - tadman

1 Answers

4
votes

In OpenCL 1.x, this sort of thing is basically not possible. You'll need to convert your data such that it fits into a single buffer object, or at least into a fixed number of buffer objects. Pointers on the host don't make sense on the device. (With OpenCL 2's SVM feature, you can pass pointer values between host and kernel code, but you'll still need to ensure the memory is allocated in a way that's appropriate for this.)

One option I can think of, bearing in mind I know nothing about the rest of your program, is as follows:

  1. Create an OpenCL buffer for all the strings. Add up the number of bytes required for all your strings. (possibly including nul termination, depending on what you're trying to do)
  2. Create a buffer for looking up string start offsets (and possibly length). Looks like you have 2 dimensions of lookup (nested vectors) so how you lay this out will depend on whether your inner vectors (second dimension) are all the same size or not.
  3. Write your strings back-to-back into the first buffer, recording start offsets (and length, if necessary) in the second buffer.