Browse Source

Provider Control: expose separate control for LA and FFT, more sensible semantics on the Try variants

benchmark-la
Christoph Ruegg 10 years ago
parent
commit
999d1454aa
  1. 49
      src/Numerics/Control.cs
  2. 29
      src/Numerics/Providers/FourierTransform/FourierTransformControl.cs
  3. 47
      src/Numerics/Providers/LinearAlgebra/LinearAlgebraControl.cs

49
src/Numerics/Control.cs

@ -3,7 +3,7 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// //
// Copyright (c) 2009-2015 Math.NET // Copyright (c) 2009-2016 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -72,6 +72,25 @@ namespace MathNet.Numerics
FourierTransformControl.UseManaged(); FourierTransformControl.UseManaged();
} }
/// <summary>
/// Use a specific provider if configured, e.g. using
/// environment variables, or fall back to the best providers.
/// </summary>
public static void UseDefaultProviders()
{
LinearAlgebraControl.UseDefault();
FourierTransformControl.UseDefault();
}
/// <summary>
/// Use the best provider available.
/// </summary>
public static void UseBestProviders()
{
LinearAlgebraControl.UseBest();
FourierTransformControl.UseBest();
}
#if NATIVE #if NATIVE
/// <summary> /// <summary>
@ -125,7 +144,9 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public static bool TryUseNativeMKL() public static bool TryUseNativeMKL()
{ {
return Try(UseNativeMKL); bool linearAlgebra = LinearAlgebraControl.TryUseNativeMKL();
bool fourierTransform = FourierTransformControl.TryUseNativeMKL();
return linearAlgebra || fourierTransform;
} }
/// <summary> /// <summary>
@ -146,7 +167,8 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public static bool TryUseNativeCUDA() public static bool TryUseNativeCUDA()
{ {
return Try(UseNativeCUDA); bool linearAlgebra = LinearAlgebraControl.TryUseNativeCUDA();
return linearAlgebra;
} }
/// <summary> /// <summary>
@ -167,7 +189,8 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public static bool TryUseNativeOpenBLAS() public static bool TryUseNativeOpenBLAS()
{ {
return Try(UseNativeOpenBLAS); bool linearAlgebra = LinearAlgebraControl.TryUseNativeOpenBLAS();
return linearAlgebra;
} }
/// <summary> /// <summary>
@ -179,21 +202,9 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public static bool TryUseNative() public static bool TryUseNative()
{ {
return TryUseNativeCUDA() || TryUseNativeMKL() || TryUseNativeOpenBLAS(); bool linearAlgebra = LinearAlgebraControl.TryUseNative();
} bool fourierTransform = FourierTransformControl.TryUseNative();
return linearAlgebra || fourierTransform;
static bool Try(Action action)
{
try
{
action();
return true;
}
catch
{
// intentionally swallow exceptions here - use the non-try variants if you're interested in why
return false;
}
} }
#endif #endif

29
src/Numerics/Providers/FourierTransform/FourierTransformControl.cs

@ -31,7 +31,7 @@ using System;
namespace MathNet.Numerics.Providers.FourierTransform namespace MathNet.Numerics.Providers.FourierTransform
{ {
internal static class FourierTransformControl public static class FourierTransformControl
{ {
const string EnvVarFFTProvider = "MathNetNumericsFFTProvider"; const string EnvVarFFTProvider = "MathNetNumericsFFTProvider";
@ -45,9 +45,14 @@ namespace MathNet.Numerics.Providers.FourierTransform
{ {
Control.FourierTransformProvider = new Mkl.MklFourierTransformProvider(); Control.FourierTransformProvider = new Mkl.MklFourierTransformProvider();
} }
public static bool TryUseNativeMKL()
{
return TryUse(new Mkl.MklFourierTransformProvider());
}
#endif #endif
public static bool TryUse(IFourierTransformProvider provider) static bool TryUse(IFourierTransformProvider provider)
{ {
try try
{ {
@ -66,10 +71,21 @@ namespace MathNet.Numerics.Providers.FourierTransform
} }
} }
/// <summary>
/// Try to use a native provider, if available.
/// </summary>
public static bool TryUseNative()
{
return TryUseNativeMKL();
}
/// <summary>
/// Use the best provider available.
/// </summary>
public static void UseBest() public static void UseBest()
{ {
#if NATIVE #if NATIVE
if (!TryUse(new Mkl.MklFourierTransformProvider())) if (!TryUseNative())
{ {
UseManaged(); UseManaged();
} }
@ -78,6 +94,11 @@ namespace MathNet.Numerics.Providers.FourierTransform
#endif #endif
} }
/// <summary>
/// Use a specific provider if configured, e.g. using the
/// "MathNetNumericsFFTProvider" environment variable,
/// or fall back to the best provider.
/// </summary>
public static void UseDefault() public static void UseDefault()
{ {
#if NATIVE #if NATIVE
@ -94,7 +115,7 @@ namespace MathNet.Numerics.Providers.FourierTransform
break; break;
} }
#else #else
UseManaged(); UseBest();
#endif #endif
} }
} }

