Skip to content

Commit

Permalink
Add Histogram sort (#15)
Browse files Browse the repository at this point in the history
Add histogram sort
  • Loading branch information
thilinarmtb authored Jul 25, 2019
1 parent 7f9403f commit 8ed6e5a
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 211 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ CSRCS:= $(SRCDIR)/genmap.c \
$(SRCDIR)/genmap-vector.c $(SRCDIR)/genmap-handle.c $(SRCDIR)/genmap-comm.c \
$(SRCDIR)/genmap-eigen.c $(SRCDIR)/genmap-laplacian.c $(SRCDIR)/genmap-lanczos.c \
$(SRCDIR)/genmap-rsb.c \
$(SRCDIR)/parrsb-binsort.c \
$(SRCDIR)/parrsb-histogram.c \
$(SRCDIR)/genmap-chelpers.c \
$(SRCDIR)/parRSB.c
COBJS:=$(CSRCS:.c=.o)
Expand Down
2 changes: 1 addition & 1 deletion example/quality.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void printPartStat(long long *vtx, int nel, int nv, comm_ext ce) {
np = comm.np;
id = comm.id;

if (np == 1) return;
if(np == 1) return;

numPoints = nel * nv;
data = (long long*) malloc(numPoints * sizeof(long long));
Expand Down
16 changes: 16 additions & 0 deletions src/genmap-comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ int GenmapGop(GenmapComm c, void *v, GenmapInt size,
return 0;
}

int GenmapReduce(GenmapComm c, void *out, void *in, GenmapInt size,
GenmapDataType type, GenmapInt op) {
if(op == GENMAP_SUM) {
MPI_Reduce(in, out, size, type, MPI_SUM, 0, c->gsComm.c);
} else if(op == GENMAP_MAX) {
MPI_Reduce(in, out, size, type, MPI_MAX, 0, c->gsComm.c);
} else if(op == GENMAP_MIN) {
MPI_Reduce(in, out, size, type, MPI_MIN, 0, c->gsComm.c);
}
return 0;
}

int GenmapBcast(GenmapComm c, void *in, GenmapInt count, GenmapDataType type) {
return MPI_Bcast(in, count, type, 0, c->gsComm.c);
}

void GenmapSplitComm(GenmapHandle h, GenmapComm *c, int bin) {
GenmapCommExternal local;
int id = GenmapCommRank(*c);
Expand Down
23 changes: 22 additions & 1 deletion src/genmap-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ struct GenmapHandle_private {

int dbgLevel;
int printStat;

parRSBHistogram histogram;
};
//
// GenmapHandle
// GenmapHandle: Create, Destroy
//
int GenmapCreateHandle(GenmapHandle h);
int GenmapDestroyHandle(GenmapHandle h);
Expand All @@ -81,5 +83,24 @@ struct GenmapVector_private {
#define GenmapMalloc(n, p) GenmapMallocArray ((n), sizeof(**(p)), p)
#define GenmapCalloc(n, p) GenmapCallocArray ((n), sizeof(**(p)), p)
#define GenmapRealloc(n, p) GenmapReallocArray((n), sizeof(**(p)), p)
//
// Binsort
//
void GenmapFiedlerMinMax(GenmapHandle h, GenmapScalar *min, GenmapScalar *max);
void GenmapGlobalIdMinMax(GenmapHandle h, GenmapLong *min, GenmapLong *max);
GenmapInt GenmapSetFiedlerBin(GenmapHandle h);
GenmapInt GenmapSetGlobalIdBin(GenmapHandle h);
void GenmapAssignBins(GenmapHandle h, int field, buffer *buf0);
void GenmapTransferToBins(GenmapHandle h, int field, buffer *buf0);
void GenmapBinSort(GenmapHandle h, int field, buffer *buf0);
//
// HistoSort
//
void parRSBHistogramSort(GenmapHandle h,GenmapComm c,int field,buffer *buf0);

struct parRSBHistogram_private {
GenmapLong *count;
GenmapScalar *probes;
};

#endif
2 changes: 1 addition & 1 deletion src/genmap-lanczos.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ int GenmapLanczosLegendary(GenmapHandle h, GenmapComm c, GenmapVector f,
}
#if defined(GENMAP_DEBUG)
if(GenmapCommRank(GenmapGetGlobalComm(h)) == 0) {
printf("diag[%d]="GenmapScalarFormat"\n", iter+1, diag->data[iter]);
printf("diag[%d]="GenmapScalarFormat"\n", iter + 1, diag->data[iter]);
}
#endif

Expand Down
211 changes: 3 additions & 208 deletions src/genmap-rsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,6 @@
#include <time.h>
#include <limits.h>

void GenmapFiedlerMinMax(GenmapHandle h, GenmapScalar *min, GenmapScalar *max) {
*min = 1; *max = -1;

GenmapElements e = GenmapGetElements(h);
GenmapInt i;
for(i = 0; i < GenmapGetNLocalElements(h); i++) {
if(e[i].fiedler < *min) {
*min = e[i].fiedler;
}
if(e[i].fiedler > *max) {
*max = e[i].fiedler;
}
}

GenmapGop(GenmapGetLocalComm(h), min, 1, GENMAP_SCALAR, GENMAP_MIN);
GenmapGop(GenmapGetLocalComm(h), max, 1, GENMAP_SCALAR, GENMAP_MAX);
}

void GenmapGlobalIdMinMax(GenmapHandle h, GenmapLong *min, GenmapLong *max) {
*min = LONG_MAX; *max = LONG_MIN;

GenmapElements e = GenmapGetElements(h);
GenmapInt i;
for(i = 0; i < GenmapGetNLocalElements(h); i++) {
if(e[i].globalId0 < *min) {
*min = e[i].globalId0;
}
if(e[i].globalId0 > *max) {
*max = e[i].globalId0;
}
}

GenmapGop(GenmapGetLocalComm(h), min, 1, GENMAP_SCALAR, GENMAP_MIN);
GenmapGop(GenmapGetLocalComm(h), max, 1, GENMAP_SCALAR, GENMAP_MAX);
}

GenmapInt GenmapSetFiedlerBin(GenmapHandle h) {
GenmapScalar min, max;
GenmapFiedlerMinMax(h, &min, &max);
GenmapScalar range = max - min;

int np = GenmapCommSize(GenmapGetLocalComm(h));
GenmapInt nbins = np;
GenmapInt lelt = GenmapGetNLocalElements(h);
GenmapElements elements = GenmapGetElements(h);

GenmapElements p, e;
for(p = elements, e = p + lelt; p != e; p++) {
int id;
for(id = 0; id < np; id++) {
GenmapScalar start = min + (range * id) / nbins;
GenmapScalar end = min + (range * (id + 1)) / nbins;
if(start <= p->fiedler && p->fiedler < end) {
p->proc = id;
break;
}
}
if(id == np) p->proc = np - 1;
}

return 0;
}

GenmapInt GenmapSetGlobalIdBin(GenmapHandle h) {
GenmapLong min, max;
GenmapGlobalIdMinMax(h, &min, &max);
GenmapLong range = max - min;

GenmapInt np = GenmapCommSize(GenmapGetLocalComm(h));
GenmapInt nbins = np;
GenmapInt lelt = GenmapGetNLocalElements(h);
GenmapElements elements = GenmapGetElements(h);

GenmapElements p, e;
for(p = elements, e = p + lelt; p != e; p++) {
GenmapInt id;
for(id = 0; id < np; id++) {
GenmapLong start = min + (range * id) / nbins;
GenmapLong end = min + (range * (id + 1)) / nbins;
if(start <= p->globalId0 && p->globalId0 < end) {
p->proc = id;
break;
}
}
if(id == np) p->proc = np - 1;
}

return 0;
}

int GenmapFiedler(GenmapHandle h, GenmapComm c, int maxIter, int global) {
// 1. Do lanczos in local communicator.
GenmapInt lelt = GenmapGetNLocalElements(h);
Expand All @@ -111,7 +21,7 @@ int GenmapFiedler(GenmapHandle h, GenmapComm c, int maxIter, int global) {
// initVec->data[i] = GenmapGetLocalStartIndex(h) + i + 1 + 1000. *
// GenmapGetNGlobalElements(h);
//else
initVec->data[i] = GenmapGetLocalStartIndex(h) + i + 1;
initVec->data[i] = GenmapGetLocalStartIndex(h) + i + 1;
}
} else {
for(i = 0; i < lelt; i++) {
Expand Down Expand Up @@ -239,121 +149,6 @@ int GenmapFiedler(GenmapHandle h, GenmapComm c, int maxIter, int global) {
return iter;
}

void GenmapSplitByGlobalId(GenmapHandle h) {
GenmapLong start = GenmapGetLocalStartIndex(h);
GenmapLong nel = GenmapGetNGlobalElements(h);
GenmapInt id = GenmapCommRank(GenmapGetLocalComm(h));
GenmapInt np = GenmapCommSize(GenmapGetLocalComm(h));
GenmapInt lelt = GenmapGetNLocalElements(h);
GenmapElements elements = GenmapGetElements(h);

GenmapInt pNel = (GenmapInt)(nel / np);
GenmapInt nrem = (GenmapInt)(nel - pNel * np);
GenmapInt idCount = 0;
while(idCount * pNel + ((idCount < nrem) ? idCount : nrem) < start)
idCount++;

GenmapLong upLimit = idCount * pNel + ((idCount < nrem) ? idCount : nrem);
GenmapLong downLimit = start;
do {
GenmapInt end = upLimit - start < lelt ? (GenmapInt)(upLimit - start) : lelt;
GenmapInt i;
for(i = (GenmapInt)(downLimit - start); i < end; i++)
elements[i].proc = idCount - 1;
downLimit = upLimit;
idCount++;
upLimit = idCount * pNel + ((idCount < nrem) ? idCount : nrem);
} while(downLimit - start < lelt);
}

void GenmapSplitByMedian(GenmapHandle h) {
GenmapLong start = GenmapGetLocalStartIndex(h);
GenmapLong nel = GenmapGetNGlobalElements(h);
GenmapInt id = GenmapCommRank(GenmapGetLocalComm(h));
GenmapInt np = GenmapCommSize(GenmapGetLocalComm(h));
GenmapInt lelt = GenmapGetNLocalElements(h);
GenmapElements elements = GenmapGetElements(h);

GenmapInt pNel = (GenmapInt)(nel / np);
GenmapInt nrem = (GenmapInt)(nel - pNel * np);
GenmapInt idCount = 0;
while(idCount * pNel + ((idCount < nrem) ? idCount : nrem) < start)
idCount++;

GenmapLong upLimit = idCount * pNel + ((idCount < nrem) ? idCount : nrem);
GenmapLong downLimit = start;
do {
GenmapInt end = upLimit - start < lelt ? (GenmapInt)(upLimit - start) : lelt;
GenmapInt i;
for(i = (GenmapInt)(downLimit - start); i < end; i++)
elements[i].proc = idCount - 1;
downLimit = upLimit;
idCount++;
upLimit = idCount * pNel + ((idCount < nrem) ? idCount : nrem);
} while(downLimit - start < lelt);
}

void GenmapAssignBins(GenmapHandle h, int field, buffer *buf0) {
GenmapElements elements = GenmapGetElements(h);
GenmapInt lelt = GenmapGetNLocalElements(h);

if(field == GENMAP_FIEDLER) { // Fiedler
// sort locally according to Fiedler vector
sarray_sort(struct GenmapElement_private, elements, (GenmapUInt)lelt, fiedler,
TYPE_DOUBLE, buf0);
//sarray_sort_2(struct GenmapElement_private, elements, (GenmapUInt)lelt, fiedler,
// TYPE_DOUBLE, globalId, TYPE_LONG, buf0);
// set the bin based on Fiedler vector
GenmapSetFiedlerBin(h);
} else if(GENMAP_GLOBALID) {
// sort locally according to globalId
sarray_sort(struct GenmapElement_private, elements, (GenmapUInt)lelt,
globalId0, TYPE_LONG, buf0);
// set the bin based on globalId
GenmapSetGlobalIdBin(h);
}
}

void GenmapTransferToBins(GenmapHandle h, int field, buffer *buf0) {
GenmapElements elements = GenmapGetElements(h);
GenmapInt lelt = GenmapGetNLocalElements(h);

if(field == GENMAP_FIEDLER) { // Fiedler
sarray_transfer(struct GenmapElement_private, &(h->elementArray), proc, 0,
&(h->cr));
GenmapScan(h, GenmapGetLocalComm(h));
elements = GenmapGetElements(h);
lelt = GenmapGetNLocalElements(h);
sarray_sort(struct GenmapElement_private, elements, (GenmapUInt)lelt, fiedler,
TYPE_DOUBLE, buf0);
//sarray_sort_2(struct GenmapElement_private, elements, (GenmapUInt)lelt, fiedler,
// TYPE_DOUBLE, globalId0, TYPE_LONG, buf0);
} else if(field == GENMAP_GLOBALID) {
sarray_transfer(struct GenmapElement_private, &(h->elementArray), proc, 0,
&(h->cr));
GenmapScan(h, GenmapGetLocalComm(h));
elements = GenmapGetElements(h);
lelt = GenmapGetNLocalElements(h);
sarray_sort(struct GenmapElement_private, elements, (GenmapUInt)lelt,
globalId0, TYPE_LONG, buf0);
}
}

void GenmapBinSort(GenmapHandle h, int field, buffer *buf0) {
GenmapElements elements = GenmapGetElements(h);
GenmapInt lelt = GenmapGetNLocalElements(h);

GenmapAssignBins(h, field, buf0);
GenmapTransferToBins(h, field, buf0);
GenmapScan(h, GenmapGetLocalComm(h));
if(field == GENMAP_FIEDLER) {
GenmapSplitByMedian(h);
} else if(field == GENMAP_GLOBALID) {
GenmapSplitByGlobalId(h);
}
GenmapTransferToBins(h, field, buf0);
}

void GenmapRSB(GenmapHandle h) {
int maxIter = 50;
int npass = 50;
Expand Down Expand Up @@ -404,15 +199,15 @@ void GenmapRSB(GenmapHandle h) {
if(GenmapCommRank(GenmapGetGlobalComm(h)) == 0) {
for(int i = 0; i < GenmapGetNLocalElements(h); i++) {
printf("\nglobalId="GenmapLongFormat",Fiedler(%d):"GenmapScalarFormat,
e[i].globalId, i, e[i].fiedler);
e[i].globalId, i, e[i].fiedler);
fflush(stdout);
}
}
MPI_Barrier(GenmapGetGlobalComm(h)->gsComm.c);
if(GenmapCommRank(GenmapGetGlobalComm(h)) == 1) {
for(int i = 0; i < GenmapGetNLocalElements(h); i++) {
printf("\nglobalId="GenmapLongFormat",Fiedler(%d):"GenmapScalarFormat,
e[i].globalId, i, e[i].fiedler);
e[i].globalId, i, e[i].fiedler);
fflush(stdout);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/genmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ int GenmapInit(GenmapHandle *h, GenmapCommExternal ce) {
h_->dbgLevel = 0;
h_->printStat = 0;

GenmapMalloc(1,&h_->histogram);
return 0;
}
//
Expand All @@ -32,6 +33,8 @@ int GenmapFinalize(GenmapHandle h) {

array_free(&(h->elementArray));

GenmapFree(h->histogram);

GenmapFree(h);

return 0;
Expand Down
6 changes: 6 additions & 0 deletions src/genmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct GenmapComm_private *GenmapComm;
typedef struct GenmapHandle_private *GenmapHandle;
typedef struct GenmapVector_private *GenmapVector;
typedef struct GenmapElement_private *GenmapElements;
typedef struct parRSBHistogram_private *parRSBHistogram;
//
// Genmap: Init, Finalize
//
Expand Down Expand Up @@ -89,8 +90,13 @@ void GenmapScan(GenmapHandle h, GenmapComm c);
int GenmapCreateComm(GenmapComm *c, GenmapCommExternal ce);
int GenmapCommSize(GenmapComm c);
int GenmapCommRank(GenmapComm c);

int GenmapGop(GenmapComm c, void *v, GenmapInt size, GenmapDataType type,
GenmapInt op);
int GenmapReduce(GenmapComm c, void *out, void *in, GenmapInt size,
GenmapDataType type, GenmapInt op);
int GenmapBcast(GenmapComm c, void *in, GenmapInt count, GenmapDataType type);

int GenmapDestroyComm(GenmapComm c);
void GenmapSplitComm(GenmapHandle h, GenmapComm *c, int bin);
int GenmapCrystalInit(GenmapHandle h, GenmapComm c);
Expand Down
Loading

0 comments on commit 8ed6e5a

Please sign in to comment.