你如何从clEnqueueReadBuffer中读取多维数组?

时间:2013-07-01 13:46:37

标签: opencl

我有二维数组C,我实例化如下:

const int wA = 16;
float * C[wA];
for(int i = 0; i < hA; i++)
{
C[i] = new float[hA];
for(int i2 = 0; i2 < hA; i2++)
    C[i][i2] = 0;
}

/* looks like this:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
*/

我创建了一个在C:

上运行的内核
__kernel void simpleMultiply(__global float* outputC, 
                            int widthA, 
                            int heightA, 
                            int widthB, 
                            int heightB, 
                            __global float * inputA, 
                            __global float * inputB)
{
    int row = get_global_id(1);
    int col = get_global_id(0);
    float sum = 0.0f;
    for(int i = 0; i < widthA; i++)
    {
        sum += inputA[row*widthA+i] * inputB[i*widthB+col];
    }
    outputC[row*widthB+col] = sum;
}

一切都很好。从设置上下文到创建缓冲区,创建内核,程序,clEnqueueNDRangeKernel,clEnqueueReadBuffer等,我获得了CL_SUCCESS状态。

但是当我去阅读输出时它会崩溃。

status = clEnqueueNDRangeKernel(cmdQueue, kernel, 2, NULL, globalws, localws, 0, NULL, NULL);
cout << "\nclEnqueueNDRangeKernel: " << (status == CL_SUCCESS ? "SUCCESS" : "FAIL"); // prints SUCCESS
status = clEnqueueReadBuffer(cmdQueue, bufferC, CL_TRUE, 0, wC*hC*sizeof(float), (void*)C, 0, NULL, NULL);
cout << "\nclEnqueueReadBuffer: " << (status == CL_SUCCESS ? "SUCCESS" : "FAIL"); // prints SUCCESS
cout << "\nC[0][0]: " << C[0][0]; // <--crash

enter image description here

我和C ++一样陌生,因为我是OpenCL,所以这可能是由于对c ++中数组和指针的理解不足。

整个代码为here

1 个答案:

答案 0 :(得分:4)

数组

float * C[wA];

float *指针的一维数组。因此,您没有在内存中创建具有连续行和列的2D数组。但是你已经创建了一个指向行的指针数组。

所以你应该在主机数组C上压扁并以与在内核上相同的方式对其进行索引。

float * C;

c = new float[ha * ha]; // Create a  contiguous memory area  to be addressed  in a 2D pattern

memset( C, 0, ha * ha  * sizeof(float) ); // Set all bytes to zero


...

现在你可以在运行内核后解决C数组

cout << C[ icolumn  + irow * ha ];  // icolumn and irow are your row and columns indices