Exemplo n.º 1
0
        /// <summary>
        /// Get a CUDA context with preference for a certain deviceId, optionally accept any device.
        /// </summary>
        /// <param name="deviceId">The device id.</param>
        /// <param name="acceptOtherDevices">Optionally indicate if any device is acceptable (if preferred deviceId is not available).</param>
        /// <returns></returns>
        internal static CudaContext GetContextForDeviceId(int deviceId, bool acceptOtherDevices = true)
        {
            foreach (CudaContext context in RegisteredContexts)
            {
                if (context.DeviceId == deviceId)
                {
                    return(context);
                }
            }

            if (acceptOtherDevices && RegisteredContexts.Count > 0)
            {
                return(RegisteredContexts.FirstAvailable());
            }

            if (acceptOtherDevices)
            {
                throw new InvalidOperationException($"No valid cuda context is currently registered.");
            }

            throw new InvalidOperationException($"No valid cuda context with the device id {deviceId} is currently registered " +
                                                $"(use {nameof(acceptOtherDevices)} flag to accept any devices if the preferred one is unavailable).");
        }