How to process string in opencl kernel from buffer of N fixed length strings?

人盡茶涼 提交于 2021-01-29 14:33:17

问题


I am required to process N fixed-length strings in parallel on an OpenCL device.

Processing a string involves calling function that is provided, that takes a string as input represented as a buffer, and the length of the string in that buffer.

void Function(const char *input_buffer, const int string_length, const char *output_buffer)

Inside the host application I have concatenated the N strings into a large char buffer, with no separator between them.

I would like to create a kernel with a definition similar to

__kernel void myKernel(global char *buffer_of_strings, char length_of_string, global char *output_buffer) {

     char *string_input = ??? (no dynamic allocation allowed)
     Function(string_input, length_of_string, output_buffer);
}

Out of all kernels, only one will ever "succeed" and write to the output buffer.

How do I assign a subrange of the *global char buffer_of_strings to the string_input buffer since string length do vary?

Am I supposed to create a multi-dimensional input rather than a 1-D array?


回答1:


Your question is not 100% clear, so I'll briefly outline my understanding of the situation before answering:

You have a buffer_of_strings, which contains N strings of length_of_string bytes. This means each string starts at an offset i * length_of_string into the buffer:

      +--------length_of_string--------+
      |          |          |          |
 <----+----><----+----><----+----><----+---->
"String0    String1    Str2       String3    "
 ^          ^                     ^
 |          |                     |
 0     (1 * length_of_string)     (3 * length_of_string)

So that leads me to something like this, with plain old-fashioned pointer arithmetic:

__kernel void myKernel(global char *buffer_of_strings, char length_of_string, global char *output_buffer) {
     uint offset = get_global_id(0) * length_of_string;
     global char *string_input = buffer_of_strings + offset;
     Function(string_input, length_of_string, output_buffer);
}

Make sure to annotate all your pointers with the appropriate memory region. (global in this case)



来源:https://stackoverflow.com/questions/55103811/how-to-process-string-in-opencl-kernel-from-buffer-of-n-fixed-length-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!