예제 #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.");
            }
        }
예제 #2
0
        protected override void DisposeOverride()
        {
            DiagnosticsWriter.WriteInformation("Disconnecting from device");

            var resources = mResources?.ToArray() ?? new HardwareResource[0];

            if (resources.Length > 0)
            {
                DiagnosticsWriter.WriteWarning("The is being disconnected from but there are still {0} associated resource(s) allocated. The resources will be disposed to avoid memory leaks", resources.Length);

                foreach (var resource in resources)
                {
                    resource?.Dispose();
                }
            }

            mResources?.Clear();
            mResources = null;

            System.Diagnostics.Debug.Assert(!mDevice.Disposed);

            mDevice?.Dispose();
            mFactory?.Dispose();

            mDevice  = null;
            mFactory = null;

            DiagnosticsWriter.WriteInformation("Disconnection completed.");
        }
예제 #3
0
        internal void RemoveResource([NotNull] HardwareResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            mResources?.Remove(resource);
            DiagnosticsWriter.WriteDebug("Removed association of resource \"{0}\" from device.", resource);
        }
예제 #4
0
        internal void AddResource([NotNull] HardwareResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            if (mResources == null)
            {
                throw new ObjectDisposedException(nameof(AccelerationDevice), "The device is not available anymore.");
            }

            DiagnosticsWriter.WriteDebug("Associating resource \"{0}\" with device.", resource);
            mResources.Add(resource);
        }