Can I use MKL from Java,from C# or from Python?

Can I use MKL from Java,from C# or from Python?

imagem de Ying H (Intel)

Can I use MKL from Java, From C# or from Python or from ......? Many developers may ask the first question when learn Intel MKL. The answer isYES. Then a following question is how to?

Here is briefintrodution about how to use:
Intel MKL is composed of a set of libraries, which support C/C++ and Fortran language. When use MKL from other languages like Java, from C# or from Python, the common method is to build a custom dll based onthe set of mkl libraries. (A build tool is available in the tools/builder sub-directory of the Intel MKL package)

But since Intel MKL 10.3,we introduce a new dynamic library- the Single Dynamic Library interface (SDL interface) mkl_rt.so, which removes the need to create your own custom library.

For example,
# Load the share library
mkl= cdll.LoadLibrary("./libmkl_rt.so")
# prior to version 10.3, you may use the created.so as below
# mkl = dll.LoadLibrary("./libmy_mkl_py.so")

Moreover, the SDL library allows us link mkl by single library and dynamically select the interface and threading layer for Intel MKL.

For example
>CC application.c mkl_rt.lib

Is it easy, right?

Please click the below KB for more details,
How do I use Intel MKL with Java*?
Using Intel MKL in your C# program Some more additional tips "How to call MKL from your C# code" Using Intel MKL in your Python program
http://software.intel.com/en-us/articles/using-intel-mkl-in-your-c-program/

7 posts / 0 new
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.
imagem de Ying H (Intel)

Hi

I noticed some questions regarding the MKL C# sample were asked under the article, Using Intel MKL in your C# program.
The main problem looks be that: the path of MKL DLLare not correct orCannot load "mkl_intel_thread.dll".
becausethere are below code, whichimport theMKL dll directly.

[DllImport("mkl.dll", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true, SetLastError = false)]

