Array of CUcontexts and cufftHandles

Hello,

I am trying to create an array of CUcontexts and cufftHandles, but things just dont seem to be working. The results I am getting from my routines aren’t right so I wanted to post to see what everyones opinions were on how I am acquiring contexts and setting up handles.

This is my fuction for initializing the contexts:

CUcontext * initGPUs(int nGPUs, int *gpuIDs)
{
	cuInit(0);

	CUcontext * contexts = (CUcontext *)malloc(nGPUs * sizeof(CUcontext));

	for (int i = 0; i < nGPUs; i++)
	{
		CUdevice device;
		cuDeviceGet(&device, gpuIDs[i]);

		CUcontext context;
		cuCtxCreate_v2(&context, 0, device);
                contexts[i] = context;
	}

	return contexts;
}

and an example of how I use the function:

int gpuIDs[1] = {0};

CUcontext *contexts = initGPUs(1, gpuIDs);

Here is how I am create cufftHandles. My cufftHandles are stored as static cufftHandle *plan_fwd, *plan_bwd; and are in a class called CUDAImageTile

static void initFunc(int nGPUs)
	{
	        CUDAImageTile::plan_fwd = (cufftHandle *)malloc(sizeof(cufftHandle) * nGPUs);
		CUDAImageTile::plan_bwd = (cufftHandle *)malloc(sizeof(cufftHandle) * nGPUs);
	}

	static void initPlans(int width, int height, CUcontext context, int dev)
	{
		cuCtxSetCurrent(context);

		CUDAImageTile::fftSize = (width/2+1)*height;

		cufftHandle fwd;
		cufftHandle bwd;

		cufftCreate(&fwd);
		cufftCreate(&bwd);
		cufftPlan2d(&fwd, height, width, CUFFT_D2Z);
		cufftPlan2d(&bwd, height, width, CUFFT_Z2D);

		plan_fwd[dev] = fwd;
		plan_bwd[dev] = bwd;
	}

Any help on this and if this throws up any red flags would be greatly appreciated.

I believe I resolved my issues. Mod can delete this if need be. There is nothing wrong with the code above, just my failure to access static variables in C++ :facedesk:

It might still produce errors, it depends a bit on how the code is used… for example if multiple threads are involved.

I don’t think the cuda driver api is thread-safe, so a critical section may be required around any context setting.