Browse Source

Native: MKL optional max threads control, integrated with Control #223

provider
Christoph Ruegg 12 years ago
parent
commit
59c41819e6
  1. 8
      src/NativeProviders/MKL/capabilities.cpp
  2. 20
      src/Numerics/Control.cs
  3. 7
      src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs
  4. 7
      src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs

8
src/NativeProviders/MKL/capabilities.cpp

@ -41,8 +41,9 @@ extern "C" {
#endif #endif
// COMMON/SHARED // COMMON/SHARED
case 64: return 5; // revision case 64: return 6; // revision
case 65: return 1; // numerical consistency, precision and accuracy modes case 65: return 1; // numerical consistency, precision and accuracy modes
case 66: return 1; // threading control
// LINEAR ALGEBRA // LINEAR ALGEBRA
case 128: return 1; // basic dense linear algebra case 128: return 1; // basic dense linear algebra
@ -68,6 +69,11 @@ extern "C" {
vmlSetMode(mode); vmlSetMode(mode);
} }
DLLEXPORT void set_max_threads(const MKL_INT num_threads)
{
mkl_set_num_threads(num_threads);
}
/* Obsolete, will be dropped in the next revision */ /* Obsolete, will be dropped in the next revision */
DLLEXPORT void SetImprovedConsistency(void) DLLEXPORT void SetImprovedConsistency(void)
{ {

20
src/Numerics/Control.cs

@ -54,9 +54,9 @@ namespace MathNet.Numerics
{ {
// Random Numbers & Distributions // Random Numbers & Distributions
CheckDistributionParameters = true; CheckDistributionParameters = true;
ThreadSafeRandomNumberGenerators = true;
// Parallelization & Threading // Parallelization & Threading
ThreadSafeRandomNumberGenerators = true;
_maxDegreeOfParallelism = Environment.ProcessorCount; _maxDegreeOfParallelism = Environment.ProcessorCount;
_blockSize = 512; _blockSize = 512;
_parallelizeOrder = 64; _parallelizeOrder = 64;
@ -91,6 +91,16 @@ namespace MathNet.Numerics
{ {
_maxDegreeOfParallelism = 1; _maxDegreeOfParallelism = 1;
ThreadSafeRandomNumberGenerators = false; ThreadSafeRandomNumberGenerators = false;
LinearAlgebraProvider.InitializeVerify();
}
public static void UseMultiThreading()
{
_maxDegreeOfParallelism = Environment.ProcessorCount;
ThreadSafeRandomNumberGenerators = true;
LinearAlgebraProvider.InitializeVerify();
} }
public static void UseManaged() public static void UseManaged()
@ -154,7 +164,13 @@ namespace MathNet.Numerics
public static int MaxDegreeOfParallelism public static int MaxDegreeOfParallelism
{ {
get { return _maxDegreeOfParallelism; } get { return _maxDegreeOfParallelism; }
set { _maxDegreeOfParallelism = Math.Max(1, Math.Min(1024, value)); } set
{
_maxDegreeOfParallelism = Math.Max(1, Math.Min(1024, value));
// Reinitialize providers:
LinearAlgebraProvider.InitializeVerify();
}
} }
/// <summary> /// <summary>

7
src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs

@ -149,11 +149,18 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl
throw new NotSupportedException("MKL Native Provider found but too old or not compatible."); 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) if (SafeNativeMethods.query_capability(65) > 0)
{ {
SafeNativeMethods.set_consistency_mode((int)_consistency); SafeNativeMethods.set_consistency_mode((int)_consistency);
SafeNativeMethods.set_vml_mode((uint)_precision | (uint)_accuracy); 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);
}
} }
public override string ToString() public override string ToString()

7
src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs

@ -41,6 +41,8 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl
[SecurityCritical] [SecurityCritical]
internal static class SafeNativeMethods internal static class SafeNativeMethods
{ {
// ReSharper disable InconsistentNaming
/// <summary> /// <summary>
/// Name of the native DLL. /// Name of the native DLL.
/// </summary> /// </summary>
@ -55,6 +57,9 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl
[DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern void set_vml_mode(uint mode); internal static extern void set_vml_mode(uint mode);
[DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern void set_max_threads(int num_threads);
#region BLAS #region BLAS
[DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
@ -342,6 +347,8 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl
internal static extern void z_vector_divide(int n, Complex[] x, Complex[] y, [In, Out] Complex[] result); internal static extern void z_vector_divide(int n, Complex[] x, Complex[] y, [In, Out] Complex[] result);
#endregion Vector Functions #endregion Vector Functions
// ReSharper restore InconsistentNaming
} }
} }

Loading…
Cancel
Save