CUDA_ERROR_INVALID_CONTEXT error in cuMemGetInfo strange behaviour, and contect error

Hi,

I was experiencing some strange behavior in my program, and I tried to see the current free memory with cuMemGetInfo.

the results were very weirds, so I checked for the return value of cuMemGetInfo. and I get 201, which means CUDA_ERROR_INVALID_CONTEXT.

can someone explain me what i’m doing wrong ?

unsigned int free, total;

 cuInit(0);

 printf("success=%d, free=%d, total=%d \n", cuMemGetInfo(&free, &total), free, total);

another pb :my program seemed to freeze always at the same position, and when I set the _DEBUG flag to 1, I got a ‘out of memory’ message from a previous malloc … The pb is that at this point I have 60 Mo free memory, and I try to allocate 40 Mo …

thanks

I know this is an old post, but did you resolve it? I’m a new user and I think I’m having the same problem i.e. strange results which look like memory issues, and when I call cuMemGetInfo I too get CUDA_ERROR_INVALID_CONTEXT…

Thanks

don’t you need to select the device and create a context as well?

Eg cuDeviceGet() and cCtxCreate()

Many thanks for the reply. I now do call cudaSetDevice without errors; does this set up the context as well?
I still get the same error from cuMemGetInfo.

Thanks.

I managed to get cuCtxCreate working and it now seems to work without errors…

So, to initialize I need to call cudaSetDevice and cuCtxCreate each time?

At least some of the SDK examples don’t seem to use cuCtxCreate, unless I’m missing them somewhere (probably :blink: )

Thanks. Andy

Yes, for the driver API you need init, getdevice and createcontext. You do this on application startup and as long as you stay in the same thread, things will work. Now with different threads… you need to pop/push the contexts. I believe the SDK has the initialisation hidden in some cutil function somewhere :)