【发布时间】:2021-10-12 08:49:05
【问题描述】:
以下程序在子程序不包含任何内部过程时编译(在 f2py 中),但在 f2py 中编译失败(使用 gfortran 编译)当它包含任何内部过程时
Fortran 代码:
subroutine example(array)
implicit none
real*8::array(:,:)
INTEGER::i,j
!f2py intent(inout)::array
do i=1,3
do j=1,3
array(i,j)=10*i+j
enddo
! print*,''
enddo
call tentimes(array)
RETURN
contains
subroutine tentimes(array)
implicit none
!f2py intent(inout):array
real*8::array(:,:)
array=array*10
end subroutine tentimes
end subroutine example
program dummy
implicit none
end program dummy
用python -m numpy.f2py -c allocate.f90 -m ftest编译
和python代码:
import numpy as np
a=np.zeros((3,3),order="F")
print(a)
res=ftest.example(a)
print(a)
为什么会这样?
f2py 产生的错误:
compiling Fortran sources
Fortran f77 compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran f90 compiler: /usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
Fortran fix compiler: /usr/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops
compile options: '-I/tmp/tmpwd_qygau/src.linux-x86_64-3.9 -I/home/srj/.local/lib/python3.9/site-packages/numpy/core/include -I/usr/include/python3.9 -c'
gfortran:f90: allocate.f90
allocate.f90:17:31:
17 | subroutine tentimes(array_i)
| 1
Warning: Unused dummy argument ‘array_i’ at (1) [-Wunused-dummy-argument]
allocate.f90:17:23:
17 | subroutine tentimes(array_i)
| ^
Warning: ‘tentimes’ defined but not used [-Wunused-function]
gfortran:f90: /tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.f90
/tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.f90:13:17:
13 | subroutine tentimes(array_i)
| 1
Error: Unclassifiable statement at (1)
/tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.f90:15:39:
15 | end subroutine tentimes
| 1
Error: Expected label ‘example’ for END SUBROUTINE statement at (1)
/tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.f90:14:53:
14 | real*8, dimension(:,:) :: array_i
| 1
Error: Array ‘array_i’ at (1) cannot have a deferred shape
error: Command "/usr/bin/gfortran -Wall -g -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmpwd_qygau/src.linux-x86_64-3.9 -I/home/srj/.local/lib/python3.9/site-packages/numpy/core/include -I/usr/include/python3.9 -c -c /tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.f90 -o /tmp/tmpwd_qygau/tmp/tmpwd_qygau/src.linux-x86_64-3.9/ftest-f2pywrappers2.o" failed with exit status 1
类似的嵌套过程在 gfortran 中运行良好。为什么会发生此错误?如何纠正?
【问题讨论】:
标签: numpy fortran gfortran f2py