Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6956 Discussions

How to create shared .so library which use MKL?

Grigorii_F_
Beginner
506 Views

Hello,

I would like to create native C dynamically loaded shared library on Linux which use MKL FFT functions. The final goal - to call it from C# (mono) through P/Invoke.

When I run test example C program (not shared library) MKL works fine,

But if I pack my functions to the .so file and call it from C# (mono) through P/Invoke, then I got "DllNotFound" exeption. Without MKL my .so library works also fine.

I supposed that I have to build shared MKL library with -fPIC flag.

So, I went to /opt/intel/mkl/tools/builder and created the libmkldft.so library with dft_example_list functions by adding -fPIC to all command lines in makefile and run "make sointel64 export=dft_example_list name=libmkldft"

then copied the libmkldft.so to /opt/intel/mkl/lib/intel64/ directory and build my test library by

ld -shared -soname libfftt.so -o libfftt.so -L /opt/intel/mkl/lib/intel64/ -lmkldft Fft.o

And finally I have "System.DllNotFoundExeption" at (wrapper managed-to-native) 

If I comment MKL FFT functions in the libfftt.so then I don't have exeptions and all works fine

How to make my shared MKL library work?

0 Kudos
3 Replies
Evarist_F_Intel
Employee
506 Views

Hi Grigorii,

Do you have libmkldft.so (and libiomp5.so) in LD_LIBRARY_PATH during your C# application run? Alternatively, you may use rpath in order to let libfftt.so know where libmkldft.so is located.

If libmkldft.so and libiomp5.so are in the LD_LIBRARY_PATH, could you please try to run the application under ld debug mode so that we can figure out what goes wrong:

$ LD_DEBUG=libs ./a.out # or LD_DEBUG=all for more verbose output

// caution: a lot of output possible.

0 Kudos
Grigorii_F_
Beginner
506 Views

Hello, Evarist,

Thank you for LD_DEBUG advice, it is very helpful tool to analyze. Now the problem is solved. The reason was in wrong libiomp5 and lstdc++ linking

0 Kudos
Grigorii_F_
Beginner
506 Views

By the way, I found that in my test program there is no benefit from OpenMP parallelization, because the call of native function from C# takes a lot of time in Mono.

To compare I created 2 test programs:

The first calls once from c# the following native function:

for(int i=0;i<100;i++)
{
    omp_set_num_threads(nThread);
    fft_handle=init_FFT2D_descriptor();
    DftiComputeForward(fft_handle,x);
    DftiFreeDescriptor(&fft_handle);
}

The second calls 100 times from c# the following native function:

    omp_set_num_threads(nThread);
    fft_handle=init_FFT2D_descriptor();
    DftiComputeForward(fft_handle,x);
    DftiFreeDescriptor(&fft_handle);

In the first case the global program execution time depends on nThread

In the second case doesn't depend.

Am I right, that the reason is in Mono call time of native functions?

0 Kudos
Reply