我正在尝试使用 f2py 为我的同时使用 PETSc 和 SLEPc 的 Fortran 代码创建一个 python 模块(这个问题与这篇文章密切相关,但是那里的讨论无法解决我的问题——请参阅底部的评论邮政)。代码由许多文件和子程序组成,但我只需要访问 python 中最外层的 main/driver 函数。我采取的第一步是使用 SLEPc 提供的 makefile 模板来生成所有目标文件(最终链接失败,因为外部函数现在是子程序而不是主程序,但我现在不用担心,因为我'就在目标文件之后)。
从那里,我尝试了两种方法。首先是简单地调用f2py
如下:
f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o...
这似乎可行,但在尝试导入 python 时,找不到 SLEPc(并且,我认为是 PETSc)对象/函数:
>>> import mod_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(./mod_name.so, 2): Symbol not found: _epscreate_
Referenced from: ./mod_name.so
Expected in: flat namespace
in ./mod_name.so
第二种方法假设我也需要链接到 SLEPc/PETSc 库。因此,我在 makefile 中添加了一行:
modname.so: outer_driver.f90
f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o ${SLEPC_EPS_LIB}
运行“make modname.so”后f2py
,无法完全运行。这是输出:
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "modname" sources
f2py options: []
f2py:> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/modnamemodule.c
creating /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
Reading fortran codes...
Reading file 'outer_driver.f90' (format:free)
Post-processing...
Block: modname
Block: outer_driver
Post-processing (stage 2)...
Building modules...
Building module "modname"...
Constructing wrapper function "outer_driver"...
outer_driver()
Wrote C/API module "modname" to file "/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/modnamemodule.c"
adding '/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/fortranobject.c' to sources.
adding '/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7' to include_dirs.
copying /usr/local/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
copying /usr/local/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/local/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'modname' extension
compiling C sources
C compiler: clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
error: unknown file type '' (from '-Wl,-rpath,/usr/local/Cellar/slepc/3.7.3_4/real/lib')
make: *** [modname.so] Error 1
在链接到 PETSc 的 fortran 代码上使用 f2py的 OP表明他能够通过将环境变量设置为 PETSc 库来绕过使用-Wl,-rpath
标志,这在此处似乎也是一个问题。LD_LIBRARY_PATH
但是,如果我做同样的事情(设置为 SLEPc 库,我相信这也意味着 PETSc 库),这两种方法都对我没有帮助。有人可以建议我可能使用的方法吗?
更新-Wl,rpath
:要求提供有关错误及其来源的更多信息。据我了解,这些标志来自使用 SLEPc/PETSc 链接。因此,在我的 makefile 中,它们来自${SLEPC_EPS_LIB}
变量。解释时,完整的命令非常长,但无论如何我都会包含它,以防可以从那里挖掘某些东西。请记住,我在上面的示例中没有使用实际的文件名——为了清楚起见,我只是简化了。下面的文件名会有所不同。
f2py -c -m iga_blade_py run_analysis.f90 modules.o force.o param.o cname.o curve_input_nrb_mc.o shell_input_nrb_mp.o shell_reduce_node.o shell_input_nrb.o material_input.o material_calculate.o shell_laminate2.o iosol.o shell_gp_info.o shell_profile_vector。 o shell_genIEN_INN.o shell_genSparStruc.o shell_genGP_GW.o shell_eval_SHAPE.o shell_dersbasisfuns.o shell_e3LRhs_KLShell.o shell_e3LRhs_KLShell_pen_disp.o shell_e3LRhs_KLShell_pen_rot.o shell_KL_geo.o cross_prod.o shell_BCLhs_3D.o shell_FillSparseMat_3D.o shell_SparseMatLoc_3D.o shell_SparseCG.o shell_skyline.o shell_SparseProd.o shell_IntElmAss_post2 .o shell_e3LRhs_KLShell_post.o curve_element_project.o shell_mnrb_find_search_locs.o shell_nrb_find_point.o patch_point_proj.o petsc_genSparStruc.o viz_read_mesh_points.o driver.o input_shell.o solflow.o shell_IntElmAss.o petsc_fillMat。o shell_IntElmAss_pen.o petsc_prepSolve.o shell_allocate_LRhs.o petsc_rank.o solflow_lin_buck.o solflow_vibr.o shell_IntElmAss_mass.o -Wl,-rpath,/usr/local/Cellar/slepc/3.7.3_4/real/lib -L/usr/local /Cellar/slepc/3.7.3_4/real/lib -lslepc -Wl,-rpath,/usr/local/opt/arpack/lib -L/usr/local/opt/arpack/lib -lparpack -larpack -Wl,- rpath,/usr/local/Cellar/petsc/3.7.5/real/lib -L/usr/local/Cellar/petsc/3.7.5/real/lib -lpetsc -L/usr/local/opt/superlu_dist/lib -lsuperlu_dist -Wl,-rpath,/usr/local/opt/mumps/libexec/lib -L/usr/local/opt/mumps/libexec/lib -lcmumps -ldmumps -lsmumps -lzmumps -lmumps_common -lpord -Wl,- rpath,/usr/local/opt/parmetis/lib -L/usr/local/opt/parmetis/lib -lparmetis -Wl,-rpath,/usr/local/opt/metis/lib -L/usr/local/opt /metis/lib -lmetis -Wl,-rpath,/usr/local/opt/hypre/lib -L/usr/local/opt/hypre/lib -lHYPRE -Wl,-rpath,/usr/local/opt/libevent/lib -L/usr/local/opt/libevent/lib -Wl,-rpath,/usr/local/Cellar/open-mpi/2.0.2/lib -L/usr/local /Cellar/open-mpi/2.0.2/lib -Wl,-rpath,/usr/local/lib -L/usr/local/lib -Wl,-rpath,/System/Library/Frameworks/OpenGL.framework/Versions /Current/Libraries -L/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -Wl,-rpath,/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang /8.0.0/lib/darwin -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/8.0.0/lib/darwin -lclang_rt.osx -lc++ -Wl,- rpath,/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/lib/darwin -L/Applications/Xcode.app/Contents/Developer/工具链/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/lib/darwin -lclang_rt。osx -Wl,-rpath,/usr/local/opt/scalapack/lib -L/usr/local/opt/scalapack/lib -lscalapack -Wl,-rpath,/usr/local/opt/suite-sparse/lib - L/usr/local/opt/suite-sparse/lib -lumfpack -lklu -lcholmod -lbtf -lccolamd -lcolamd -lcamd -lamd -lsuitesparseconfig -Wl,-rpath,/usr/local/opt/sundials/lib -L/ usr/local/opt/sundials/lib -lsundials_cvode -lsundials_nvecserial -lsundials_nvecparallel -llapack -lblas -Wl,-rpath,/usr/local/opt/netcdf/lib -L/usr/local/opt/netcdf/lib -lnetcdf - wl,-rpath,/usr/local/opt/hdf5/lib -L/usr/local/opt/hdf5/lib -lhdf5_hl -lhdf5 -Wl,-rpath,/usr/local/opt/hwloc/lib -L/ usr/local/opt/hwloc/lib -lhwloc -Wl,-rpath,/usr/local/opt/fftw/lib -L/usr/local/opt/fftw/lib -lfftw3_mpi -lfftw3 -lmpi_usempif08 -lmpi_usempi_ignore_tkr -lmpi_mpifh - lgfortran -Wl,-rpath,/usr/local/Cellar/gcc/6.3.0_1/lib/gcc/6/gcc/x86_64-apple-darwin15.6.0/6.3。0 -L/usr/local/Cellar/gcc/6.3.0_1/lib/gcc/6/gcc/x86_64-apple-darwin15.6.0/6.3.0 -Wl,-rpath,/usr/local/Cellar/gcc/ 6.3.0_1/lib/gcc/6 -L/usr/local/Cellar/gcc/6.3.0_1/lib/gcc/6 -lgfortran -lgcc_ext.10.5 -lquadmath -lm -lclang_rt.osx -lc++ -lclang_rt.osx - wl,-rpath,/usr/local/opt/libevent/lib -L/usr/local/opt/libevent/lib -Wl,-rpath,/usr/local/Cellar/open-mpi/2.0.2/lib - l/usr/local/Cellar/open-mpi/2.0.2/lib -Wl,-rpath,/usr/local/lib -L/usr/local/lib -Wl,-rpath,/System/Library/Frameworks/ OpenGL.framework/Versions/Current/Libraries -L/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries -ldl -lmpi -lSystem -Wl,-rpath,/Applications/Xcode.app/Contents/Developer/工具链/XcodeDefault.xctoolchain/usr/bin/../lib/clang/8.0.0/lib/darwin -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../ lib/clang/8.0.0/lib/darwin -lclang_rt.osx -ldl
已解决:我最终能够找到几种不同的方法来解决这个问题。最直接的方法与我描述的第二种(基于makefile)方法基本相同,但我手动从变量中删除了每个-Wl,rpath
命令。SLEPC_EPS_LIB
此外,我通过 PETSc 邮件列表了解到,可以仅使用 f2py 来生成必需的 .c、.f90 和 .pyc 文件。然后,您可以自己编译,链接必要的库。显然这涉及更多,但它也是一种选择。