Unexpected Device Error with OpenGL

Hi Im currently using 9800GT on PCIE slot and 9500GT on PCI slot on Windows platform. I have tried to use OpenGL to boost my displaying speed but somehow it is not working properly. If shows black screen and when cutilSafeCall was used as code bellow on CreateVBO, it says

“cutilSafeCall… unexpected device error”

I have tried to use various cuda error checking but they throw message saying I cannot use such error for the function that Im using. Does anybody recognize my problem?

PS: I dont know why this is happening but as a method of olving such problem I have tried to use cudaInit and related calls but it says

1>Resampling_GL.obj : error LNK2019: unresolved external symbol _cuGLCtxCreate_v2@12 referenced in function _main

1>Resampling_GL.obj : error LNK2019: unresolved external symbol _cuGLInit@0 referenced in function _main

1>Resampling_GL.obj : error LNK2019: unresolved external symbol _cuCtxCreate_v2@12 referenced in function _main

1>Resampling_GL.obj : error LNK2019: unresolved external symbol _cuInit@4 referenced in function _main

1>C:\Users\RETINALOCT2\Desktop\Jay\OCTProcessor\gpu_testers\Resampling_GL\Debug\Resampling_GL.exe : fatal error LNK1120: 4 unresolved externals

Would this help? Following is my main and creatVBO function with header files that Im calling

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

// includes, GL

#include <GL/glew.h>

//#include <GL/freeglut.h>

#include <GL/glut.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>

#include <cuda.h>

#include <cudaGL.h>

.....

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

{

	// 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); 

	cudaGraphicsGLRegisterBuffer(cuda_pixel_resource, *pixel_buffer, cudaGraphicsMapFlagsWriteDiscard);

}

int main() { 

	// Pre-Calculation

	readData();

	convertor(); //convert values into single float

	calc_resample_coefficients();

	//Cuda Memory Allocation

	int frame = 0, sum =0;

	

	CUcontext *pCtx;

	cuInit(0);

	cuCtxCreate(pCtx, CU_CTX_MAP_HOST, 0);

	cuGLInit();

	cuGLCtxCreate(pCtx, CU_CTX_MAP_HOST, 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);

	int numArgs = 0;

	glutInit(&numArgs, NULL);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(window_width, window_height);

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

	glutDisplayFunc(display);

	glewInit();

	// Create buffer object and register it with CUDA 

	createVBO(&pbo, &cuda_pbo_resource);

	// Loop 

	glutMainLoop(); 

	deleteVBO();

	cudaThreadExit();

}

Your linker errors means you’re not linking the .lib files to your project correctly.
Your call to cuInit() would be fine, but if you’re looking to have CUDA <–> OpenGL interoperability, you need to have an OpenGL context active prior to creating your CUDA context. I believe glut will create an OpenGL context upon your call to glutInit(&numArgs, NULL);

I placed the lines that define cuda context after GL context but still I have linker problem. If it is not linking correctly what would be the case? Thank you.

I have been linking to additional libraries that are provided by Nvidia but still it sain I have linker problem. Does anybody know which library in which directory I have to link? Thank you

Oh I actually handled with the linker problem. Thing is that I now have a problem saying that Im havging

Unhandled exception at 0x42097627 in Resampling_GL.exe: 0xC0000005: Access violation reading location 0x00000000.

at glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE) on the code bellow (this is from main fuction)

glewInit();
int numArgs = 0;

glutInit(&numArgs, NULL);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_width, window_height);
glutCreateWindow(“Cuda GL Interop (VBO)”);
glutDisplayFunc(display);

Does this ring the bell for anyone? Thanks

Sorry for confusion actually it seems like the one with the problem is

glutInit(&numArgs, NULL);

Is it because Im using NULL? I set numArgs to be 0. Thank you

I thought I remembered doing that and it not causing any problems, but my memory isn’t what it used to be. since the NULL is representing a pointer to a pointer, if your code is trying to deference it, then it’ll generate that access violation. I figured since the numargs is 0, it wouldn’t try that, but who knows. You could just have your main be:

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

   ... 

   glutInit(&argc, argv);

   ...

}

such that you’re passing the argc and argv into your glutInit function call. At least then you’ll know if that’s the culprit

Yes that kind of solves the problem. However it says that I have another problem in the following function where I create the pixel buffer.

so for creatVBO()
{
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) ) );
}

}

if from debugging tool I see that the global variable

GLuint pbo;

that I plug into creatVBO call in main function gives error since pbo is 0. I initialized the GL units using glutInit so it seems quite strange. However if it is 0 then it should jump to else case doesnt it? Somehow my program says there is access violation at

glGenBuffers(1, pixel_buffer);

Can somebody notice anything wrong? thank you

for better view this is my entire portion of code with regarding OpenGL parts

// 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);

	glewInit();

	glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize(window_width, window_height);

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

	glutDisplayFunc(display);

	

	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();

}