作为初学者一直,经过多次的上网搜索你一定会看到迈克老狼的向量加法的示例,不知道你是否和我一样,刚开始并不是很准确的知道他的add.cl写的代码的意思,源码如下:

#pragma OPENCL EXTENSION cl_amd_printf : enable
__kernel void vecadd(__global const float* a, __global const float* b, __global float* c){int x = get_global_id(0);int y = get_global_id(1);int width = get_global_size(0);int height = get_global_size(1);if(x == 1 && y ==1)printf("%d, %d,%d,%d,%d,%d\n",get_local_size(0),get_local_size(1),get_local_id(0),get_local_id(1),get_group_id(0),get_group_id(1));c[x + y * width] = a[x + y * width] + b[x + y * width];}我们在k

这里面我把二维的这些值全部罗列出来如下:

int idx = get_global_id(0);
int idy = get_global_id(1);

uint wiWidth = get_global_size(0);
uint wiHeight = get_global_size(1);

uint gix_t = get_group_id(0);
uint giy_t = get_group_id(1);

uint num_of_blocks_x = get_num_groups(0);
uint num_of_blocks_y = get_num_groups(1);

uint lix = get_local_id(0);
uint liy = get_local_id(1);

你会奇怪为什么他的add.cl优化会这么写:

c[x + y * width] = a[x + y * width] + b[x + y * width];

下面进行分析:

add.cpp关键的代码部分如下(只罗列出部分代码):

#define width 8//256
#define height 16//256
#define NWITEMS  width*height//262144size_t globalThreads[] = {width, height};size_t localx, localy;localx = 2;localy = 4;size_t localThreads[] = {localx, localy};clEnqueueNDRangeKernel( queue,kernel,2,NULL,globalThreads,localThreads, 0, NULL, &ev);

我自己的add.cl代码如下(里面添加啦打印,以便我们可以看出结果):

   int idx = get_global_id(0);int idy = get_global_id(1);uint wiWidth  = get_global_size(0);uint wiHeight  = get_global_size(1);printf("Magnum Global idx = %d, idy = %d, sizeX =%d,sizeY =%d\n",idx,idy,wiWidth,wiHeight);uint gix_t = get_group_id(0); uint giy_t = get_group_id(1);    uint num_of_blocks_x = get_num_groups(0); uint num_of_blocks_y = get_num_groups(1); printf("Magnum Group idx = %d, idy = %d, blockX=%d,blockY=%d\n",gix_t,gix_t,num_of_blocks_x,num_of_blocks_y);
uint lix = get_local_id(0); uint liy = get_local_id(1);uint LocalX = get_local_size(0);uint LocalY = get_local_size(1);printf("Magnum Local idx = %d, idy = %d, localX=%d,localY=%d\n\n",lix,liy,LocalX,LocalY);

输出的结果如下:

