OpenCL:在循环调用内核多次后,从设备到主机内存传输获取INVALID_COMMAND_QUEUE

时间:2012-10-28 07:19:25

标签: c osx-snow-leopard opencl nvidia

我正在开发一个OpenCL程序,它在循环中多次调用同一个内核。当我使用clEnqueueReadBuffer将设备内存传回主机时,它会报告命令队列无效。

下面是一个被调用以启动bitonic排序的函数,它被缩短以使其更具可读性。设备列表,上下文,命令队列和内核在外部创建并传递给此函数。 list 包含要排序的列表, size list 中的元素数。

cl_int OpenCLBitonicSort(cl_device_id device, cl_context context, 
    cl_command_queue commandQueue, cl_kernel bitonicSortKernel, 
    unsigned int * list, unsigned int size){

    //create OpenCL specific variables
    cl_int error = CL_SUCCESS;
    size_t maximum_local_ws;
    size_t local_ws;
    size_t global_ws;

    //create variables that keep track of bitonic sorting progress
    unsigned int stage = 0;
    unsigned int subStage;
    unsigned int numberOfStages = 0;

    //get maximum work group size
    clGetKernelWorkGroupInfo(bitonicSortKernel, device, 
        CL_KERNEL_WORK_GROUP_SIZE, sizeof(maximum_local_ws), 
         &maximum_local_ws, NULL);

    //make local_ws the largest perfect square allowed by OpenCL
    for(i = 1; i <= maximum_local_ws; i *= 2){
        local_ws = (size_t) i;
    }
    //total number of comparators will be half the items in the list
    global_ws = (size_t) size/2;

    //transfer list to the device
    cl_mem list_d = clCreateBuffer(context, CL_MEM_COPY_HOST_PTR, 
        size * sizeof(unsigned int), list, &error);

    //find the number of stages needed (numberOfStages = ln(size))
    for(numberOfStages = 0; (1 << numberOfStages ^ size); numberOfStages++){
    }

    //loop through all stages
    for(stage = 0; stage < numberOfStages; stage++){
        //loop through all substages in each stage
        for(subStage = stage, i = 0; i <= stage; subStage--, i++){
            //add kernel parameters
            error = clSetKernelArg(bitonicSortKernel, 0, 
                sizeof(cl_mem), &list_d);
            error = clSetKernelArg(bitonicSortKernel, 1, 
                sizeof(unsigned int), &size);
            error = clSetKernelArg(bitonicSortKernel, 2, 
                sizeof(unsigned int), &stage);
            error = clSetKernelArg(bitonicSortKernel, 3, 
                sizeof(unsigned int), &subStage);

            //call the kernel
            error = clEnqueueNDRangeKernel(commandQueue, bitonicSortKernel, 1, 
                NULL, &global_ws, &local_ws, 0, NULL, NULL);

            //wait for the kernel to stop executing
            error = clEnqueueBarrier(commandQueue);
        }
    }

    //read the result back to the host
    error = clEnqueueReadBuffer(commandQueue, list_d, CL_TRUE, 0,  
        size * sizeof(unsigned int), list, 0, NULL, NULL);

    //free the list on the device
    clReleaseMemObject(list_d);

    return error;
}

在此代码中:clEnqueueReadBuffer表示commandQueue无效。但是当我调用clEnqueueNDRangeKernel和clEnqueueBarrier时它是有效的。

当我将 numberOfStages 设置为1而 stage 只是为0时,clEnqueueNDRangeKernel只调用一次,代码工作时没有返回错误(虽然结果不正确)。调用clEnqueueNDRangeKernel多次出现问题(我真的需要这样做)。

我使用的是Mac OS 10.6 Snow Leopard,而我正在使用Apple的OpenCL 1.0平台和NVidia GeForce 9600m。在其他平台上的OpenCL中是否可以在循环内运行内核? OS X上的OpenCL有没有这样的问题?什么可能导致命令队列无效?

2 个答案:

答案 0 :(得分:0)

可能有多种原因,如内核中的(全局或本地)内存溢出。

global_ws也应该是local_ws的倍数。

查看SortingNetworks示例 http://developer.download.nvidia.com/compute/cuda/3_0/sdk/website/OpenCL/website/samples.html

答案 1 :(得分:0)

回答您的一个问题:是的,您可以将任意数量的内核排入命令队列(无论是来自循环内还是其他内容)。我可以确认这在Windows上的至少NVIDIA,AMD和Intel驱动程序上的工作正常。