以下代碼不使用任何回調函數或clWaitForEvents
,但它完美地工作。但我認爲clEnqueueNDRangeKernel是非阻塞的。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());
}
如果您使用類型爲「in-order」的隊列並且只存在一個隊列,則非阻塞但順序同步。 –