CUDA+OPENGL cudaGraphicsGLRegisterBuffer initialization error

I am new here. please help me. I don’t know what’s wrong. When cudaGraphicsGLRegisterBuffer, it failed and show initialization error.

#define GL_GLEXT_PROTOTYPES

#include <GL\glew.h>
#include “cuda.h”
#include “CUDA_BY_EXAMPLE\book.h”
#include “CUDA_BY_EXAMPLE\cpu_bitmap.h”
#include “cuda_gl_interop.h”
#include

using namespace std;

#define DIM 512

GLuint bufferObj;
cudaGraphicsResource *resource;

global void kernel( uchar4 *ptr ){
int x = threadIdx.x + blockIdx.x * blockDim.x;
int y = threadIdx.y + blockIdx.y * blockDim.y;
int offset = x + y * blockDim.x * gridDim.x;

float fx = x / (float)DIM - 0.5f;
float fy = y / (float)DIM - 0.5f;
unsigned char green = 128 + 127 * sin(abs(fx * 100) - abs(fy*100) );

ptr[offset].x = 0;
ptr[offset].y = green;
ptr[offset].z = 0;
ptr[offset].w = 255;

}

static void draw_func( void ){
glDrawPixels( DIM, DIM, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glutSwapBuffers();
}

static void key_func( unsigned char key, int x, int y ){
switch (key) {
case 27:
HANDLE_ERROR( cudaGraphicsUnregisterResource( resource ) );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glDeleteBuffers( 1, &bufferObj );
exit(0);

}

}
void initialCuda(cudaDeviceProp &prop, int &dev){
memset( &prop, 0, sizeof( cudaDeviceProp));
prop.major = 1;
prop.minor = 0;
HANDLE( cudaChooseDevice( &dev, &prop));
HANDLE( cudaGLSetGLDevice(dev));
}

void initialGL(int argc, char **argv, uchar4 *devPtr){
glutInit( &argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(DIM, DIM);
glutCreateWindow( “bitmap”);

GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  printf("glew Error: %s\n", glewGetErrorString(err));
}

glGenBuffers( 1, &bufferObj);
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, bufferObj);
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, DIM * DIM * 4, NULL, GL_DYNAMIC_DRAW_ARB);
//glBindBuffer( GL_PIXEL_UNPACK_BUFFER, bufferObj);
//glBufferData( GL_PIXEL_UNPACK_BUFFER, DIM * DIM * 4, NULL, GL_DYNAMIC_DRAW);

HANDLE_ERROR( cudaGraphicsGLRegisterBuffer( &resource, bufferObj, cudaGraphicsMapFlagsNone));	
size_t size;
HANDLE_ERROR( cudaGraphicsMapResources( 1, &resource, NULL));
HANDLE_ERROR( cudaGraphicsResourceGetMappedPointer( (void **)&devPtr, &size, resource));

}

int main(int argc, char **argv){
cudaDeviceProp prop;
int dev;
initialCuda(prop, dev);
uchar4 *devPtr;
initialGL(argc, argv, devPtr);

dim3 grids(DIM / 16, DIM/16);
dim3 threads(16, 16);
kernel<<<grids, threads>>>( devPtr );

HANDLE_ERROR( cudaGraphicsUnmapResources( 1, &resource, NULL ) );

glutKeyboardFunc( key_func );
glutDisplayFunc( draw_func );
glutMainLoop();
getchar();

return 0;

}

Just as a guess, I would propose your problem is due to your CUDA environment not being created properly. Could this be because of your lines

void initialCuda(cudaDeviceProp &prop, int &dev){

memset( &prop, 0, sizeof( cudaDeviceProp));

prop.major = 1;

prop.minor = 0;

HANDLE( cudaChooseDevice( &dev, &prop));

HANDLE( cudaGLSetGLDevice(dev));

}

use HANDLE instead of HANDLE_ERROR? I’m assuming you were trying to use your HANDLE_ERROR function there instead, which depending on what HANDLE does, could be causing your CUDA initialization code to fail, which would mean you wouldn’t have a valid CUDA context by the time you try to register your buffer.

One way to know for sure is to get the error message your failing function call gives. You might need to manually grab the error message from the failing operation, depending on what the HANDLE_ERROR function does.

(Also, in the future consider putting your code in a code block for ease of reading)