我正在使用通过f2py(Ver.2)编译的Fortran(gfortran 4.4.7)编写的Python(2.7.2)扩展.
我可以使用Python配置文件cProfile,但结果不会提供有关Fortran函数的任何信息.相反,时间归因于调用Fortran函数的Python函数.
我为我构建的所有Fortran对象启用了"-pg -O"标志,并在f2py调用中通过以下f2py --opt="-pg -O"方式创建了共享对象:...
任何有关如何获取Fortran信息的提示都非常受欢迎.
如果有人使用类似的设置,使用不同的分析器,我也会感兴趣.
我有一个Fortran模块,我试图用f2py编译(下面列出).当我删除模块声明并将子程序单独保留在文件中时,一切正常.但是,如果声明模块如下所示,我得到以下结果:
> f2py.py -c -m its --compiler=mingw itimes-s2.f
...
Reading fortran codes...
Reading file 'itimes-s2.f' (format:fix,strict)
crackline: groupcounter=1 groupname={0: '', 1: 'module', 2: 'interface', 3: 'subroutine'}
crackline: Mismatch of blocks encountered. Trying to fix it by assuming "end" statement.
...
c:\users\astay13\appdata\local\temp\tmpgh5ag8\Release\users\astay13\appdata\local\temp\tmpgh5ag8\src.win32-3.2\itsmodule.o:itsmodule.c:(.data+0xec): undefined reference to `itimes_'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
在f2py中编译模块或子程序有什么不同?我是否在模块中留下了一些重要的东西导致f2py出现问题?请注意,当我单独使用gfortran时,模块编译得很好.
软件:Windows 7; gcc,gfortran 4.6.1(MinGW); python 3.2.2; f2py v2
itimes-s2.f:
module its
contains
subroutine itimes(infile,outfile)
implicit none
! Constants
integer, parameter :: dp = selected_real_kind(15)
! Subroutine Inputs
character(*), …Run Code Online (Sandbox Code Playgroud) 最新版本的f2py是否支持包含数组值的fortran函数?在一些古代文献中,这不受支持.现在怎么样?
让我们将以下函数保存为func.f95.
function func(x)
implicit none
double precision :: x(:),func(size(x))
integer :: i
do i=1,size(x)
func(i) = i*x(i)
end do
end function
Run Code Online (Sandbox Code Playgroud)
我用它编译 f2py --fcompiler=gnu95 -c -m func func.f95
然后让以下python代码为test_func.py
import func
from numpy import array
x = array(xrange(1,10),dtype='float64')
print 'x=',x
y = func.func(x)
print 'func(x)=',y
Run Code Online (Sandbox Code Playgroud)
输出
python test_func.py是
x= [ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
Segmentation fault
Run Code Online (Sandbox Code Playgroud) 我有一个FORTRAN代码,需要以下编译命令
gfortran -c interp.f -ffixed-format -ffix-line-length-none
Run Code Online (Sandbox Code Playgroud)
我在python中使用f2py模块编译了相同的内容
from numpy import f2py
f2py.compile(open('interp.f').read(),modulename='interp',extra_flags='-ffixed-format -ffix-line-length-none',verbose=0)
Run Code Online (Sandbox Code Playgroud)
它无法编译模块.它出现错误,指出无效的文件格式''at'ffize-format'
请帮忙
我试图使用f2py将我的python程序与我的Fortran模块连接起来.
我在Win7平台上.
我使用最新的Anaconda 64(1.7)作为Python + NumPy堆栈.
My Fortran编译器是最新的Intel Fortran编译器64(版本14.0.0.103 Build 20130728).
我在执行时遇到了很多问题 f2py -c -m PyModule FortranModule.f90 --fcompiler=intelvem
最后一个,我似乎无法理清的是,看起来像f2py/distutils传递给编译器的标志序列与ifort所期望的不匹配.
调用ifort时,我收到一系列有关未知选项的警告消息.
ifort: command line warning #10006: ignoring unknown option '/LC:\Anaconda\libs'
ifort: command line warning #10006: ignoring unknown option'/LC:\Anaconda\PCbuild\amd64'
ifort: command line warning #10006: ignoring unknown option '/lpython27'
Run Code Online (Sandbox Code Playgroud)
我怀疑这与我最后从链接器获得的错误有关
error LNK2019: unresolved external symbol __imp_PyImport_ImportModule referenced in function _import_array
error LNK2019... and so forth (there are about 30-40 lines like that, with different python modules missing)
Run Code Online (Sandbox Code Playgroud)
它的结论很简单
fatal error LNK1120: 42 unresolved …Run Code Online (Sandbox Code Playgroud) 我一直在努力创建一个python包,其中包含一些我想在numpy中使用f2py合并的fortran代码.目标是将其上传到PyPI,以便用户可以使用pip进行安装.我做了一些研究,发现setuptools和numpy.distutils.core具有我想要的功能(我认为).包的结构如下,
rabacus
--setup.py
--README.txt
--MANIFEST.in
--rabacus
----f2py
----python_mod1
----python_mod2
----python_mod3
Run Code Online (Sandbox Code Playgroud)
我在目录f2py中有fortran代码.我的包需要numpy和另一个名为quantity的python包,这两个包都在PyPI中.我已经使用install_requires关键字向setup函数指出了这一点.我的测试周期是这样的,
注册并上传包到PyPI
python setup.py register sdist upload
Run Code Online (Sandbox Code Playgroud)
创建一个没有系统包的虚拟环境
virtualenv --no-site-packages venv
Run Code Online (Sandbox Code Playgroud)
进入虚拟环境并尝试使用pip进行安装
cd venv/bin
./pip install rabacus
Run Code Online (Sandbox Code Playgroud)
我想要检测未满足的要求(numpy和数量),下载它们然后继续我的包安装.我很困惑我应该调用哪个设置函数(来自setuptools的那个或来自numpy.distutils.core的那个).似乎有一个有install_requires,一个有ext_modules.到目前为止,当我在虚拟环境中执行pip命令时,我得到一个错误,抱怨找不到numpy
Downloading/unpacking rabacus
Downloading rabacus-0.9.0.tar.gz (1.7MB): 1.7MB downloaded
Running setup.py egg_info for package rabacus
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/home/galtay/bitbucket/rabacus/venv/build/rabacus/setup.py", line 3, in <module>
import numpy.distutils.core
ImportError: No module named numpy.distutils.core
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line …Run Code Online (Sandbox Code Playgroud) 当我在NumPy的源代码树中列出所有Fortran文件时,我得到:
./doc/source/f2py/scalar.f
./doc/source/f2py/string.f
./doc/source/f2py/calculate.f
./doc/source/f2py/moddata.f90
./doc/source/f2py/array.f
./doc/source/f2py/allocarr.f90
./doc/source/f2py/extcallback.f
./doc/source/f2py/common.f
./doc/source/f2py/ftype.f
./doc/source/f2py/fib3.f
./doc/source/f2py/callback.f
./doc/source/f2py/fib1.f
./doc/f2py/f2python9-final/src/examples/exp1.f
./doc/f2py/simple.f
./doc/f2py/multiarray/foo.f
./doc/f2py/hello.f
./doc/f2py/ex1/bar.f
./doc/f2py/ex1/foobar-smart.f90
./doc/f2py/ex1/foo.f
./doc/f2py/ex1/arr.f
./doc/f2py/ex1/foobar.f90
./numpy/f2py/tests/src/mixed/foo_fixed.f90
./numpy/f2py/tests/src/mixed/foo_free.f90
./numpy/f2py/tests/src/mixed/foo.f
./numpy/f2py/tests/src/size/foo.f90
./numpy/f2py/tests/src/kind/foo.f90
./numpy/f2py/tests/src/assumed_shape/precision.f90
./numpy/f2py/tests/src/assumed_shape/foo_use.f90
./numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap
./numpy/f2py/tests/src/assumed_shape/foo_free.f90
./numpy/f2py/tests/src/assumed_shape/foo_mod.f90
./numpy/f2py/src/test/bar.f
./numpy/f2py/src/test/foo.f
./numpy/f2py/src/test/foo90.f90
./numpy/f2py/src/test/wrap.f
./numpy/distutils/tests/f2py_ext/src/fib1.f
./numpy/distutils/tests/f2py_f90_ext/include/body.f90
./numpy/distutils/tests/f2py_f90_ext/src/foo_free.f90
Run Code Online (Sandbox Code Playgroud)
所以除了f2py之外,其他人都使用Fortran.我研究了线性代数模块.对于LAPACK,有一个make_lite.py从一个LAPACK源树中仅提取必要的子程序文件,以及使用它们转换成C f2c.因此,在创建NumPy的过程中,创建它是否方便f2py?我错过了什么吗?
编辑
事实证明,SciPy中的很多软件包都使用f2py.运行
$ find . -iname '*.f*' | cut -d'/' -f3,4 | uniq
Run Code Online (Sandbox Code Playgroud)
给我填写Fortran文件的确切目录:
linalg/src
fftpack/src
odr/odrpack
special/cdflib
special/amos
special/mach
special/specfun
integrate/quadpack
integrate/odepack
integrate/dop
integrate/linpack_lite
integrate/mach
sparse/linalg
interpolate/fitpack
optimize/minpack2
optimize/minpack
optimize/nnls
optimize/cobyla
optimize/lbfgsb
optimize/slsqp …Run Code Online (Sandbox Code Playgroud) 我已经尝试过但是失败了,以获得最低限度的工作示例.因为我不需要将我的大量代码暴露给python,所以我不需要f2py来包装它的大部分内容.此外,由于传递可分配数组和使用派生类型,我特别希望f2py只包装我创建的接口模块(在下面的示例'main.f90'中).但是我有问题要把我单独编译的其他模块链接到我的主模块.
请注意,所有源文件都在一个目录中.
我创建了一个我想要编译的fortran模块(libtest.f90):
module testmod
implicit none
contains
subroutine testsub(arr)
real, allocatable, intent(in) :: arr(:,:)
print *, 'testsub executed'
end subroutine testsub
end module testmod
Run Code Online (Sandbox Code Playgroud)
和一个我想用f2py(main.f90)包装的fortran模块:
module mainmod
use testmod
implicit none
contains
subroutine mainsub
real, allocatable :: arr(:,:)
call testsub(arr)
end subroutine main sub
end module mainmod
Run Code Online (Sandbox Code Playgroud)
gfortran -c -fPIC libtest.f90
Run Code Online (Sandbox Code Playgroud)
它生成'libtest.o'和'testmod.mod',以及
f2py -c --fcompiler=gfortran -L. -I. -llibtest -m Main main.f90
Run Code Online (Sandbox Code Playgroud)
这给了我' ld:找不到-llibtest的库 '.
我不明白为什么会这样,因为它似乎适用于其他人(F2PY找不到模块) …
我希望使用openmp来加速我通过f2py运行的Fortran代码.但是,在成功编译之后,我无法在Python中导入模块.
对于像这样的Fortran95模块:
module test
implicit none
contains
subroutine readygo()
real(kind = 8), dimension(10000) :: q
!$OMP WORKSHARE
q = 7
!$OMP END WORKSHARE
end subroutine
end module
Run Code Online (Sandbox Code Playgroud)
使用以下命令编译和导入:
f2py -m SOmod --fcompiler=gnu95 --f90flags='-march=native -O3 -fopenmp' -c SOtest.f95
python2 -c "import SOmod"
Run Code Online (Sandbox Code Playgroud)
我收到一个错误.错误是导入 - 编译直接使用f2py或gfortran工作正常(仅获得有关'使用已弃用的NumPy API'的警告).
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: ./SOmod.so: undefined symbol: GOMP_barrier
Run Code Online (Sandbox Code Playgroud)
我为不同的OMP指令得到了不同的GOMP_*错误.没有指令(但使用-openmp标志)它可以工作.
任何帮助将不胜感激.
我正在尝试编译fortran90库(特别是这个)以便从python(3.4.0)调用它.通常在这种情况下,我会为f2py编写一个包装器并将其称为一天,但是库本身会使用派生类型,这似乎使f2py失败.完整的stderr 粘贴在这里,但相关的线是
getctype: No C-type found in "{'typename': 'optim_type', 'typespec': 'type'}", assuming void.
Run Code Online (Sandbox Code Playgroud)
另一个基于numpy文档的选项是使用ctypes,它也会失败
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.ctypeslib.load_library('libLBFGS', '/home/kaplane/src/TOOLBOX_OPTIMIZATION_shared/lib')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/kaplane/.local/lib/python3.4/site-packages/numpy/ctypeslib.py", line 123, in load_library
return ctypes.cdll[libpath]
File "/usr/lib/python3.4/ctypes/__init__.py", line 426, in __getitem__
return getattr(self, name)
File "/usr/lib/python3.4/ctypes/__init__.py", line 421, in …Run Code Online (Sandbox Code Playgroud) f2py ×10
python ×9
fortran ×8
numpy ×5
compilation ×1
cprofile ×1
distutils ×1
function ×1
gfortran ×1
module ×1
openmp ×1
profiler ×1
python-2.7 ×1
setuptools ×1