【发布时间】:2014-09-22 08:37:43
【问题描述】:
我有一个缓冲区创建设备端,我想初始化这个缓冲区中的值。 OpenCL 1.2 为此类操作提供了函数 clEnqueueFillBuffer (http://www.khronos.org/registry/cl/sdk/1.2/docs/man/xhtml/clEnqueueFillBuffer.html)。
调用此函数未能正确初始化我的内存,但未返回任何错误代码提示该函数失败。
这是我使用的代码:
int initGridSize = 64*64*64; // initial 3D volume size
cl_uint initVoxelValue = 255; // initial value of each voxel in the volume
// create the buffer on the device
cl_mem buff = clCreateBuffer(context, CL_MEM_READ_ONLY, initGridSize*sizeof(cl_uint), NULL, &err);
if(err < 0) {
perror("Couldn't create a buffer object");
exit(1);
}
// Fill the buffer with the initial value
err = clEnqueueFillBuffer(queue, buff, &initVoxelValue, sizeof(cl_uint), 0, initGridSize*sizeof(cl_uint), 0, NULL, NULL);
if(err != CL_SUCCESS) {
perror("Couldn't fill a buffer object");
exit(1);
}
clFinish(queue);
// create a host side buffer and read the device side buffer into the host buffer
cl_uint *grid = new cl_uint [ initGridSize ];
err = clEnqueueReadBuffer(queue, buff, CL_TRUE, 0, initGridSize*sizeof(cl_int), &grid[0], 0, NULL, NULL);
if (err != CL_SUCCESS)
{
perror("Couldn't read a buffer object");
exit(1);
}
// print the first 11 values in the buffer
cl_uint *g = grid;
for( int i=0; i<11; i++, g++ )
{
printf("Voxel %i, %u\n", i, *g );
}
delete [] grid;
输出是这样的:
Voxel 0, 0
Voxel 1, 0
Voxel 2, 0
Voxel 3, 0
Voxel 4, 0
Voxel 5, 0
Voxel 6, 0
Voxel 7, 0
Voxel 8, 0
Voxel 9, 0
Voxel 10, 0
编写一个内核来填充缓冲区很简单,但理想情况下我想让它工作。
谁能看到我哪里出错了,或者这是驱动程序错误?
【问题讨论】:
-
碰巧,在你的系统中int是32位的?因为如果是16bits的话,64*64*64会导致4次溢出,最终结果为0。
-
您的设备是否符合 OpenCL 1.2 标准?
-
我的系统是 32 位的,我很确定它没有溢出,我设置了断点并且 initGridSize 是正确的,initGridSize*sizeof(cl_uint) 也是正确的。我使用的是 2013 年中期的 iMac,我的系统报告说 OpenCL 1.2 也完全受支持...
CL_PLATFORM_PROFILE: FULL_PROFILE CL_PLATFORM_VERSION: OpenCL 1.2 (Apr 25 2014 22:04:25)