private static GPGPU DoCreateDevice(eGPUType target, int deviceId = 0) { try { if (target == eGPUType.Cuda) { return(new CudaGPU(deviceId)); } else if (target == eGPUType.Emulator) { return(new EmulatedGPU(deviceId)); } else if (target == eGPUType.OpenCL) { return(new OpenCLDevice(deviceId)); } else { throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, target); } } catch (Exception) { throw; } throw new NotSupportedException(target.ToString()); }
/// <summary> /// Gets the device count. /// </summary> /// <param name="type">The type of device.</param> /// <returns>Number of devices of type specified.</returns> public static int GetDeviceCount(eGPUType type) { int cnt = 0; if (type == eGPUType.Emulator) { cnt = GPGPUs.Count(g => g.Key.StartsWith(eGPUType.Emulator.ToString())); if (cnt == 0) { GetDevice(eGPUType.Emulator, 0); cnt++; } } else if (type == eGPUType.Cuda) { cnt += CudaGPU.GetDeviceCount(); } else if (type == eGPUType.OpenCL) { foreach (var platform in ComputePlatform.Platforms) { cnt += platform.Devices.Count; } } else { throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, type); } return(cnt); }
/// <summary> /// Static constructor for the <see cref="CudafyModes"/> class. /// Sets CodeGen to CudaC, Compiler to CudaNvcc, Target to Cuda and Mode to Cuda. /// </summary> static CudafyModes() { //CodeGen = eGPUCodeGenerator.CudaC; Compiler = eGPUCompiler.CudaNvcc; Target = eGPUType.Cuda; Mode = eCudafyQuickMode.Cuda; DeviceId = 0; }
/// <summary> /// Creates the specified gpu type. /// </summary> /// <param name="gpuType">Type of the gpu.</param> /// <returns></returns> public static GPGPUR Create(eGPUType gpuType) { if (gpuType == eGPUType.Cuda) { return(new CudaR()); } else { throw new NotSupportedException(gpuType.ToString()); } }
/// <summary> /// Gets the device properties. /// </summary> /// <param name="type">The type of GPU.</param> /// <param name="useAdvanced">Whether to get the additional device settings via the cudart dll.</param> /// <returns>Device properties for all devices of the specified type.</returns> public static IEnumerable <GPGPUProperties> GetDeviceProperties(eGPUType type, bool useAdvanced = true) { if (type == eGPUType.Emulator) { foreach (var kvp in GPGPUs.Where(g => g.Value is EmulatedGPU)) { yield return(kvp.Value.GetDeviceProperties(useAdvanced)); } } else if (type == eGPUType.Cuda) { // Store the current context CUcontext?ctx = CUDA.TryGetCurrentContext(); GPGPU currentGPU = null; int devCnt = CudaGPU.GetDeviceCount(); for (int i = 0; i < devCnt; i++) { CudaGPU gpu = null; GPGPUProperties props; gpu = (CudaGPU)GetDevice(eGPUType.Cuda, i); if (gpu == null) { throw new CudafyHostException(CudafyHostException.csDEVICE_X_NOT_FOUND, string.Format("{0}{1}", eGPUType.Cuda.ToString(), i)); } props = gpu.GetDeviceProperties(useAdvanced); if (ctx != null && gpu.GetDeviceContext().Pointer == ctx.Value.Pointer) { currentGPU = gpu; } yield return(props); } // Reset context to current GPU if (ctx != null && currentGPU != null) { currentGPU.SetCurrentContext(); } } else if (type == eGPUType.OpenCL) { int deviceId = 0; foreach (ComputeDevice computeDevice in OpenCLDevice.ComputeDevices) { yield return(OpenCLDevice.GetDeviceProperties(computeDevice, deviceId++)); } } else { throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, type); } }
/// <summary> /// Creates a new GPGPU and adds to cache. If GPGPU already exists then it is first destroyed and removed from cache. /// </summary> /// <param name="type">The target type.</param> /// <param name="deviceId">The device id.</param> /// <returns>GPGPU instance.</returns> public static GPGPU CreateDevice(eGPUType type, int deviceId = 0) { string name = BuildGPUName(type, deviceId); GPGPU gpu; if (GPGPUs.ContainsKey(name)) { gpu = GPGPUs[name]; RemoveDevice(gpu); } gpu = DoCreateDevice(type, deviceId); GPGPUs.Add(name, gpu); return(gpu); }
/// <summary> /// Gets device of type specified from the cache. Creates one if it does not already exist. /// Sets the current context to the returned device. /// </summary> /// <param name="type">The target type.</param> /// <param name="deviceId">The device id.</param> /// <returns>GPGPU instance.</returns> public static GPGPU GetDevice(eGPUType type = eGPUType.Cuda, int deviceId = 0) { string name = BuildGPUName(type, deviceId); GPGPU gpu = null; if (!GPGPUs.ContainsKey(name)) { return(CreateDevice(type, deviceId)); } else { gpu = GPGPUs[name]; if (gpu.IsDisposed) { gpu = CreateDevice(type, deviceId); } } gpu.SetCurrentContext(); return(gpu); }
/// <summary> /// Gets the device properties. /// </summary> /// <param name="type">The type of GPU.</param> /// <param name="useAdvanced">Whether to get the additional device settings via the cudart dll.</param> /// <returns>Device properties for all devices of the specified type.</returns> public static IEnumerable<GPGPUProperties> GetDeviceProperties(eGPUType type, bool useAdvanced = true) { if (type == eGPUType.Emulator) { foreach (var kvp in GPGPUs.Where(g => g.Value is EmulatedGPU)) yield return kvp.Value.GetDeviceProperties(useAdvanced); } else if (type == eGPUType.Cuda) { // Store the current context CUcontext? ctx = CUDA.TryGetCurrentContext(); GPGPU currentGPU = null; int devCnt = CudaGPU.GetDeviceCount(); for (int i = 0; i < devCnt; i++) { CudaGPU gpu = null; GPGPUProperties props; gpu = (CudaGPU)GetDevice(eGPUType.Cuda, i); if (gpu == null) throw new CudafyHostException(CudafyHostException.csDEVICE_X_NOT_FOUND, string.Format("{0}{1}", eGPUType.Cuda.ToString(), i)); props = gpu.GetDeviceProperties(useAdvanced); if (ctx != null && gpu.GetDeviceContext().Pointer == ctx.Value.Pointer) currentGPU = gpu; yield return props; } // Reset context to current GPU if (ctx != null && currentGPU != null) currentGPU.SetCurrentContext(); } else if (type == eGPUType.OpenCL) { int deviceId = 0; foreach (ComputeDevice computeDevice in OpenCLDevice.ComputeDevices) yield return OpenCLDevice.GetDeviceProperties(computeDevice, deviceId++); } else throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, type); }
/// <summary> /// Checks if the specified device has already been created and added to the cache. /// </summary> /// <param name="type">The type.</param> /// <param name="deviceId">The device id.</param> /// <returns>True if created, else false.</returns> public static bool DeviceCreated(eGPUType type, int deviceId = 0) { string name = BuildGPUName(type, deviceId); return GPGPUs.ContainsKey(name); }
private static string BuildGPUName(eGPUType type, int deviceId) { string name = type.ToString() + deviceId.ToString(); return(name); }
public static GPGPU GetGPGPU(eGPUType type, int deviceId = 0) { return(GetDevice(type, deviceId)); }
/// <summary> /// Checks if the specified device has already been created and added to the cache. /// </summary> /// <param name="type">The type.</param> /// <param name="deviceId">The device id.</param> /// <returns>True if created, else false.</returns> public static bool DeviceCreated(eGPUType type, int deviceId = 0) { string name = BuildGPUName(type, deviceId); return(GPGPUs.ContainsKey(name)); }
private static string BuildGPUName(eGPUType type, int deviceId) { string name = type.ToString() + deviceId.ToString(); return name; }
private void InitializeGPUs() { eGPUType[] gpuTypes = new eGPUType[] { eGPUType.Cuda, eGPUType.OpenCL, eGPUType.Emulator }; eLanguage[] languages = new eLanguage[] { eLanguage.Cuda, eLanguage.OpenCL }; foreach (eGPUType gpuType in gpuTypes) { try { int numberOfAvailableDevices = CudafyHost.GetDeviceCount(gpuType); for (int deviceNumber = 0; deviceNumber < numberOfAvailableDevices; deviceNumber++) { GPGPU gpgpu = CudafyHost.GetDevice(gpuType, deviceNumber); GPGPUProperties gpgpuProperties = gpgpu.GetDeviceProperties(true); CudafyModes.Target = gpuType; foreach (eLanguage language in languages) { string cudaRandomFilename = Path.GetRandomFileName(); try { CudafyTranslator.Language = language; CompileProperties compileProperties = CompilerHelper.Create(ePlatform.Auto, eArchitecture.Unknown, eCudafyCompileMode.Default, CudafyTranslator.WorkingDirectory, CudafyTranslator.GenerateDebug); // Use a random filename to prevent conflict on default temp file when multithreading (unit tests) compileProperties.InputFile = cudaRandomFilename; // If this line fails with NCrunch/Unit tests, there probably is a new version of Cudafy.NET // and it needs to be registered in the GAC like this: gacutil -i Cudafy.NET.dll CudafyModule cudafyModule = CudafyTranslator.Cudafy(compileProperties, typeof(Primitives)); if (!gpgpu.IsModuleLoaded(cudafyModule.Name)) { gpgpu.LoadModule(cudafyModule); } gpgpu.EnableMultithreading(); string gpuName = gpgpuProperties.Name.Trim() + " - " + gpuType.ToString() + " - " + language.ToString(); ////this.gpgpus.Add(gpuName, gpgpu); ////this.gpgpuProperties.Add(gpuName, gpgpuProperties); ////this.gpuTypes.Add(gpuName, gpuType); } catch (CudafyCompileException) { // Language not supported } finally { File.Delete(cudaRandomFilename); // ncrunch: no coverage start } } } } catch (DllNotFoundException) { } catch (InvalidOperationException) { // Language not supported } catch (Cloo.ComputeException) { // Language not supported } // ncrunch: no coverage end } }
/// <summary> /// Gets device of type specified from the cache. Creates one if it does not already exist. /// Sets the current context to the returned device. /// </summary> /// <param name="type">The target type.</param> /// <param name="deviceId">The device id.</param> /// <returns>GPGPU instance.</returns> public static GPGPU GetDevice(eGPUType type = eGPUType.Cuda, int deviceId = 0) { string name = BuildGPUName(type, deviceId); GPGPU gpu = null; if (!GPGPUs.ContainsKey(name)) { return CreateDevice(type, deviceId); } else { gpu = GPGPUs[name]; if (gpu.IsDisposed) { gpu = CreateDevice(type, deviceId); } } gpu.SetCurrentContext(); return gpu; }
public GPUModule(eGPUType gpuType = eGPUType.Cuda) { _gpuType = gpuType; }
private static GPGPU DoCreateDevice(eGPUType target, int deviceId = 0) { try { if (target == eGPUType.Cuda) return new CudaGPU(deviceId); else if (target == eGPUType.Emulator) return new EmulatedGPU(deviceId); else if (target == eGPUType.OpenCL) return new OpenCLDevice(deviceId); else throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, target); } catch (Exception) { throw; } throw new NotSupportedException(target.ToString()); }
/// <summary> /// Creates a new GPGPU and adds to cache. If GPGPU already exists then it is first destroyed and removed from cache. /// </summary> /// <param name="type">The target type.</param> /// <param name="deviceId">The device id.</param> /// <returns>GPGPU instance.</returns> public static GPGPU CreateDevice(eGPUType type, int deviceId = 0) { string name = BuildGPUName(type, deviceId); GPGPU gpu; if (GPGPUs.ContainsKey(name)) { gpu = GPGPUs[name]; RemoveDevice(gpu); } gpu = DoCreateDevice(type, deviceId); GPGPUs.Add(name, gpu); return gpu; }
/// <summary> /// Gets the GPU from cache of type implied by specified architecture. Creates one if it does not already exist. /// Sets the current context to the returned device. /// </summary> /// <param name="arch">Architecture type.</param> /// <param name="deviceId">The device id.</param> /// <returns>GPGPU instance.</returns> public static GPGPU GetDevice(eArchitecture arch, int deviceId = 0) { eGPUType type = CompilerHelper.GetGPUType(arch); return(GetDevice(type, deviceId)); }
/// <summary> /// Gets the device count. /// </summary> /// <param name="type">The type of device.</param> /// <returns>Number of devices of type specified.</returns> public static int GetDeviceCount(eGPUType type) { int cnt = 0; if (type == eGPUType.Emulator) { cnt = GPGPUs.Count(g => g.Key.StartsWith(eGPUType.Emulator.ToString())); if (cnt == 0) { GetDevice(eGPUType.Emulator, 0); cnt++; } } else if (type == eGPUType.Cuda) { cnt += CudaGPU.GetDeviceCount(); } else if (type == eGPUType.OpenCL) { foreach (var platform in ComputePlatform.Platforms) cnt += platform.Devices.Count; } else throw new CudafyHostException(CudafyHostException.csX_NOT_CURRENTLY_SUPPORTED, type); return cnt; }
public static GPGPU GetGPGPU(eGPUType type, int deviceId = 0) { return GetDevice(type, deviceId); }