47
src/Numerics/Providers/LinearAlgebra/LinearAlgebraControl.cs

@ -31,7 +31,7 @@ using System;
namespace MathNet.Numerics.Providers.LinearAlgebra namespace MathNet.Numerics.Providers.LinearAlgebra
{ {
internal static class LinearAlgebraControl public static class LinearAlgebraControl
{ {
const string EnvVarLAProvider = "MathNetNumericsLAProvider"; const string EnvVarLAProvider = "MathNetNumericsLAProvider";
@ -41,6 +41,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
} }
#if NATIVE #if NATIVE
[CLSCompliant(false)]
public static void UseNativeMKL( public static void UseNativeMKL(
Common.Mkl.MklConsistency consistency = Common.Mkl.MklConsistency.Auto, Common.Mkl.MklConsistency consistency = Common.Mkl.MklConsistency.Auto,
Common.Mkl.MklPrecision precision = Common.Mkl.MklPrecision.Double, Common.Mkl.MklPrecision precision = Common.Mkl.MklPrecision.Double,
@ -49,18 +50,37 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
Control.LinearAlgebraProvider = new Mkl.MklLinearAlgebraProvider(consistency, precision, accuracy); Control.LinearAlgebraProvider = new Mkl.MklLinearAlgebraProvider(consistency, precision, accuracy);
} }
[CLSCompliant(false)]
public static bool TryUseNativeMKL(
Common.Mkl.MklConsistency consistency = Common.Mkl.MklConsistency.Auto,
Common.Mkl.MklPrecision precision = Common.Mkl.MklPrecision.Double,
Common.Mkl.MklAccuracy accuracy = Common.Mkl.MklAccuracy.High)
{
return TryUse(new Mkl.MklLinearAlgebraProvider(consistency, precision, accuracy));
}
public static void UseNativeCUDA() public static void UseNativeCUDA()
{ {
Control.LinearAlgebraProvider = new Cuda.CudaLinearAlgebraProvider(); Control.LinearAlgebraProvider = new Cuda.CudaLinearAlgebraProvider();
} }
public static bool TryUseNativeCUDA()
{
return TryUse(new Cuda.CudaLinearAlgebraProvider());
}
public static void UseNativeOpenBLAS() public static void UseNativeOpenBLAS()
{ {
Control.LinearAlgebraProvider = new OpenBlas.OpenBlasLinearAlgebraProvider(); Control.LinearAlgebraProvider = new OpenBlas.OpenBlasLinearAlgebraProvider();
} }
public static bool TryUseNativeOpenBLAS()
{
return TryUse(new OpenBlas.OpenBlasLinearAlgebraProvider());
}
#endif #endif
public static bool TryUse(ILinearAlgebraProvider provider) static bool TryUse(ILinearAlgebraProvider provider)
{ {
try try
{ {
@ -79,12 +99,21 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
} }
} }
/// <summary>
/// Try to use a native provider, if available.
/// </summary>
public static bool TryUseNative()
{
return TryUseNativeCUDA() || TryUseNativeMKL() || TryUseNativeOpenBLAS();
}
/// <summary>
/// Use the best provider available.
/// </summary>
public static void UseBest() public static void UseBest()
{ {
#if NATIVE #if NATIVE
if (!(TryUse(new Cuda.CudaLinearAlgebraProvider()) if (!TryUseNative())
|| TryUse(new Mkl.MklLinearAlgebraProvider())
|| TryUse(new OpenBlas.OpenBlasLinearAlgebraProvider())))
{ {
UseManaged(); UseManaged();
} }
@ -93,6 +122,11 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
#endif #endif
} }
/// <summary>
/// Use a specific provider if configured, e.g. using the
/// "MathNetNumericsLAProvider" environment variable,
/// or fall back to the best provider.
/// </summary>
public static void UseDefault() public static void UseDefault()
{ {
#if NATIVE #if NATIVE
@ -110,12 +144,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
case "OPENBLAS": case "OPENBLAS":
UseNativeOpenBLAS(); UseNativeOpenBLAS();
break; break;
default: default:
UseBest(); UseBest();
break; break;
} }
#else #else
UseManaged(); UseBest();
#endif #endif
} }
} }

Loading…
Cancel
Save