Unspecified Device Error with OpenGL

Hello Im using 9800GT and 9500GT in windows platform. Following is my code.

// includes, system

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

// includes, GL

#include <GL/glew.h>

//#include <GL/freeglut.h>

#include <GL/glut.h>

#include <cuda.h>

#include <cudaGL.h>

// includes CUDA <--> C++ interlops 

#include <cuda_runtime.h>

#include <cutil_inline.h>

#include <cutil_gl_inline.h>

#include <cutil_gl_error.h>

#include <cuda_gl_interop.h>

//define variables

#define XDIM 1000 //original value 1000

#define YDIM 1024 //original value 1024

const unsigned int window_width = 1000;

const unsigned int window_height = 1024;

float4 *d_vbo_buffer;

// pbo variables

GLuint pbo;

struct cudaGraphicsResource *cuda_pbo_resource;

extern "C"

void launch_kernel(int* sum, float* original_data, int* frame_num, cuComplex* d, float* k_resamp, float* dc_subtracted, float* resample, float4 *ptr);

//simply use cudaGraphicsMapFlagsWriteDiscard as cudaGraphicsResource

void createVBO(GLuint* pixel_buffer, struct cudaGraphicsResource **cuda_pixel_resource)

{

	if(pixel_buffer){

	// create buffer object

	glGenBuffers(1, pixel_buffer);

	glBindBuffer(GL_ARRAY_BUFFER, *pixel_buffer);

	//register buffer on cuda

	unsigned int size = XDIM * YDIM * 4 * sizeof(float); 

	glBufferData(GL_ARRAY_BUFFER, size, 0, GL_DYNAMIC_DRAW); 

	glBindBuffer(GL_ARRAY_BUFFER, 0); 

	cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard));

	}

	else{

	cutilSafeCall( cudaMalloc( (void **)&d_vbo_buffer, XDIM*YDIM*4*sizeof(float) ) );

	}

}

void display() { 

	// Map buffer object for writing from CUDA 

	float4* pix_buff; 

	cudaGraphicsMapResources(1, &cuda_pbo_resource, 0); 

	size_t num_bytes; 

	cudaGraphicsResourceGetMappedPointer((void**)&pix_buff, &num_bytes, cuda_pbo_resource);

	// Execute kernel 

	// will execute by calling kernel.cu file

	if(pbo)

	launch_kernel(dev_sum, dev_original, dev_frame, d_in, dev_k_resampledspacing, dc_subtracted, dev_resamp, pix_buff);

	else launch_kernel(dev_sum, dev_original, dev_frame, d_in, dev_k_resampledspacing, dc_subtracted, dev_resamp, d_vbo_buffer);

	// Unmap buffer object 

	cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0);

	// copy to Open_gl texture

	// Render from buffer object 

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	glBindBuffer(GL_ARRAY_BUFFER, pbo); 

	glVertexPointer(4, GL_FLOAT, 0, 0); 

	glEnableClientState(GL_VERTEX_ARRAY); 

	glDrawArrays(GL_POINTS, 0, XDIM * YDIM); 

	glDisableClientState(GL_VERTEX_ARRAY); 

	

	// Swap buffers 

	glutSwapBuffers(); 

	glutPostRedisplay();

	

}

void deleteVBO() { 

	cudaGraphicsUnregisterResource(cuda_pbo_resource); 

	glDeleteBuffers(1, &pbo);

}

int main(int argc, char *argv[]) { 

	// Pre-Calculation

	readData();

	convertor(); //convert values into single float

	calc_resample_coefficients();

	//Cuda Memory Allocation

	int frame = 0, sum =0;

	cudaMalloc((void**)&dev_frame, sizeof(int));

	cudaMalloc((void**)&dev_sum, sizeof(int));

	cudaMalloc((void**)&dev_original, sizeof(float)*FRAMES*XDIM*YDIM);

	cudaMalloc((void**)&d_in,sizeof(cuComplex)*YDIM*XDIM);

	cudaMalloc((void**)&dev_resamp, sizeof(float)*LINE_LENGTH*2);

	cudaMalloc((void**)&dev_k_resampledspacing, sizeof(float));

	cudaMalloc((void**)&dc_subtracted, sizeof(float)*XDIM*YDIM);

		

	cudaMemcpy(dev_k_resampledspacing, &k_resampledspacing, sizeof(float), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_resamp, resamp_1D, sizeof(float)*LINE_LENGTH*2, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_original, original_cpu, sizeof(float)*FRAMES*XDIM*YDIM, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_frame, &frame, sizeof(int), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_sum, &sum, sizeof(int), cudaMemcpyHostToDevice);

	// Explicitly set device 

	cudaGLSetGLDevice(2);

	glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(window_width, window_height);

    glutCreateWindow("Cuda GL Interop (VBO)");

	glutDisplayFunc(display);

	

	glewInit();

	CUcontext *pCtx = NULL;

	cuInit(0);

	cuCtxCreate(pCtx, CU_CTX_MAP_HOST, 0);

	cuGLInit();

	cuGLCtxCreate(pCtx, CU_CTX_MAP_HOST, 0);	

	// Create buffer object and register it with CUDA 

	createVBO(&pbo, &cuda_pbo_resource);

	// Loop 

	glutMainLoop(); 

	deleteVBO();

	cudaThreadExit();

}

