Quantcast

Julia calling MKL functions from fortran shared library

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Julia calling MKL functions from fortran shared library

博陈
These days, I tried a walkaround to implement FFT in julia with the help of the MKL fft properties. 

I wrote a fortran module with several subroutines, in some of which the MKL FFT function was called. The fortran file was compiled to a shared library.

       ifort modules.f90  -parallel -mkl -lm -O3 -I/opt/intel/composer_xe_2015.0.090/mkl/include -                    L/opt/intel/composer_xe_2015.0.090/mkl/lib/intel64   -shared -fpic -o new.so

the test.jl reads:
      ccall((:mymodule_mp_ground_, "new"), Int,())

when I ran the julia code with:
      julia test.jl
I got 
      Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.

What might go wrong? Do I need to give the whole fortran module code for analysis?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Julia calling MKL functions from fortran shared library

Steven G. Johnson


On Tuesday, November 15, 2016 at 4:23:11 AM UTC-5, 博陈 wrote:
I got 
      Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
 
Possibly you need to add the directory containing these files (/opt/intel/composer_xe_2015.0.090/mkl/lib or similar?) to your LD_LIBRARY_PATH environment variable, so that the runtime linker knows where to find them.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Julia calling MKL functions from fortran shared library

Angel de Vicente
Hi,

I wanted to try this out. My test Fortran module is the following, which
I compile with: ifort -mkl -o test_mod.so test_mod.f90 -shared -fpic
,----
| module testmodule
|
|   implicit none
|
|   double precision, external :: ddot
|   double precision, dimension(3) :: d,e
|
|   integer :: i
|
| contains
|
|   function dot_f()
|     double precision :: dot_f
|
|     do i = 1,3
|        d(i) = 1.0d0*i
|        e(i) = 3.5d0*i
|     end do
|
|     dot_f = ddot(3,d,1,e,1)
|   end function dot_f
|
| END module testmodule
`----

and my test.jl
,----
| ccall((:testmodule_mp_dot_f_, "./test_mod"),Float64, ())
`----

If I try to run test.jl I get, as per the OP:
,----
| julia> include("test.jl")                                                                                                                                                                              
| Intel MKL FATAL ERROR: Cannot load libmkl_avx.so or libmkl_def.so.
`----


"Steven G. Johnson" <[hidden email]> writes:
>     I got
>     Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
>    
> Possibly you need to add the directory containing these files
> (/opt/intel/composer_xe_2015.0.090/mkl/lib or similar?) to your LD_LIBRARY_PATH
> environment variable, so that the runtime linker knows where to find them.

In this case this is not enough.

If you try to open the library directly you get:
,----
| julia> Libdl.dlopen("/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so")
| ERROR: could not load library
| "/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so"                                                                            
| /opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so:
| undefined symbol: mkl_dft_fft_fix_twiddle_table_32f                                                      
|  in dlopen(::String, ::UInt32) at ./libdl.jl:90 (repeats 2 times)
`----

and nm confirms that those symbols are undefined:
,----
| [angelv@duna intel64]$ nm libmkl_avx2.so | grep fft_fix
|                  U mkl_dft_fft_fix_twiddle_table_32f
|                  U mkl_dft_fft_fix_twiddle_table_64f
`----

and they are acutally defined in libmkl_core.so
,----
| [angelv@duna intel64]$ nm libmkl_core.so | grep fft_fix
| 00000000018e3020 D mkl_dft_fft_fix_twiddle_table_32f
| 00000000018e2800 D mkl_dft_fft_fix_twiddle_table_64f
| [angelv@duna intel64]$
`----


So, a workaround is to open libmkl_core.so first with the flag
RTLD_GLOBAL and then run the test.jl code:

