使用“字符串"在openCl内核中 [英] Using "String" in openCl Kernel

查看:125
本文介绍了使用“字符串"在openCl内核中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对OpenCl编程有疑问.场景是:我有一个列表,该列表取自具有不同长度的文件中的单词,并且我必须将此列表传递给OpenCl内核.我尝试使用由包含单词的char数组和包含大小的int数组构成的结构.但是这种解决方案不起作用,因为在内核中,我必须创建一个具有结构中指示的大小的新数组,但是内核不喜欢可变大小的数组.有一种方法可以实现此解决方案(我的意思是为每个不同大小的线程创建一个数组)?如果没有这种解决方案,我该怎么办? 谢谢:)

I have a question about OpenCl programming. The scenario is : I have a list of words taken from a file with different length and I have to pass this list to OpenCl Kernel. I've tried to use a struct compposed by an array of char that contains the word and an int that contains the size. But this kind of solution doesn't work because, in the Kernel, I must create a new array with the size indicated in the struct, but the Kernel doesn't like arrays of variable size. There is a way to implement this solution (I mean creating one array per thread of different size)? If there is no solution in this way, how can I do? Thank you :)

这是示例代码..我希望它可以弄清楚事情

EDIT : This is the example code.. I hope it clarify the things

typedef struct word{
    char word[100];
    int len;
}word_t;
__kernel void test(__global word_t *data,  __global res_t *result)
{
   size_t id=get_global_id(0);
   int size=0;
   size=data[id].len;
   char word[size];
   //working with the word

}

但是clBuildProgram表示我不能拥有大小可变的数组.

But the clBuildProgram says that I cannot have an array with variable size..

推荐答案

在OpenCL中,您不能使用像这样的可变长度数组,因此,如果可以限制字长,那么可以使用固定最大长度数组.

You can't use variable length arrays like this in OpenCL so you could use a fixed maximum length array if you can limit your word length as an easy solution.

#define MAX_WORD_LEN 100
#define WORK_GROUP_SIZE 128

typedef struct word{
    char word[MAX_WORD_LEN];
    int len;
}word_t;

__kernel void test(__global word_t *data,  __global res_t *result)
{
   size_t id=get_global_id(0);
   int size=0;
   size=data[id].len;
   __local char word[MAX_WORD_LEN * WORK_GROUP_SIZE];
   //working with the word
   __local char* pThreadWord = &word[ MAX_WORD_LEN * id];
}

我已将数组放在本地内存中,就像您执行char word[MAX_WORD_LENGTH]一样,几乎可以肯定会用完所有寄存器并溢出(即超级慢).

I've put the array in local memory as if you do char word[MAX_WORD_LENGTH] you will almost certainly use up all your registers and spill (i.e. super slow).

如果您必须处理非常长的可变长度单词,那么您将不得不为每个带有原子的线程动态地在本地内存中分配"内存

If you do have to cope with very long variable length words then you will have to dynamically "allocate" memory in your local memory for each thread with an atomic

__kernel void test(__global word_t *data,  __global res_t *result)
{
   size_t id=get_global_id(0);
   int size=0;
   int size=data[id].len;
   // local memory "allocator"
   __local int offset = 0;
   volatile __local int* pOffset = &offset;
   __local char wordBuffer[BUFFER_SIZE];
   // "allocate" memory
   int myOffset = atomic_add( pOffset, size );
   //working with the word
   __local char* pThreadWord = &wordBuffer[ myOffset ];
}

这篇关于使用“字符串"在openCl内核中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