C++ 为什么我的CUDA应用程序不是';我不能开始吗?

C++ 为什么我的CUDA应用程序不是';我不能开始吗?,c++,opengl,cuda,interop,C++,Opengl,Cuda,Interop,下面的代码编译时没有任何错误,但当我运行它时,它显示“应用程序无法正确启动(0xc000007b)。单击“确定”关闭应用程序。” #包括 #包括 #包括 #包括 #包括 #包括 GLuint vbo; 结构cudaGraphicsResource*vbo_cuda; 无符号整数宽度、高度; 浮动时间; __全局无效创建顶点(浮动4*位置、浮动时间、, 无符号整数宽度、无符号整数高度){ 无符号整数x=blockIdx.x*blockDim.x+threadIdx.x; 无符号整数y=blockI

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

#包括
#包括
#包括
#包括
#包括
#包括
GLuint vbo;
结构cudaGraphicsResource*vbo_cuda;
无符号整数宽度、高度;
浮动时间;
__全局无效创建顶点(浮动4*位置、浮动时间、,
无符号整数宽度、无符号整数高度){
无符号整数x=blockIdx.x*blockDim.x+threadIdx.x;
无符号整数y=blockIdx.y*blockDim.y+threadIdx.y;
浮动u=x/(浮动)宽度;
浮动v=y/(浮动)高度;
u=u*2.0f-1.0f;
v=v*2.0f-1.0f;
//计算简单正弦波波形
浮动频率=4.0f;
浮点数w=sinf(u*频率+tim)
*cosf(v*freq+tim)*0.5f;
位置[y*宽度+x]=使_浮动4(u、w、v、1.0f);
}
void init(void){
glClearColor(0,0,0,0);
glShadeModel(GLU平面);
}
空洞重塑(整数w,整数h){
GLVIEW(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_投影);
glLoadIdentity();
glu(60,(GLfloat)w/(GLfloat)h,1200);
}
无效显示(){
4*个职位;
cudaGraphicsMapResources(1和vbo_cuda,0);
大小\u t数量\u字节;
cudaGraphicsResourceGetMappedPointer((void**)和positions,
&num_字节,
vbo_cuda);
//执行内核
dim3 dimBlock(16,16,1);
dim3 dimGrid(宽度/dimBlock.x,高度/dimBlock.y,1);
创建顶点(位置,tim,
宽度、高度);
CudAgraphic SunMapResources(1和vbo_cuda,0);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glMatrixMode(GLU模型视图);
glLoadIdentity();
//从vbo渲染
glBindBuffer(GL_数组_BUFFER,vbo);
glvertexofinter(4,GL_FLOAT,0,0);
glEnableClientState(GL_顶点_数组);
glDrawArrays(GLU点,0,宽度*高度);
glDisableClientState(GL_顶点_数组);
glutSwapBuffers();
再发现();
}
void deleteVBO(){
注册资源(vbo_cuda);
glDeleteBuffers(1和vbo);
}
int main(int argc,字符**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(500500);
位置(100100);
glutCreateWindow(“Cuda OpenGL互操作”);
init();
glutDisplayFunc(显示器);
GLUTREFORUNC(重塑);
cudaGLSetGLDevice(0);
glGenBuffers(1,&vbo);
glBindBuffer(GL_数组_BUFFER,vbo);
无符号整数大小=宽度*高度*4*sizeof(浮点);
glBufferData(GL\ U数组\ U缓冲区,大小,0,GL\ U动态\ U绘图);
glBindBuffer(GL_数组_BUFFER,0);
cudaGLRegisterBufferObject(vbo);
glutMainLoop();
返回0;
}

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

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


如果您不明白哪里出了问题,您可以尝试查找失败的地方。

第一个错误是启动此线程的原因,它通过将正确的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;
}
#包括
#包括
#包括
#包括
#包括
#包括
GLuint vbo;
结构cudaGraphicsResource*vbo_cuda;
常量无符号整数窗口宽度=512;
const unsigned int window_height=512;
常量无符号整数网格\u宽度=256;
常量无符号整数网格高度=256;
浮动时间=0.0;
__全局无效创建顶点(浮动4*位置、浮动时间、,
无符号整数网格(宽度,无符号整数网格(高度){
无符号整数x=blockIdx.x*blockDim.x+threadIdx.x;
无符号整数y=blockIdx.y*blockDim.y+threadIdx.y;
浮动u=x/(浮动)网格宽度;
浮动v=y/(浮动)网格高度;
u=u*2.0f-1.0f;
v=v*2.0f-1.0f;
//计算简单正弦波波形
浮动频率=4.0f;
浮点数w=sinf(u*频率+tim)
*cosf(v*freq+tim)*0.5f;
位置[y*网格宽度+x]=使网格浮动4(u、w、v、1.0f);
}
void runCuda(GLuint vbo)
{
//映射用于从CUDA写入的OpenGL缓冲区对象
4*个职位;
cudaGraphicsMapResources(1和vbo_cuda,0);
大小\u t数量\u字节;
cudaGraphicsResourceGetMappedPointer((void**)和positions,
&num_字节,
vbo_cuda);
//执行内核
dim3 dimBlock(16,16,1);
dim3 dimGrid(网格宽度/dimBlock.x,网格高度/dimBlock.y,1);
创建顶点(位置,tim,
网格宽度、网格高度);
CudAgraphic SunMapResources(1和vbo_cuda,0);
}
void init(void){
glewInit();
glClearColor(0,0,0,1);
glDisable(GLU深度测试);
}
空洞重塑(整数w,整数h){
//视区
glViewport(0,0,w,h);
//投影
glMatrixMode(GL_投影);
glLoadIdentity();
葡萄糖(60,(GLfloat)w/(GLfloat)h,0.1,10);
}
void createVBO(GLuint*vbo){
//创建缓冲区对象
glGenBuffers(1,vbo);
glBindBuffer(GL_数组_BUFFER,*vbo);
//初始化缓冲区对象
无符号整数大小=网格宽度*网格高度*4*sizeof(浮点);
glBufferData(GL\ U数组\ U缓冲区,大小,0,GL\ U动态\ U绘图);
glBindBuffer(GL_数组_BUFFER,0);
cudaGLRegisterBufferObject(*vbo);
#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;
}