我目前正在尝试编写一个 OpenCL 应用程序来执行一些内存密集型计算。为了跟踪所有计算的进度,我创建了一个 for 循环,它创建了不同的内核组。不幸的是,计算填满了我的整个内存。我的猜测是内核是在添加下一个堆之前未完成执行。
for (unsigned long i=1; i<maxGlobalThreads; i+=1000000) {
// Calculating Offset
size_t temp = 1000000;
size_t offset = 0;
if (i>1000000) {
offset = i-1000000;
}
cl_event tmp;
clEnqueueNDRangeKernel(command_queue, kernel, 1, &offset, &temp, NULL, NULL, 0, &tmp);
// Wait until Threads finished (-- not working)
clWaitForEvents(1, &tmp);
// Copy results from memory buffer
int *res = (int*)malloc(64*sizeof(int));
int *indexNum = (int*)malloc(14*sizeof(int));
err = clEnqueueReadBuffer(command_queue, foundCombiMem, CL_TRUE, 0, 64*sizeof(int), res, 0, NULL, NULL);
err = clEnqueueReadBuffer(command_queue, indexNumMem, CL_TRUE, 0, 14*sizeof(int), indexNum, 0, NULL, NULL);
// Calculate Time for 1000000 checked combinations
diff = clock() - start;
double msec = diff * 1000 / CLOCKS_PER_SEC;
printf("%f\n", (msec/(i*1000000))*1000000);
[ ... ]
}
最佳答案
您正在对循环的每次迭代中永远不会释放的 malloc 执行操作。这就是内存不足的原因。
此外,您的循环使用的是 unsigned int 变量,这可能是一个问题,具体取决于 maxGloablThreads 的值。
关于c++ - clEnqueueNDRangeKernel 填满整个内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925417/