query_capability() private method

private query_capability ( int capability ) : int
capability int
return int
        /// <summary>
        /// Measures peak memory usage of the MKL memory pool.
        /// </summary>
        /// <param name="reset">Whether the usage counter should be reset.</param>
        /// <returns>The peak number of bytes allocated to all memory buffers.</returns>
        public long PeakMemoryStatistics(bool reset = true)
        {
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
            {
                throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
            }

            return(SafeNativeMethods.peak_mem_usage((int)(reset ? MklMemoryRequestMode.PeakMemoryReset : MklMemoryRequestMode.PeakMemory)));
        }
        /// <summary>
        /// Retrieves information about the MKL memory pool.
        /// </summary>
        /// <param name="allocatedBuffers">On output, returns the number of memory buffers allocated.</param>
        /// <returns>Returns the number of bytes allocated to all memory buffers.</returns>
        public long MemoryStatistics(out int allocatedBuffers)
        {
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
            {
                throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
            }

            return(SafeNativeMethods.mem_stat(out allocatedBuffers));
        }
        /// <summary>
        /// Disable gathering of peak memory statistics of the MKL memory pool.
        /// </summary>
        public void DisablePeakMemoryStatistics()
        {
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
            {
                throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
            }

            SafeNativeMethods.peak_mem_usage((int)MklMemoryRequestMode.Disable);
        }
        /// <summary>
        /// Frees the memory allocated to the MKL memory pool on the current thread.
        /// </summary>
        public void ThreadFreeBuffers()
        {
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
            {
                throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
            }

            SafeNativeMethods.thread_free_buffers();
        }
        /// <summary>
        /// Disable the MKL memory pool. May impact performance.
        /// </summary>
        public void DisableMemoryPool()
        {
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Memory) < 1)
            {
                throw new NotSupportedException("MKL Native Provider does not support memory management functions. Consider upgrading to a newer version.");
            }

            SafeNativeMethods.disable_fast_mm();
        }
