【问题标题】:Issues with interfacing Fortran Logicals with Cython Bint将 Fortran 逻辑与 Cython Bint 接口的问题
【发布时间】:2021-09-24 21:11:33
【问题描述】:

我目前正在使用 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)

setup.py

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 可以用于 C 的布尔值,但我对它们的工作方式不太满意,而且我对 C/Fortran 还是很陌生,所以我对如何指针(以及一般的语言)的工作仍然非常初级。有谁知道我对这些逻辑的使用可能有什么问题?

谢谢!

【问题讨论】:

  • 正如我上次告诉你的:删除所有演员表&lt;geo*&gt;。要么类型匹配且强制转换是不必要的,要么类型不匹配并且强制转换使编译器更难告诉你。

标签: fortran cython boolean-logic


【解决方案1】:

Cython bint 类型不会将 Python 布尔值映射到 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多