1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/*
* mutil.c
*
* This file contains various utility functions for the MOC portion of the
* code
*
* Started 2/15/98
* George
*
* $Id: mcutil.c 13901 2013-03-24 16:17:03Z karypis $
*
*/
#include "metislib.h"
/*************************************************************************/
/*! This function compares two vectors x & y and returns true
if \forall i, x[i] <= y[i].
*/
/**************************************************************************/
int rvecle(idx_t n, real_t *x, real_t *y)
{
for (n--; n>=0; n--) {
if (x[n] > y[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function compares two vectors x & y and returns true
if \forall i, x[i] >= y[i].
*/
/**************************************************************************/
int rvecge(idx_t n, real_t *x, real_t *y)
{
for (n--; n>=0; n--) {
if (x[n] < y[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function compares vectors x1+x2 against y and returns true
if \forall i, x1[i]+x2[i] <= y[i].
*/
/**************************************************************************/
int rvecsumle(idx_t n, real_t *x1, real_t *x2, real_t *y)
{
for (n--; n>=0; n--) {
if (x1[n]+x2[n] > y[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function returns max_i(x[i]-y[i]) */
/**************************************************************************/
real_t rvecmaxdiff(idx_t n, real_t *x, real_t *y)
{
real_t max;
max = x[0]-y[0];
for (n--; n>0; n--) {
if (max < x[n]-y[n])
max = x[n]-y[n];
}
return max;
}
/*************************************************************************/
/*! This function returns true if \forall i, x[i] <= z[i]. */
/**************************************************************************/
int ivecle(idx_t n, idx_t *x, idx_t *z)
{
for (n--; n>=0; n--) {
if (x[n] > z[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function returns true if \forall i, x[i] >= z[i]. */
/**************************************************************************/
int ivecge(idx_t n, idx_t *x, idx_t *z)
{
for (n--; n>=0; n--) {
if (x[n] < z[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function returns true if \forall i, a*x[i]+y[i] <= z[i]. */
/**************************************************************************/
int ivecaxpylez(idx_t n, idx_t a, idx_t *x, idx_t *y, idx_t *z)
{
for (n--; n>=0; n--) {
if (a*x[n]+y[n] > z[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function returns true if \forall i, a*x[i]+y[i] >= z[i]. */
/**************************************************************************/
int ivecaxpygez(idx_t n, idx_t a, idx_t *x, idx_t *y, idx_t *z)
{
for (n--; n>=0; n--) {
if (a*x[n]+y[n] < z[n])
return 0;
}
return 1;
}
/*************************************************************************/
/*! This function checks if v+u2 provides a better balance in the weight
vector that v+u1 */
/*************************************************************************/
int BetterVBalance(idx_t ncon, real_t *invtvwgt, idx_t *v_vwgt, idx_t *u1_vwgt,
idx_t *u2_vwgt)
{
idx_t i;
real_t sum1=0.0, sum2=0.0, diff1=0.0, diff2=0.0;
for (i=0; i<ncon; i++) {
sum1 += (v_vwgt[i]+u1_vwgt[i])*invtvwgt[i];
sum2 += (v_vwgt[i]+u2_vwgt[i])*invtvwgt[i];
}
sum1 = sum1/ncon;
sum2 = sum2/ncon;
for (i=0; i<ncon; i++) {
diff1 += rabs(sum1 - (v_vwgt[i]+u1_vwgt[i])*invtvwgt[i]);
diff2 += rabs(sum2 - (v_vwgt[i]+u2_vwgt[i])*invtvwgt[i]);
}
return (diff1 - diff2 >= 0);
}
/*************************************************************************/
/*! This function takes two ubfactor-centered load imbalance vectors x & y,
and returns true if y is better balanced than x. */
/*************************************************************************/
int BetterBalance2Way(idx_t n, real_t *x, real_t *y)
{
real_t nrm1=0.0, nrm2=0.0;
for (--n; n>=0; n--) {
if (x[n] > 0) nrm1 += x[n]*x[n];
if (y[n] > 0) nrm2 += y[n]*y[n];
}
return nrm2 < nrm1;
}
/*************************************************************************/
/*! Given a vertex and two weights, this function returns 1, if the second
partition will be more balanced than the first after the weighted
additional of that vertex.
The balance determination takes into account the ideal target weights
of the two partitions.
*/
/*************************************************************************/
int BetterBalanceKWay(idx_t ncon, idx_t *vwgt, real_t *ubvec,
idx_t a1, idx_t *pt1, real_t *bm1,
idx_t a2, idx_t *pt2, real_t *bm2)
{
idx_t i;
real_t tmp, nrm1=0.0, nrm2=0.0, max1=0.0, max2=0.0;
for (i=0; i<ncon; i++) {
tmp = bm1[i]*(pt1[i]+a1*vwgt[i]) - ubvec[i];
//printf("BB: %d %+.4f ", (int)i, (float)tmp);
nrm1 += tmp*tmp;
max1 = (tmp > max1 ? tmp : max1);
tmp = bm2[i]*(pt2[i]+a2*vwgt[i]) - ubvec[i];
//printf("%+.4f ", (float)tmp);
nrm2 += tmp*tmp;
max2 = (tmp > max2 ? tmp : max2);
//printf("%4d %4d %4d %4d %4d %4d %4d %.2f\n",
// (int)vwgt[i],
// (int)a1, (int)pt1[i], (int)tpt1[i],
// (int)a2, (int)pt2[i], (int)tpt2[i], ubvec[i]);
}
//printf(" %.3f %.3f %.3f %.3f\n", (float)max1, (float)nrm1, (float)max2, (float)nrm2);
if (max2 < max1)
return 1;
if (max2 == max1 && nrm2 < nrm1)
return 1;
return 0;
}
/*************************************************************************/
/*! Computes the maximum load imbalance of a partitioning solution over
all the constraints. */
/**************************************************************************/
real_t ComputeLoadImbalance(graph_t *graph, idx_t nparts, real_t *pijbm)
{
idx_t i, j, ncon, *pwgts;
real_t max, cur;
ncon = graph->ncon;
pwgts = graph->pwgts;
max = 1.0;
for (i=0; i<ncon; i++) {
for (j=0; j<nparts; j++) {
cur = pwgts[j*ncon+i]*pijbm[j*ncon+i];
if (cur > max)
max = cur;
}
}
return max;
}
/*************************************************************************/
/*! Computes the maximum load imbalance difference of a partitioning
solution over all the constraints.
The difference is defined with respect to the allowed maximum
unbalance for the respective constraint.
*/
/**************************************************************************/
real_t ComputeLoadImbalanceDiff(graph_t *graph, idx_t nparts, real_t *pijbm,
real_t *ubvec)
{
idx_t i, j, ncon, *pwgts;
real_t max, cur;
ncon = graph->ncon;
pwgts = graph->pwgts;
max = -1.0;
for (i=0; i<ncon; i++) {
for (j=0; j<nparts; j++) {
cur = pwgts[j*ncon+i]*pijbm[j*ncon+i] - ubvec[i];
if (cur > max)
max = cur;
}
}
return max;
}
/*************************************************************************/
/*! Computes the difference between load imbalance of each constraint across
the partitions minus the desired upper bound on the load imabalnce.
It also returns the maximum load imbalance across the partitions &
constraints. */
/**************************************************************************/
real_t ComputeLoadImbalanceDiffVec(graph_t *graph, idx_t nparts, real_t *pijbm,
real_t *ubfactors, real_t *diffvec)
{
idx_t i, j, ncon, *pwgts;
real_t cur, max;
ncon = graph->ncon;
pwgts = graph->pwgts;
for (max=-1.0, i=0; i<ncon; i++) {
diffvec[i] = pwgts[i]*pijbm[i] - ubfactors[i];
for (j=1; j<nparts; j++) {
cur = pwgts[j*ncon+i]*pijbm[j*ncon+i] - ubfactors[i];
if (cur > diffvec[i])
diffvec[i] = cur;
}
if (max < diffvec[i])
max = diffvec[i];
}
return max;
}
/*************************************************************************/
/*! Computes the load imbalance of each constraint across the partitions. */
/**************************************************************************/
void ComputeLoadImbalanceVec(graph_t *graph, idx_t nparts, real_t *pijbm,
real_t *lbvec)
{
idx_t i, j, ncon, *pwgts;
real_t cur;
ncon = graph->ncon;
pwgts = graph->pwgts;
for (i=0; i<ncon; i++) {
lbvec[i] = pwgts[i]*pijbm[i];
for (j=1; j<nparts; j++) {
cur = pwgts[j*ncon+i]*pijbm[j*ncon+i];
if (cur > lbvec[i])
lbvec[i] = cur;
}
}
}
|