【问题标题】:Why my CUDA application isn't starting?为什么我的 CUDA 应用程序没有启动?
【发布时间】:2014-05-20 10:52:58
【问题描述】:

下面的代码编译没有任何错误,但是当我运行它时,它说“应用程序无法正确启动(0xc000007b)。单击确定关闭应用程序。”。

#include <math.h>
#include <GL\glew.h>
#include <GL\glut.h>
#include <cuda_gl_interop.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>

GLuint vbo;
struct cudaGraphicsResource* vbo_cuda;

unsigned int width, height;
float tim;

__global__ void createVertices(float4* positions, float tim, 
                                unsigned int width, unsigned int height) {
    unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;

    float u = x / (float)width;
    float v = y / (float)height;
    u = u * 2.0f - 1.0f;
    v = v * 2.0f - 1.0f;

    // calculate simple sine wave pattern
    float freq = 4.0f;
    float w = sinf(u * freq + tim)
    * cosf(v * freq + tim) * 0.5f;

    positions[y * width + x] = make_float4(u, w, v, 1.0f);
}

void init(void) {
    glClearColor(0, 0, 0, 0);
    glShadeModel(GL_FLAT);
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w/(GLfloat)h, 1, 200);
}

void display() {
    float4* positions;
    cudaGraphicsMapResources(1, &vbo_cuda, 0);
    size_t num_bytes;
    cudaGraphicsResourceGetMappedPointer((void**)&positions,
                                        &num_bytes,
                                        vbo_cuda);

    // execute kernel
    dim3 dimBlock(16, 16, 1);
    dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1);
    createVertices<<<dimGrid, dimBlock>>>(positions, tim,
                                            width, height);

    cudaGraphicsUnmapResources(1, &vbo_cuda, 0);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // render from the vbo
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexPointer(4, GL_FLOAT, 0, 0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glDrawArrays(GL_POINTS, 0, width * height);
    glDisableClientState(GL_VERTEX_ARRAY);

    glutSwapBuffers();
    glutPostRedisplay();
}

void deleteVBO() {
    cudaGraphicsUnregisterResource(vbo_cuda);
    glDeleteBuffers(1, &vbo);
}

int main (int argc, char**argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Cuda OpenGL Interop");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    cudaGLSetGLDevice(0);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    unsigned int size = width * height * 4 * sizeof(float);

    glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    cudaGLRegisterBufferObject(vbo);

    glutMainLoop();

    return 0;
}

【问题讨论】:

    标签: c++ opengl cuda interop


    【解决方案1】:

    错误来自 Windows:您的尝试非常失败,因为您生成的可执行文件对 Windows 无效。您可能正在使用带有 RELEASE 版本的 DEBUG DLL。或者您正在将 32 位构建与 64 位 DLL 或许多其他奇怪的组合混合在一起......(32 位系统上的 64 位 exe?......)

    通常,您可以在 Windows 事件查看器中获得有关 DLL 问题的更多信息,但如果您开始在调试器中运行应用程序(肯定是使用 Visual Studio),您将获得有关错误的更多信息。

    如果您不明白哪里出了问题,您可以尝试使用http://www.dependencywalker.com/ 找出问题所在。

    【讨论】:

    • 非常感谢。我今晚会试试,然后回信。
    • 现在它不再给我上述错误,但调试器在“glGenBuffers(1, &vbo);”行停止。有什么想法吗?
    【解决方案2】:

    第一个错误是启动此线程的原因,通过将正确的 glew32.dll 库安装到正确的文件夹中消失了。

    第二个错误,调试器停止在glGenBuffers(1, vbo),是因为我忘记了glewInit()

    您可以在下面找到正在运行的应用程序:

    #include <math.h>
    #include <GL\glew.h>
    #include <GL\glut.h>
    #include <cuda_gl_interop.h>
    #include <cuda_runtime.h>
    #include <device_launch_parameters.h>
    
    GLuint vbo;
    struct cudaGraphicsResource* vbo_cuda;
    
    const unsigned int window_width = 512;
    const unsigned int window_height = 512;
    
    const unsigned int mesh_width = 256;
    const unsigned int mesh_height = 256;
    
    float tim = 0.0;
    
    __global__ void createVertices(float4* positions, float tim, 
                                    unsigned int mesh_width, unsigned int mesh_height) {
        unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
        unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
    
        float u = x / (float)mesh_width;
        float v = y / (float)mesh_height;
        u = u * 2.0f - 1.0f;
        v = v * 2.0f - 1.0f;
    
        // calculate simple sine wave pattern
        float freq = 4.0f;
        float w = sinf(u * freq + tim)
        * cosf(v * freq + tim) * 0.5f;
    
        positions[y * mesh_width + x] = make_float4(u, w, v, 1.0f);
    }
    
    void runCuda(GLuint vbo)
    {
        // map OpenGL buffer object for writing from CUDA
        float4* positions;
        cudaGraphicsMapResources(1, &vbo_cuda, 0);
        size_t num_bytes;
        cudaGraphicsResourceGetMappedPointer((void**)&positions,
                                            &num_bytes,
                                            vbo_cuda);
    
        // execute kernel
        dim3 dimBlock(16, 16, 1);
        dim3 dimGrid(mesh_width / dimBlock.x, mesh_height / dimBlock.y, 1);
        createVertices<<<dimGrid, dimBlock>>>(positions, tim,
                                                mesh_width, mesh_height);
    
        cudaGraphicsUnmapResources(1, &vbo_cuda, 0);
    }
    
    void init(void) {
        glewInit();
        glClearColor(0, 0, 0, 1);
        glDisable(GL_DEPTH_TEST);
    }
    
    void reshape(int w, int h) {
        // viewport
        glViewport(0, 0, w, h);
        // projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60, (GLfloat)w/(GLfloat)h, 0.1, 10);
    }
    
    void createVBO(GLuint* vbo) {
        // create buffer object
        glGenBuffers(1, vbo);
        glBindBuffer(GL_ARRAY_BUFFER, *vbo);
    
        // initialize buffer object
        unsigned int size = mesh_width * mesh_height * 4 * sizeof(float);
        glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW);
    
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        cudaGLRegisterBufferObject(*vbo);
    }
    
    void deleteVBO(GLuint* vbo) {
        cudaGraphicsUnregisterResource(vbo_cuda);
        glBindBuffer(1, *vbo);
        glDeleteBuffers(1, vbo);
        cudaGLUnregisterBufferObject(*vbo);
    }
    
    void display() {
        // run CUDA kernel to generate vertex positions
        runCuda(vbo);
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // set view matrix
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // render from the vbo
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexPointer(4, GL_FLOAT, 0, 0);
    
        glEnableClientState(GL_VERTEX_ARRAY);
        glColor3f(1, 0, 0);
        glDrawArrays(GL_POINTS, 0, mesh_width * mesh_height);
        glDisableClientState(GL_VERTEX_ARRAY);
    
        glutSwapBuffers();
        glutPostRedisplay();
    
        tim+=1;
    }
    
    void keyboard(unsigned char key, int x, int y)
    {
        switch(key) {
        case(27) :
            deleteVBO(&vbo);
            exit(0);
        }
    }
    
    int main (int argc, char**argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
        glutInitWindowSize(window_width, window_height);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("Cuda GL interop");
        init();
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutReshapeFunc(reshape);
    
        // create VBO
        createVBO(&vbo);
    
        // run the cuda part
        runCuda(vbo);
    
        cudaGLSetGLDevice(0);
    
        glutMainLoop();
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2015-09-03
      • 2011-03-01
      • 2020-11-03
      相关资源
      最近更新 更多