我正在开发一个python工具,用于对用fortran仿真工具编写的数据进行后处理。由于许多方法已经在fortran中实现,并且我不想在python中进行大量资源的计算,因此我做了以下工作:
编译程序模块时出现第一个问题。编译退出并出现错误,因为它找不到模块文件(已编译且位于模块目录中)。
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -c src//decomp_2d.f90 -o obj//decomp_2d.o
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -c src//glassman.f90 -o obj//glassman.o
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -c src//ftt_generic.f90 -o obj//ftt_generic.o
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -c src//module_param.f90 -o obj//module_param.o
src//module_param.f90(3): error #7002: Error in opening the compiled module file$
check INCLUDE paths. [DECOMP_2D]
use decomp_2d, only : mytype
-----------^
这里要注意的是,glassman.f90和ftt_generic都还使用了decomp_2d,并且看起来没有问题。 现在,如果我再次运行make,所有内容都会编译并创建我的共享库:
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ ./obj//decomp_2d.o ./obj//glassman.o ./obj/ /fft_generic.o ./obj//module_param.o ./obj//io.o ./obj//variables.o ./obj//poisson.o ./obj//schemes.o ./obj//BC-Channel-flow.o ./obj//derive.o ./obj//parameters.o ./obj//tools.o -DVISU -DVISUEXTRA -DDOUBLE_PREC -DSTRETCHING -DPOST -o lib/i3dlib.so
要创建其他共享库,请执行以下操作:
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -Ilib/i3dlib.so ./obj//ipp.o -o lib/ipplib.so
在python中,我运行:
from ctypes import cdll
ipp = cdll.LoadLibrary('./lib/ipplib.so')
ipp.ipp_mp_init_()
我得到的是:
Traceback (most recent call last):
File "test/derivtest.py", line 5, in <module>
Ipp = cdll.LoadLibrary('./lib/ipplib.so')
File "/usr/lib/python3.5/ctypes/__init__.py", line 425, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python3.5/ctypes/__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: ./lib/ipplib.so: undefined symbol: decomp_2d_mp_decomp_2d_init_
这是我的编译包装共享对象中的符号
>> nm ./lib/ipplib.so
00000000002013b8 B __bss_start
00000000002013c0 b completed.7594
w __cxa_finalize@@GLIBC_2.2.5
0000000000000000 N .debug_info_seg
U decomp_2d_mp_decomp_2d_init_
0000000000000c20 t deregister_tm_clones
0000000000000cb0 t __do_global_dtors_aux
0000000000201088 t __do_global_dtors_aux_fini_array_entry
00000000002013b0 d __dso_handle
0000000000201098 d _DYNAMIC
00000000002013b8 D _edata
00000000002014e8 B _end
0000000000000e80 T _fini
U for_read_seq_lis
U for_write_seq_lis
0000000000000cf0 t frame_dummy
0000000000201080 t __frame_dummy_init_array_entry
0000000000001078 r __FRAME_END__
0000000000201398 d _GLOBAL_OFFSET_TABLE_
w __gmon_start__
0000000000000f70 r __GNU_EH_FRAME_HDR
0000000000000be0 T _init
0000000000000d20 T ipp._
0000000000000d50 T ipp_mp_getfield_
0000000000000d60 T ipp_mp_init_
0000000000000d30 T ipp_mp_main_
0000000000000d40 T ipp_mp_setfield_
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000201090 d __JCR_END__
0000000000201090 d __JCR_LIST__
w _Jv_RegisterClasses
00000000002014a0 B mpi_fortran_argv_null_
00000000002014e0 B mpi_fortran_argvs_null_
0000000000201420 B mpi_fortran_bottom_
0000000000201460 B mpi_fortran_errcodes_ignore_
0000000000201440 B mpi_fortran_in_place_
00000000002013e0 B mpi_fortran_statuses_ignore_
0000000000201400 B mpi_fortran_status_ignore_
0000000000201480 B mpi_fortran_unweighted_
00000000002014c0 B mpi_fortran_weights_empty_
U mpi_init_
U parameter_
0000000000000c60 t register_tm_clones
U schemes_
0000000000000e8c r __STRLITPACK_0
0000000000000e98 r __STRLITPACK_2.0.3
0000000000000ea0 r __STRLITPACK_3.0.3
00000000002013b8 d __TMC_END__
U var_mp_init_variables_
i3dlib.so上有一行
0000000000049fc0 T decomp_2d_mp_decomp_2d_init_
答案 0 :(得分:0)
问题(部分)已解决。我在编译器命令中输入错误:
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -Ilib/i3dlib.so ./obj//ipp.o -o lib/ipplib.so
代替:
mpifort -fPIC -xHost -fpp -O3 -heap-arrays -shared -mcmodel=large -safe-cray-ptr -g -traceback -I./obj/ -module ./obj/ -I./obj/ lib/i3dlib.so ./obj//ipp.o -o lib/ipplib.so
在未链接库之前。现在,我可以从python调用我想要的方法。
另一个问题仍然存在-为什么我必须编译两次?为什么它在第一次运行时看不到包含目录中的已编译模块,而在第二次运行中看到它?