OpenCL的NDRange中globalSize,globalSize参数理解及调试笔记



先上源码:

    const size_t globalSize[2] = {6,4} ;
    const size_t localSize[2] = {3,2} ;
    const size_t offset[2] = {3,5} ;
    size_t dim = 2 ;
    errNum = clEnqueueNDRangeKernel(commandQueue, kernel, dim, offset,
                                        globalSize, localSize,
                                        0, NULL, NULL);


kernel是:


kernel void id_check(global float *output)
{
	int global_id_0 = get_global_id(0);
	int global_id_1 = get_global_id(1);
	printf("STEP_GLOBAL_ID global_id_0:[%d] global_id_1:[%d]\n",global_id_0,global_id_1);
	
	
	int global_size_0 = get_global_size(0);
	int global_size_1 = get_global_size(1);
	printf("STEP_GLOBAL_SIZE global_size_0:[%d] global_size_1:[%d]\n",global_size_0,global_size_1);
	
	int offset_0 = get_global_offset(0);
	int offset_1 = get_global_offset(1);
	printf("STEP_GLOBAL_OFFSET offset_0:[%d] offset_1:[%d]\n",offset_0,offset_1);
	
	int local_id_0 = get_local_id(0);
	int local_id_1 = get_local_id(1);
	printf("STEP_GLOBAL_LOCAL local_id_0:[%d] local_id_1:[%d]\n",local_id_0,local_id_1);
	
	int index_0 = global_id_0 - offset_0 ;
	int index_1 = global_id_1 - offset_1 ;
	
	int index = index_1 * global_size_0 + index_0 ;
	printf("STEP_GLOBAL_INDEX index_0:[%d] index_1:[%d] index:[%d]\n",index_0,index_1,index);
	
	float f = global_id_0 * 10.0f + global_id_1 * 1.0f ;
	f += local_id_0 * 0.1f + local_id_1 * 0.01f ;
	output[index] = f ;
	printf("STEP_GLOBAL_FINAL f:[%f] output:[%f]\n",f,output[index]);

}


分析结果:

    总共的计算次数为: 6*4 = 24 ;

    local size 是 global size 的一个 ,虚拟区域分区。   get_global_id(0) = get_group_id(0) * get_group_size(0) + get_local_id(0) + offset ;

    kernel中的打印,绝大多数 pc上有,但是 可能有的板子没有这种功能。

    offset 是 global id 的偏移, 可能偏移大于 size ,如果大于的话,直接向后偏移即可,size还是不变。具体可以调试本代码。





你可能感兴趣的:(机器视觉)