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 1 commit
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
Loading