Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping C++ code with function pointer as template parameter in cython

Tags:

c++

python

cython

I am trying to wrap the following declaration written in C++ in cython:

template<typename T, double (*distance)(const DataPoint&, const DataPoint&)>
class VpTree
{...}

I've also got the following definition in C++:

inline double euclidean_distance(const DataPoint &t1, const DataPoint &t2) {...}

and I'm trying to wrap this in cython. This is what I've been able to come up with following the documentation:

cdef extern from "vptree.h":
    # declaration of DataPoint omitted here

    cdef inline double euclidean_distance(DataPoint&, DataPoint&)

    cdef cppclass VpTree[T, F]:  # F is almost certainly wrong
        ...

and build a wrapper around this:

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree

    def __cinit__(self):
        self.tree = VpTree[DataPoint, euclidean_distance]()

Unfortunately, this results in the following errors:

------------------------------------------------------------

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree
                                            ^
------------------------------------------------------------

unknown type in template argument

------------------------------------------------------------

cdef class VPTree:
    cdef VpTree[DataPoint, euclidean_distance] tree

    def __cinit__(self):
        self.tree = VpTree[DataPoint, euclidean_distance]()
                                     ^
------------------------------------------------------------

unknown type in template argument

I suspect the problem lies in the F part of the definition, and I've tried various things in place of that e.g. double(*)(DataPoint&, DataPoint&) but this obviously results in a syntax error.

like image 691
Pavlin Avatar asked Oct 17 '25 09:10

Pavlin


1 Answers

As far as I know, Cython doesn't support non-type template parameters (that is what function pointer is) directly (I might have missed the memo though), but there is a well known cname-hack to achieve the goal.

Here, for a much simpler example:

%%cython --cplus            
cdef extern from *:
    """
    template<int val>
    int fun(){return val;}
    """
    int fun[T]()

i.e. an int-value as template parameter.

Now we have a dilemma: Cython expects T to be type and g++ (or other compilers) expects an integer value - here comes the cname-hack to our rescue:

%%cython --cplus            
...
cdef extern from *:
    ctypedef int val2 "2" 

def doit():
    return fun[val2]()

Cython believes val2 to be a type (alias for int), but replaces it with 2 in the resulting c++ code (fun<2>()), thus c++-compiler sees an integer-value (2 in this case), as it expects.


For your case that means adding:

%%cython --cplus            
...
cdef extern from *:
    ctypedef int euclidean_distance_t "euclidean_distance" 

cdef class VPTree:
     cdef VpTree[DataPoint, euclidean_distance_t] tree

     def __cinit__(self):
         self.tree = VpTree[DataPoint, euclidean_distance_t]()

You actually don't have to wrap "euclidean_distance" at all, if you don't use it anywhere else in Cython code.

like image 50
ead Avatar answered Oct 19 '25 23:10

ead



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!