【问题标题】:How to process string in opencl kernel from buffer of N fixed length strings?如何从 N 个固定长度字符串的缓冲区处理 opencl 内核中的字符串?
【发布时间】:2019-03-14 05:10:31
【问题描述】:

我需要在 OpenCL 设备上并行处理 N 个固定长度的字符串。

处理字符串涉及调用提供的函数,该函数将字符串作为输入表示为缓冲区,以及该缓冲区中字符串的长度。

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

在主机应用程序中,我将 N 个字符串连接到一个大的 char 缓冲区中,它们之间没有分隔符。

我想创建一个定义类似于

的内核
__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);
}

在所有内核中,只有一个会“成功”并写入输出缓冲区。

由于字符串长度不同,如何将 *global char buffer_of_strings 的子范围分配给 string_input 缓冲区?

我应该创建一个多维输入而不是一维数组吗?

【问题讨论】:

    标签: parallel-processing opencl


    【解决方案1】:

    你的问题不是100%清楚,所以在回答之前我会简要概述一下我对情况的理解:

    您有一个buffer_of_strings,其中包含N 字符串length_of_string 字节。这意味着每个字符串都从缓冲区的偏移量i * length_of_string 开始:

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

    所以这让我想到了这样的事情,使用简单的老式指针算法:

    __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

    【讨论】:

    • 谢谢,GPU 上的内存划分让我大吃一惊。 :D
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多