correct vbo re-allocation with cuda 2.3? currently failing

Hi,

I’m having trouble trying to re-allocate cuda interop vbos. eg the following code fails on the second copy_to_vbo() at cudaMemcpy (with ‘invalid argument’) after the deallocation of vbo1. If the deallocation of vbo1 is commented out or moved to the end then the code works fine. Any ideas what I’m doing wrong? I’ve tried to follow the form of the simpleGL example.

toolkit: 2.3

driver: linux 64 190.29

thanks,

Chris

#include <GL/glew.h>

#include <GL/glut.h>

#include <GL/glx.h>

#include <cuda_runtime_api.h>

#include <cuda_gl_interop.h>

#include <vector>

#include <iostream>

#include <stdexcept>

#define checkErrorGL() \

{ \

	GLenum errCode; \

 \

	if ((errCode = glGetError()) != GL_NO_ERROR) { \

	   std::cerr << "** OpenGL Error at " << __FILE__ << ":" << __LINE__ << ": " << gluErrorString(errCode) << "\n"; \

	   throw std::runtime_error( "OpenGL error" ); \

	} \

}

#define SAFE_GL( fn ) \

{ \

	{fn;} \

	checkErrorGL(); \

}

#define SAFE_CUDA( fn ) \

{ \

	cudaError errCode; \

\

	if( (errCode = fn) != cudaSuccess ) { \

	   std::cerr << "** CUDA Error at " << __FILE__ << ":" << __LINE__ << ": " << cudaGetErrorString( errCode ) << "\n"; \

	   throw std::runtime_error( "CUDA error" ); \

	} \

}

#define LOGLINE() \

{ \

	std::cout << __FILE__ << ":" << __LINE__ << "\n"; \

}

void create_gl_context( int argc, char* argv[] )

{

	glutInit(&argc, argv);

	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

	glutInitWindowSize(64, 64);

	glutCreateWindow("Cuda GL Interop");

	glewInit();

}

bool has_open_gl()

{

	return glXGetCurrentContext() != NULL;

}

void alloc_vbo( GLuint& vbo, uint sz )

{

	std::cout << "alloc_vbo()\n";

	GLuint target = GL_ARRAY_BUFFER;

	GLuint usage = GL_DYNAMIC_DRAW;

	SAFE_GL( glGenBuffers( 1, &vbo ) );

	SAFE_GL( glBindBuffer( target, vbo ) );

	SAFE_GL( glBufferData( target, sz * sizeof(float), 0, usage ) );

	SAFE_GL( glBindBuffer( target, 0 ) );

	SAFE_CUDA( cudaGLRegisterBufferObject( vbo ) );

	std::cout << "allocated buffer: " << vbo << '\n';

}

void copy_to_vbo( GLuint vbo, uint sz )

{

	std::cout << "copy_to_vbo(" << vbo << ")\n";

	std::vector<float> hv(sz);

	float *dptr;

	SAFE_CUDA( cudaGLMapBufferObject((void**)&dptr, vbo ));

	SAFE_CUDA( cudaMemcpy( dptr, &hv[ 0 ], sz * sizeof(float), cudaMemcpyHostToDevice ) );

	SAFE_CUDA( cudaGLUnmapBufferObject(vbo) );

}

void dealloc_vbo( GLuint& vbo )

{

	std::cout << "dealloc_vbo(" << vbo << ")\n";

	GLuint target = GL_ARRAY_BUFFER;

	SAFE_GL( glBindBuffer( target, vbo ) );

	SAFE_GL( glDeleteBuffers( 1, &vbo ) );

	SAFE_CUDA( cudaGLUnregisterBufferObject(vbo) );

	vbo = 0;

}

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

{

	create_gl_context( argc, argv );

	std::cout << "has opengl: " << has_open_gl << '\n';

	GLuint vbo1(0), vbo2(0);

	size_t sz = 1000;

	LOGLINE();

	alloc_vbo( vbo1, sz );

	LOGLINE();

	copy_to_vbo( vbo1, sz );

	LOGLINE();

	dealloc_vbo( vbo1 );

	LOGLINE();

	alloc_vbo( vbo2, sz );

	LOGLINE();

	copy_to_vbo( vbo2, sz );

	LOGLINE();

	dealloc_vbo( vbo2 );

	return 0;

}

I’m also interested if anyone can repro this problem using the code provided under linux64 / cuda 2.3?

thanks,
Chris