Skip to content

added c++ backend for brick sort and brick sort parallel #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include "misc_algorithms.hpp"

static PyMethodDef algorithms_PyMethodDef[] = {
{"brick_sort_parallel_impl", (PyCFunction) brick_sort_parallel_impl,
METH_VARARGS | METH_KEYWORDS, ""},
{"brick_sort_impl", (PyCFunction) brick_sort_impl,
METH_VARARGS | METH_KEYWORDS, ""},
{"brick_sort", (PyCFunction) brick_sort,
METH_VARARGS | METH_KEYWORDS, ""},
{"quick_sort", (PyCFunction) quick_sort,
METH_VARARGS | METH_KEYWORDS, ""},
{"bubble_sort", (PyCFunction) bubble_sort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,157 @@ static PyObject* insertion_sort(PyObject* self, PyObject* args, PyObject* kwds)
}


static PyObject* brick_sort_impl(PyObject* array, size_t lower, size_t upper, PyObject* comp){
bool is_sorted = false;

while(!is_sorted){
is_sorted = true;

for (size_t i = lower + 1; i < upper; i += 2) {
PyObject* i_PyObject = PyLong_FromSize_t(i);
PyObject* i1_PyObject = PyLong_FromSize_t(i + 1);
if (_comp(PyObject_GetItem(array, i_PyObject), PyObject_GetItem(array, i1_PyObject), comp) != 1) {
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
PyObject_SetItem(array, i_PyObject, tmp);
is_sorted = false;
}
}

for (size_t i = lower; i < upper; i += 2) {
PyObject* i_PyObject = PyLong_FromSize_t(i);
PyObject* i1_PyObject = PyLong_FromSize_t(i + 1);
if (_comp(PyObject_GetItem(array, i_PyObject), PyObject_GetItem(array, i1_PyObject), comp) != 1) {
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
PyObject_SetItem(array, i_PyObject, tmp);
is_sorted = false;
}
}
}

return array;
}

static PyObject* brick_sort(PyObject* self, PyObject* args, PyObject* kwds){
PyObject *args0 = NULL, *start = NULL, *end = NULL;
PyObject *comp = NULL;
size_t lower, upper;

args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if (!is_DynamicOneDimensionalArray && !is_OneDimensionalArray) {
raise_exception_if_not_array(args0);
return NULL;
}

comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if (comp == NULL) {
PyErr_Clear();
}

start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if (start == NULL) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}

end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if (end == NULL) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}

args0 = brick_sort_impl(args0, lower, upper, comp);
if (is_DynamicOneDimensionalArray) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}

Py_INCREF(args0);
return args0;
}


static PyObject* brick_sort_parallel_impl(PyObject* array, size_t lower, size_t upper, PyObject* comp) {
bool is_sorted = false;

while (!is_sorted) {
is_sorted = true;

#pragma omp parallel for
for (size_t i = lower + 1; i < upper; i += 2) {
PyObject* i_PyObject = PyLong_FromSize_t(i);
PyObject* i1_PyObject = PyLong_FromSize_t(i + 1);
if (_comp(PyObject_GetItem(array, i_PyObject), PyObject_GetItem(array, i1_PyObject), comp) != 1) {
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
PyObject_SetItem(array, i_PyObject, tmp);
is_sorted = false;
}
}

#pragma omp parallel for
for (size_t i = lower; i < upper; i += 2) {
PyObject* i_PyObject = PyLong_FromSize_t(i);
PyObject* i1_PyObject = PyLong_FromSize_t(i + 1);
if (_comp(PyObject_GetItem(array, i_PyObject), PyObject_GetItem(array, i1_PyObject), comp) != 1) {
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
PyObject_SetItem(array, i_PyObject, tmp);
is_sorted = false;
}
}
}

return array;
}

static PyObject* brick_sort_parallel(PyObject* self, PyObject* args, PyObject* kwds) {
PyObject *args0 = NULL, *start = NULL, *end = NULL;
PyObject *comp = NULL;
size_t lower, upper;

args0 = PyObject_GetItem(args, PyZero);
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
if (!is_DynamicOneDimensionalArray && !is_OneDimensionalArray) {
raise_exception_if_not_array(args0);
return NULL;
}

comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
if (comp == NULL) {
PyErr_Clear();
}

start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
if (start == NULL) {
PyErr_Clear();
lower = 0;
} else {
lower = PyLong_AsSize_t(start);
}

end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
if (end == NULL) {
PyErr_Clear();
upper = PyObject_Length(args0) - 1;
} else {
upper = PyLong_AsSize_t(end);
}

args0 = brick_sort_parallel_impl(args0, lower, upper, comp);
if (is_DynamicOneDimensionalArray) {
PyObject_CallMethod(args0, "_modify", "O", Py_True);
}

Py_INCREF(args0);
return args0;
}

#endif
8 changes: 8 additions & 0 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def brick_sort(array, **kwargs):
==========
.. [1] https://www.geeksforgeeks.org/odd-even-sort-brick-sort/
"""
backend = kwargs.pop("backend", Backend.PYTHON)
if backend == Backend.CPP:
return _algorithms.brick_sort(array, **kwargs)

raise_if_backend_is_not_python(
brick_sort, kwargs.get('backend', Backend.PYTHON))
start = kwargs.get('start', 0)
Expand Down Expand Up @@ -257,6 +261,10 @@ def brick_sort_parallel(array, num_threads, **kwargs):

.. [1] https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
"""
backend = kwargs.pop("backend", Backend.PYTHON)
if backend == Backend.CPP:
return _algorithms.brick_sort_parallel(array, num_threads, **kwargs)

raise_if_backend_is_not_python(
brick_sort_parallel, kwargs.get('backend', Backend.PYTHON))
start = kwargs.get('start', 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import random, timeit, functools, os, pytest
from pydatastructs import (OneDimensionalArray, Backend,
DynamicOneDimensionalArray, quick_sort, bubble_sort, selection_sort,
insertion_sort, is_ordered, linear_search, binary_search, jump_search)
insertion_sort, is_ordered, linear_search, binary_search, jump_search,
brick_sort, brick_sort_parallel)

def _test_common_sort(sort, **kwargs):
cpp = Backend.CPP
Expand Down Expand Up @@ -52,6 +53,16 @@ def test_insertion_sort():
_test_common_sort(insertion_sort, size=2000)


@pytest.mark.xfail
def test_brick_sort():
_test_common_sort(brick_sort, size=500)


@pytest.mark.xfail
def test_brick_sort_parallel():
_test_common_sort(brick_sort_parallel, size=500, num_threads=4)


@pytest.mark.xfail
def test_is_ordered():
cpp = Backend.CPP
Expand Down
2 changes: 2 additions & 0 deletions pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ def test_merge_sort_parallel():

def test_brick_sort():
_test_common_sort(brick_sort)
_test_common_sort(brick_sort, Backend = Backend.CPP)

def test_brick_sort_parallel():
_test_common_sort(brick_sort_parallel, num_threads=3)
_test_common_sort(brick_sort_parallel, num_threads=3, Backend = Backend.CPP)

def test_heapsort():
_test_common_sort(heapsort)
Expand Down
Loading