Skip to content
Michael Kunz edited this page Aug 1, 2015 · 2 revisions

The CudaContext class: This is one of the three main classes and represents a Cuda context. From Cuda 4.0 on, the Cuda API demands (at least) one context per process per device. So for each device you want to use, you need to create a CudaContext instance. In the different constructors you can define several properties, e.g. the deviceID to use. As nearly all managedCuda classes, CudaContext implements IDisposable and the wrapped Cuda context is valid until Dispose() is called. Further CudaContext defines a bunch of static methods to retrieve general information about (possible) Cuda devices. Important for multi threaded applications: In order to use any cuda object related to a context, you must activate the cudaContext by calling the SetCurrent() method from the current thread. This holds for all thread switches. (See the Cuda programming guide for more information).

Example code:

int deviceID = 0;
CudaContext ctx = new CudaContext(deviceID, CUCtxFlags.MapHost | CUCtxFlags.BlockingSync);

//for default setting with device 0:
CudaContext ctx = new CudaContext();

CudaDeviceProperties props = CudaContext.GetDeviceInfo(deviceID);
int deviceCount = CudaContext.GetDeviceCount();
Clone this wiki locally