如何在C ++中使用模板化函数在Cython中使用两种类型?

时间:2017-10-07 10:50:18

标签: python c++ templates cython

我有一个模板化的C ++函数,我希望能够使用这两种类型。由于Python不支持重载,我有点陷入困境,如何解决这个问题。我有一个.pyx,如下所示。如何在floatdouble中使用C ++函数?

import cython
import numpy as np
cimport numpy as np

# declare the interface to the C code
cdef extern from "diff_cpp.cpp" namespace "diff":
    cdef void diff_cpp[float] (float* at, const float* a, const float visc,
                               const float dxidxi, const float dyidyi, const float dzidzi,
                               const int itot, const int jtot, const int ktot)

cdef extern from "diff_cpp.cpp" namespace "diff":
    cdef void diff_cpp[double] (double* at, const double* a, const double visc,
                                const double dxidxi, const double dyidyi, const double dzidzi,
                                const int itot, const int jtot, const int ktot)

@cython.boundscheck(False)
@cython.wraparound(False)
def diff(np.ndarray[double, ndim=3, mode="c"] at not None,
         np.ndarray[double, ndim=3, mode="c"] a not None,
         double visc, double dxidxi, double dyidyi, double dzidzi):
    cdef int ktot, jtot, itot
    ktot, jtot, itot = at.shape[0], at.shape[1], at.shape[2]
    diff_cpp[double](&at[0,0,0], &a[0,0,0], visc, dxidxi, dyidyi, dzidzi, itot, jtot, ktot)
    return None

@cython.boundscheck(False)
@cython.wraparound(False)
def diff_f(np.ndarray[float, ndim=3, mode="c"] at not None,
           np.ndarray[float, ndim=3, mode="c"] a not None,
           float visc, float dxidxi, float dyidyi, float dzidzi):
    cdef int ktot, jtot, itot
    ktot, jtot, itot = at.shape[0], at.shape[1], at.shape[2]
    diff_cpp[float](&at[0,0,0], &a[0,0,0], visc, dxidxi, dyidyi, dzidzi, itot, jtot, ktot)
    return None

更新解决方案

@ oz1的答案提供了正确的方法。这个代码适用于那些对这个特定问题的解决方案感兴趣的人。

import cython
import numpy as np
cimport numpy as np

# declare the interface to the C code
cdef extern from "diff_cpp.cpp" namespace "diff":
    cdef void diff_cpp[T](T* at, const T* a, const T visc,
                          const T dxidxi, const T dyidyi, const T dzidzi,
                          const int itot, const int jtot, const int ktot)

ctypedef fused float_t:
    cython.float
    cython.double

@cython.boundscheck(False)
@cython.wraparound(False)
def diff(np.ndarray[float_t, ndim=3, mode="c"] at not None,
         np.ndarray[float_t, ndim=3, mode="c"] a not None,
         float_t visc, float_t dxidxi, float_t dyidyi, float_t dzidzi):
    cdef int ktot, jtot, itot
    ktot, jtot, itot = at.shape[0], at.shape[1], at.shape[2]
    diff_cpp(&at[0,0,0], &a[0,0,0], visc, dxidxi, dyidyi, dzidzi, itot, jtot, ktot)
    return None

1 个答案:

答案 0 :(得分:1)

两个注释:

  1. Cython支持c ++模板(http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html
  2. Cython已融合类型(http://docs.cython.org/en/latest/src/userguide/fusedtypes.html
  3. 一个例子:

    # lib_wrapper.pyx
    cimport cython
    
    ctypedef fused  float_t:
        cython.float
        cython.double
    
    cdef extern from "lib.cpp" nogil:
        T arr_sum[T](T *arr, size_t size)
    
    def py_arr_sum(float_t[:] arr not None):
        print(sizeof(arr[0]))  # check the element size
        return arr_sum(&arr[0], arr.shape[0])
    
    # use.py
    import numpy as np
    from lib_wrapper import py_arr_sum
    
    print(py_arr_sum(np.array([1,2,3], dtype=np.float32)))
    print(py_arr_sum(np.array([1,2,3], dtype=np.float64)))
    print(py_arr_sum(np.array([1,2,3], dtype=np.int32)))  # oops
    
    android:background="@drawable/ic_play_arrow_black_24dp"