[DllImport("mkl_rt.dll", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]

MKl.dll is acustom dll which build from MKL static library.
mkl_rt.dll isSingle Dynamic Library, itcan be used directly.
So I guess the problem "Cannot load "mkl_intel_thread.dll"may be resolved by DllImport("mkl_rt.dll") as mkl_intel_thread is not needed.

Another issue,if you are building C#project inMSVS IDE.and whateveryou do, the dll can't beload.

one way may be toadd paths of the MKL redistributable tomain path environment variable or system enviorment etc

The dll search order is as the discription by
http://msdn.microsoft.com/en-us/library/ms682586(v=vs.85).aspx
in general, the search order is as follows:

  1. The directory from which the application loaded.
  2. The system directory. Use the GetSystemDirectory function to get the path of this directory.
  3. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  4. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  5. The current directory.
  6. The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

Best Regards,
Ying.

Attach one more sample, which for caculate matrix inverse, using dgetri and dgetrf.

using System;
using System.Security;
using System.Runtime.InteropServices;
using mkl;

namespace testMKL
{
class Program
{
static void Main(string[] args)
{
double[] tm = new double[] { 1.0, 2.0, 3.0, 4.0 };
int n = 2;
int[] ipiv = new int[n];
int info;
printMatrix("Matrix A:",tm,n,n);
info = LAPACK.dgetrf(ref n, ref n, tm, ref n, ipiv);
info = LAPACK.dgetri(ref n, tm, ref n, ipiv);
Console.WriteLine("info on exit: " + info);
printMatrix("Matrix A on exit:",tm,n,n);
Console.WriteLine("Finished");

}

/** Print the matrix X assuming row-major order of elements. */
private static void printMatrix(String prompt, double[] X, int I, int J)
{
Console.WriteLine(prompt);
for (int i=0; i {
for (int j=0; j Console.Write("\t" + X[i*J+j]);
Console.WriteLine();
}
}

}
}

namespace mkl
{
public sealed class LAPACK
{
private LAPACK() { }

public static int dgetrf(ref int m,
ref int n, [In, Out] double[] a, ref int lda, [In, Out] int[] ipiv)
{
int info =0;
LAPACKNative.dgetrf(ref m, ref n, a, ref lda, ipiv,ref info);
return info;
}

public static int dgetri(
ref int n, [In, Out] double[] a, ref int lda, [In, Out] int[] ipiv)
{
int info=0;
double[] work;
double[] work1 = new double[1];
int lwork = -1;
LAPACKNative.dgetri(ref n, a, ref lda, ipiv,work1, ref lwork,ref info);
if(info != 0)
{
return info;
}
lwork = (int)work1[0];
work = new double[lwork];
LAPACKNative.dgetri(ref n, a, ref lda, ipiv,work, ref lwork,ref info);
return info;
}

}

[SuppressUnmanagedCodeSecurity]
internal sealed class LAPACKNative
{
private LAPACKNative() { }

[DllImport("mkl_rt_orig", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern void dgetrf( ref int m,
ref int n, [In, Out] double[] a, ref int lda, [In,Out] int[] ipiv, ref int info );

[DllImport("mkl_rt_orig", CallingConvention = CallingConvention.Cdecl,
ExactSpelling = true, SetLastError = false)]
internal static extern void dgetri(
ref int n, [In, Out] double[] a, ref int lda, [In,Out] int[] ipiv,
[In, Out] double[] work, ref int lwork,ref int info);

}
}

imagem de Vipin Kumar E K (Intel)

Additionaly please referthe article http://software.intel.com/en-us/articles/numpy-user-note/ which would be helpful for Numpy users to take advantage of MKL.

--Vipin

imagem de clemens_adolphs

Brilliant, this is exactly what I need. However, I have some problems with a more advanced situation.

I get the example.py running all right, but then I wanted to call the pardiso solver and there, the code crashes with an "undefined symbol" error because it cannot find "omp_get_num_threads".

This happens even when I make sure that libiomp5.so is in all the LD_LIBRARY_PATH and LD_RUN_PATH and LIBRARY_PATH variables, and I made extra sure by trying to load libiomp5.so manually into python using the cdll.LoadLibrary command. The library is found by that command, but again, apparently it's not found by libmkl_intel_thread

This is the error message I get:

python: symbol lookup error: /opt/intel/composerxe-2011.1.107/mkl/lib/intel64/libmkl_intel_thread.so: undefined symbol: omp_get_num_procs

All web searches for this type of error just relate to problems during compiling/linking, not during dynamically loading the library, so I'm at a loss.

EDIT: Here's one more thing I discovered: If I set the environment variable MKL_THREADING_LAYER to SEQUENTIAL, then my little test call to pardiso works without problem, so the problem lies indeed in the multithreading part.

EDIT 2: Well, I got it working now with a custom built library using the tools/builder folder. I confused the version of the Compiler (11.1) with the verison of the MKL (10.2) installed on my system...

imagem de Ying H (Intel)

Hi Clemens_adolphs,

thanks for thequestion and solutionsharing.

We saw there is similiar report in stackOverflowforum: http://stackoverflow.com/questions/8995518/calling-a-threaded-function-of-the-mkl-from-python-openmp-library-not-found.
may be same as yoursolutionvia a custom built library. We will check the mkl_rt.so problem and get back to you if any news.

Regarding the version information of compiler 11.1 or Composerxe-2011.1.107, you may refer to the two articles:

http://software.intel.com/en-us/articles/which-version-of-ipp--mkl--tbb-is-installed-with-intel-compiler-professional-edition/

http://software.intel.com/en-us/articles/which-version-of-the-intel-ipp-intel-mkl-and-intel-tbb-libraries-are-included-in-the-intel-composer-bundles/

Thanks
Ying

imagem de Ying H (Intel)

Hi Clemens_adolphs,

We can reproduce the problem whenusing libmkl_rt.so from MKL 10.3.1 (or Composer xe 2011 update 1)in matmult.py, which cause the error: python: symbol lookup error: /opt/intel/composerxe-2011.1.107/mkl/lib/intel64/libmkl_intel_thread.so: undefined symbol: omp_get_num_procs.
So it is reasonable tosuspect that loading the libmkl_rt.so doesn't properly trigger a load of libiomp5.so

Butthe sameprogram runfinewhen using libmkl_rt.so from MKL 10.3.7, ($source /opt/intel/composer_xe_sp1.0.015/composer_xe_2011_sp1.7.256/mkl/bin/mklvars.sh intel64,
strace python matmult.py>strace7.log 2>&1)

Is it interesting, right?

There were actuallyissue in early version ofMKL 10.3.x (1~5) indeed.Luckily we fixed MKL loader located in libmkl_rt.sosince MKL 10.3.6. In previous version of MKL it doesnt try to load libiomp5.so at all, no matter if LD_LIBRARY_PATH or something else is specified correctly.

So for early versions before MKL 10.3.6, the solution is sameas yours, to use custom dll vi MKL custom dll builder tools.
Or please to upgrade the MKL version, the latest is MKL 10.3. 8 also from Composer XE 2011_Sp1 update 8.
Thanks
Ying Hu.

imagem de Ying H (Intel)
Intel® MKL with Numpy, Scipy, Matlab, C#, Python, NAG, R and more

The following Knowledge Base article about MKL Compatibility for 3rd party libraries: 

http://software.intel.com/en-us/articles/intel-mkl-and-third-party-applications-how-to-use-them-togethe

Faça login para deixar um comentário.