Magnum Global idx = 0, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 1, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 0, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 1, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 0, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 1, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 0, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 1, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 2, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 3, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 2, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 3, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 2, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 3, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 2, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 3, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 4, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 5, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 4, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 5, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 4, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 5, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 4, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 5, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 6, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 7, idy = 0, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 6, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 7, idy = 1, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 6, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 7, idy = 2, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 6, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 7, idy = 3, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 0, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 1, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 0, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 1, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 0, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 1, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 0, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 1, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 2, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 3, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 2, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 3, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 2, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 3, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 2, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 3, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 4, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 5, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 4, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 5, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 4, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 5, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 4, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 5, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 6, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 7, idy = 4, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 6, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 7, idy = 5, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 6, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 7, idy = 6, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 6, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 7, idy = 7, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 0, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 1, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 0, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 1, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 0, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 1, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 0, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 1, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 2, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 3, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 2, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 3, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 2, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 3, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 2, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 3, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 4, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 5, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 4, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 5, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 4, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 5, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 4, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 5, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 6, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 7, idy = 8, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 6, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 7, idy = 9, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 6, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 7, idy = 10, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 6, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 7, idy = 11, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 0, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 1, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 0, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 1, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 0, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 1, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 0, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 1, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 0, idy = 0, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 2, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 3, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 2, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 3, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 2, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 3, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 2, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 3, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 1, idy = 1, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 4, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 5, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 4, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 5, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 4, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 5, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 4, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 5, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 2, idy = 2, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4Magnum Global idx = 6, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 0, localX=2,localY=4Magnum Global idx = 7, idy = 12, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 0, localX=2,localY=4Magnum Global idx = 6, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 1, localX=2,localY=4Magnum Global idx = 7, idy = 13, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 1, localX=2,localY=4Magnum Global idx = 6, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 2, localX=2,localY=4Magnum Global idx = 7, idy = 14, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 2, localX=2,localY=4Magnum Global idx = 6, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 0, idy = 3, localX=2,localY=4Magnum Global idx = 7, idy = 15, sizeX =8,sizeY =16
Magnum Group idx = 3, idy = 3, blockX=4,blockY=4
Magnum Local idx = 1, idy = 3, localX=2,localY=4

View Code

从从这个结果首先能说明几个问题:

首先我们在:

  1. clEnqueueNDRangeKernel( queue, kernel, 2, NULL, globalThreads, localThreads, 0, NULL, &ev); 设置维度是2,否则add.cl中所有类似get_local_size(1)将只会返回一。
  2. 设置GlobaThreads 和 LocalThreads 不一定是 GlobalThreads= {8, 8}; 也可以不相等,LocalThreads也是一样。
  3. 理解opencl中的内存分布可以理解为二维数组,而且内存寻址的方向就像是:左上角(0,0)为原点,一行一行的扫描下去。
  4. WorkGroup的大小,目前我认为X,Y可以分开来理解(后面若发现错误我会及时改正) WorkGroup number(x) = GlobalThread(x)/LocalThread(x).
  5. 最小的单位是LocalX * LocalY, 然后用总的GlobalThreads按照这个单位进行分割成一个一个的组。每个二维的小单位还是一行一行的扫描过去的。目前我们的这个是8*16,你会发现总的Thread也是8*16,对应的每一个线程执行一次加法操作,有人会问可不可以不用这么多呢,用4*16个Threads,每个线程里面做两次加法动作,其实是可以的,这个原则是根据你的具体的Device来说的,当前我们说的是Device是GPU,比如你的GPU当前支持最大的线程数是1024*1024,这种情况,你不用多的,而是用少的线程来做,会降低你的效率,你需要尽可能的让Device满负荷工作这样才能达到提高运算速度。

继续分析为什么 NDRange改成二维的之后,add.cl变成:

c[x + y * width] = a[x + y * width] + b[x + y * width];

因为在CPU端每个 C[8*16],通过打印可以在这里C[x+y*width],x 最大是7,width是8,y最大是17,只有这样才能做完所有的向量相加。

