Skip to content

Instantly share code, notes, and snippets.

@nico-zck
Last active February 1, 2023 05:58
Show Gist options
  • Select an option

  • Save nico-zck/3b6b7aede686d7b3ec403e6e50beda0c to your computer and use it in GitHub Desktop.

Select an option

Save nico-zck/3b6b7aede686d7b3ec403e6e50beda0c to your computer and use it in GitHub Desktop.

Revisions

  1. nico-zck renamed this gist Jan 8, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nico-zck revised this gist Jan 8, 2022. 2 changed files with 1 addition and 2 deletions.
    2 changes: 0 additions & 2 deletions parallelsort.pyx
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    import numpy as np
    cimport numpy as np
    import cython
    cimport cython

    1 change: 1 addition & 0 deletions how-to-use.md → python-parallel-sort.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    build:

    `python3 setup.py build_ext -if`

    using:
  3. nico-zck renamed this gist Jan 8, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. nico-zck renamed this gist Jan 8, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. nico-zck revised this gist Jan 8, 2022. No changes.
  6. nico-zck revised this gist Jan 8, 2022. 2 changed files with 14 additions and 1 deletion.
    14 changes: 14 additions & 0 deletions compile.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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)
    ```
    1 change: 0 additions & 1 deletion compile.sh
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    python3 setup.py build_ext -if
  7. nico-zck created this gist Jan 8, 2022.
    1 change: 1 addition & 0 deletions compile.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    python3 setup.py build_ext -if
    29 changes: 29 additions & 0 deletions parallelsort.pyx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import numpy as np
    cimport numpy as np
    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]])
    17 changes: 17 additions & 0 deletions setup.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    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"}),
    )