602
社区成员
发帖
与我相关
我的任务
分享
//程序所要实现的功能是:输入一个图片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);
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】对比:最普通的参数形式
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);也是正常运行的
/* 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);