,----
| julia>
| Libdl.dlopen("/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_core.so",Libdl.RTLD_GLOBAL)                                                                  
| Ptr{Void} @0x0000000003af6fa0                                                                                                                                                                          
|                                                                                                                                                                                                        
| julia> include("test.jl")                                                                                                                                                                              
| 49.0    
`----

But, to be honest, I don't fully understand if this will be enough for
all codes using MKL or perhaps other dependencies are there which forces
you to open more libraries manually with RTLD_GLOBAL. But at least it
points in the right direction.

Cheers,
--
Ángel de Vicente
http://www.iac.es/galeria/angelv/         
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Julia calling MKL functions from fortran shared library

博陈


在 2016年11月16日星期三 UTC+8上午12:49:13,Ángel de Vicente写道:
Hi,

I wanted to try this out. My test Fortran module is the following, which
I compile with: ifort -mkl -o test_mod.so test_mod.f90 -shared -fpic
,----
| module testmodule
|
|   implicit none
|
|   double precision, external :: ddot
|   double precision, dimension(3) :: d,e
|
|   integer :: i
|
| contains
|
|   function dot_f()
|     double precision :: dot_f
|
|     do i = 1,3
|        d(i) = 1.0d0*i
|        e(i) = 3.5d0*i
|     end do
|
|     dot_f = ddot(3,d,1,e,1)
|   end function dot_f
|
| END module testmodule
`----

and my test.jl
,----
| ccall((:testmodule_mp_dot_f_, "./test_mod"),Float64, ())
`----

If I try to run test.jl I get, as per the OP:
,----
| julia> include("test.jl")                                                                                                                                                                              
| Intel MKL FATAL ERROR: Cannot load libmkl_avx.so or libmkl_def.so.
`----


"Steven G. Johnson" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="f60P8uDqBgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">steve...@...> writes:
>     I got
>     Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.
>    
> Possibly you need to add the directory containing these files
> (/opt/intel/composer_xe_2015.0.090/mkl/lib or similar?) to your LD_LIBRARY_PATH
> environment variable, so that the runtime linker knows where to find them.

In this case this is not enough.

If you try to open the library directly you get:
,----
| julia> Libdl.dlopen("/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so")
| ERROR: could not load library
| "/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so"                                                                            
| /opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_avx2.so:
| undefined symbol: mkl_dft_fft_fix_twiddle_table_32f                                                      
|  in dlopen(::String, ::UInt32) at ./libdl.jl:90 (repeats 2 times)
`----

and nm confirms that those symbols are undefined:
,----
| [angelv@duna intel64]$ nm libmkl_avx2.so | grep fft_fix
|                  U mkl_dft_fft_fix_twiddle_table_32f
|                  U mkl_dft_fft_fix_twiddle_table_64f
`----

and they are acutally defined in libmkl_core.so
,----
| [angelv@duna intel64]$ nm libmkl_core.so | grep fft_fix
| 00000000018e3020 D mkl_dft_fft_fix_twiddle_table_32f
| 00000000018e2800 D mkl_dft_fft_fix_twiddle_table_64f
| [angelv@duna intel64]$
`----


So, a workaround is to open libmkl_core.so first with the flag
RTLD_GLOBAL and then run the test.jl code:

,----
| julia>
| Libdl.dlopen("/opt/intel/2016.0.1/compilers_and_libraries_2016.1.150/linux/mkl/lib/intel64/libmkl_core.so",Libdl.RTLD_GLOBAL)                                                                  
| Ptr{Void} @0x0000000003af6fa0                                                                                                                                                                          
|                                                                                                                                                                                                        
| julia> include("test.jl")                                                                                                                                                                              
| 49.0    
`----

But, to be honest, I don't fully understand if this will be enough for
all codes using MKL or perhaps other dependencies are there which forces
you to open more libraries manually with RTLD_GLOBAL. But at least it
points in the right direction.

Cheers,
--
Ángel de Vicente
<a href="http://www.iac.es/galeria/angelv/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.iac.es%2Fgaleria%2Fangelv%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHWgWJwB7xZkScAhM-XaTehlCOVww&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.iac.es%2Fgaleria%2Fangelv%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHWgWJwB7xZkScAhM-XaTehlCOVww&#39;;return true;">http://www.iac.es/galeria/angelv/          


You have solved the problem, that's nice. But maybe I should reconsider whether to use mkl in such a uncomfortable way. 
Loading...