remove need for static class member variables in Dump and Irregular

The dump and irregular classes were using qsort() from the C-library
for sorting lists through custom comparison functions, which required
access to additional data, which was passed via static class variables,
i.e. globals. This collides with having multiple LAMMPS instances in
the same address space.

the calls to qsort() are replaced with a custom merge sort, which passes
a void pointer to the comparison functions, which can contain any kind
of desired information, e.g. a class handle or a list
This commit is contained in:
Axel Kohlmeyer
2017-06-14 23:10:53 -04:00
parent 286d4f2743
commit 1f17e8ebbb
4 changed files with 45 additions and 70 deletions

View File

@ -21,13 +21,13 @@
#include "comm.h"
#include "memory.h"
#include "mergesort.h"
using namespace LAMMPS_NS;
// allocate space for static class variable
// prototype for non-class function
int *Irregular::proc_recv_copy;
int compare_standalone(const void *, const void *);
static int compare_standalone(const int, const int, void *);
enum{LAYOUT_UNIFORM,LAYOUT_NONUNIFORM,LAYOUT_TILED}; // several files
@ -423,8 +423,7 @@ 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;
proc_recv_copy = proc_recv;
qsort(order,nrecv_proc,sizeof(int),compare_standalone);
merge_sort(order,nrecv_proc,(void *)proc_recv,compare_standalone);
int j;
for (i = 0; i < nrecv_proc; i++) {
@ -451,15 +450,13 @@ int Irregular::create_atom(int n, int *sizes, int *proclist, int sortflag)
}
/* ----------------------------------------------------------------------
comparison function invoked by qsort()
accesses static class member proc_recv_copy, set before call to qsort()
comparison function invoked by merge_sort()
void pointer contains proc_recv list;
------------------------------------------------------------------------- */
int compare_standalone(const void *iptr, const void *jptr)
int compare_standalone(const int i, const int j, void *ptr)
{
int i = *((int *) iptr);
int j = *((int *) jptr);
int *proc_recv = Irregular::proc_recv_copy;
int *proc_recv = (int *) ptr;
if (proc_recv[i] < proc_recv[j]) return -1;
if (proc_recv[i] > proc_recv[j]) return 1;
return 0;
@ -671,8 +668,7 @@ 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;
proc_recv_copy = proc_recv;
qsort(order,nrecv_proc,sizeof(int),compare_standalone);
merge_sort(order,nrecv_proc,(void *)proc_recv,compare_standalone);
int j;
for (i = 0; i < nrecv_proc; i++) {