Why cuCtxCreate fails ? Return code is CUDA_ERROR_OUT_OF_MEMORY

As it’s told in the subject, cuCtxCreate returns CUDA_ERROR_OUT_OF_MEMORY.

Context creation is used during the identification of available memory on graphics card. After a long computation process (lots of kernels are called one after another) this error suddenly occurs, all the previous calls to cuCtxCreate were successful.

This is the code I use to get the device memory capabilities:

extern "C++" CUresult GetMemInfo(unsigned int* pFree, unsigned int* pTotal,

	int nDevice, int* pStep)

{

	CUresult cuResult;

	*pStep = 0;

	CUdevice cuDevice;

	cuResult = cuDeviceGet(&cuDevice, nDevice);

	if (cuResult != CUDA_SUCCESS)

  return cuResult;

	*pStep = 1;

	CUcontext cuContext;

	cuResult = cuCtxCreate(&cuContext, 0, cuDevice);

	if (cuResult != CUDA_SUCCESS)

  return cuResult;

	*pStep = 2;

	cuResult = cuMemGetInfo(pFree, pTotal);

	if (cuResult != CUDA_SUCCESS)

  return cuResult;

	*pStep = 3;

	cuResult = cuCtxDetach(cuContext);

	if (cuResult != CUDA_SUCCESS)

  return cuResult;

	return cuResult;

}

EDIT: I thought you might need a call to cuCtxDestroy(), but that seems not to be the case (after doing a bit of reading)…sorry.

Do you know which statement is returning the non-CUDA_SUCCESS value? Try returning different values than cuResult in those if statements so you can see what specifically is causing the problems.
**edit: Just thought about the pstep variable … I’m assuming that’s what the pstep variable is doing. Tracking at what point the error occurs.

I know I was getting a CUFFT_ALLOC_FAILED error before, which seemed to indicate it was out of memory. In fact, there was an error in the kernel called before my CUFFT call. The kernel was totally unrelated, but the error slipped by until CUFFT choked on it and it made it seem like an error with CUFFT, when in fact it wasn’t. If you’re stumped, don’t focus just on the cuCtxCreate, but look at what’s happening before that call. My guess is that whatever is happening before the cuCtxCreate() call is what’s causing the error.

Yeah, pStep tracks what exactly goes wrong … thank you for the advice, will try to figure out what can be wrong before cuCtxCreate. However, the {check memory, allocate memory, run kernel, get results, free memory} sequence works for thousands of times before cuCtxCreate fails.