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

@ -30,9 +30,7 @@
using namespace LAMMPS_NS;
// allocate space for static class variable
Dump *Dump::dumpptr;
#include "mergesort.h"
#define BIG 1.0e20
#define EPSILON 1.0e-6
@ -690,11 +688,10 @@ void Dump::sort()
}
if (!reorderflag) {
dumpptr = this;
for (i = 0; i < nme; i++) index[i] = i;
if (sortcol == 0) qsort(index,nme,sizeof(int),idcompare);
else if (sortorder == ASCEND) qsort(index,nme,sizeof(int),bufcompare);
else qsort(index,nme,sizeof(int),bufcompare_reverse);
if (sortcol==0) merge_sort(index,nme,(void *)this,idcompare);
else if (sortorder==ASCEND) merge_sort(index,nme,(void *)this,bufcompare);
else merge_sort(index,nme,(void *)this,bufcompare_reverse);
}
// reset buf size and maxbuf to largest of any post-sort nme values
@ -718,62 +715,52 @@ void Dump::sort()
/* ----------------------------------------------------------------------
compare two atom IDs
called via qsort() in sort() method
is a static method so access data via dumpptr
called via merge_sort() in sort() method
------------------------------------------------------------------------- */
int Dump::idcompare(const void *pi, const void *pj)
{
tagint *idsort = dumpptr->idsort;
int i = *((int *) pi);
int j = *((int *) pj);
int Dump::idcompare(const int i, const int j, void *ptr) {
tagint *idsort = ((Dump *)ptr)->idsort;
if (idsort[i] < idsort[j]) return -1;
if (idsort[i] > idsort[j]) return 1;
return 0;
else if (idsort[i] > idsort[j]) return 1;
else return 0;
}
/* ----------------------------------------------------------------------
compare two buffer values with size_one stride
called via qsort() in sort() method
is a static method so access data via dumpptr
called via merge_sort() in sort() method
sort in ASCENDing order
------------------------------------------------------------------------- */
int Dump::bufcompare(const void *pi, const void *pj)
{
double *bufsort = dumpptr->bufsort;
int size_one = dumpptr->size_one;
int sortcolm1 = dumpptr->sortcolm1;
int Dump::bufcompare(const int i, const int j, void *ptr) {
Dump *dptr = (Dump *) ptr;
double *bufsort = dptr->bufsort;
const int size_one = dptr->size_one;
const int sortcolm1 = dptr->sortcolm1;
int i = *((int *) pi)*size_one + sortcolm1;
int j = *((int *) pj)*size_one + sortcolm1;
if (bufsort[i] < bufsort[j]) return -1;
if (bufsort[i] > bufsort[j]) return 1;
return 0;
const int ii=i*size_one + sortcolm1;
const int jj=j*size_one + sortcolm1;
if (bufsort[ii] < bufsort[jj]) return -1;
else if (bufsort[ii] > bufsort[jj]) return 1;
else return 0;
}
/* ----------------------------------------------------------------------
compare two buffer values with size_one stride
called via qsort() in sort() method
is a static method so access data via dumpptr
called via merge_sort() in sort() method
sort in DESCENDing order
------------------------------------------------------------------------- */
int Dump::bufcompare_reverse(const void *pi, const void *pj)
{
double *bufsort = dumpptr->bufsort;
int size_one = dumpptr->size_one;
int sortcolm1 = dumpptr->sortcolm1;
int Dump::bufcompare_reverse(const int i, const int j, void *ptr) {
Dump *dptr = (Dump *) ptr;
double *bufsort = dptr->bufsort;
const int size_one = dptr->size_one;
const int sortcolm1 = dptr->sortcolm1;
int i = *((int *) pi)*size_one + sortcolm1;
int j = *((int *) pj)*size_one + sortcolm1;
if (bufsort[i] > bufsort[j]) return -1;
if (bufsort[i] < bufsort[j]) return 1;
return 0;
const int ii=i*size_one + sortcolm1;
const int jj=j*size_one + sortcolm1;
if (bufsort[ii] < bufsort[jj]) return 1;
else if (bufsort[ii] > bufsort[jj]) return -1;
else return 0;
}
/* ----------------------------------------------------------------------