问题
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