我有一个奇怪的问题。我刚开始使用 openCL 开发并实现了一个简单的矩阵乘法:
void MatrixMult(float *M, float *N, float *P, int width) {
cl_context context = core.getContext();
cl_command_queue commandQueue = core.getCommandQueue();
// create the kernel
cl_kernel kernel = core.createKernel("MatrixMultKernel");
// reserve memory for i/o
int size = width * width * sizeof(float);
cl_mem Md = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, size, M, NULL);
cl_mem Nd = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, size, N, NULL);
cl_mem Pd = clCreateBuffer(context, CL_MEM_WRITE_ONLY , size, NULL, NULL);
// define kernel args
clSetKernelArg(kernel, 0, sizeof(cl_mem), &Md);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &Nd);
clSetKernelArg(kernel, 2, sizeof(cl_mem), &Pd);
clSetKernelArg(kernel, 3, sizeof(int), &width);
// create kernel instances
size_t globalSize[] = {static_cast<size_t>(width), static_cast<size_t>(width)};
cl_int kernelError = clEnqueueNDRangeKernel(commandQueue, kernel, 2, NULL, globalSize, NULL, 0, NULL, NULL);
if (kernelError != CL_SUCCESS) {
NSLog(@"Error executing kernel");
}
// read back results
clEnqueueReadBuffer(commandQueue, Pd, false, 0, size, P, 0, NULL, &readEvent);
clSetEventCallback(readEvent, CL_COMPLETE, &eventCallback, NULL);
}
我的问题是永远不会调用回调。我用应该立即完成的小输入进行测试。
奇怪的是,每当我第二次调用函数(MatrixMult)时,回调应该会触发。每次连续通话都一样。当我在未触发时检查事件时,它始终具有状态 CL_SUBMITTED。这里发生了什么?