在opencl内核函数中声明的全局变量的副本数在全局地址空间中维护 [英] How many copies of a global variable declared inside an opencl kernel function is maintained in the global address space

查看:559
本文介绍了在opencl内核函数中声明的全局变量的副本数在全局地址空间中维护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Opencl编程的新手.为了更好地学习opencl,在花了一些时间阅读一些教程之后,我开始开发一个简单的模式匹配内核函数.但是我有一些疑问:

I'm new to Opencl programming. To learn opencl better, after spending some time reading some tutorials, I started developing a simple pattern matching kernel function. But I have some doubts:

首先,我在内核函数中声明了全局变量.这是否意味着每个工作项都共享每个变量的单个副本?

First, I have global variables declared inside the kernel function. Does it mean every work item shares a single copy of each variable?

第二,我该如何使用标准的C库,尤其是. "string.h".

Second, how can I use the standard C libraries, esp. "string.h".

   __kernel void matchPatterns_V1(__global char *strings, __global char *patterns, __global int *matchCount,
                            int strCount, int strLength, int patCount, int patLength) {


    int id = get_global_id(0);
    int rowIndex = id*strLength;
    int i, matches = 0;     

    __global char *pos = strings;
    __global char *temp = strings;
    __global char *pat = patterns;

    for(i = 0; i < patCount; i++)
    {
            temp = &strings[rowIndex];      
            pat = &patterns[i*patLength];
            while(pos != '\0') {
                    pos = StrStr(temp, pat);
                    if(pos != '\0') {
                            matches++;
                            temp = pos + patLength;
                    }
            }
    }
    matchCount[id] = matches;
    }

总而言之,每个工作项是否都有变量'pos','temp'和'pat'的副本?

To summarize, does each work item has its own copy of the variables 'pos', 'temp', and 'pat'?

在学习Opencl方面的任何建议都受到高度赞赏,其中包括对最佳书籍/教程站点的建议.

Any advice in learning Opencl is highly appreciated, including suggestion for best books/tutorial sites.

推荐答案

不,它在全局内存空间中,因此,通常每个内核调用只有一个副本,所有工作项都可以共享.如果您不能保证每个工作组在全局内存中都有自己唯一的项目",则写入全局内存是危险的;或者,更广泛地说,内核中没有两个工作项将同时写入内存中的同一位置)因为会有比赛条件.

No, it's in global memory space so there is, in general, a single copy per kernel invocation, to be shared by all work items. Writing to global memory is dangerous if you cannot guarantee each work group has its own unique "item" in global memory - or, more generally, that no two work items in your kernel will write to the same location in memory at the same time) because there will be race conditions.

如果您只是从这些全局内存变量中读取数据,那没关系.

If you are simply reading data from those global memory variables, it doesn't matter, of course.

您还不能在内核中声明__global变量,因为它们只能是内核自变量.如果尝试这样做,您将从OpenCL编译器中获得以下信息:

You also cannot declare __global variables inside your kernel as they can only be kernel arguments. If you attempt to do so, you will get the following from the OpenCL compiler:

error: global variables cannot be allocated inside kernel code

有充分的理由,除非有技术上的可能性,否则:全局变量将毫无用处……我什至能想到的唯一可能的原因是工作项之间的交流,这将是一种疯狂的设计模式.

For good reason, barring technical impossibilities: a global variable would serve no purpose at all... the only possible reason I can even think of would be communicating between work items, which would be an insane design pattern.

对于您的问题,请在注释中查阅OpenCL规范6.5节中的以下代码段:

As for your question, in comments, consult this snippet in the OpenCL specification, section 6.5:

// declares a pointer p in the __private address space that
// points to an int object in address space __global
__global int *p;

因此,与指针类型相关联的内存空间表示它们指向的变量的内存空间,而不是指针本身,指针始终为__private(即每个工作项).

So the memory space associated with pointer types represents the memory space of the variable they point to, not the pointers themselves, which are always __private (i.e. per work item).

您无法使用OpenCL中标准C库中的字符串操作函数,尽管您可以根据需要重新编码它们以便在GPU上使用(大多数很难实现).

You cannot use string manipulation functions from the standard C library in OpenCL, though you can recode them for use on the GPU if you want to (most of them are hardly difficult to implement).

这篇关于在opencl内核函数中声明的全局变量的副本数在全局地址空间中维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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