Example #1
0
        private void EnsureDevicePresent()
        {
            if (mDevice != null)
            {
                return;
            }

            DiagnosticsWriter.WriteInformation("Connecting to computation device");

            var flags = mIsDebugModeEnabled
                                ? DeviceCreationFlags.Debug
                                : DeviceCreationFlags.None;

            if (flags.HasFlag(DeviceCreationFlags.Debug))
            {
                DiagnosticsWriter.WriteWarning("Computations are being run in debug mode. Performance will degrade.");
            }

            var adapterCount = mFactory.GetAdapterCount();
            var error        = (Exception)null;

            for (var i = 0; i < adapterCount; i++)
            {
                var adapter      = mFactory.GetAdapter(i);
                var featureLevel = Device.GetSupportedFeatureLevel(adapter);

                if (featureLevel < FeatureLevel.Level_11_0)
                {
                    DiagnosticsWriter.WriteDebug("Skipping \"{0}\" because of an insufficient feature level ({1} < {2})",
                                                 adapter.Description.Description,
                                                 featureLevel.ToString(),
                                                 nameof(FeatureLevel.Level_11_0));

                    continue;
                }

                try
                {
                    mDevice = new Device(adapter, flags, FeatureLevel.Level_11_0);
                    DiagnosticsWriter.WriteInformation("Connection to device established: {0}", adapter.Description.Description);
                }
                catch (Exception ex)
                {
                    error = ex;
                    continue;
                }

                break;
            }

            if (mDevice == null)
            {
                if (error != null)
                {
                    throw new Exception(string.Format(error.Message, error));
                }

                throw new NotSupportedException("No suitable adapter could be found.");
            }
        }