Skip to content

Commit

Permalink
Streamlined cnbrpool during k-way refinement.
Browse files Browse the repository at this point in the history
  • Loading branch information
karypis committed Jan 10, 2022
1 parent 3e1bb5a commit 2ecef80
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 78 deletions.
4 changes: 2 additions & 2 deletions libmetis/contig.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ void MoveGroupContigForCut(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t gid,

myrinfo = graph->ckrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->cnbrpool + myrinfo->inbr;
Expand Down Expand Up @@ -619,7 +619,7 @@ void MoveGroupContigForVol(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t gid,

myrinfo = graph->vkrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->vnbrpool + myrinfo->inbr;
Expand Down
3 changes: 3 additions & 0 deletions libmetis/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ idx_t CheckRInfo(ctrl_t *ctrl, ckrinfo_t *rinfo)
idx_t i, j;
cnbr_t *nbrs;

ASSERT(ctrl->nbrpoolcpos >= 0);
ASSERT(rinfo->nnbrs < ctrl->nparts);

nbrs = ctrl->cnbrpool + rinfo->inbr;

for (i=0; i<rinfo->nnbrs; i++) {
Expand Down
58 changes: 1 addition & 57 deletions libmetis/kmetis.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ idx_t MlevelKWayPartitioning(ctrl_t *ctrl, graph_t *graph, idx_t *part)

/* Re-allocate the work space */
AllocateWorkSpace(ctrl, graph);
AllocateRefinementWorkSpace(ctrl, 2*cgraph->nedges);
AllocateRefinementWorkSpace(ctrl, graph->nedges, 2*cgraph->nedges);

IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(ctrl->InitPartTmr));
IFSET(ctrl->dbglvl, METIS_DBG_IPART,
Expand Down Expand Up @@ -248,7 +248,6 @@ void InitKWayPartitioning(ctrl_t *ctrl, graph_t *graph)
}



/*************************************************************************/
/*! This function computes a k-way partitioning of a graph that minimizes
the specified objective function.
Expand Down Expand Up @@ -283,14 +282,12 @@ idx_t BlockKWayPartitioning(ctrl_t *ctrl, graph_t *graph, idx_t *part)
irandArrayPermute(nvtxs, part, 4*nvtxs, 0);
printf("Random cut: %d\n", (int)ComputeCut(graph, part));


/* create the initial multi-section */
mynparts = GrowMultisection(ctrl, graph, mynparts, part);

/* balance using label-propagation and refine using a randomized greedy strategy */
BalanceAndRefineLP(ctrl, graph, mynparts, part);


/* determine the size of the fine partitions */
fpwgts = iset(mynparts, 0, iwspacemalloc(ctrl, mynparts));
for (i=0; i<nvtxs; i++)
Expand Down Expand Up @@ -564,56 +561,3 @@ void BalanceAndRefineLP(ctrl_t *ctrl, graph_t *graph, idx_t nparts, idx_t *where

WCOREPOP;
}


/*************************************************************************/
/*! This uses Metis' routines for balancing and refining the multi-BFS
solution.
*/
/*************************************************************************/
void BalanceAndRefine(ctrl_t *origctrl, graph_t *graph, idx_t nparts, idx_t *where)
{
idx_t i;
idx_t options[METIS_NOPTIONS];
ctrl_t *ctrl;

FreeWorkSpace(origctrl);

METIS_SetDefaultOptions(options);
options[METIS_OPTION_NITER] = origctrl->niter;
options[METIS_OPTION_DBGLVL] = origctrl->dbglvl;
options[METIS_OPTION_UFACTOR] = origctrl->ufactor;
options[METIS_OPTION_OBJTYPE] = METIS_OBJTYPE_CUT;

ctrl = SetupCtrl(METIS_OP_KMETIS, options, 1, nparts, NULL, NULL);

AllocateWorkSpace(ctrl, graph);
AllocateRefinementWorkSpace(ctrl, 2*graph->nedges);

AllocateKWayPartitionMemory(ctrl, graph);
icopy(graph->nvtxs, where, graph->where);

ComputeKWayPartitionParams(ctrl, graph);

IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_startcputimer(origctrl->RefTmr));

SetupKWayBalMultipliers(ctrl, graph);

if (!IsBalanced(ctrl, graph, .02)) {
ComputeKWayBoundary(ctrl, graph, BNDTYPE_BALANCE);
Greedy_KWayOptimize(ctrl, graph, 1, 0, OMODE_BALANCE);
ComputeKWayBoundary(ctrl, graph, BNDTYPE_REFINE);
}

Greedy_KWayOptimize(ctrl, graph, ctrl->niter, 5.0, OMODE_REFINE);
icopy(graph->nvtxs, graph->where, where);

IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(origctrl->RefTmr));

FreeRData(graph);
FreeCtrl(&ctrl);

AllocateWorkSpace(origctrl, graph);
}


