[英]How to copy an image from global memory to local memory in openCL
我有两个输入图像,这些图像是我从Host传递到内核的。 我的图片尺寸为370x427。
我想让自己熟悉本地内存,因此我也将本地映像传递给内核,并尝试将全局映像复制到本地。
我将图像作为1D数组发送。当我尝试显示结果不起作用时。我的全局工作尺寸为{width*height}
并且为本地大小clEnqueueNDRangeKernel
传递了null
,假设opencl将为本地内存选择适当的大小。
下面是我的内核代码。
请有人告诉我。
__kernel void computeSSD(__global unsigned char *imgL,__global unsigned char *imgR,__global unsigned char *result,__global unsigned char *diff,int width,int MAX_DISP,int WIN_SIZE,__local unsigned char *localLeft,__local unsigned char *localRight )
{
int xCord=get_global_id(0);
int yCord=get_local_id(0);
// copy both images to local memory
localRight[yCord]= imgR[yCord];
localLeft[yCord] = imgL[yCord];
barrier(CLK_LOCAL_MEM_FENCE);
// do operation on local images
result[xCord]=localRight[yCord];
//
}
如果要使用3x3滤镜过滤图像。 每个工作组的每一侧都需要工作组像素+1边距。
因此您的内核可以是这样的:
__kernel filter(...){
int x_start = get_group_id(0)*get_local_size(0)-1;
int y_start = get_group_id(1)*get_local_size(1)-1;
int x_end = (get_group_id(0)+1)*get_local_size(0)+1;
int y_end = (get_group_id(1)+1)*get_local_size(1)+1;
__local mytype l[18][18]; //just an example for work sizes 16x16!
//Fetch
//Normally a direct operation per work item is preferred, since it is simpler and the driver will pack all the memory accesses together.
//So just follow coalesced access
//Using CL async workgroup copy has a complex sintax
for(int j=y_start+get_local_id(1); j<y_end; j+=get_local_size(1) ){
for(int i=x_start+get_local_id(0); i<x_end; i+=get_local_size(0) ){
l[j-y_start][i-x_start] = global[j][i];
}
}
barrier(CLK_GLOBAL_MEM);
//Use the memory for your filtering! (remember to substract y_start & x_start from your original indexes)
//....
}
本地内存不是“是的,让所有都复制到本地,然后使用它,这样会更快”。 它只是复制组所需的区域,然后在本地重用这些读取,因此避免了从全局读取大量数据(即:3x3过滤器,从全局读取8个冗余读取,从而将内存需求减少至1/9)。
另外,如果您不真正在工作项之间重用读取,则本地内存可能会变慢。 如果每个项目只读取和写入一个内存位置,则本地内存会使内核变慢,而不是变快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.