restore old qsort() based code and add preprocessor directives to switch

-DLMP_USE_LIBC_QSORT will use qsort() from libc to sort (requires static/global variables).
-DLMP_USE_MERGE_SORT will use a plain merge sort. slightly slower for expensive comparisons.
-DLMP_USE_HYBRID_SORT will use hybrid merge sort. faster than merge sort (no static/global variables)
This commit is contained in:
Axel Kohlmeyer
2017-06-16 18:17:48 -04:00
parent 2a6f026853
commit c1b0b1b3f9
5 changed files with 162 additions and 9 deletions

View File

@ -21,13 +21,22 @@
#include "comm.h"
#include "memory.h"
#include "mergesort.h"
using namespace LAMMPS_NS;
#if defined(LMP_USE_LIBC_QSORT)
// allocate space for static class variable
// prototype for non-class function
int *Irregular::proc_recv_copy;
static int compare_standalone(const void *, const void *);
#else
#include "mergesort.h"
// prototype for non-class function
static int compare_standalone(const int, const int, void *);
#endif
enum{LAYOUT_UNIFORM,LAYOUT_NONUNIFORM,LAYOUT_TILED}; // several files
@ -423,7 +432,13 @@ int Irregular::create_atom(int n, int *sizes, int *proclist, int sortflag)
int *length_recv_ordered = new int[nrecv_proc];
for (i = 0; i < nrecv_proc; i++) order[i] = i;
#if defined(LMP_USE_LIBC_QSORT)
proc_recv_copy = proc_recv;
qsort(order,nrecv_proc,sizeof(int),compare_standalone);
#else
merge_sort(order,nrecv_proc,(void *)proc_recv,compare_standalone);
#endif
int j;
for (i = 0; i < nrecv_proc; i++) {
@ -449,6 +464,25 @@ int Irregular::create_atom(int n, int *sizes, int *proclist, int sortflag)
return nrecvsize;
}
#if defined(LMP_USE_LIBC_QSORT)
/* ----------------------------------------------------------------------
comparison function invoked by qsort()
accesses static class member proc_recv_copy, set before call to qsort()
------------------------------------------------------------------------- */
int compare_standalone(const void *iptr, const void *jptr)
{
int i = *((int *) iptr);
int j = *((int *) jptr);
int *proc_recv = Irregular::proc_recv_copy;
if (proc_recv[i] < proc_recv[j]) return -1;
if (proc_recv[i] > proc_recv[j]) return 1;
return 0;
}
#else
/* ----------------------------------------------------------------------
comparison function invoked by merge_sort()
void pointer contains proc_recv list;
@ -462,6 +496,8 @@ int compare_standalone(const int i, const int j, void *ptr)
return 0;
}
#endif
/* ----------------------------------------------------------------------
communicate atoms via PlanAtom
sendbuf = list of atoms to send
@ -668,8 +704,13 @@ int Irregular::create_data(int n, int *proclist, int sortflag)
int *num_recv_ordered = new int[nrecv_proc];
for (i = 0; i < nrecv_proc; i++) order[i] = i;
merge_sort(order,nrecv_proc,(void *)proc_recv,compare_standalone);
#if defined(LMP_USE_LIBC_QSORT)
proc_recv_copy = proc_recv;
qsort(order,nrecv_proc,sizeof(int),compare_standalone);
#else
merge_sort(order,nrecv_proc,(void *)proc_recv,compare_standalone);
#endif
int j;
for (i = 0; i < nrecv_proc; i++) {
j = order[i];