fortran - 将 Fortran 逻辑与 Cython Bint 接口(interface)的问题

标签 fortran cython boolean-logic

我目前正在使用 Cython 包装要从 Python 调用的 Fortran 模块,Fortran 子例程目前有一个可选的逻辑参数。我已经使用 iso_c_binding 来包装子例程,并且我已经编写了 cython .pyx 文件来创建 .so 文件。但是,当我运行安装脚本时,会收到两个警告:

warning: passing argument 4 of 'c_geo_manipulate' from incompatible pointer type [-Wincompatible-pointer-types]
     c_geo_manipulate(((geo *)(&__pyx_v_v1)), ((geo *)(&__pyx_v_v2)), ((geo *)(&__pyx_v_value)), ((int *)(&__pyx_v_optional_weights_output)));
pyginterpolation.h:17:13: note: expected '_Bool *' but argument is of type 'int *'
 extern void c_geo_manipulate(geo *v1, geo *v2, geo *v3, bool *output_weight);
以下是我正在使用的文件:
interpolation.f90 
module interpolation_module 
use iso_c_binding
implicit none 


type, bind(c) :: geo
    real, dimension(2) :: coordinates 
    real :: weight 
end type geo

contains 


    subroutine geo_manipulate(v1, v2, v3, output_weight)
        type(geo), intent(in) :: v1, v2
        logical, optional, intent(in) :: output_weight
        type(geo), intent(out) :: v3

        if (present(output_weight).and.(output_weight .eqv. .true.)) then 
            v3%coordinates = v1%coordinates * v1%weight + v2%coordinates * v2%weight 
            v3%weight = v1%weight + v2%weight 
        
        else
            print *, 'false'
            v3%coordinates = v1%coordinates * v1%weight + v2%coordinates * v2%weight
            v3%weight = 0
        end if 
        
    end subroutine geo_manipulate

end module interpolation_module
pyginterpolation.f90
module interpolation_interface 
use interpolation_module 
use iso_c_binding 
implicit none 


contains 


    subroutine c_geo_manipulate(v1, v2, v3, output_weight) bind(c)
        type(geo), intent(in) :: v1, v2
        type(geo), intent(out) :: v3
        logical(c_bool), optional, intent(in) :: output_weight

        if ((present(output_weight)).and.(output_weight .eqv. .true.)) then
            call geo_manipulate(v1, v2, v3, output_weight = .true.)
        else
            call geo_manipulate(v1, v2, v3)

        end if 
    end subroutine c_geo_manipulate

end module interpolation_interface
pyginterpolation.h
#include <stdbool.h>

struct _geo {
    float coordinates[2];
    float weight;
};

typedef struct _geo geo;



extern void c_geo_manipulate(geo *v1, geo *v2, geo *v3, bool *output_weight);
pyginterpolation.pyx
import numpy as np

cdef bint boolean_variable = True 

cdef extern from "pyginterpolation.h":
    ctypedef struct geo:
        float coordinates[2]
        float weight

    cdef void c_geo_manipulate(geo *v1, geo *v2, geo *v3, bint *output_weight)


def f(geo v1, geo v2, output_weights = False):
    cdef geo value
    if output_weights is True: 
        optional_weights_output = True 
        c_geo_manipulate(<geo*> &v1,<geo*> &v2,<geo*> &value, <bint*> &optional_weights_output)
        return np.array(value)
    else: 
        optional_weights_output = False
        c_geo_manipulate(<geo*> &v1,<geo*> &v2,<geo*> &value, <bint*> &optional_weights_output)
        return np.array(value.coordinates)
设置文件
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
# This line only needed if building with NumPy in Cython file.
from numpy import get_include
from os import system

# compile the fortran modules without linking
fortran_mod_comp = 'gfortran interpolation.f90 -c -o interpolation.o -O3 -fPIC'
print (fortran_mod_comp)
system(fortran_mod_comp)
shared_obj_comp = 'gfortran pyginterpolation.f90 -c -o pyginterpolation.o -O3 -fPIC'
print (shared_obj_comp)
system(shared_obj_comp)

ext_modules = [Extension(# module name:
                         'pyginterpolation',
                         # source file:
                         ['pyginterpolation.pyx'],
                         # other compile args for gcc
                         extra_compile_args=['-fPIC', '-O3'],
                         # other files to link to
                         extra_link_args=['interpolation.o', 'pyginterpolation.o'])]

setup(name = 'pyginterpolation',
      cmdclass = {'build_ext': build_ext},
      # Needed if building with NumPy.
      # This includes the NumPy headers when compiling.
      include_dirs = [get_include()],
      ext_modules = ext_modules)
据我所知,cython 有 bint 可用于 C 的 bool 值,但我对它们的工作方式不太满意,而且我对 C/Fortran 还是很陌生,所以我对指针(和一般的语言)工作仍然很初级。有谁知道我对这些逻辑的使用可能有什么问题?
谢谢!

最佳答案

Cython bint type 不会将 Python bool 值映射到 C99 _Bool类型,而是到 int .所以 output_weight 参数的类型应该是 integer(c_int) .也就是说,像


    subroutine c_geo_manipulate(v1, v2, v3, output_weight) bind(c)
        type(geo), intent(in) :: v1, v2
        type(geo), intent(out) :: v3
        logical(c_int), optional, intent(in) :: output_weight
        logical :: ow

        if ((present(output_weight)).and.(output_weight == 0)) then
          ow = .FALSE.
        else
          ow = .TRUE.
        end if

        if (ow) then
            call geo_manipulate(v1, v2, v3, ow)
        else
            call geo_manipulate(v1, v2, v3)

        end if 
    end subroutine c_geo_manipulate

关于fortran - 将 Fortran 逻辑与 Cython Bint 接口(interface)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69321145/

相关文章:

cython - 如何在 Cython 中扩展宏

mingw - Windows 上的 Cython pyximport 错误

c# - 如何判断两个 boolean 表达式是否相同

file-io - 在 Fortran 90 中从文件末尾读取

input - 读取未知长度的字符串

fortran - OpenMP 子程序中没有线程

fortran - 具有无限多态实体的 Fortran 是否可以进行通用解析

python - Cython 可以将 int 65 转换为 char 'A' 吗?

c++ - 在与多个字段进行比较时提供严格的排序

Prolog SAT 求解器