// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2010 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics { using System; using Algorithms.LinearAlgebra; /// /// Sets parameters for the library. /// public static class Control { /// /// Initial number of threads to use; /// private static int _numberOfThreads = Environment.ProcessorCount; /// /// Initial block size for the native linear algebra provider. /// private static int _blockSize = 512; /// /// Initial parallel order size for the matrix multiply in linear algebra provider. /// private static int _parallelizeOrder = 64; /// /// The default cutoff point for order size for the matrix multiply in linear algebra provider. /// private static int _parallelizeElements = 300; /// /// Initializes static members of the Control class. /// static Control() { CheckDistributionParameters = true; ThreadSafeRandomNumberGenerators = true; DisableParallelization = false; LinearAlgebraProvider = new ManagedLinearAlgebraProvider(); } /// /// Gets or sets a value indicating whether the distribution classes check validate each parameter. /// For the multivariate distributions this could involve an expensive matrix factorization. /// The default setting of this property is true. /// public static bool CheckDistributionParameters { get; set; } /// /// Gets or sets a value indicating whether to use thread safe random number generators (RNG). /// Thread safe RNG about two and half time slower than non-thread safe RNG. /// /// /// true to use thread safe random number generators ; otherwise, false. /// public static bool ThreadSafeRandomNumberGenerators { get; set; } /// /// Gets or sets a value indicating whether parallelization shall be disabled globally. /// public static bool DisableParallelization { get; set; } /// /// Gets or sets the linear algebra provider. /// /// The linear algebra provider. public static ILinearAlgebraProvider LinearAlgebraProvider { get; set; } /// /// Gets or sets a value indicating how many parallel worker threads shall be used /// when parallelization is applicable. /// /// The Silverlight version of the library defaults to one thread. public static int NumberOfParallelWorkerThreads { get { return _numberOfThreads; } set { // instead of throwing an out of range exception, simply normalize _numberOfThreads = Math.Max(1, Math.Min(1024, value)); } } /// /// Gets or sets the the block size to use for the native linear /// algebra provider. /// /// The block size. Must be at least 32. public static int BlockSize { get { return _blockSize; } set { if (_blockSize > 31) { _blockSize = value; } } } /// /// Gets or sets the order of the matrix when linear algebra provider must calculate multiply in parallel threads. /// /// The order. Default is 64. public static int ParallelizeOrder { get { return _parallelizeOrder; } set { if (_parallelizeOrder > 2) { _parallelizeOrder = value; } } } /// /// Gets or sets the number of elements a vector or matrix must contain before we multiply threads. /// /// Number of elements. Default is 300. public static int ParallelizeElements { get { return _parallelizeElements; } set { if (_parallelizeElements > 2) { _parallelizeElements = value; } } } /// /// Given the number elements, should the operation be parallelized. /// /// The number elements to check. /// true if the operation should be parallelized; false otherwise. public static bool ParallelizeOperation(int elements) { return elements < ParallelizeElements || DisableParallelization || NumberOfParallelWorkerThreads < 2; } } }