Fortran使用OpenMP和Intel编译器共享Python库

时间:2014-02-26 14:21:25

标签: python fortran openmp intel ctypes

我在Fortran中创建一个从Python加载的共享库时遇到了问题。我已经汇总了一个最小的例子来说明问题:

子程序:

subroutine sgesvf() bind(C, name="sgesvf")

implicit none

real*4 a(4,4), b(4), c(4,4)
integer pivs(4), inf, n, i, j

! Externals from the LAPACK library
external sgesv

!     Initialize the matrix a and the vector b
data a/ 1, 2, 3, 4, &
   6, 7, 9, 9, &
   11,12,19,14, &
   16,17,18,12/
data b/ 1, 3, 5, 6/

n=4
!$OMP PARALLEL DO DEFAULT(NONE) &
!$OMP SHARED(A,C,N) &
!$OMP PRIVATE(I,J) &
!$OMP SCHEDULE(DYNAMIC,1)
do i = 2, n
  do j = 1, i
     c(j,i) = ( a(j,i) + a(j,i-1) ) / 2.0
  end do
end do

!     Compute the solution
call sgesv(4, 1, a, 4, pivs, b, 4, inf)

!     Figure out if sgesv found a solution or not
if (inf .eq. 0) then
   write (*,*) 'successful solution'
else if (inf .lt. 0) then
   write (*,*) 'illegal value at: %d', -inf
   stop
else if (inf .gt. 0) then
   write (*,*) 'matrix was singular'
   stop
else
   write (*,*) 'unknown result (can''t happen!)'
   stop
end if

write(*,*) 'pivs=', pivs

end subroutine sgesvf

测试我在实际代码中使用的最重要的工具,即OpenMP和Lapack / Blas。

如果我编译忽略OpenMP指令,那就是:

ifort -O2 -warn all -fPIC -I/opt/intel/mkl/include -module Release/ -c main.f90 -o Release/main.o
ifort -shared  Release/main.o -o Release/libshared_lib_for_python_example.so -L/opt/intel/lib/intel64 -lmkl_rt -lpthread -lm

并用作:

from ctypes import cdll
lib = cdll.LoadLibrary('libshared_lib_for_python_example.so')
print lib.sgesvf()

我明白了:

successful solution
pivs=           4           4           3           4           0

如果我包含OpenMP:

ifort -O2 -warn all -fPIC -openmp -I/opt/intel/mkl/include -module Release/ -c main.f90 -o Release/main.o
ifort -shared  Release/main.o -o Release/libshared_lib_for_python_example.so -L/opt/intel/lib/intel64 -openmp -lmkl_rt -lpthread -lm

我明白了:

Traceback (most recent call last):
  File "test.py", line 16, in <module>
    lib = cdll.LoadLibrary('libshared_lib_for_python_example.so')
  File "/usr/lib/python2.7/ctypes/__init__.py", line 443, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /opt/intel/composer_xe_2013_sp1.2.144/ipp/../compiler/lib/intel64/libifcoremt.so.5: undefined symbol: _intel_fast_memmove

我尝试过在论坛上发现的几种变体(比如将-lifport -lifcoremt -lifcore -lsvml -lirc添加到链接中,确保我链接到正确的库等)。我也试过在2周内在英特尔论坛上发帖,没有任何结果。

我在Debian Linux 7.0下工作。我的英特尔编译器是:版本14.0.2.144 Build 20140120。

交叉发布于:http://software.intel.com/forums/topic/506517

提前致谢

0 个答案:

没有答案