简体   繁体   English

使用 F2PY 创建 Fortran 扩展模块,自定义签名文件和子例程存储在单独的 Fortran 文件中?

[英]Create a Fortran extension module using F2PY with a custom signature file and subroutines stored in separate Fortran files?

As an exercise, I am trying to create a python module fibadd1 which contains a single method fibadd .作为练习,我正在尝试创建一个 python 模块fibadd1 ,其中包含一个方法fibadd This method will eat an integer and spit out a numpy array of the first n fibonacci numbers with 1 added to them (ie 1, 2, 2, 3,...).此方法将吃掉一个 integer 并吐出一个 numpy 数组,其中前 n 个斐波那契数加上 1(即 1、2、2、3,...)。 The Fortran code containing this routine is fib2.f , and uses a subroutine FIB contained in fib1.f .包含此例程的 Fortran 代码是fib2.f ,并使用fib1.f中包含的子例程FIB This subroutine calculates the first N fibonacci numbers.该子程序计算前 N 个斐波那契数。 With just these two files I have been able to successfully compile the given routine straight from the source, using a python script called test2.py .仅使用这两个文件,我就能够使用名为test2.py的 python 脚本直接从源代码成功编译给定的例程。 However, in trying to create a custom .pyf signature file as in this tutorial , I have run into a road block.但是,在尝试像本教程中那样创建自定义.pyf签名文件时,我遇到了障碍。 The signature file is called fib2.pyf .签名文件称为fib2.pyf

I was able to successfully compile and run everything using the command python -m numpy.f2py -c fib2.pyf fib2.f fib1.f --compiler=mingw32 from the command line.我能够使用命令行中的命令python -m numpy.f2py -c fib2.pyf fib2.f fib1.f --compiler=mingw32成功编译和运行所有内容。 However, I would like to be able to use the numpy.f2py.run_main(...) and numpy.f2py.compile(...) functions to be able to do this in a python script.但是,我希望能够使用numpy.f2py.run_main(...)numpy.f2py.compile(...)函数能够在 Z23EEEB4347BDD26BFC6B7EE9A 脚本中执行此操作。 Based on this documentation it seems like they ought to have the same (or similar) functionalities.根据此文档,它们似乎应该具有相同(或相似)的功能。 The script I tried is called test3.py .我尝试的脚本称为test3.py

It just gives a 1 on exit.它只是在退出时给出 1。 Is it possible to do this in a python script?是否可以在 python 脚本中执行此操作? And also is there further documentation available than what I have linked?还有比我链接的更多的文档吗? I have been having trouble parsing the numpy.f2py.compile function.我在解析numpy.f2py.compile function 时遇到问题。

The source code is as follows:源代码如下:

fib2.f

C FILE: FIB2.F
      SUBROUTINE FIBADD(A,N)
C
C     ADD 1 TO THE FIBONACCI ROUTINE
C
      INTEGER N
      REAL*8 A(N)
      CALL FIB(A, N)
      DO I=1,N
          A(I) = A(I) + 1.0D0
      ENDDO
      END
C END FILE FIB2.F

fib1.f

C FILE: FIB1.F
      SUBROUTINE FIB(A,N)
C
C     CALCULATE FIRST N FIBONACCI NUMBERS
C
      INTEGER N
      REAL*8 A(N)
      DO I=1,N
         IF (I.EQ.1) THEN
            A(I) = 0.0D0
         ELSEIF (I.EQ.2) THEN
            A(I) = 1.0D0
         ELSE 
            A(I) = A(I-1) + A(I-2)
         ENDIF
      ENDDO
      RETURN
      END
C END FILE FIB1.F

test2.py

import numpy.f2py
import numpy as np

with open('fib2.f') as file:
    source = file.read()
module = 'fibadd1'
args = ['fib1.f', '--compiler=mingw32']
failure = numpy.f2py.compile(source, modulename=module, 
                             extra_args=args, verbose=False)
print(failure)

import fibadd1
a = np.zeros(8, 'd')
fibadd1.fibadd(a)
print(a)

fib2.pyf

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module fib2
    interface
        subroutine fibadd(a,n)
            real*8 dimension(n),intent(out),depend(n) :: a
            integer intent(in) :: n
        end subroutine fibadd
    end interface 
end python module fib2

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

test3.py

import numpy.f2py
import numpy as np

with open('fib2.f') as file:
    source = file.read()
module = 'fibadd1'
args = ['fib2.pyf','fib1.f', '--compiler=mingw32']
failure = numpy.f2py.compile(source, modulename=module, 
                             extra_args=args, verbose=False)
print(failure)

It turns out that, in the modified .pyf file there are two lines python module fib2 and end python module fib2 which dictate what the imported python module name will be (go figure).事实证明,在修改后的.pyf文件中有两行python module fib2end python module fib2指示导入的 python 模块名称将是什么(见图)。 In order to successfully compile, the modulename keyword of the numpy.f2py.compile function must match that name.为了成功编译, numpy.f2py.compile function 的modulename关键字必须与该名称匹配。 Thus, the corrected .py compilation script is:因此,更正后的.py编译脚本为:

import numpy.f2py

with open('fib2.f') as file:
    source = file.read()
    
module = 'fib2'
args = ['fib2.pyf','fib1.f', '--compiler=mingw32']

failure = numpy.f2py.compile(source, extra_args=args, 
                             modulename=module, verbose=False)
print(failure)

import fib2

a = fib2.fibadd(8)
print(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM