From 44f3541f42f5bedfe1b25282207f87f0ac147498 Mon Sep 17 00:00:00 2001 From: Martin Posch Date: Thu, 17 Oct 2013 09:45:57 +0200 Subject: [PATCH 1/2] Set Improved MKL Consistency --- src/NativeWrappers/MKL/lapack.cpp | 9 +++++++++ .../LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs | 8 ++++++++ .../Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs | 3 +++ 3 files changed, 20 insertions(+) diff --git a/src/NativeWrappers/MKL/lapack.cpp b/src/NativeWrappers/MKL/lapack.cpp index cb1fc691..1ae473c2 100644 --- a/src/NativeWrappers/MKL/lapack.cpp +++ b/src/NativeWrappers/MKL/lapack.cpp @@ -8,6 +8,8 @@ #include "lapack_common.h" #include "wrapper_common.h" #include "mkl_lapacke.h" +#include "mkl.h" +#include "mkl_trans.h" template inline MKL_INT lu_factor(MKL_INT m, T a[], MKL_INT ipiv[], @@ -748,4 +750,11 @@ extern "C" { return eigen_complex_factor(n, a, vectors, values, d, LAPACKE_zgees, LAPACKE_ztrevc); } } + + DLLEXPORT void SetImprovedConsistency(void) + { + // set improved consistency for mkl and vector functions + mkl_cbwr_set(MKL_CBWR_COMPATIBLE); + vmlSetMode(VML_HA|VML_DOUBLE_CONSISTENT); + } } \ No newline at end of file diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs index 1da96c36..8de60480 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs @@ -42,6 +42,14 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { + /// + /// Improves MKL Consistency to get bit consistent results on repeated identical calculations + /// + public MklLinearAlgebraProvider() + { + SafeNativeMethods.SetImprovedConsistency(); + } + /// /// Computes the requested of the matrix. /// diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs index 78fc835b..768f3527 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/SafeNativeMethods.cs @@ -100,6 +100,9 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl #region LAPACK + [DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] + internal static extern void SetImprovedConsistency(); + [DllImport(DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)] internal static extern float s_matrix_norm(byte norm, int rows, int columns, [In] float[] a, [In, Out] float[] work); From c7bd5c795af98541cf8536417c22a671ce017c45 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 20 Oct 2013 15:34:51 +0200 Subject: [PATCH 2/2] MKL: Make bit-consistency optional and disabled by default Disabled by default mainly for backwards compatibility --- .../LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs index 8de60480..84b0a77f 100644 --- a/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs +++ b/src/Numerics/Providers/LinearAlgebra/Mkl/MklLinearAlgebraProvider.cs @@ -42,12 +42,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra.Mkl /// public partial class MklLinearAlgebraProvider : ManagedLinearAlgebraProvider { - /// - /// Improves MKL Consistency to get bit consistent results on repeated identical calculations - /// - public MklLinearAlgebraProvider() + /// If true improves MKL Consistency to get bit consistent results on repeated identical calculations + public MklLinearAlgebraProvider(bool bitConsistent = false) { - SafeNativeMethods.SetImprovedConsistency(); + if (bitConsistent) + { + SafeNativeMethods.SetImprovedConsistency(); + } } ///