/// <summary> /// Detects OpenCL devices. /// </summary> /// <param name="predicate"> /// The predicate to include a given device. /// </param> /// <param name="registry">The registry to add all devices to.</param> private static void GetDevicesInternal( Predicate <CLDevice> predicate, DeviceRegistry registry) { var devices = new IntPtr[MaxNumDevicesPerPlatform]; // Resolve all platforms if (!CurrentAPI.IsSupported || CurrentAPI.GetNumPlatforms(out int numPlatforms) != CLError.CL_SUCCESS || numPlatforms < 1) { return; } var platforms = new IntPtr[numPlatforms]; if (CurrentAPI.GetPlatforms(platforms, ref numPlatforms) != CLError.CL_SUCCESS) { return; } foreach (var platform in platforms) { // Resolve all devices int numDevices = devices.Length; Array.Clear(devices, 0, numDevices); if (CurrentAPI.GetDevices( platform, CLDeviceType.CL_DEVICE_TYPE_ALL, devices, out numDevices) != CLError.CL_SUCCESS) { continue; } for (int i = 0; i < numDevices; ++i) { // Resolve device and ignore invalid devices var device = devices[i]; if (device == IntPtr.Zero) { continue; } // Check for available device if (CurrentAPI.GetDeviceInfo <int>( device, CLDeviceInfoType.CL_DEVICE_AVAILABLE) == 0) { continue; } var desc = new CLDevice(platform, device); registry.Register(desc, predicate); } } }
static CLAccelerator() { var accelerators = ImmutableArray.CreateBuilder <CLAcceleratorId>(); var allAccelerators = ImmutableArray.CreateBuilder <CLAcceleratorId>(); var devices = new IntPtr[MaxNumDevicesPerPlatform]; try { // Resolve all platforms if (!CurrentAPI.IsSupported || CurrentAPI.GetNumPlatforms(out int numPlatforms) != CLError.CL_SUCCESS || numPlatforms < 1) { return; } var platforms = new IntPtr[numPlatforms]; if (CurrentAPI.GetPlatforms(platforms, out numPlatforms) != CLError.CL_SUCCESS) { return; } foreach (var platform in platforms) { // Resolve all devices int numDevices = devices.Length; Array.Clear(devices, 0, numDevices); if (CurrentAPI.GetDevices( platform, CLDeviceType.CL_DEVICE_TYPE_ALL, devices, out numDevices) != CLError.CL_SUCCESS) { continue; } for (int i = 0; i < numDevices; ++i) { // Resolve device and ignore invalid devices var device = devices[i]; if (device == IntPtr.Zero) { continue; } // Check for available device if (CurrentAPI.GetDeviceInfo <int>( device, CLDeviceInfoType.CL_DEVICE_AVAILABLE) == 0) { continue; } var acceleratorId = new CLAcceleratorId(platform, device); allAccelerators.Add(acceleratorId); if (acceleratorId.CVersion >= CLBackend.MinimumVersion) { accelerators.Add(acceleratorId); } } } } catch (Exception) { // Ignore API-specific exceptions at this point } finally { CLAccelerators = accelerators.ToImmutable(); AllCLAccelerators = allAccelerators.ToImmutable(); } }