我只是使用F2PY将Fortran 90子程序包装到python中.这里的细微之处在于Fortran子例程aslo将python回调函数作为其参数之一:
SUBROUTINE f90foo(pyfunc, a)
real(kind=8),intent(in) :: a
!f2py intent(callback) pyfunc
external pyfunc
!f2py real*8 y,x
!f2py y = pyfunc(x)
!*** debug begins***
print *, 'Start Loop'
do i=1,1000
p = pyfunc(a)
end do
total = etime(elapsed)
print *, 'End: total=', total, ' user=', elapsed(1), ' system=', elapsed(2)
stop
!*** debug ends ***
Run Code Online (Sandbox Code Playgroud)
这pyfunc是我的python代码中其他地方定义的python函数.包装工作正常,但运行上面的包装版本,我得到的时间大约是我使用纯python的5倍,如下所示,
def pythonfoo(k):
""" k: scalar
returns: scalar
"""
print('Pure Python: Start Loop')
start = time.time()
for i in xrange(1000):
p = pyfunc(k)
elapsed = …Run Code Online (Sandbox Code Playgroud) 我可以将一维数组传递给python,如下所示.我想知道我是否可以通过使用ctypes,numpy将c ++双指针数组传递给python.
TEST.CPP:
#include <stdio.h>
extern "C" void cfun(const void * indatav, int rowcount, int colcount, void * outdatav);
void cfun(const void * indatav, int rowcount, int colcount, void * outdatav) {
//void cfun(const double * indata, int rowcount, int colcount, double * outdata) {
const double * indata = (double *) indatav;
double * outdata = (double *) outdatav;
int i;
puts("Here we go!");
for (i = 0; i < rowcount * colcount; ++i) {
outdata[i] = indata[i] * …Run Code Online (Sandbox Code Playgroud) 我花了将近一个小时来搜索解决方案,但numpy.distutils的文档非常稀疏.
我有一个f2py包装的模块.它基本上由3个文件组成:
a.f90
a.pyf
lib.a <- this is a static library that contains most of the computational code
Run Code Online (Sandbox Code Playgroud)
使用以下shell-script命令很好地编译该模块.
f2py --build-dir temp -c a.pyf a.f90 lib.a --fcompiler=gnu95
--fcompiler-flags="Zillions of compiler options"
Run Code Online (Sandbox Code Playgroud)
结果,我有python模块a.so(名称在.pyf文件中指定).
我如何使用numpy.distutils(或其他一些面向python的构建工具)来做到这一点?一个不太重要的问题是,我是否还可以包含lib.a的依赖(并在必要时重建它?)
我正在玩f2py.我对numpy内在类型与fortran 90类型有点混淆.在与python进行交互时,似乎我只能在fortran 90中使用单精度实数.让我用一个例子来说明:
假设我有这个fortran 90模块test.f90,用f2py编译并在python中导入:
module test
implicit none
integer, parameter :: sp = selected_real_kind(6,37) ! single precision
integer, parameter :: dp = selected_real_kind(15,307) ! double precision
real(sp) :: r_sp = 1.0
real(dp) :: r_dp = 1.0_dp
end module
Run Code Online (Sandbox Code Playgroud)
我这样编译:
f2py -c -m test test.f90
然后,在python中:
>>> import test
>>> test.test.r_sp
array(1.0, dtype=float32)
>>> test.test.r_dp
array(1.0)
Run Code Online (Sandbox Code Playgroud)
IOW,似乎f2py不接受双精度.当从python传递输入到fortran 90子例程时,这变得更加成问题.假设我将模块扩展到:
module test
implicit none
integer, parameter :: sp = selected_real_kind(6,37) ! single precision
integer, parameter :: dp = selected_real_kind(15,307) ! double precision …Run Code Online (Sandbox Code Playgroud) 我不能让f2py在单独的子例程中引用模块中的参数,在子例程中它用于定义输入数组维度.即参数在一个模块中是失败的:
! File: testmod.f90
MODULE testmod
INTEGER, PARAMETER :: dimsize = 20
END MODULE testmod
Run Code Online (Sandbox Code Playgroud)
并且参数dimsize需要在另一个文件的子例程(不包含在模块中)中引用,该文件将是我的python模块的入口点:
! File testsub.f90
SUBROUTINE testsub(arg)
USE testmod
REAL, INTENT(IN) :: arg(dimsize)
END SUBROUTINE testsub
Run Code Online (Sandbox Code Playgroud)
我像这样编译:
f2py -m testmod -h testmod.pyf testsub.f90
pgf90 -g -Mbounds -Mchkptr -c -fPIC testmod.f90 -o testmod.o
pgf90 -g -Mbounds -Mchkptr -c -fPIC testsub.f90 -o testsub.o
f2py -c testmod.pyf testmod.o testsub.o
Run Code Online (Sandbox Code Playgroud)
但得到这个错误:
testmodmodule.c: In function 'f2py_rout_testmod_testsub':
testmodmodule.c:180: error: 'dimsize' undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)
我已经尝试修改testsub.g90以包含以下指令,如其他建议ni:
SUBROUTINE testsub(arg)
USE testmod
!f2py …Run Code Online (Sandbox Code Playgroud) 我正在尝试cython化一个python项目,该项目位于https://pypi.python.org/packages/source/p/phaseshifts/phaseshifts-0.1.2-dev.zip,其最终目标是为phsh创建一个独立的可执行文件. py模块.我希望嵌入的项目的核心结构具有以下层次结构:
phaseshifts/
__init__.py
atorb.py
conphas.py
elements.py
leed.py
phsh.py
lib/
__init__.py
libphsh.pyd`
Run Code Online (Sandbox Code Playgroud)
注意:libphsh.pyd模块使用编译f2py.
我想我的问题实际上分为多个部分:
1)如何使用cython创建包含多个.py[x]?文件的独立可执行文件?(比如使用--embed单个模块的选项来嵌入python解释器时)
2)如果1)是可能的,那么可以编译.pyd或.so包含文件,如果是,如何?
非常感谢SO退伍军人的任何帮助:-)
到目前为止,粗略的解决方法是编译libphsh.f成dll,gfortran -shared -o libphsh.dll libphsh.f然后编辑相关的源文件以使用ctypes加载库.接下来,我使用cython *.py创建每个Python模块我希望,除了phsh.py,其中I使用的C源文件cython --embed phsh.py.
最后,我使用GCC编译源代码并链接到python库,例如
gcc -o phsh.exe phsh.c __init__.c atorb.c conphas.c elements.c leed.c model.c \
-I"C:\Python27\include" -L"C:\Python27\libs" -lpython27
Run Code Online (Sandbox Code Playgroud)
但是,在运行已编译的可执行文件时,应用程序终止显示消息:
This application has requested the Runtime to terminate it in …Run Code Online (Sandbox Code Playgroud) 我正在使用通过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信息的提示都非常受欢迎.
如果有人使用类似的设置,使用不同的分析器,我也会感兴趣.
我需要在Python中调用Fortran模块中的例程.我用f2py和做了python2.7.它工作得很好.
现在,我必须使用它,python3但f2py似乎不兼容python3.
我看到有些人使用的是一个名为的版本f2py3,但它既pip不是通过macports也不是通过macports(我使用的是Mac).此外,python3 + numpy + f2py似乎已经集成在Fedora中.
有没有人设法在Mac上使用f2py3(或等效)python3?如果没有,您建议将Fortran库与哪些替代解决方案相关联python3?
我试图计算大约十万点之间的所有距离.我有以下代码用Fortran编写并使用以下代码编译f2py:
C 1 2 3 4 5 6 7
C123456789012345678901234567890123456789012345678901234567890123456789012
subroutine distances(coor,dist,n)
double precision coor(n,3),dist(n,n)
integer n
double precision x1,y1,z1,x2,y2,z2,diff2
cf2py intent(in) :: coor,dist
cf2py intent(in,out):: dist
cf2py intent(hide)::n
cf2py intent(hide)::x1,y1,z1,x2,y2,z2,diff2
do 200,i=1,n-1
x1=coor(i,1)
y1=coor(i,2)
z1=coor(i,3)
do 100,j=i+1,n
x2=coor(j,1)
y2=coor(j,2)
z2=coor(j,3)
diff2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)
dist(i,j)=sqrt(diff2)
100 continue
200 continue
end
Run Code Online (Sandbox Code Playgroud)
我正在使用以下python代码编译fortran代码setup_collision.py:
# System imports
from distutils.core import *
from distutils import sysconfig
# Third-party modules
import numpy
from numpy.distutils.core import Extension, setup
# Obtain the numpy include directory. …Run Code Online (Sandbox Code Playgroud) 我有一个Fortran子程序,我想在Python中使用.
subroutine create_hist(a, n, dr, bins, hist)
integer, intent(in) :: n
real(8), intent(in) :: a(n)
real(8), intent(in) :: dr
integer, intent(out), allocatable :: hist(:)
real(8), intent(out), allocatable :: bins(:)
n_b = n_bins(a, n, dr) ! a function calculating the number of bins
allocate(bins(n_b+1))
allocate(hist(n_b))
hist = 0
!... Create the histogram hist by putting elems of a into bins
end subroutine
Run Code Online (Sandbox Code Playgroud)
这是一个简单的程序,用于获取数字数组a并根据给定的bin大小创建直方图dr.首先,它得到使用功能窗口的数量n_bins,然后相应地分配用于阵列中的空间bins和hist.
虽然gfortran编译此代码很好,但f2py会引发错误:
/tmp/tmpY5badz/src.linux-x86_64-2.6/mat_ops-f2pywrappers2.f90:32.37:
call create_hist_gnu(a, …Run Code Online (Sandbox Code Playgroud)