【问题标题】:glFinish hanging forever after clEnqueueReleaseGLObjectsglFinish 在 clEnqueueReleaseGLObjects 之后永远挂起
【发布时间】:2016-07-22 05:14:29
【问题描述】:

以下代码在 Nvidia 驱动程序 361.91(及更早版本)下运行良好(Windows 7,Nvidia GTX 750 Ti),但在 364.72 和 368.69 等较新版本上运行良好。现在 glFinish 仅在调用 clEnqueueReleaseGLObjects 后才阻止程序的执行。在责怪驱动程序之前,我怀疑我的 OpenCL/OpenGL 互操作方式有问题,所以这里是重现问题的整个小程序的代码,问题在最后:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <SDL.h>
#include <gl/glew.h>
#include <SDL_opengl.h>
#include <gl/glut.h>

#pragma comment (lib, "C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v7.5\\lib\\x64\\OpenCL.lib")
#include <CL/cl.h>
#include <CL/cl_gl.h>

cl_int init_cl_context(cl_context *context, cl_command_queue *command_queue)
{
    cl_int i, ret, pf_index=-1;
    cl_platform_id  platform_id[16];
    cl_device_id    device_id[16];
    cl_uint     ret_num_platforms;
    cl_uint     ret_num_devices;

    ret = clGetPlatformIDs(sizeof(platform_id)/sizeof(*platform_id), platform_id, &ret_num_platforms);  // get all the platforms

    for (i=0; i<ret_num_platforms; i++)     // go through all the platforms
    {
        ret = clGetDeviceIDs(platform_id[i], CL_DEVICE_TYPE_GPU, sizeof(device_id)/sizeof(*device_id), device_id, &ret_num_devices);    // get all the suitable GPU devices

        if (ret_num_devices > 0)        // stop trying platforms when a suitable device is found
        {
            pf_index = i;
            break;
        }
    }

    cl_context_properties properties[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(), CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(), CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id[pf_index], 0 };
    *context = clCreateContextFromType(properties, CL_DEVICE_TYPE_GPU, NULL, NULL, &ret);
    *command_queue = clCreateCommandQueue(*context, device_id[0], 0*CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE | 0*CL_QUEUE_PROFILING_ENABLE, &ret);

    return ret;
}

int main(int argc, char *argv[])
{
    cl_int ret=0;
    int w = 800, h = 600;
    SDL_Window *window;
    SDL_Renderer *renderer;
    cl_context context;
    cl_command_queue command_queue;
    cl_mem cltex;           // CL buffer of type image_2d_t pointing to the GL texture
    uint32_t gltex;         // ID of the GL texture for cltex

    //**** Init SDL, OpenGL/glew ****
    SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_AUDIO);

    window = SDL_CreateWindow ("Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, SDL_WINDOW_OPENGL);
    SDL_GetWindowSize(window, &w, &h);

    SDL_GL_CreateContext(window);
    glewExperimental = 1; 
    glewInit();

    renderer = SDL_CreateRenderer(window, -1, 0*SDL_RENDERER_PRESENTVSYNC);
    //-------------------------------

    ret = init_cl_context(&context, &command_queue);    // initialise the CL context to match GL as to make the interop possible

    // create an OpenGL 2D texture normally
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &gltex);                                   // generate the texture ID
    glBindTexture(GL_TEXTURE_2D, gltex);                                // binding the texture
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);        // specify texture dimensions, format etc

    cltex = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, gltex, &ret);   // Creating the OpenCL image corresponding to the texture (once)

    ret = clFinish(command_queue);
    //glFinish();                  // this works fine
    ret = clEnqueueReleaseGLObjects(command_queue, 1, &cltex, 0, 0, NULL);          // release the ownership from CL back to GL
    clFinish(command_queue);
    glFlush();

    printf("This blocks the execution forever:\n");
    glFinish();                   // this blocks everything
    printf("This never gets printed\n");

    return 0;
}

在我更大的程序(有完全相同的问题)中,在驱动程序更新之前,一切都运行良好,现在甚至在更新之前编译的二进制文件也显示出与上述相同的冻结。为了便于阅读,我删除了对返回码的检查,但是在这个小程序和更大的程序中,根本没有报告任何问题。而且我看不出我可能做的任何明显错误...

【问题讨论】:

  • 作为旁注,glFinish 在技术上不能保证永远返回,除非之前调用 glFlush,保证在“有限时间内”执行。只有少数供应商足够笨拙,不会在每次完成时都进行隐式冲洗。错误的代码可能从此过上幸福的生活……
  • 我终于再次尝试了最新的驱动程序并添加了 glFlush() 但遗憾的是它仍然挂起。

标签: opengl opencl nvidia


【解决方案1】:

显然我的问题来自倒退,因为我在将要做的事情排入队列之前尝试从前一帧获取结果。

如果在第一帧我跳过该部分并直接将第一个任务排入队列,那么它就不会再阻塞了。

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多