在f2py中使用Fortran回调函数时出错 [英] Errors when using a Fortran call back function in f2py

查看:141
本文介绍了在f2py中使用Fortran回调函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用python编写代码,该代码利用了Fortran 90优化算法. 想法是在python中有MAIN例程,该例程调用Fortran中的算法,该例程调用另一个传递某些参数的python函数以计算目标函数的值(即,它返回标量),如下所示:

I am writing a code in python that exploits a Fortran 90 optimization algorithm. The idea is to have the MAIN routine in python that calls the algorithm in Fortran that calls another python function passing some parameters in order to calculate the value of an objective function (that is, it returns a scalar), like this:

[Python) MAIN  ⇒ [Fortran] BB_algo ⇒ [Python] FUNCT

应该通过使用python中的f2py库解决此问题. 为了尝试使这种方法起作用,我编写了一些更简单的代码来进行类似的操作.

Such problem should be solved by using the f2py libraries in python. In order to try to make such method work I have written some simpler code that makes similar operations.

Fortran子例程称为writepy:

The Fortran subroutine is called writepy:

subroutine writepy(funct,n)
    implicit none
    real, EXTERNAL :: funct
      INTEGER :: I,n
      REAL*8  :: r
!f2py intent(in), n
!f2py intent(out), r
!f2py real y
!f2py y=funct(y)
      r = 1D0
      DO I=1,5
         write(*,*) 'funct:', funct(n)
         r = r + funct(n)
      ENDDO
    return
end

而Python中的代码如下:

while the code in Python is the following:

import os
os.system("f2py -m wp  -c writepy.f90")
import wp

def funct(n):
    print(n);    
    return n

if __name__ == '__main__':
    n=3;    
    wp.writepy(funct,n)

Fortran使用gfortran可以很好地编译,但是当我尝试使用f2py进行编译时,似乎Fortran C的包装器存在一些错误,但是我现在真的不知道该怎么做.

Fortran compiles well with gfortran, but when I try to compile it with f2py it seems that there are some errors with the wrapper in C of Fortran, but I do not really know what to do at this point.

我得到以下信息:

running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "wp" sources
f2py options: []
f2py:> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
Reading fortran codes...
    Reading file 'writepy.f90' (format:free)
{'after': '(in), n', 'before': '', 'this': 'intent'}
Line #12 in writepy.f90:"      intent(in), n"
    analyzeline: no name pattern found in intent statement for ''. Skipping.
{'after': '(out), r', 'before': '', 'this': 'intent'}
Line #13 in writepy.f90:"      intent(out), r"
    analyzeline: no name pattern found in intent statement for ''. Skipping.
Post-processing...
    Block: wp
            Block: writepy
Post-processing (stage 2)...
Building modules...
    Building module "wp"...
        Constructing wrapper function "writepy"...
sign2map: Confused: external funct is not in lcb_map[].
append_needs: unknown need 'funct'
append_needs: unknown need 'funct'
          writepy(funct,n,[funct_extra_args])
    Wrote C/API module "wp" to file "/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c"
  adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c' to sources.
  adding '/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5' to include_dirs.
copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
copying /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'wp' extension
compiling C sources
C compiler: /usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64

creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm
creating /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5
compile options: '-I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c'
clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:2:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.c:139:18: warning: comparison of integers of different signs: 'Py_ssize_t' (aka 'long') and 'unsigned long' [-Wsign-compare]
        if (size < sizeof(notalloc)) {
            ~~~~ ^ ~~~~~~~~~~~~~~~~
2 warnings generated.
clang: /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
4 warnings and 8 errors generated.
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:277:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:293:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:295:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:298:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:299:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:273:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:313:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:332:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:334:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c:335:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
4 warnings and 8 errors generated.
error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpi34fejrm/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1

修改

由于@DavidW的回答,代码现在可以工作了,但是如果我尝试从另一个函数调用fortran函数"writepy",则使用未声明的标识符"的错误再次出现.

Thanks to @DavidW's answer the code now works, but if I try to call the fortran function "writepy" from another function, the error of "use of undeclared identifier" is once again there.

新的Fortran代码为

the new Fortran code is

subroutine writepy(funct,n,r)
    implicit none
    EXTERNAL :: funct
    REAL     :: funct
    INTEGER :: I,n
    intent(in):: n
    REAL*8  :: r
    intent(out) ::r
    r = 1D0
    DO I=1,5
        write(*,*) 'funct:', funct(I)
        r = r + funct(n)
    ENDDO
    return
end

subroutine call_writepy(funct,n,r)
    implicit none
    EXTERNAL :: funct
    REAL     :: funct
    INTEGER :: n
    intent(inout):: n
    REAL*8  :: r
    intent(inout) ::r
    call writepy(funct,n,r)
    return
end

它可以使用GFortran进行编译,但是当我将其与Python结合使用时,我会得到:

it compiles with GFortran, but when I use it with Python I get:

/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  if (capi_j>capi_i)
      ^~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here
  return return_value;
         ^~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true
  if (capi_j>capi_i)
  ^~~~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning
  float return_value;
                    ^
                     = 0.0
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
5 warnings and 8 errors generated.
routsign2map: Confused: function call_writepy has externals ['funct'] but no "use" statement.
sign2map: Confused: external funct is not in lcb_map[].
append_needs: unknown need 'funct'
append_needs: unknown need 'funct'
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:18:
In file included from /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/fortranobject.h:13:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/arrayobject.h:4:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarrayobject.h:18:
In file included from /Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788:
/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by "          "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it by " \
 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:7: warning: variable 'return_value' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
  if (capi_j>capi_i)
      ^~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:446:10: note: uninitialized use occurs here
  return return_value;
         ^~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:431:3: note: remove the 'if' if its condition is always true
  if (capi_j>capi_i)
  ^~~~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:363:21: note: initialize the variable 'return_value' to silence this warning
  float return_value;
                    ^
                     = 0.0
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:590:3: error: use of undeclared identifier 'funct_typedef'
  funct_typedef funct_cptr;
  ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:611:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = F2PyCapsule_AsVoidPtr(funct_capi);
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:3: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
  funct_cptr = funct;
  ^~~~~~~~~~
  funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:613:16: error: use of undeclared identifier 'funct'
  funct_cptr = funct;
               ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:616:24: error: use of undeclared identifier 'funct_nofargs'
  funct_nofargs_capi = funct_nofargs;
                       ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:617:50: error: expected expression
  if (create_cb_arglist(funct_capi,funct_xa_capi,#maxnofargs#,#nofoptargs#,&funct_nofargs,&funct_args_capi,"failed in processing argument list for call-back funct.")) {
                                                 ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: error: use of undeclared identifier 'funct_cptr'; did you mean 'funct_capi'?
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
                     funct_capi
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:586:13: note: 'funct_capi' declared here
  PyObject *funct_capi = Py_None;
            ^
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:634:22: warning: incompatible pointer types passing 'PyObject *' (aka 'struct _object *') to parameter of type 'float *' [-Wincompatible-pointer-types]
        (*f2py_func)(funct_cptr,&n,&r);
                     ^~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:660:16: warning: explicitly assigning a variable of type 'PyObject *' (aka 'struct _object *') to itself [-Wself-assign]
    funct_capi = funct_capi;
    ~~~~~~~~~~ ^ ~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:662:21: warning: explicitly assigning a variable of type 'PyTupleObject *' to itself [-Wself-assign]
    funct_args_capi = funct_args_capi;
    ~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~
/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c:663:5: error: use of undeclared identifier 'funct_nofargs'
    funct_nofargs = funct_nofargs_capi;
    ^
5 warnings and 8 errors generated.
error: Command "/usr/bin/clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/Valyria/anaconda/include -arch x86_64 -I/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5 -I/Users/Valyria/anaconda/lib/python3.5/site-packages/numpy/core/include -I/Users/Valyria/anaconda/include/python3.5m -c /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.c -o /var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/var/folders/4q/g84zdhps5gzbk3t5mfplqjsc0000gn/T/tmpe9wq9teg/src.macosx-10.6-x86_64-3.5/wpmodule.o" failed with exit status 1
256

推荐答案

问题是由于编译器无法从Python识别函数"funct"而导致的.为了使编译器意识到这种功能及其作用,需要修改.pyf文件.

The problem is due to the fact that the compiler did not recognize the function "funct" from Python. In order to make the compiler aware of such function and its role, one needs to modify the .pyf file.

自动生成的.pyf文件(使用命令f2py writepy.f90 -m wp -h wp.pyf)如下:

The automatically generated .pyf file (with the command f2py writepy.f90 -m wp -h wp.pyf) is the following:

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

python module __user__routines 
    interface writepy_user_interface 
        function funct(i) ! in :wp:writepy.f90:writepy:unknown_interface
            integer :: i
            real :: funct
        end function funct
    end interface writepy_user_interface
end python module __user__routines

python module wp ! in 
    interface  ! in :wp
        subroutine writepy(funct,n,r) ! in :wp:writepy.f90
            use writepy__user__routines
            external funct
            integer intent(in) :: n
            real*8 intent(out) :: r
        end subroutine writepy

        subroutine call_writepy(funct,n,r,q) ! in :wp:writepy.f90
            external funct
            integer intent(inout) :: n
            real*8 intent(out) :: r
            real*8 intent(out) :: q
        end subroutine call_writepy
    end interface 
end python module wp

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

很容易注意到f2py在"__user__routines"模块中识别出"funct".但是,它不理解在子例程"writepy"和"call_writepy"中都使用了这种功能.这是错误的根源. 因此,用户必须修改.pyf文件,并在所有以"funct"作为外部对象存在的子例程中添加命令"use __user__routines":

It is easy to notice that f2py recognizes "funct" in the "__user__routines" module. However it does not understand that such function is used in both the subroutines "writepy" and "call_writepy". This is the source of the error. Therefore the user must modify the .pyf file, adding the command "use __user__routines", in all the subroutines where "funct" is present as an external object:

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


python module __user__routines
    interface
        function funct(i) 
            integer :: i
            real*8  :: r
        end function funct
    end interface
end python module __user__routines


python module wp ! in 
    interface  ! in :wp
        subroutine writepy(funct,n,r) ! in :wp:writepy.f90
            use __user__routines
            external funct
            integer intent(in) :: n
            real*8 intent(out) :: r
        end subroutine writepy

        subroutine call_writepy(funct,n,r)! in :wp:writepy.f90
            use __user__routines
            external funct
            integer intent(inout) :: n
            real*8 intent(out) :: r
            real*8 intent(out) :: q
        end subroutine call_writepy
    end interface 
end python module wp

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

在f2py中编译Fortran代码时,请使用以下命令: f2py -c wp.pyf writepy.f90 告诉编译器显式使用编辑后的文件"wp.pyf"作为参考

And use the following command when compiling in f2py the Fortran code: f2py -c wp.pyf writepy.f90 telling to the compiler to explicitly use the edited file "wp.pyf" as a reference

这篇关于在f2py中使用Fortran回调函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