From 4026fc896a48c8e7a0ce4865d671793e8951385e Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 4 Apr 2013 23:03:00 +0200 Subject: [PATCH] Tweak Control class --- src/Numerics/Control.cs | 107 ++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 64 deletions(-) diff --git a/src/Numerics/Control.cs b/src/Numerics/Control.cs index 122c09fc..8fc5e0ee 100644 --- a/src/Numerics/Control.cs +++ b/src/Numerics/Control.cs @@ -38,38 +38,33 @@ namespace MathNet.Numerics /// 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; + private static int _numberOfThreads; + private static int _blockSize; + private static int _parallelizeOrder; + private static int _parallelizeElements; - /// - /// Initializes static members of the Control class. - /// static Control() { + ConfigureAuto(); + } + + public static void ConfigureAuto() + { + // Random Numbers & Distributions CheckDistributionParameters = true; ThreadSafeRandomNumberGenerators = true; - DisableParallelization = false; - #if PORTABLE + // Parallelization & Threading + _numberOfThreads = Environment.ProcessorCount; + DisableParallelization = _numberOfThreads < 2; + _blockSize = 512; + _parallelizeOrder = 64; + _parallelizeElements = 300; + + // Linear Algebra Provider +#if PORTABLE LinearAlgebraProvider = new ManagedLinearAlgebraProvider(); - #else +#else try { const string name = "MathNetNumericsLAProvider"; @@ -89,7 +84,14 @@ namespace MathNet.Numerics // We don't care about any failures here at all LinearAlgebraProvider = new ManagedLinearAlgebraProvider(); } - #endif +#endif + } + + public static void ConfigureSingleThread() + { + _numberOfThreads = 1; + DisableParallelization = true; + ThreadSafeRandomNumberGenerators = false; } /// @@ -117,73 +119,50 @@ namespace MathNet.Numerics /// Gets or sets the linear algebra provider. /// /// The linear algebra provider. - public static ILinearAlgebraProvider LinearAlgebraProvider - { - get; - set; - } + 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. + /// Default to the number of processor cores, must be between 1 and 1024 (inclusive). 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)); - } + set { _numberOfThreads = Math.Max(1, Math.Min(1024, value)); } } /// - /// Gets or sets the the block size to use for the native linear - /// algebra provider. + /// Gets or sets the the block size to use for + /// the native linear algebra provider. /// - /// The block size. Must be at least 32. + /// The block size. Default 512, must be at least 32. public static int BlockSize { get { return _blockSize; } - set - { - if (_blockSize > 31) - { - _blockSize = value; - } - } + set { _blockSize = Math.Max(32, value); } } /// - /// Gets or sets the order of the matrix when linear algebra provider must calculate multiply in parallel threads. + /// Gets or sets the order of the matrix when linear algebra provider + /// must calculate multiply in parallel threads. /// - /// The order. Default is 64. + /// The order. Default 64, must be at least 3. public static int ParallelizeOrder { get { return _parallelizeOrder; } - set - { - if (_parallelizeOrder > 2) - { - _parallelizeOrder = value; - } - } + set { _parallelizeOrder = Math.Max(3, value); } } /// - /// Gets or sets the number of elements a vector or matrix must contain before we multiply threads. + /// Gets or sets the number of elements a vector or matrix + /// must contain before we multiply threads. /// - /// Number of elements. Default is 300. + /// Number of elements. Default 300, must be at least 3. public static int ParallelizeElements { get { return _parallelizeElements; } - set - { - if (_parallelizeElements > 2) - { - _parallelizeElements = value; - } - } + set { _parallelizeElements = Math.Max(3, value); } } ///