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);