my code shoots me unspecified device error at

cutilSafeCall(cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard));

of creatVBO function. Someone said to use other debugging tool rather than cutilSafeCall but I dont know what to use so Im confused with my

error messages. Can somebody help me with this? Thank you.

Another issue. If I place glewInit() infront of glutInit() i get access violation at glGenBuffers(1, pixel_buffer); of createVBO function. Thank you.

order:

  1. glutInit
  2. glewInit
  3. cudaGLSetGLDevice
  4. the rest

important: no cuda runtime calls which create a context before cudaGLSetGLDevice. Actually your cudamalloc and cudamemcpy calls allocate and copy on/to your default device (0)

I changed as accordingly to your suggestion following is the code.

int main(int argc, char *argv[]) { 

	// Pre-Calculation

	readData();

	convertor(); //convert values into single float

	calc_resample_coefficients();

	//Cuda Memory Allocation

	// Explicitly set device 

	

	glutInit(&argc, argv);

	glewInit();

	

	cudaGLSetGLDevice(2);

	CUcontext *pCtx = NULL;

	cuInit(0);

	cuCtxCreate(pCtx, CU_CTX_MAP_HOST, 0);

	cuGLInit();

	cuGLCtxCreate(pCtx, CU_CTX_MAP_HOST, 0);	

	int frame = 0, sum =0;

	cudaMalloc((void**)&dev_frame, sizeof(int));

	cudaMalloc((void**)&dev_sum, sizeof(int));

	cudaMalloc((void**)&dev_original, sizeof(float)*FRAMES*XDIM*YDIM);

	cudaMalloc((void**)&d_in,sizeof(cuComplex)*YDIM*XDIM);

	cudaMalloc((void**)&dev_resamp, sizeof(float)*LINE_LENGTH*2);

	cudaMalloc((void**)&dev_k_resampledspacing, sizeof(float));

	cudaMalloc((void**)&dc_subtracted, sizeof(float)*XDIM*YDIM);

		

	cudaMemcpy(dev_k_resampledspacing, &k_resampledspacing, sizeof(float), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_resamp, resamp_1D, sizeof(float)*LINE_LENGTH*2, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_original, original_cpu, sizeof(float)*FRAMES*XDIM*YDIM, cudaMemcpyHostToDevice);

	cudaMemcpy(dev_frame, &frame, sizeof(int), cudaMemcpyHostToDevice);

	cudaMemcpy(dev_sum, &sum, sizeof(int), cudaMemcpyHostToDevice);

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(window_width, window_height);

    glutCreateWindow("Cuda GL Interop (VBO)");

	glutDisplayFunc(display);

	// Create buffer object and register it with CUDA 

	createVBO(&pbo, &cuda_pbo_resource);

	// Loop 

	glutMainLoop(); 

	deleteVBO();

	cudaThreadExit();

}

Now its saying Im having access violation on glGenBuffers(1, pixel_buffer) (its on createVBO function)

I had the same problem for quite awhile. I used CUT_CHECK_ERROR on this for debugging and it says its having.

1>.\Resampling_GL.cpp(126) : error C2664: ‘fprintf’ : cannot convert parameter 3 from ‘void’ to ‘…’

1> Expressions of type void cannot be converted to other types

1>.\Resampling_GL.cpp(126) : error C2664: ‘fprintf’ : cannot convert parameter 3 from ‘void’ to ‘…’

1> Expressions of type void cannot be converted to other types

Im not sure why glGenBuffers has anything to do with fprintf and why this error is happening. Does anyone recognize whats going on? Thanks.

Regards,

MK

Oh Im sorry it was because CUT_CHECK_ERROR cannot use void type functions. if I get rid of it, it says

Unhandled exception at 0x68699d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x000027d9.

Im not sure how this error is getting out since Im ignoring LIBMCT.lib so there should be no conflict between default libraries… Is this something to do with my setting? Can anyone tell me how to fix this?

you’re still mixing the driver api with the runtime api.