使用二维NDRange workgroup相关推荐

  1. OpenCL 学习step by step (5) 使用二维NDRange workgroup

    http://www.cnblogs.com/mikewolf2002/archive/2012/09/07/2675634.html 在本教程中,我们使用二维NDRange来设置workgroup, ...

  2. Wpf DataGrid 绑定Dynamic (二维数据表)

    最近有一个需求,需要根据用户数据动态生成二维数据表,绑定到表格后,用户再次编辑数据,最后再将编辑过的数据,生成类型数据,存入数据库. 仔细分析过后,觉得最适合的方式,莫过于给DataGrid绑定Dyn ...

  3. 条形码?二维码?生成、解析都在这里!

    二维码生成与解析 一.生成二维码 二.解析二维码 三.生成一维码 四.全部的代码 五.pom依赖 直接上代码: 一.生成二维码 public class demo {private static fi ...

  4. OpenCV 笔记(08)— 二维点、三维点、基于 Mat 的 std::vector 等常用数据结构的定义和输出

    1. 定义和输出二维点 Point2f p2(3, 4);cout << "[二维点] is "<< endl << p2 << e ...

  5. Java IDEA Debug进制二维数组

    1.Debug模式 1.1 什么是Debug模式 是供程序员使用的程序调试工具,它可以用于查看程序的执行流程,也可以用于追踪程序执行过程来调试程序. 1.2 Debug介绍与操作流程 如何加断点 选择 ...

  6. python3生成二维码中间带logo,有底图,可自定义文字

    效果: qrcode_result.png 代码: #!/user/bin/Python3 """ @Lanson @2019-11-02 ""&qu ...

  7. Python数据挖掘1:创建一位数组和二维数组,取最大最小值,切片

    ''' 来源:天善智能韦玮老师课堂笔记 1.numpy 可以高效处理数据.提供数组支持.很多模块都依赖他,比如pandas.scipy.matplotlib都依赖他,所以这个模块是基础. 2.pand ...

  8. Asp.Net Core在线生成二维码

    前言: 原先用zxing Code写过基于Winfrom的批量生成二维码工具,以及单个生成二维码工具:批量生成二维码Gihub源代码 今天尝试用QRCoder 加 Asp.Net Core 写了一个在 ...

  9. Numpy 生成 Bool型数组、一维转多维数组reshape、多维转一维数组、替换数组元素、提取数组元素、数组交集、差集、过滤数组元素、二维数组反转行、交换数组维度

    1. 创建一个 [3,5] 所有元素为 True 的数组 In [162]: b = np.ones((3,5), dtype=bool)In [163]: b Out[163]: array([[ ...

最新文章

  1. 【青少年编程(第24周)】Python-Turtle组队学习结营!
  2. hdu5955 Guessing the Dice Roll【AC自动机】【高斯消元】【概率】
  3. Kubernetes架构为什么是这样的?
  4. BZOJ#3786. 星系探索(平衡树,fhq-treap,弱化版ETT)
  5. JavaScript玩转机器学习:模型转换
  6. java的继承啥考点,java中到底什么是继承?
  7. 深入剖析Android音频(三)AudioPolicyService
  8. Linux使用/proc/stat计算CPU使用率
  9. android硬编码封装mp4,音频编解码(软/硬编码),音频转码
  10. 运动控制第一篇之直流电动机建模
  11. Xcode创建头文件
  12. ADKAR模型简介(转)
  13. 山石sg6000e1700配置手册_Hillstone SG-6000安装手册.pdf
  14. java 树什么意思是什么意思是什么_Java数据结构和算法 - 什么是2-3-4树
  15. 工业循环水过滤浅层介质过滤器(浅层砂过滤器)介绍
  16. DBeaver-Driver-All ( DBeaver驱动包,所有JDBC驱动整合包)
  17. CS61C Spring 2021笔记
  18. 爱在深秋-稻城亚丁旅游途中的风花雪月
  19. PHP preg_match()函数
  20. Android 实践:做一款新闻 APP

热门文章

  1. VB如何读取快捷方式的目标路径
  2. 第3章 FOR命令中的变量
  3. 不被大神Hinton认同,否定现有同行成果,谷歌这篇烧脑研究最终拿下ICML2019最佳论文...
  4. 16篇论文、70多页PPT帮你优化深度学习模型,免费下载 | 资源
  5. 活动报名 | DEF CON之后,最令人期待的网络安全盛会(内附赠票)
  6. 特别看好高校团队的联想创投,在中科大拉开高校AI精英挑战赛大幕
  7. lvs + keepalived + nginx 实现高可用
  8. 有效提升 Virtual Server 2005 R2 with SP1 的速度
  9. 事件管理决定一个网管软件的成功与失败
  10. 思杰彻底简化浏览器应用的安全交付