Skip to content

Instantly share code, notes, and snippets.

@nico-zck
Last active February 1, 2023 05:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nico-zck/3b6b7aede686d7b3ec403e6e50beda0c to your computer and use it in GitHub Desktop.
Save nico-zck/3b6b7aede686d7b3ec403e6e50beda0c to your computer and use it in GitHub Desktop.
Make Python could access c++ built-in parallel in-place sort via Cython.

build:

python3 setup.py build_ext -if

using:

from parallelsort import parallelsort
import numpy as np

arr = np.random.uniform(0,100,100000000)

%timeit np.sort(arr)
%timeit parallelsort(arr)

import cython
cimport cython
ctypedef fused real:
cython.short
cython.ushort
cython.int
cython.uint
cython.long
cython.ulong
cython.longlong
cython.ulonglong
cython.float
cython.double
cdef extern from "<parallel/algorithm>" namespace "__gnu_parallel":
cdef void sort[T](T first, T last) nogil
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def parallelsort(real[:] a):
"""
In-place parallel sort for numpy types
"""
sort(&a[0], &a[a.shape[0]])
from setuptools import Extension, setup
from Cython.Build import cythonize
ext_modules = [
Extension(
"parallelsort",
["parallelsort.pyx"],
language="c++",
extra_compile_args=['-fopenmp'],
extra_link_args=['-fopenmp'],
)
]
setup(
name='parallelsort',
ext_modules=cythonize(ext_modules, compiler_directives={'language_level': "3"}),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment