我有我这样实例化的二维数组 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
我对 C++ 和 OpenCL 一样陌生,所以这可能是由于对 C++ 中的数组和指针理解不足。
整个代码在这里