Example #6
0
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            int a, b, linearAlgebra;

            try
            {
                // Load the native library
                NativeProviderLoader.TryLoad(SafeNativeMethods.DllName);

                a = SafeNativeMethods.query_capability(0);
                b = SafeNativeMethods.query_capability(1);

                _nativeIX86 = SafeNativeMethods.query_capability((int)ProviderPlatform.x86) > 0;
                _nativeX64  = SafeNativeMethods.query_capability((int)ProviderPlatform.x64) > 0;
                _nativeIA64 = SafeNativeMethods.query_capability((int)ProviderPlatform.ia64) > 0;

                _nativeRevision = SafeNativeMethods.query_capability((int)ProviderConfig.Revision);
                linearAlgebra   = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebra);
            }
            catch (DllNotFoundException e)
            {
                throw new NotSupportedException("MKL Native Provider not found.", e);
            }
            catch (BadImageFormatException e)
            {
                throw new NotSupportedException("MKL Native Provider found but failed to load. Please verify that the platform matches (x64 vs x32, Windows vs Linux).", e);
            }
            catch (EntryPointNotFoundException e)
            {
                throw new NotSupportedException("MKL Native Provider does not support capability querying and is therefore not compatible. Consider upgrading to a newer version.", e);
            }

            if (a != 0 || b != -1 || _nativeRevision < 4)
            {
                throw new NotSupportedException("MKL Native Provider too old. Consider upgrading to a newer version.");
            }

            // we only support exactly one major version, since major version changes imply a breaking change.
            if (linearAlgebra != 2)
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting linear algebra v2 but provider implements v{0}.", linearAlgebra));
            }

            // set numerical consistency, precision and accuracy modes, if supported
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Precision) > 0)
            {
                SafeNativeMethods.set_consistency_mode((int)_consistency);
                SafeNativeMethods.set_vml_mode((uint)_precision | (uint)_accuracy);
            }

            // set threading settings, if supported
            if (SafeNativeMethods.query_capability((int)ProviderConfig.Threading) > 0)
            {
                SafeNativeMethods.set_max_threads(Control.MaxDegreeOfParallelism);
            }
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            // TODO: Choose x86 or x64 based on Environment.Is64BitProcess

            int a, b, linearAlgebra;

            try
            {
                a = SafeNativeMethods.query_capability(0);
                b = SafeNativeMethods.query_capability(1);

                _nativeIX86 = SafeNativeMethods.query_capability(8) > 0;
                _nativeX64  = SafeNativeMethods.query_capability(9) > 0;
                _nativeIA64 = SafeNativeMethods.query_capability(10) > 0;

                _nativeRevision = SafeNativeMethods.query_capability(64);
                linearAlgebra   = SafeNativeMethods.query_capability(128);
            }
            catch (DllNotFoundException e)
            {
                throw new NotSupportedException("MKL Native Provider not found.", e);
            }
            catch (BadImageFormatException e)
            {
                throw new NotSupportedException("MKL Native Provider found but failed to load. Please verify that the platform matches (x64 vs x32, Windows vs Linux).", e);
            }
            catch (EntryPointNotFoundException e)
            {
                // we currently accept this to continue to support the old version for a while.
                // however, this is planned to be dropped for the final v3 release at latest.
                // TODO: drop return statement and instead fail with the exception below
                return;

                throw new NotSupportedException("MKL Native Provider found but does not support capability querying and is therefore not compatible. Try to upgrade to a newer version.", e);
            }

            if (a != 0 || b != -1 || linearAlgebra <= 0 || _nativeRevision < 4)
            {
                throw new NotSupportedException("MKL Native Provider found but too old or not compatible.");
            }

            // set numerical consistency, precision and accuracy modes, if supported
            if (SafeNativeMethods.query_capability(65) > 0)
            {
                SafeNativeMethods.set_consistency_mode((int)_consistency);
                SafeNativeMethods.set_vml_mode((uint)_precision | (uint)_accuracy);
            }

            // set threading settings, if supported
            if (SafeNativeMethods.query_capability(66) > 0)
            {
                SafeNativeMethods.set_max_threads(Control.MaxDegreeOfParallelism);
            }
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            MklProvider.Load(minRevision: 4);
            MklProvider.ConfigurePrecision(_consistency, _precision, _accuracy);

            int linearAlgebra = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMajor);

            // we only support exactly one major version, since major version changes imply a breaking change.
            if (linearAlgebra != 2)
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting linear algebra v2 but provider implements v{0}.", linearAlgebra));
            }
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            int a, b, linearAlgebra;

            try
            {
                // Load the native library
                NativeProviderLoader.TryLoad(SafeNativeMethods.DllName);

                a = SafeNativeMethods.query_capability(0);
                b = SafeNativeMethods.query_capability(1);

                _nativeIX86 = SafeNativeMethods.query_capability(8) > 0;
                _nativeX64  = SafeNativeMethods.query_capability(9) > 0;
                _nativeIA64 = SafeNativeMethods.query_capability(10) > 0;

                _nativeRevision = SafeNativeMethods.query_capability(64);
                linearAlgebra   = SafeNativeMethods.query_capability(128);
            }
            catch (DllNotFoundException e)
            {
                throw new NotSupportedException("MKL Native Provider not found.", e);
            }
            catch (BadImageFormatException e)
            {
                throw new NotSupportedException("MKL Native Provider found but failed to load. Please verify that the platform matches (x64 vs x32, Windows vs Linux).", e);
            }
            catch (EntryPointNotFoundException e)
            {
                throw new NotSupportedException("MKL Native Provider does not support capability querying and is therefore not compatible. Consider upgrading to a newer version.", e);
            }

            if (a != 0 || b != -1 || linearAlgebra <= 0 || _nativeRevision < 4)
            {
                throw new NotSupportedException("MKL Native Provider too old or not compatible. Consider upgrading to a newer version.");
            }

            // set numerical consistency, precision and accuracy modes, if supported
            if (SafeNativeMethods.query_capability(65) > 0)
            {
                SafeNativeMethods.set_consistency_mode((int)_consistency);
                SafeNativeMethods.set_vml_mode((uint)_precision | (uint)_accuracy);
            }

            // set threading settings, if supported
            if (SafeNativeMethods.query_capability(66) > 0)
            {
                SafeNativeMethods.set_max_threads(Control.MaxDegreeOfParallelism);
            }
        }
        /// <summary>
        /// Initialize and verify that the provided is indeed available.
        /// If calling this method fails, consider to fall back to alternatives like the managed provider.
        /// </summary>
        public override void InitializeVerify()
        {
            int revision = MklProvider.Load(_hintPath, _consistency, _precision, _accuracy);

            if (revision < MinimumCompatibleRevision)
            {
                throw new NotSupportedException($"MKL Native Provider revision r{revision} is too old. Consider upgrading to a newer version. Revision r{MinimumCompatibleRevision} and newer are supported.");
            }

            _linearAlgebraMajor   = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMajor);
            _linearAlgebraMinor   = SafeNativeMethods.query_capability((int)ProviderCapability.LinearAlgebraMinor);
            _vectorFunctionsMajor = SafeNativeMethods.query_capability((int)ProviderCapability.VectorFunctionsMajor);
            _vectorFunctionsMinor = SafeNativeMethods.query_capability((int)ProviderCapability.VectorFunctionsMinor);

            // we only support exactly one major version, since major version changes imply a breaking change.
            if (_linearAlgebraMajor != 2)
            {
                throw new NotSupportedException(string.Format("MKL Native Provider not compatible. Expecting linear algebra v2 but provider implements v{0}.", _linearAlgebraMajor));
            }
        }