Browse Source

Moved initialization of default LinearAlgebraProvider out of Control.ConfigureAuto to prevent unnecessary initialization and exceptions.

Initialization code moved to new method, InitializeDefaultLinearAlgebraProvider(), which is called from the LinearAlgebraProvider getter only if the current _linearAlgebraProvider is null. This prevents trying to initialize multiple providers when calling any of the 'UseXX' methods.
netstandard
Kuan Bartel 11 years ago
parent
commit
b3e16d8070
  1. 75
      src/Numerics/Control.cs

75
src/Numerics/Control.cs

@ -46,6 +46,7 @@ namespace MathNet.Numerics
static int _parallelizeOrder; static int _parallelizeOrder;
static int _parallelizeElements; static int _parallelizeElements;
static ILinearAlgebraProvider _linearAlgebraProvider; static ILinearAlgebraProvider _linearAlgebraProvider;
static readonly object _staticLock = new object();
static Control() static Control()
{ {
@ -64,48 +65,50 @@ namespace MathNet.Numerics
_parallelizeOrder = 64; _parallelizeOrder = 64;
_parallelizeElements = 300; _parallelizeElements = 300;
TaskScheduler = TaskScheduler.Default; TaskScheduler = TaskScheduler.Default;
}
// Linear Algebra Provider private static void InitializeDefaultLinearAlgebraProvider()
#if PORTABLE {
LinearAlgebraProvider = new ManagedLinearAlgebraProvider(); lock (_staticLock)
#else
try
{ {
var value = Environment.GetEnvironmentVariable(EnvVarLAProvider); if (_linearAlgebraProvider == null)
switch (value != null ? value.ToUpperInvariant() : string.Empty)
{ {
#if NATIVE #if NATIVE
case "MKL": try
UseNativeMKL(); {
break; var value = Environment.GetEnvironmentVariable(EnvVarLAProvider);
switch (value != null ? value.ToUpperInvariant() : string.Empty)
case "CUDA":
UseNativeCUDA();
break;
case "OPENBLAS":
UseNativeOpenBLAS();
break;
default:
if (!TryUseNative())
{ {
UseManaged(); case "MKL":
UseNativeMKL();
break;
case "CUDA":
UseNativeCUDA();
break;
case "OPENBLAS":
UseNativeOpenBLAS();
break;
default:
if (!TryUseNative())
{
UseManaged();
}
break;
} }
break; }
#else catch
default: {
// We don't care about any failures here at all (because "auto")
UseManaged(); UseManaged();
break; }
#else
UseManaged();
#endif #endif
} }
} }
catch
{
// We don't care about any failures here at all (because "auto")
UseManaged();
}
#endif
} }
public static void UseManaged() public static void UseManaged()
@ -261,7 +264,13 @@ namespace MathNet.Numerics
/// <value>The linear algebra provider.</value> /// <value>The linear algebra provider.</value>
public static ILinearAlgebraProvider LinearAlgebraProvider public static ILinearAlgebraProvider LinearAlgebraProvider
{ {
get { return _linearAlgebraProvider; } get
{
if (_linearAlgebraProvider == null)
InitializeDefaultLinearAlgebraProvider();
return _linearAlgebraProvider;
}
set set
{ {
value.InitializeVerify(); value.InitializeVerify();

Loading…
Cancel
Save