关于OpenCL基础的相关问题:关于图片处理的内存分配等问题clEnqueueReadImage clEnqueueWriteBufferRect..

嘤鸣求友 2015-07-22 08:45:02
2015年7月22日 关于图片处理的内存分配等问题clEnqueueReadImage clEnqueueWriteBufferRect..

最近针对图像处理做了大量的学习,遇到不少问题,故总结在此请求各位前辈的指点


//程序所要实现的功能是:输入一个图片d_inputImage,通过d_filter进行卷积(7*7,半径3),得到图片d_outputImage

size_t buffer_origin[3] = {0,0,0};

size_t host_origin[3] = {0,0,0};

size_t region[3] = {deviceWidth*sizeof(float), //【疑问1】这是什么情况,为什么第一个要乘以4(float)?后面就不乘了?

imageHeight, 1};

clEnqueueWriteBufferRect(

queue,

d_inputImage, CL_TRUE,

buffer_origin,

host_origin,

region, //

deviceWidth*sizeof(float), //

0, //

imageWidth*sizeof(float),

0,

inputImage, 0, NULL, NULL);



……



// Begin reading output from (3,3) on the device

// (for 7x7 filter with radius 3) 因为半径是3,所以从3开始读取数据

buffer_origin[0] = 3*sizeof(float);//【与疑问1一样的问题】

buffer_origin[1] = 3;

buffer_origin[2] = 0;



// Read data into (3,3) on the host

host_origin[0] = 3*sizeof(float);//【与疑问1一样的问题】

host_origin[1] = 3;

host_origin[2] = 0;



// Region is image size minus padding pixels

region[0] = (imageWidth-paddingPixels)*sizeof(float);//【与疑问1一样的问题】

region[1] = (imageHeight-paddingPixels);

region[2] = 1;



// Perform the read

clEnqueueReadBufferRect(queue, d_outputImage, CL_TRUE,

buffer_origin, host_origin, region,

deviceWidth*sizeof(float), 0, imageWidth*sizeof(float), 0,

outputImage, 0, NULL, NULL);




疑问2 :下面对于最基本的clBuildProgram()有了疑问,原因在于我参考了3本书书籍,分别使用了png.h bmpfuncs.h FreeImage.lib三种方式读取图片,可能出现了差异。

这是我们最常见的基本编译程序代码:使用的是FreeImage.lib来源《OpenCL编程指南》8.4节高斯滤波 以及15章 sobel边缘检测



cl_int errNum;

cl_program program;



std::ifstream kernelFile(fileName, std::ios::in);

std::ostringstream oss;

oss << kernelFile.rdbuf();



std::string srcStdStr = oss.str();

const char *srcStr = srcStdStr.c_str();

program = clCreateProgramWithSource(context, 1,(const char**)&srcStr,

NULL, NULL);

errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);//【疑问2】对比:最普通的参数形式



下面是使用bmpfuncs.h读取图片,来源:OpenCL异构计算(2版) 4章 8章 综合 图像卷积



const char* source = readSource("convolution_hC4.cl");



// Create a program object with source and build it

cl_program program;

program = clCreateProgramWithSource(context, 1, &source, NULL, NULL);

status = clBuildProgram(program, 1, &device, NULL, NULL, NULL);//【疑问2】对比:多了一些东东然而</P>

//修改成clBuildProgram(program, 0, NULL, NULL, NULL, NULL);也是正常运行的


下面使用png.h读取图片,镜像图像缩放,来源:OpenCL实战 6.3节


/* Create program from file */

program = clCreateProgramWithSource(ctx, 1,

free(program_buffer);



/* Create build argument */

sprintf(arg, "-DSCALE=%u", SCALE_FACTOR);//这里是编译参数,然而SCALE设置为3不知道原因是什么?



/* Build program */

err = clBuildProgram(program, 0, NULL, arg, NULL, NULL);//【疑问2】对比:出现了arg

……

//【疑问3】另外在图像读取部分/* Read the image object */
origin[0] = 0; origin[1] = 0; origin[2] = 0;//【疑问3】#define SCALE_FACTOR 3 这里乘以3的目的是什么?
region[0] = SCALE_FACTOR * width;
region[1] = SCALE_FACTOR * height; region[2] = 1;
err = clEnqueueReadImage(queue, output_image, CL_TRUE, origin,
region, 0, 0, (void*)output_pixels, 0, NULL, NULL);

请各位前辈指点一二

GPU论坛的此贴链接:
http://bbs.gpuworld.cn/forum.php?mod=viewthread&tid=10019&page=1&extra=#pid16652

...全文
2327 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
fronteer 2015-08-17
  • 打赏
  • 举报
回复
对 2DImage 或 3DImage 类型的 Memory object, 最好别去设想它的数据格式和尺寸,按文档规定的 API 来初始化数据,并按规定的 kernel API 来读写数据就行了

602

社区成员

发帖
与我相关
我的任务
社区描述
异构开发技术
社区管理员
  • OpenCL和异构编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