clEnqueueNDRangeKernel 填满整个内存

clEnqueueNDRangeKernel fills up entire memory

我目前正在尝试编写一个 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 的值。

在这种情况下,您最好的办法是添加

clFinish(CommandQueue);

之后
clEnqueueNDRangeKernel