9

I'm trying to wrap a parallel sort written in c++ as a template, to use it with numpy arrays of any numeric type. I'm trying to use Cython to do this.

My problem is that I don't know how to pass a pointer to the numpy array data (of a correct type) to a c++ template. I believe I should use fused dtypes for this, but I don't quite understand how.

The code in .pyx file is below

# importing c++ template
cdef extern from "test.cpp":
    void inPlaceParallelSort[T](T* arrayPointer,int arrayLength)

def sortNumpyArray(np.ndarray a):
    # This obviously will not work, but I don't know how to make it work. 
    inPlaceParallelSort(a.data, len(a))

In the past I did similar tasks with ugly for-loops over all possible dtypes, but I believe there should be a better way to do this.

0

1 Answer 1

7

Yes, you want to use a fused type to have Cython call the sorting template for the appropriate specialization of the template. Here's a working example for all non-complex data types that does this with std::sort.

# cython: wraparound = False
# cython: boundscheck = False

cimport cython

cdef extern from "<algorithm>" namespace "std":
    cdef void sort[T](T first, T last) nogil

ctypedef fused real:
    cython.char
    cython.uchar
    cython.short
    cython.ushort
    cython.int
    cython.uint
    cython.long
    cython.ulong
    cython.longlong
    cython.ulonglong
    cython.float
    cython.double

cpdef void npy_sort(real[:] a) nogil:
    sort(&a[0], &a[a.shape[0]-1])
Sign up to request clarification or add additional context in comments.

2 Comments

This did not compile as is, the compiler complained a lot about nogil. I had to kill it, but parallelization worked without it.
@MaximImakaev Yep, the nogil is really only necessary if you want to call the Cython function inside a parallel loop. The parallization inside your sorting function should be fine without it. It's odd that it wouldn't compile with it there though. There's probably some difference in Cython versions. I have a recent development version (pretty similar to 0.22) and it worked fine for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.