使用 f2py 编译 Fortran 模块

发布于 2024-12-21 23:07:51 字数 1502 浏览 1 评论 0原文

我有一个 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

在 f2py 中编译模块或子例程有什么不同?我是否在模块中遗漏了一些导致 f2py 出现问题的重要内容?请注意,当我单独使用 gfortran 时,该模块可以正常编译。

软件:Windows 7; gcc、gfortran 4.6.1 (MinGW);蟒蛇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(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

  end module its

I have a Fortran module which I am trying to compile with f2py (listed below). When I remove the module declaration and leave the subroutine in the file by itself, everything works fine. However, if the module is declared as shown below, I get the following results:

> 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

What is different about compiling a module or subroutine in f2py? Have I left something important out in the module that causes f2py to have trouble? Note that the module compiles fine when I use gfortran alone.

Software: 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(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

  end module its

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南…巷孤猫 2024-12-28 23:07:51

您正在尝试在 Python 模块中包含 Fortran 模块。如果你想要这样,名称必须不同,例如

 f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f

结果将被称为pythonmodule.fortranmodule.yourfunction()

您也可以将其导入,

from pythonmodule import fortranmodule
fortranmodule.yourfunction()

否则它可以在我的机器上运行。

You are trying to have a Fortran module in a Python module. If you want that, the names must be different, e.g.

 f2py.py -c -m SOMEDIFFERENTNAME itimes-s2.f

The result will be called as pythonmodule.fortranmodule.yourfunction().

You can also import it as

from pythonmodule import fortranmodule
fortranmodule.yourfunction()

Otherwise it worked on my machine.

物价感观 2024-12-28 23:07:51

为了使 f2py 工作,您需要有一个签名文件来指导界面创建或使用 f2py 注释修改源代码以帮助界面。请参阅 https://numpy.org/doc/stable/f2py/signature-file .html 了解更多信息。

从该站点:

C FILE: FIB3.F
      SUBROUTINE FIB(A,N)
C
C     CALCULATE FIRST N FIBONACCI NUMBERS
C
      INTEGER N
      REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
      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
      END
C END FILE FIB3.F

现在可以通过一个命令来构建扩展模块:

f2py -c -m fib3 fib3.f

For f2py to work you need to have a signature file to direct the interface creation or modify your source code with f2py comments to help with the interface. See https://numpy.org/doc/stable/f2py/signature-file.html for more information.

From that site:

C FILE: FIB3.F
      SUBROUTINE FIB(A,N)
C
C     CALCULATE FIRST N FIBONACCI NUMBERS
C
      INTEGER N
      REAL*8 A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
      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
      END
C END FILE FIB3.F

Building the extension module can be now carried out in one command:

f2py -c -m fib3 fib3.f
魔法唧唧 2024-12-28 23:07:51

要使用 f2py 包装 F90 模块,您需要指定可以从模块外部访问哪些功能。为此,您有 private 和 public fortran90 关键字。您还应该将扩展名从 .f 更改为 .F90 或 .f90。我对您的代码进行了一些更改,以演示 publicprivate 关键字的使用。

首先创建包含模块对象的 Fortran 接口的签名文件:

f2py -h interface.pyf -m module_name module_its.f90 --overwrite-signature

然后编译模块:

f2py interface.pyf -c module_its.f90 

您的代码已修改并命名为 module_its.f90:

module its   
  ! use other modules
  implicit none
  private          ! everything in this module is private by standard 
                   ! (it can be used only inside this F90 module)
  public :: itimes ! Make you subroutine public
 
  contains

  subroutine itimes(infile,outfile)
    ! use other modules here to

    !implicit none holds for everthing in this module

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

end module its

To wrap a F90 module with f2py you do need to specify what features can be accessible from outside the module. For that you have the private and public fortran90 keywords. You should also change the extension from .f to .F90 or .f90. I made some changes in your code that demonstrates the use of public and private keywords.

First of all create the signature file that hols the fortran interfaces for the module objects:

f2py -h interface.pyf -m module_name module_its.f90 --overwrite-signature

THen compile the module:

f2py interface.pyf -c module_its.f90 

Your code modified and named as module_its.f90:

module its   
  ! use other modules
  implicit none
  private          ! everything in this module is private by standard 
                   ! (it can be used only inside this F90 module)
  public :: itimes ! Make you subroutine public
 
  contains

  subroutine itimes(infile,outfile)
    ! use other modules here to

    !implicit none holds for everthing in this module

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile
    character(*), intent(in) :: outfile

    ! Internal variables
    real(dp) :: num
    integer :: inu
    integer :: outu
    integer :: ios

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

end module its
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文