| 1 |
/** |
| 2 |
\file |
| 3 |
\brief This file contains various routines for dealing with options and ctrl_t. |
| 4 |
|
| 5 |
\date Started 5/12/2011 |
| 6 |
\author George |
| 7 |
\author Copyright 1997-2011, Regents of the University of Minnesota |
| 8 |
\version\verbatim $Id: options.c 17717 2014-10-03 19:09:31Z dominique $ \endverbatim |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "metislib.h" |
| 12 |
|
| 13 |
|
| 14 |
/*************************************************************************/ |
| 15 |
/*! This function creates and sets the run parameters (ctrl_t) */ |
| 16 |
/*************************************************************************/ |
| 17 |
ctrl_t *SetupCtrl(moptype_et optype, idx_t *options, idx_t ncon, idx_t nparts, |
| 18 |
real_t *tpwgts, real_t *ubvec) |
| 19 |
{ |
| 20 |
idx_t i, j; |
| 21 |
ctrl_t *ctrl; |
| 22 |
|
| 23 |
ctrl = (ctrl_t *)malloc(sizeof(ctrl_t)); |
| 24 |
|
| 25 |
memset((void *)ctrl, 0, sizeof(ctrl_t)); |
| 26 |
|
| 27 |
switch (optype) { |
| 28 |
case METIS_OP_OMETIS: |
| 29 |
ctrl->rtype = GETOPTION(options, METIS_OPTION_RTYPE, METIS_RTYPE_SEP1SIDED); |
| 30 |
ctrl->iptype = GETOPTION(options, METIS_OPTION_IPTYPE, METIS_IPTYPE_EDGE); |
| 31 |
ctrl->nseps = GETOPTION(options, METIS_OPTION_NSEPS, 1); |
| 32 |
ctrl->niter = GETOPTION(options, METIS_OPTION_NITER, 10); |
| 33 |
ctrl->ufactor = GETOPTION(options, METIS_OPTION_UFACTOR, OMETIS_DEFAULT_UFACTOR); |
| 34 |
ctrl->compress = GETOPTION(options, METIS_OPTION_COMPRESS, 1); |
| 35 |
ctrl->ccorder = GETOPTION(options, METIS_OPTION_CCORDER, 0); |
| 36 |
ctrl->pfactor = 0.1*GETOPTION(options, METIS_OPTION_PFACTOR, 0); |
| 37 |
|
| 38 |
ctrl->CoarsenTo = 100; |
| 39 |
break; |
| 40 |
|
| 41 |
default: |
| 42 |
GK_ERREXIT("Unknown optype of %d\n", optype); |
| 43 |
} |
| 44 |
|
| 45 |
/* common options */ |
| 46 |
ctrl->ctype = GETOPTION(options, METIS_OPTION_CTYPE, METIS_CTYPE_SHEM); |
| 47 |
ctrl->no2hop = GETOPTION(options, METIS_OPTION_NO2HOP, 0); |
| 48 |
ctrl->seed = GETOPTION(options, METIS_OPTION_SEED, -1); |
| 49 |
ctrl->dbglvl = GETOPTION(options, METIS_OPTION_DBGLVL, 0); |
| 50 |
ctrl->dropedges = GETOPTION(options, METIS_OPTION_DROPEDGES, 0); |
| 51 |
|
| 52 |
/* set non-option information */ |
| 53 |
ctrl->optype = optype; |
| 54 |
ctrl->ncon = ncon; |
| 55 |
ctrl->nparts = nparts; |
| 56 |
ctrl->maxvwgt = ismalloc(ncon, 0); |
| 57 |
|
| 58 |
ctrl->rng_state = (ctrl->seed == -1 ? 4321 : ctrl->seed); |
| 59 |
|
| 60 |
|
| 61 |
/* setup the ubfactors */ |
| 62 |
ctrl->ubfactors = rsmalloc(ctrl->ncon, I2RUBFACTOR(ctrl->ufactor)); |
| 63 |
if (ubvec) |
| 64 |
rcopy(ctrl->ncon, ubvec, ctrl->ubfactors); |
| 65 |
for (i=0; i<ctrl->ncon; i++) |
| 66 |
ctrl->ubfactors[i] += 0.0000499; |
| 67 |
|
| 68 |
/* Allocate memory for balance multipliers. |
| 69 |
Note that for PMETIS/OMETIS routines the memory allocated is more |
| 70 |
than required as balance multipliers for 2 parts is sufficient. */ |
| 71 |
ctrl->pijbm = rmalloc(nparts*ncon); |
| 72 |
|
| 73 |
IFSET(ctrl->dbglvl, METIS_DBG_INFO, PrintCtrl(ctrl)); |
| 74 |
|
| 75 |
if (!CheckParams(ctrl)) { |
| 76 |
FreeCtrl(&ctrl); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
else { |
| 80 |
return ctrl; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/*************************************************************************/ |
| 85 |
/*! Computes the per-partition/constraint balance multipliers */ |
| 86 |
/*************************************************************************/ |
| 87 |
void Setup2WayBalMultipliers(ctrl_t *ctrl, graph_t *graph, real_t *tpwgts) |
| 88 |
{ |
| 89 |
idx_t i, j; |
| 90 |
|
| 91 |
for (i=0; i<2; i++) { |
| 92 |
for (j=0; j<graph->ncon; j++) |
| 93 |
ctrl->pijbm[i*graph->ncon+j] = graph->invtvwgt[j]/tpwgts[i*graph->ncon+j]; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/*************************************************************************/ |
| 99 |
/*! This function prints the various control fields */ |
| 100 |
/*************************************************************************/ |
| 101 |
void PrintCtrl(ctrl_t *ctrl) |
| 102 |
{ |
| 103 |
idx_t i, j, modnum; |
| 104 |
|
| 105 |
HIGHS_ORDERING_PRINT(" Runtime parameters:\n"); |
| 106 |
|
| 107 |
HIGHS_ORDERING_PRINT(" Coarsening type: "); |
| 108 |
switch (ctrl->ctype) { |
| 109 |
case METIS_CTYPE_RM: |
| 110 |
HIGHS_ORDERING_PRINT("METIS_CTYPE_RM\n"); |
| 111 |
break; |
| 112 |
case METIS_CTYPE_SHEM: |
| 113 |
HIGHS_ORDERING_PRINT("METIS_CTYPE_SHEM\n"); |
| 114 |
break; |
| 115 |
default: |
| 116 |
HIGHS_ORDERING_PRINT("Unknown!\n"); |
| 117 |
} |
| 118 |
|
| 119 |
HIGHS_ORDERING_PRINT(" Initial partitioning type: "); |
| 120 |
switch (ctrl->iptype) { |
| 121 |
case METIS_IPTYPE_EDGE: |
| 122 |
HIGHS_ORDERING_PRINT("METIS_IPTYPE_EDGE\n"); |
| 123 |
break; |
| 124 |
case METIS_IPTYPE_NODE: |
| 125 |
HIGHS_ORDERING_PRINT("METIS_IPTYPE_NODE\n"); |
| 126 |
break; |
| 127 |
default: |
| 128 |
HIGHS_ORDERING_PRINT("Unknown!\n"); |
| 129 |
} |
| 130 |
|
| 131 |
HIGHS_ORDERING_PRINT(" Refinement type: "); |
| 132 |
switch (ctrl->rtype) { |
| 133 |
case METIS_RTYPE_SEP2SIDED: |
| 134 |
HIGHS_ORDERING_PRINT("METIS_RTYPE_SEP2SIDED\n"); |
| 135 |
break; |
| 136 |
case METIS_RTYPE_SEP1SIDED: |
| 137 |
HIGHS_ORDERING_PRINT("METIS_RTYPE_SEP1SIDED\n"); |
| 138 |
break; |
| 139 |
default: |
| 140 |
HIGHS_ORDERING_PRINT("Unknown!\n"); |
| 141 |
} |
| 142 |
|
| 143 |
HIGHS_ORDERING_PRINT(" Perform a 2-hop matching: %s\n", (ctrl->no2hop ? "No" : "Yes")); |
| 144 |
|
| 145 |
HIGHS_ORDERING_PRINT(" Drop edges: %s\n", (ctrl->dropedges ? "Yes" : "No")); |
| 146 |
|
| 147 |
HIGHS_ORDERING_PRINT(" Number of balancing constraints: %"PRIDX"\n", ctrl->ncon); |
| 148 |
HIGHS_ORDERING_PRINT(" Number of refinement iterations: %"PRIDX"\n", ctrl->niter); |
| 149 |
HIGHS_ORDERING_PRINT(" Random number seed: %"PRIDX"\n", ctrl->seed); |
| 150 |
|
| 151 |
if (ctrl->optype == METIS_OP_OMETIS) { |
| 152 |
HIGHS_ORDERING_PRINT(" Number of separators: %"PRIDX"\n", ctrl->nseps); |
| 153 |
HIGHS_ORDERING_PRINT(" Compress graph prior to ordering: %s\n", (ctrl->compress ? "Yes" : "No")); |
| 154 |
HIGHS_ORDERING_PRINT(" Detect & order connected components separately: %s\n", (ctrl->ccorder ? "Yes" : "No")); |
| 155 |
HIGHS_ORDERING_PRINT(" Prunning factor for high degree vertices: %"PRREAL"\n", ctrl->pfactor); |
| 156 |
} |
| 157 |
|
| 158 |
HIGHS_ORDERING_PRINT(" Allowed maximum load imbalance: "); |
| 159 |
for (i=0; i<ctrl->ncon; i++) |
| 160 |
HIGHS_ORDERING_PRINT("%.3"PRREAL" ", ctrl->ubfactors[i]); |
| 161 |
HIGHS_ORDERING_PRINT("\n"); |
| 162 |
|
| 163 |
HIGHS_ORDERING_PRINT("\n"); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/*************************************************************************/ |
| 168 |
/*! This function checks the validity of user-supplied parameters */ |
| 169 |
/*************************************************************************/ |
| 170 |
int CheckParams(ctrl_t *ctrl) |
| 171 |
{ |
| 172 |
idx_t i, j; |
| 173 |
real_t sum; |
| 174 |
mdbglvl_et dbglvl=METIS_DBG_INFO; |
| 175 |
|
| 176 |
switch (ctrl->optype) { |
| 177 |
case METIS_OP_OMETIS: |
| 178 |
if (ctrl->ctype != METIS_CTYPE_RM && ctrl->ctype != METIS_CTYPE_SHEM) { |
| 179 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect coarsening scheme.\n")); |
| 180 |
return 0; |
| 181 |
} |
| 182 |
if (ctrl->iptype != METIS_IPTYPE_EDGE && ctrl->iptype != METIS_IPTYPE_NODE) { |
| 183 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect initial partitioning scheme.\n")); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
if (ctrl->rtype != METIS_RTYPE_SEP1SIDED && ctrl->rtype != METIS_RTYPE_SEP2SIDED) { |
| 187 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect refinement scheme.\n")); |
| 188 |
return 0; |
| 189 |
} |
| 190 |
if (ctrl->nseps <= 0) { |
| 191 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect nseps.\n")); |
| 192 |
return 0; |
| 193 |
} |
| 194 |
if (ctrl->niter <= 0) { |
| 195 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect niter.\n")); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
if (ctrl->ufactor <= 0) { |
| 199 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect ufactor.\n")); |
| 200 |
return 0; |
| 201 |
} |
| 202 |
if (ctrl->nparts != 3) { |
| 203 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect nparts.\n")); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
if (ctrl->ncon != 1) { |
| 207 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect ncon.\n")); |
| 208 |
return 0; |
| 209 |
} |
| 210 |
if (ctrl->compress != 0 && ctrl->compress != 1) { |
| 211 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect compress.\n")); |
| 212 |
return 0; |
| 213 |
} |
| 214 |
if (ctrl->ccorder != 0 && ctrl->ccorder != 1) { |
| 215 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect ccorder.\n")); |
| 216 |
return 0; |
| 217 |
} |
| 218 |
if (ctrl->pfactor < 0.0 ) { |
| 219 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect pfactor.\n")); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
for (i=0; i<ctrl->ncon; i++) { |
| 224 |
if (ctrl->ubfactors[i] <= 1.0) { |
| 225 |
IFSET(dbglvl, METIS_DBG_INFO, |
| 226 |
HIGHS_ORDERING_PRINT("Input Error: Incorrect ubfactor for constraint %"PRIDX".\n", i)); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
break; |
| 232 |
|
| 233 |
default: |
| 234 |
IFSET(dbglvl, METIS_DBG_INFO, HIGHS_ORDERING_PRINT("Input Error: Incorrect optype\n")); |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
return 1; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
/*************************************************************************/ |
| 243 |
/*! This function frees the memory associated with a ctrl_t */ |
| 244 |
/*************************************************************************/ |
| 245 |
void FreeCtrl(ctrl_t **r_ctrl) |
| 246 |
{ |
| 247 |
ctrl_t *ctrl = *r_ctrl; |
| 248 |
|
| 249 |
FreeWorkSpace(ctrl); |
| 250 |
|
| 251 |
gk_free((void **)&ctrl->tpwgts); |
| 252 |
gk_free((void**)&ctrl->pijbm); |
| 253 |
gk_free((void**)&ctrl->ubfactors); |
| 254 |
gk_free((void**)&ctrl->maxvwgt); |
| 255 |
gk_free((void**)&ctrl); |
| 256 |
|
| 257 |
*r_ctrl = NULL; |
| 258 |
} |
| 259 |
|
| 260 |
|