以下代码不使用任何回调,或者clWaitForEvents
它可以完美运行。但我认为 clEnqueueNDRangeKernel 是非阻塞的。
void CL::executeApp1()
{
cl_int status = 0;
const int d1Size = 1024000;
int* myInt = new int[d1Size];
cl_mem mem1 = clCreateBuffer(context, 0, sizeof(int)*d1Size, NULL, &status);
status = clEnqueueWriteBuffer(queue, mem1, CL_TRUE, 0, sizeof(int)*d1Size, myInt, 0, NULL, NULL);
status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &mem1);
size_t global[] = {d1Size};
cl_event execute;
status = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global, NULL, 0, NULL, &execute);
//clWaitForEvents(1, &execute);
status = clEnqueueReadBuffer(queue, mem1, CL_FALSE, 0, sizeof(int)*d1Size, myInt, 0, NULL, NULL);
string s = "";
for(int i = 0; i < d1Size; i++)
{
s += to_string(myInt[i]);
s += " ";
}
result = (char*)malloc(sizeof(char)*s.length());
strcpy(result, s.c_str());
}