You don’t need cuXXX Functions, cudaGLSetGLDevice will set up a proper context with all needed extensions.

try to avoid mixing up cuXXX with cudaXXX functions. Are you sure that device number 2 is a proper cuda device? Better use cudaGetDevice to obtain the device number(s);

Debug your code and tell us which function is causing the error External Image Little Hint: 0xc0000005 is a segmentation fault which usually apprears when you try to access unallocated addresses, e.g. accessing an out of bound array element

Thanks it really helped. Now this is the code for my new main function. I actually got rid of cuda and cu functions and plugged in those variables declared inside the cpp file.

int main(int argc, char *argv[]) { 

   //set GL context first.

   glutInit(&argc, argv);

   glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

   glutInitWindowSize(window_width, window_height);

   glutCreateWindow("Cuda GL Interop (VBO)");

   glutDisplayFunc(display);

glewInit();

// Pre-Calculation

   readData();

   convertor(); //convert values into single float

   calc_resample_coefficients();

//Cuda Memory Allocation

// Explicitly set device

   int device;

   cudaGetDevice(&device);

   cudaGLSetGLDevice(2);

sum = 0, frame = 0;

   // Create buffer object and register it with CUDA

   createVBO(&pbo, &cuda_pbo_resource);

// Loop

glutMainLoop();

deleteVBO();

cudaThreadExit();

Now if I use cudaGetDevice to get the device number for cudaGLSetGLDevice I get error sain

device kernel image is invalid

at cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard)

If just plugging in 2, I still get the 0xC0000005 error now on the some file called output.c. Still seems like that the program is trying to access some location that is not valid. Im using Multithreaded Debug DLL (/MDd) for Debug. Can somebody help me? Thanks :)

int device;

cudaGetDevice(&device);

cudaGLSetGLDevice(device);

place it like this just after glewInit().

check for errors at the cudaGLSetGLDevice function.

I used CUT_CHECK_ERROR on cudaGLSetDevice and it says

it has "unhandled exception at0x65e9d2f(msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000024

and breaks at some file called output.c at while(i – && *p) which

is shown bellow.

} else {

                    if (text.wz == NULL) /* NULL passed, use special string */

                        text.wz = __wnullstring;

                    bufferiswide = 1;

                    pwch = text.wz;

                    while (i-- && *pwch)

                        ++pwch;

                    textlen = (int)(pwch - text.wz);       /* in wchar_ts */

                    /* textlen now contains length in wide chars */

                }

#else  /* _UNICODE */

                if (flags & (FL_LONG|FL_WIDECHAR)) {

                    if (text.wz == NULL) /* NULL passed, use special string */

                        text.wz = __wnullstring;

                    bufferiswide = 1;

                    pwch = text.wz;

                    while ( i-- && *pwch )

                        ++pwch;

                    textlen = (int)(pwch - text.wz);

                    /* textlen now contains length in wide chars */

                } else {

                    if (text.sz == NULL) /* NULL passed, use special string */

                        text.sz = __nullstring;

                    p = text.sz;

                    while (i-- && *p)

                        ++p;

                    textlen = (int)(p - text.sz);    /* length of the string */

                }

at the output window I get

First-chance exception at 0x75fe9673 in Resampling_GL.exe: Microsoft C++ exception: cudaError_enum at memory location 0x0012f968…

First-chance exception at 0x75fe9673 in Resampling_GL.exe: Microsoft C++ exception: cudaError_enum at memory location 0x0012f964…

First-chance exception at 0x75fe9673 in Resampling_GL.exe: Microsoft C++ exception: cudaError at memory location 0x0012f9b0…

First-chance exception at 0x75fe9673 in Resampling_GL.exe: Microsoft C++ exception: cudaError at memory location 0x0012f9cc…

First-chance exception at 0x65fe9d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000024.

Unhandled exception at 0x65fe9d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000024.

First-chance exception at 0x65fe9d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000024.

Unhandled exception at 0x65fe9d2f (msvcr90d.dll) in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000024.

The program ‘[776] Resampling_GL.exe: Native’ has exited with code -1073741819 (0xc0000005).

Still Im accessing wrong directed address I think. Im not sure with the dll file which seems to show me that Im having conflict between default libraries even if Im ignoring libmct.lib Is this due to some setting?

By the way Im using Multithreaded Debug DLL (/MDd) for Debug setting.
For just curiosity. Im linking kernel.cu file with cpp by calling

extern “C”
void launch_kernel(float* sum, float* original_data, int* frame_num, cuComplex* d, float* k_resamp, float* dc_subtracted, float* resample, float4 *ptr);

and launch_kernel is a kernel function defined in kernel.cu file. Is this right? Im just wondering if this could be a source of problem as well.