Library/ManagedCuda/CudaContext.cs
/// <summary>
/// Returns a list of possible CUDA devices to use for a given DirectX device
/// </summary>
/// <param name="pD3DXDevice">DirectX device</param>
/// <param name="deviceList">SLI parameter</param>
/// <param name="dXVersion">DirectX version of the directX device</param>
/// <returns></returns>
Returns a list of possible CUDA devices to use for a given DirectX device
Returns a list of possible CUDA devices to use for a given DirectX device
public static CUdevice[] GetDirectXDevices(IntPtr pD3DXDevice, CUd3dXDeviceList deviceList, DirectXVersion dXVersion)
		{
			CUResult res;
			int deviceCount = 0;
			res = DriverAPINativeMethods.DeviceManagement.cuDeviceGetCount(ref deviceCount);
			Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuDeviceGetCount", res));
			if (res == CUResult.ErrorNotInitialized)
			{
				res = DriverAPINativeMethods.cuInit(CUInitializationFlags.None);
				Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuInit", res));
				if (res != CUResult.Success)
					throw new CudaException(res);
				res = DriverAPINativeMethods.DeviceManagement.cuDeviceGetCount(ref deviceCount);
				Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuDeviceGetCount", res));
				if (res != CUResult.Success)
					throw new CudaException(res);
			}
			else if (res != CUResult.Success)
				throw new CudaException(res);
			if (deviceCount == 0)
			{
				throw new CudaException(CUResult.ErrorNoDevice, "Cuda initialization error: There is no device supporting CUDA", null);
			}
			CUdevice[] devices;
			CUdevice[] temp = new CUdevice[deviceCount];
			int dirextXDevices = 0;
			switch (dXVersion)
			{
				case DirectXVersion.D3D9:
					res = DirectX9NativeMethods.CUDA3.cuD3D9GetDevices(ref dirextXDevices, temp, (uint)deviceCount, pD3DXDevice, deviceList);
					Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuD3D9GetDevices", res));
					break;
				case DirectXVersion.D3D10:
					res = DirectX10NativeMethods.CUDA3.cuD3D10GetDevices(ref dirextXDevices, temp, (uint)deviceCount, pD3DXDevice, deviceList);
					Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuD3D10GetDevices", res));
					break;
				case DirectXVersion.D3D11:
					res = DirectX11NativeMethods.cuD3D11GetDevices(ref dirextXDevices, temp, (uint)deviceCount, pD3DXDevice, deviceList);
					Debug.WriteLine(String.Format("{0:G}, {1}: {2}", DateTime.Now, "cuD3D11GetDevices", res));
					break;
				default:
					throw new ArgumentException("DirectX version not supported.", "dXVersion");
			}
			if (res != CUResult.Success)
				throw new CudaException(res);
			if (dirextXDevices == 0)
			{
				throw new CudaException(CUResult.ErrorNoDevice, "Cuda initialization error: There is no device supporting CUDA and DirectX", null);
			}
			devices = new CUdevice[dirextXDevices];
			Array.Copy(temp, devices, dirextXDevices);
			return devices;
		}
public static CUdevice [ ] GetDirectXDevices ( IntPtr pD3DXDevice , CUd3dXDeviceList deviceList , DirectXVersion dXVersion ) { CUResult res ; int deviceCount = 0 ; res = DriverAPINativeMethods . DeviceManagement . cuDeviceGetCount ( ref deviceCount ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; if ( res == CUResult . ErrorNotInitialized ) { res = DriverAPINativeMethods . cuInit ( CUInitializationFlags . None ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; if ( res != CUResult . Success ) throw new CudaException ( res ) ; res = DriverAPINativeMethods . DeviceManagement . cuDeviceGetCount ( ref deviceCount ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; if ( res != CUResult . Success ) throw new CudaException ( res ) ; } else if ( res != CUResult . Success ) throw new CudaException ( res ) ; if ( deviceCount == 0 ) { throw new CudaException ( CUResult . ErrorNoDevice , " " , null ) ; } CUdevice [ ] devices ; CUdevice [ ] temp = new CUdevice [ deviceCount ] ; int dirextXDevices = 0 ; switch ( dXVersion ) { case DirectXVersion . D3D9 : res = DirectX9NativeMethods . CUDA3 . cuD3D9GetDevices ( ref dirextXDevices , temp , ( uint ) deviceCount , pD3DXDevice , deviceList ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; break ; case DirectXVersion . D3D10 : res = DirectX10NativeMethods . CUDA3 . cuD3D10GetDevices ( ref dirextXDevices , temp , ( uint ) deviceCount , pD3DXDevice , deviceList ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; break ; case DirectXVersion . D3D11 : res = DirectX11NativeMethods . cuD3D11GetDevices ( ref dirextXDevices , temp , ( uint ) deviceCount , pD3DXDevice , deviceList ) ; Debug . WriteLine ( String . Format ( " " , DateTime . Now , " " , res ) ) ; break ; default : throw new ArgumentException ( " " , " " ) ; } if ( res != CUResult . Success ) throw new CudaException ( res ) ; if ( dirextXDevices == 0 ) { throw new CudaException ( CUResult . ErrorNoDevice , " " , null ) ; } devices = new CUdevice [ dirextXDevices ] ; Array . Copy ( temp , devices , dirextXDevices ) ; return devices ; }
Returns a list of possible CUDA devices to use for a given DirectX device
Returns a list of possible CUDA devices to use for a given DirectX device
//Don't use .Take to stay compatible with .net 2 and 3 //devices = temp.Take(dirextXDevices).ToArray();
None
pD3DXDevice
IntPtr
None
deviceList
CUd3dXDeviceList
None
dXVersion
DirectXVersion
DirectX version of the directX device
DirectX version of the directX device