2 changes: 1 addition & 1 deletion libmetis/kwayfm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,7 @@ void KWayVolUpdate(ctrl_t *ctrl, graph_t *graph, idx_t v, idx_t from,

myrinfo = graph->vkrinfo+ii;
if (myrinfo->inbr == -1)
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[ii+1]-xadj[ii]+1);
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[ii+1]-xadj[ii]);
mynbrs = ctrl->vnbrpool + myrinfo->inbr;

if (me == from) {
Expand Down
22 changes: 13 additions & 9 deletions libmetis/kwayrefine.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void ComputeKWayPartitionParams(ctrl_t *ctrl, graph_t *graph)
if (myrinfo->ed > 0) {
mincut += myrinfo->ed;

myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
mynbrs = ctrl->cnbrpool + myrinfo->inbr;

for (j=xadj[i]; j<xadj[i+1]; j++) {
Expand Down Expand Up @@ -271,7 +271,7 @@ void ComputeKWayPartitionParams(ctrl_t *ctrl, graph_t *graph)
if (myrinfo->ned > 0) {
mincut += myrinfo->ned;

myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
mynbrs = ctrl->vnbrpool + myrinfo->inbr;

for (j=xadj[i]; j<xadj[i+1]; j++) {
Expand Down Expand Up @@ -331,6 +331,13 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)
cgraph = graph->coarser;
cwhere = cgraph->where;

if (ctrl->objtype == METIS_OBJTYPE_CUT) {
ASSERT(CheckBnd2(cgraph));
}
else {
ASSERT(cgraph->minvol == ComputeVolume(cgraph, cgraph->where));
}

/* free the coarse graph's structure (reduce maxmem) */
FreeSData(cgraph);

Expand All @@ -351,7 +358,6 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)
/* Compute the required info for refinement */
switch (ctrl->objtype) {
case METIS_OBJTYPE_CUT:
ASSERT(CheckBnd2(cgraph));
{
ckrinfo_t *myrinfo;
cnbr_t *mynbrs;
Expand Down Expand Up @@ -380,7 +386,7 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)
myrinfo->inbr = -1;
}
else { /* Potentially an interface node */
myrinfo->inbr = cnbrpoolGetNext(ctrl, iend-istart+1);
myrinfo->inbr = cnbrpoolGetNext(ctrl, iend-istart);
mynbrs = ctrl->cnbrpool + myrinfo->inbr;

me = where[i];
Expand All @@ -406,7 +412,7 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)

/* Remove space for edegrees if it was interior */
if (ted == 0) {
ctrl->nbrpoolcpos -= iend-istart+1;
ctrl->nbrpoolcpos -= gk_min(nparts, iend-istart);
myrinfo->inbr = -1;
}
else {
Expand All @@ -430,8 +436,6 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)
vkrinfo_t *myrinfo;
vnbr_t *mynbrs;

ASSERT(cgraph->minvol == ComputeVolume(cgraph, cgraph->where));

/* go through and project partition and compute id/ed for the nodes */
for (i=0; i<nvtxs; i++) {
k = cmap[i];
Expand All @@ -452,7 +456,7 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)
myrinfo->inbr = -1;
}
else { /* Potentially an interface node */
myrinfo->inbr = vnbrpoolGetNext(ctrl, iend-istart+1);
myrinfo->inbr = vnbrpoolGetNext(ctrl, iend-istart);
mynbrs = ctrl->vnbrpool + myrinfo->inbr;

me = where[i];
Expand All @@ -479,7 +483,7 @@ void ProjectKWayPartition(ctrl_t *ctrl, graph_t *graph)

/* Remove space for edegrees if it was interior */
if (ted == 0) {
ctrl->nbrpoolcpos -= iend-istart+1;
ctrl->nbrpoolcpos -= gk_min(nparts, iend-istart);
myrinfo->inbr = -1;
}
else {
Expand Down
2 changes: 1 addition & 1 deletion libmetis/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
cnbr_t *mynbrs; \
\
if (myrinfo->inbr == -1) { \
myrinfo->inbr = cnbrpoolGetNext(ctrl, adjlen+1); \
myrinfo->inbr = cnbrpoolGetNext(ctrl, adjlen); \
myrinfo->nnbrs = 0; \
} \
ASSERT(CheckRInfo(ctrl, myrinfo)); \
Expand Down
4 changes: 2 additions & 2 deletions libmetis/minconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ void MoveGroupMinConnForCut(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t nind,

myrinfo = graph->ckrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->cnbrpool + myrinfo->inbr;
Expand Down Expand Up @@ -578,7 +578,7 @@ void MoveGroupMinConnForVol(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t nind,

myrinfo = graph->vkrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->vnbrpool + myrinfo->inbr;
Expand Down
3 changes: 1 addition & 2 deletions libmetis/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ void InitKWayPartitioning(ctrl_t *ctrl, graph_t *graph);
idx_t BlockKWayPartitioning(ctrl_t *ctrl, graph_t *graph, idx_t *part);
idx_t GrowMultisection(ctrl_t *ctrl, graph_t *graph, idx_t nparts, idx_t *where);
void BalanceAndRefineLP(ctrl_t *ctrl, graph_t *graph, idx_t nparts, idx_t *where);
void BalanceAndRefine(ctrl_t *ctrl, graph_t *graph, idx_t nparts, idx_t *where);


/* kwayfm.c */
Expand Down Expand Up @@ -337,7 +336,7 @@ int metis_rcode(int sigrval);

/* wspace.c */
void AllocateWorkSpace(ctrl_t *ctrl, graph_t *graph);
void AllocateRefinementWorkSpace(ctrl_t *ctrl, idx_t nbrpoolsize);
void AllocateRefinementWorkSpace(ctrl_t *ctrl, idx_t nbrpoolsize_max, idx_t nbrpoolsize);
void FreeWorkSpace(ctrl_t *ctrl);
void *wspacemalloc(ctrl_t *ctrl, size_t nbytes);
void wspacepush(ctrl_t *ctrl);
Expand Down
1 change: 1 addition & 0 deletions libmetis/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ typedef struct ctrl_t {
mallocs/frees */

/* These are for use by the k-way refinement routines */
size_t nbrpoolsize_max; /*!< The maximum number of {c,v}nbr_t entries that will ever be allocated */
size_t nbrpoolsize; /*!< The number of {c,v}nbr_t entries that have been allocated */
size_t nbrpoolcpos; /*!< The position of the first free entry in the array */
size_t nbrpoolreallocs; /*!< The number of times the pool was resized */
Expand Down
12 changes: 9 additions & 3 deletions libmetis/wspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ void AllocateWorkSpace(ctrl_t *ctrl, graph_t *graph)
/*************************************************************************/
/*! This function allocates refinement-specific memory for the workspace */
/*************************************************************************/
void AllocateRefinementWorkSpace(ctrl_t *ctrl, idx_t nbrpoolsize)
void AllocateRefinementWorkSpace(ctrl_t *ctrl, idx_t nbrpoolsize_max, idx_t nbrpoolsize)
{
ctrl->nbrpoolsize_max = nbrpoolsize_max;
ctrl->nbrpoolsize = nbrpoolsize;
ctrl->nbrpoolcpos = 0;
ctrl->nbrpoolreallocs = 0;
Expand Down Expand Up @@ -89,8 +90,9 @@ void FreeWorkSpace(ctrl_t *ctrl)
ctrl->nbrpoolreallocs));

gk_free((void **)&ctrl->cnbrpool, &ctrl->vnbrpool, LTERM);
ctrl->nbrpoolsize = 0;
ctrl->nbrpoolcpos = 0;
ctrl->nbrpoolsize_max = 0;
ctrl->nbrpoolsize = 0;
ctrl->nbrpoolcpos = 0;

if (ctrl->minconn) {
iFreeMatrix(&(ctrl->adids), ctrl->nparts, INIT_MAXNAD);
Expand Down Expand Up @@ -171,10 +173,12 @@ void cnbrpoolReset(ctrl_t *ctrl)
/*************************************************************************/
idx_t cnbrpoolGetNext(ctrl_t *ctrl, idx_t nnbrs)
{
nnbrs = gk_min(ctrl->nparts, nnbrs);
ctrl->nbrpoolcpos += nnbrs;

if (ctrl->nbrpoolcpos > ctrl->nbrpoolsize) {
ctrl->nbrpoolsize += gk_max(10*nnbrs, ctrl->nbrpoolsize/2);
ctrl->nbrpoolsize = gk_min(ctrl->nbrpoolsize, ctrl->nbrpoolsize_max);

ctrl->cnbrpool = (cnbr_t *)gk_realloc(ctrl->cnbrpool,
ctrl->nbrpoolsize*sizeof(cnbr_t), "cnbrpoolGet: cnbrpool");
Expand All @@ -199,10 +203,12 @@ void vnbrpoolReset(ctrl_t *ctrl)
/*************************************************************************/
idx_t vnbrpoolGetNext(ctrl_t *ctrl, idx_t nnbrs)
{
nnbrs = gk_min(ctrl->nparts, nnbrs);
ctrl->nbrpoolcpos += nnbrs;

if (ctrl->nbrpoolcpos > ctrl->nbrpoolsize) {
ctrl->nbrpoolsize += gk_max(10*nnbrs, ctrl->nbrpoolsize/2);
ctrl->nbrpoolsize = gk_min(ctrl->nbrpoolsize, ctrl->nbrpoolsize_max);

ctrl->vnbrpool = (vnbr_t *)gk_realloc(ctrl->vnbrpool,
ctrl->nbrpoolsize*sizeof(vnbr_t), "vnbrpoolGet: vnbrpool");
Expand Down
2 changes: 1 addition & 1 deletion programs/gpmetis.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void GPPrintInfo(params_t *params, graph_t *graph)
(params->nooutput ? "YES" : "NO")
);

printf(" seed=%"PRIDX", niparts-%"PRIDX", niter=%"PRIDX", ncuts=%"PRIDX"\n",
printf(" seed=%"PRIDX", niparts=%"PRIDX", niter=%"PRIDX", ncuts=%"PRIDX"\n",
params->seed, params->niparts, params->niter, params->ncuts);

if (params->ubvec) {
Expand Down

0 comments on commit 2ecef80

Please sign in to comment.