如何从N个固定长度字符串的缓冲区处理opencl内核中的字符串? [英] How to process string in opencl kernel from buffer of N fixed length strings?
问题描述
我需要在 OpenCL 设备上并行处理 N 个固定长度的字符串.
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)
在主机应用程序中,我将 N 个字符串连接到一个大的字符缓冲区中,它们之间没有分隔符.
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.
由于字符串长度不同,我如何将 *global char buffer_of_strings 的子范围分配给 string_input 缓冲区?
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?
推荐答案
你的问题不是 100% 清楚,所以我会在回答之前简要概述一下我对情况的理解:
Your question is not 100% clear, so I'll briefly outline my understanding of the situation before answering:
您有一个 buffer_of_strings
,其中包含 N
个 length_of_string
字节的字符串.这意味着每个字符串从偏移量 i * length_of_string
开始到缓冲区:
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);
}
确保使用适当的内存区域注释所有指针.(在本例中为 global
)
Make sure to annotate all your pointers with the appropriate memory region. (global
in this case)
这篇关于如何从N个固定长度字符串的缓冲区处理opencl内核中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!