Debian Codegraph @

extern/metis/libmetis/mcutil.c

raw
1 /*
2 * mutil.c
3 *
4 * This file contains various utility functions for the MOC portion of the
5 * code
6 *
7 * Started 2/15/98
8 * George
9 *
10 * $Id: mcutil.c 13901 2013-03-24 16:17:03Z karypis $
11 *
12 */
13
14 #include "metislib.h"
15
16
17 /*************************************************************************/
18 /*! This function returns true if \forall i, x[i] <= z[i]. */
19 /**************************************************************************/
20 int ivecle(idx_t n, idx_t *x, idx_t *z)
21 {
22 for (n--; n>=0; n--) {
23 if (x[n] > z[n])
24 return 0;
25 }
26
27 return 1;
28 }
29
30 /*************************************************************************/
31 /*! This function returns true if \forall i, a*x[i]+y[i] <= z[i]. */
32 /**************************************************************************/
33 int ivecaxpylez(idx_t n, idx_t a, idx_t *x, idx_t *y, idx_t *z)
34 {
35 for (n--; n>=0; n--) {
36 if (a*x[n]+y[n] > z[n])
37 return 0;
38 }
39
40 return 1;
41 }
42
43 /*************************************************************************/
44 /*! This function checks if v+u2 provides a better balance in the weight
45 vector that v+u1 */
46 /*************************************************************************/
47 int BetterVBalance(idx_t ncon, real_t *invtvwgt, idx_t *v_vwgt, idx_t *u1_vwgt,
48 idx_t *u2_vwgt)
49 {
50 idx_t i;
51 real_t sum1=0.0, sum2=0.0, diff1=0.0, diff2=0.0;
52
53 for (i=0; i<ncon; i++) {
54 sum1 += (v_vwgt[i]+u1_vwgt[i])*invtvwgt[i];
55 sum2 += (v_vwgt[i]+u2_vwgt[i])*invtvwgt[i];
56 }
57 sum1 = sum1/ncon;
58 sum2 = sum2/ncon;
59
60 for (i=0; i<ncon; i++) {
61 diff1 += rabs(sum1 - (v_vwgt[i]+u1_vwgt[i])*invtvwgt[i]);
62 diff2 += rabs(sum2 - (v_vwgt[i]+u2_vwgt[i])*invtvwgt[i]);
63 }
64
65 return (diff1 - diff2 >= 0);
66 }
67
68 /*************************************************************************/
69 /*! This function takes two ubfactor-centered load imbalance vectors x & y,
70 and returns true if y is better balanced than x. */
71 /*************************************************************************/
72 int BetterBalance2Way(idx_t n, real_t *x, real_t *y)
73 {
74 real_t nrm1=0.0, nrm2=0.0;
75
76 for (--n; n>=0; n--) {
77 if (x[n] > 0) nrm1 += x[n]*x[n];
78 if (y[n] > 0) nrm2 += y[n]*y[n];
79 }
80 return nrm2 < nrm1;
81 }
82
83 /*************************************************************************/
84 /*! Computes the maximum load imbalance of a partitioning solution over
85 all the constraints. */
86 /**************************************************************************/
87 real_t ComputeLoadImbalance(graph_t *graph, idx_t nparts, real_t *pijbm)
88 {
89 idx_t i, j, ncon, *pwgts;
90 real_t max, cur;
91
92 ncon = graph->ncon;
93 pwgts = graph->pwgts;
94
95 max = 1.0;
96 for (i=0; i<ncon; i++) {
97 for (j=0; j<nparts; j++) {
98 cur = pwgts[j*ncon+i]*pijbm[j*ncon+i];
99 if (cur > max)
100 max = cur;
101 }
102 }
103
104 return max;
105 }
106
107 /*************************************************************************/
108 /*! Computes the maximum load imbalance difference of a partitioning
109 solution over all the constraints.
110 The difference is defined with respect to the allowed maximum
111 unbalance for the respective constraint.
112 */
113 /**************************************************************************/
114 real_t ComputeLoadImbalanceDiff(graph_t *graph, idx_t nparts, real_t *pijbm,
115 real_t *ubvec)
116 {
117 idx_t i, j, ncon, *pwgts;
118 real_t max, cur;
119
120 ncon = graph->ncon;
121 pwgts = graph->pwgts;
122
123 max = -1.0;
124 for (i=0; i<ncon; i++) {
125 for (j=0; j<nparts; j++) {
126 cur = pwgts[j*ncon+i]*pijbm[j*ncon+i] - ubvec[i];
127 if (cur > max)
128 max = cur;
129 }
130 }
131
132 return max;
133 }
134
135 /*************************************************************************/
136 /*! Computes the difference between load imbalance of each constraint across
137 the partitions minus the desired upper bound on the load imabalnce.
138 It also returns the maximum load imbalance across the partitions &
139 constraints. */
140 /**************************************************************************/
141 real_t ComputeLoadImbalanceDiffVec(graph_t *graph, idx_t nparts, real_t *pijbm,
142 real_t *ubfactors, real_t *diffvec)
143 {
144 idx_t i, j, ncon, *pwgts;
145 real_t cur, max;
146
147 ncon = graph->ncon;
148 pwgts = graph->pwgts;
149
150 for (max=-1.0, i=0; i<ncon; i++) {
151 diffvec[i] = pwgts[i]*pijbm[i] - ubfactors[i];
152 for (j=1; j<nparts; j++) {
153 cur = pwgts[j*ncon+i]*pijbm[j*ncon+i] - ubfactors[i];
154 if (cur > diffvec[i])
155 diffvec[i] = cur;
156 }
157 if (max < diffvec[i])
158 max = diffvec[i];
159 }
160
161 return max;
162 }