File: selectq.c

package info (click to toggle)
parmetis 4.0.3-5
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye, buster, sid
  • size: 25,384 kB
  • ctags: 3,256
  • sloc: ansic: 41,872; makefile: 298; sh: 190; perl: 25
file content (359 lines) | stat: -rw-r--r-- 13,136 bytes parent folder | download | duplicates (3)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright 1997, Regents of the University of Minnesota
 *
 * selectq.c
 *
 * This file contains the driving routines for multilevel k-way refinement
 *
 * Started 7/28/97
 * George
 *
 * $Id: selectq.c 10542 2011-07-11 16:56:22Z karypis $
 */

#include <parmetislib.h>

/*************************************************************************/
/*! This stuff is hardcoded for up to four constraints 
*/
/*************************************************************************/
void Mc_DynamicSelectQueue(ctrl_t *ctrl, idx_t nqueues, idx_t ncon, idx_t subdomain1, 
         idx_t subdomain2, idx_t *currentq, real_t *flows, idx_t *from, idx_t *qnum, 
         idx_t minval, real_t avgvwgt, real_t maxdiff)
{
  idx_t i, j;
  idx_t hash, index = -1, current;
  idx_t *cand, *rank, *dont_cares;
  idx_t nperms, perm[24][5];
  real_t sign = 0.0;
  rkv_t *array;
  idx_t mype;
  gkMPI_Comm_rank(MPI_COMM_WORLD, &mype);

  WCOREPUSH;

  *qnum = -1;

  /* allocate memory */
  cand       = iwspacemalloc(ctrl, ncon);
  rank       = iwspacemalloc(ctrl, ncon);
  dont_cares = iwspacemalloc(ctrl, ncon);
  array      = rkvwspacemalloc(ctrl, ncon);

  if (*from == -1) {
    for (i=0; i<ncon; i++) {
      array[i].key = fabs(flows[i]);
      array[i].val = i;
    }

    /* GKTODO - Need to check the correct direction of the sort */
    rkvsorti(ncon, array);

    /* GKTODO - The following assert was disabled as it was failing. Need
                to check if it is a valid assert */
    /*ASSERT(array[ncon-1].key - array[0].key <= maxdiff) */

    if (flows[array[ncon-1].val] > avgvwgt*MOC_GD_GRANULARITY_FACTOR) {
      *from = subdomain1;
      sign  = 1.0;
      index = 0;
    }

    if (flows[array[ncon-1].val] < -1.0*avgvwgt*MOC_GD_GRANULARITY_FACTOR) {
      *from = subdomain2;
      sign  = -1.0;
      index = nqueues;
    }

    if (*from == -1) 
      goto DONE;
  }
  else {
    ASSERT(*from == subdomain1 || *from == subdomain2);

    if (*from == subdomain1) {
      sign  = 1.0;
      index = 0;
    }
    else {
      sign  = -1.0;
      index = nqueues;
    }
  }

  for (i=0; i<ncon; i++) {
    array[i].key = flows[i] * sign;
    array[i].val = i;
  }

  /* GKTODO Need to check the direction of those sorts */
  rkvsorti(ncon, array);

  iset(ncon, 1, dont_cares);
  for (current=0, i=0; i<ncon-1; i++) {
    if (array[i+1].key - array[i].key < maxdiff * MC_FLOW_BALANCE_THRESHOLD && dont_cares[current] < ncon-1) {
      dont_cares[current]++;
      dont_cares[i+1] = 0;
    }
    else
      current = i+1;
  }

  switch (ncon) {
    /***********************/
    case 2:
      nperms = 1;
      perm[0][0] = 0;   perm[0][1] = 1;

      break;
    /***********************/
    case 3:

      /* if the first and second flows are close */
      if (dont_cares[0] == 2 && dont_cares[1] == 0 && dont_cares[2] == 1) {
        nperms = 4;
        perm[0][0] = 0;   perm[0][1] = 1;   perm[0][2] = 2;
        perm[1][0] = 1;   perm[1][1] = 0;   perm[1][2] = 2;
        perm[2][0] = 0;   perm[2][1] = 2;   perm[2][2] = 1;
        perm[3][0] = 1;   perm[3][1] = 2;   perm[3][2] = 0;
        break;
      }

      /* if the second and third flows are close */
      if (dont_cares[0] == 1 && dont_cares[1] == 2 && dont_cares[2] == 0) {
        nperms = 4;
        perm[0][0] = 0;   perm[0][1] = 1;   perm[0][2] = 2;
        perm[1][0] = 0;   perm[1][1] = 2;   perm[1][2] = 1;
        perm[2][0] = 1;   perm[2][1] = 0;   perm[2][2] = 2;
        perm[3][0] = 2;   perm[3][1] = 0;   perm[3][2] = 1;
        break;
      }

      /* all or none of the flows are close */
      nperms = 3;
      perm[0][0] = 0;   perm[0][1] = 1;   perm[0][2] = 2;
      perm[1][0] = 1;   perm[1][1] = 0;   perm[1][2] = 2;
      perm[2][0] = 0;   perm[2][1] = 2;   perm[2][2] = 1;

      break;
    /***********************/
    case 4:

      if (dont_cares[0] == 2 && dont_cares[1] == 0 &&
          dont_cares[2] == 1 && dont_cares[3] == 1) {
        nperms = 14;
        perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
        perm[1][0] =  1;   perm[1][1] =  0;   perm[1][2] =  2;   perm[1][3] =  3;
        perm[2][0] =  0;   perm[2][1] =  2;   perm[2][2] =  1;   perm[2][3] =  3;
        perm[3][0] =  1;   perm[3][1] =  2;   perm[3][2] =  0;   perm[3][3] =  3;
        perm[4][0] =  0;   perm[4][1] =  1;   perm[4][2] =  3;   perm[4][3] =  2;
        perm[5][0] =  1;   perm[5][1] =  0;   perm[5][2] =  3;   perm[5][3] =  2;
        
        perm[6][0] =  0;   perm[6][1] =  3;   perm[6][2] =  1;   perm[6][3] =  2;
        perm[7][0] =  1;   perm[7][1] =  3;   perm[7][2] =  0;   perm[7][3] =  2;

        perm[8][0] =  0;   perm[8][1] =  2;   perm[8][2] =  3;   perm[8][3] =  1;
        perm[9][0] =  1;   perm[9][1] =  2;   perm[9][2] =  3;   perm[9][3] =  0;

        perm[10][0] = 2;   perm[10][1] = 0;   perm[10][2] = 1;   perm[10][3] = 3;
        perm[11][0] = 2;   perm[11][1] = 1;   perm[11][2] = 0;   perm[11][3] = 3;
        
        perm[12][0] = 0;   perm[12][1] = 3;   perm[12][2] = 2;   perm[12][3] = 1;
        perm[13][0] = 1;   perm[13][1] = 3;   perm[13][2] = 2;   perm[13][3] = 0;
        break;
      }

      if (dont_cares[0] == 1 && dont_cares[1] == 1 &&
          dont_cares[2] == 2 && dont_cares[3] == 0) {
        nperms = 14;
        perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
        perm[1][0] =  0;   perm[1][1] =  1;   perm[1][2] =  3;   perm[1][3] =  2;
        perm[2][0] =  0;   perm[2][1] =  2;   perm[2][2] =  1;   perm[2][3] =  3;
        perm[3][0] =  0;   perm[3][1] =  3;   perm[3][2] =  1;   perm[3][3] =  2;
        perm[4][0] =  1;   perm[4][1] =  0;   perm[4][2] =  2;   perm[4][3] =  3;
        perm[5][0] =  1;   perm[5][1] =  0;   perm[5][2] =  3;   perm[5][3] =  2;

        perm[6][0] =  1;   perm[6][1] =  2;   perm[6][2] =  0;   perm[6][3] =  3;
        perm[7][0] =  1;   perm[7][1] =  3;   perm[7][2] =  0;   perm[7][3] =  2;

        perm[8][0] =  2;   perm[8][1] =  0;   perm[8][2] =  1;   perm[8][3] =  3;
        perm[9][0] =  3;   perm[9][1] =  0;   perm[9][2] =  1;   perm[9][3] =  2;

        perm[10][0] = 0;   perm[10][1] = 2;   perm[10][2] = 3;   perm[10][3] = 1;
        perm[11][0] = 0;   perm[11][1] = 3;   perm[11][2] = 2;   perm[11][3] = 1;

        perm[12][0] = 2;   perm[12][1] = 1;   perm[12][2] = 0;   perm[12][3] = 3;
        perm[13][0] = 3;   perm[13][1] = 1;   perm[13][2] = 0;   perm[13][3] = 2;
        break;
      }

      if (dont_cares[0] == 2 && dont_cares[1] == 0 &&
          dont_cares[2] == 2 && dont_cares[3] == 0) {
        nperms = 14;
        perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
        perm[1][0] =  1;   perm[1][1] =  0;   perm[1][2] =  2;   perm[1][3] =  3;
        perm[2][0] =  0;   perm[2][1] =  1;   perm[2][2] =  3;   perm[2][3] =  2;
        perm[3][0] =  1;   perm[3][1] =  0;   perm[3][2] =  3;   perm[3][3] =  2;

        perm[4][0] =  0;   perm[4][1] =  2;   perm[4][2] =  1;   perm[4][3] =  3;
        perm[5][0] =  1;   perm[5][1] =  2;   perm[5][2] =  0;   perm[5][3] =  3;
        perm[6][0] =  0;   perm[6][1] =  3;   perm[6][2] =  1;   perm[6][3] =  2;
        perm[7][0] =  1;   perm[7][1] =  3;   perm[7][2] =  0;   perm[7][3] =  2;

        perm[8][0] = 2;    perm[8][1] = 0;    perm[8][2] = 1;    perm[8][3] = 3;
        perm[9][0] = 0;    perm[9][1] = 2;    perm[9][2] = 3;    perm[9][3] = 1;
        perm[10][0] = 2;   perm[10][1] = 1;   perm[10][2] = 0;   perm[10][3] = 3;
        perm[11][0] = 0;   perm[11][1] = 3;   perm[11][2] = 2;   perm[11][3] = 1;
        perm[12][0] = 3;   perm[12][1] = 0;   perm[12][2] = 1;   perm[12][3] = 2;
        perm[13][0] = 1;   perm[13][1] = 2;   perm[13][2] = 3;   perm[13][3] = 0;
        break;
      }

      if (dont_cares[0] == 3 && dont_cares[1] == 0 &&
          dont_cares[2] == 0 && dont_cares[3] == 1) {
        nperms = 14;
        perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
        perm[1][0] =  0;   perm[1][1] =  2;   perm[1][2] =  1;   perm[1][3] =  3;
        perm[2][0] =  1;   perm[2][1] =  0;   perm[2][2] =  2;   perm[2][3] =  3;
        perm[3][0] =  2;   perm[3][1] =  0;   perm[3][2] =  1;   perm[3][3] =  3;
        perm[4][0] =  1;   perm[4][1] =  2;   perm[4][2] =  0;   perm[4][3] =  3;
        perm[5][0] =  2;   perm[5][1] =  1;   perm[5][2] =  0;   perm[5][3] =  3;

        perm[6][0] =  0;   perm[6][1] =  1;   perm[6][2] =  3;   perm[6][3] =  2;
        perm[7][0] =  1;   perm[7][1] =  0;   perm[7][2] =  3;   perm[7][3] =  2;
        perm[8][0] =  0;   perm[8][1] =  2;   perm[8][2] =  3;   perm[8][3] =  1;
        perm[9][0] =  2;   perm[9][1] =  0;   perm[9][2] =  3;   perm[9][3] =  1;
        perm[10][0] = 1;   perm[10][1] = 2;   perm[10][2] = 3;   perm[10][3] = 0;
        perm[11][0] = 2;   perm[11][1] = 1;   perm[11][2] = 3;   perm[11][3] = 0;

        perm[12][0] = 0;   perm[12][1] = 3;   perm[12][2] = 1;   perm[12][3] = 2;
        perm[13][0] = 0;   perm[13][1] = 3;   perm[13][2] = 2;   perm[13][3] = 1;
        break;
      }

      if (dont_cares[0] == 1 && dont_cares[1] == 3 &&
          dont_cares[2] == 0 && dont_cares[3] == 0) {
        nperms = 14;
        perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
        perm[1][0] =  0;   perm[1][1] =  2;   perm[1][2] =  1;   perm[1][3] =  3;
        perm[2][0] =  0;   perm[2][1] =  1;   perm[2][2] =  3;   perm[2][3] =  2;
        perm[3][0] =  0;   perm[3][1] =  2;   perm[3][2] =  3;   perm[3][3] =  1;
        perm[4][0] =  0;   perm[4][1] =  3;   perm[4][2] =  1;   perm[4][3] =  2;
        perm[5][0] =  0;   perm[5][1] =  3;   perm[5][2] =  2;   perm[5][3] =  1;

        perm[6][0] =  1;   perm[6][1] =  0;   perm[6][2] =  2;   perm[6][3] =  3;
        perm[7][0] =  1;   perm[7][1] =  0;   perm[7][2] =  3;   perm[7][3] =  2;
        perm[8][0] =  2;   perm[8][1] =  0;   perm[8][2] =  1;   perm[8][3] =  3;
        perm[9][0] =  2;   perm[9][1] =  0;   perm[9][2] =  3;   perm[9][3] =  1;
        perm[10][0] = 3;   perm[10][1] = 0;   perm[10][2] = 1;   perm[10][3] = 2;
        perm[11][0] = 3;   perm[11][1] = 0;   perm[11][2] = 2;   perm[11][3] = 1;

        perm[12][0] = 1;   perm[12][1] = 2;   perm[12][2] = 0;   perm[12][3] = 3;
        perm[13][0] = 2;   perm[13][1] = 1;   perm[13][2] = 0;   perm[13][3] = 3;

        break;
      }

      nperms = 14;
      perm[0][0] =  0;   perm[0][1] =  1;   perm[0][2] =  2;   perm[0][3] =  3;
      perm[1][0] =  1;   perm[1][1] =  0;   perm[1][2] =  2;   perm[1][3] =  3;
      perm[2][0] =  0;   perm[2][1] =  2;   perm[2][2] =  1;   perm[2][3] =  3;
      perm[3][0] =  0;   perm[3][1] =  1;   perm[3][2] =  3;   perm[3][3] =  2;
      perm[4][0] =  1;   perm[4][1] =  0;   perm[4][2] =  3;   perm[4][3] =  2;

      perm[5][0] =  2;   perm[5][1] =  0;   perm[5][2] =  1;   perm[5][3] =  3;
      perm[6][0] =  0;   perm[6][1] =  2;   perm[6][2] =  3;   perm[6][3] =  1;

      perm[7][0] =  1;   perm[7][1] =  2;   perm[7][2] =  0;   perm[7][3] =  3;
      perm[8][0] =  0;   perm[8][1] =  3;   perm[8][2] =  1;   perm[8][3] =  2;

      perm[9][0] =  2;   perm[9][1] =  1;   perm[9][2] =  0;   perm[9][3] =  3;
      perm[10][0] = 0;   perm[10][1] = 3;   perm[10][2] = 2;   perm[10][3] = 1;
      perm[11][0] = 2;   perm[11][1] = 0;   perm[11][2] = 3;   perm[11][3] = 1;

      perm[12][0] = 3;   perm[12][1] = 0;   perm[12][2] = 1;   perm[12][3] = 2;
      perm[13][0] = 1;   perm[13][1] = 2;   perm[13][2] = 3;   perm[13][3] = 0;
      break;
    /***********************/
    default:
      goto DONE;
  }

  for (i=0; i<nperms; i++) {
    for (j=0; j<ncon; j++)
      cand[j] = array[perm[i][j]].val;

    for (j=0; j<ncon; j++)
      rank[cand[j]] = j;

    hash = Mc_HashVRank(ncon, rank) - minval;
    if (currentq[hash+index] > 0) {
      *qnum = hash;
      goto DONE;
    }
  }

DONE:
  WCOREPOP;
}


/*************************************************************************/
/*! This function sorts the nvwgts of a vertex and returns a hashed value 
*/
/*************************************************************************/
idx_t Mc_HashVwgts(ctrl_t *ctrl, idx_t ncon, real_t *nvwgt)
{
  idx_t i;
  idx_t multiplier, retval;
  idx_t *rank;
  rkv_t *array;

  WCOREPUSH;

  rank  = iwspacemalloc(ctrl, ncon);
  array = rkvwspacemalloc(ctrl, ncon);

  for (i=0; i<ncon; i++) {
    array[i].key = nvwgt[i];
    array[i].val = i;
  }

  rkvsorti(ncon, array);
  for (i=0; i<ncon; i++)
    rank[array[i].val] = i;

  multiplier = 1;

  retval = 0;
  for (i=0; i<ncon; i++) {
    multiplier *= (i+1);
    retval += rank[ncon-i-1] * multiplier;
  }

  WCOREPOP;

  return retval;
}


/*************************************************************************/
/*! This function sorts the vwgts of a vertex and returns a hashed value 
*/
/*************************************************************************/
idx_t Mc_HashVRank(idx_t ncon, idx_t *vwgt)
{
  idx_t i, multiplier, retval;

  multiplier = 1;

  retval = 0;
  for (i=0; i<ncon; i++) {
    multiplier *= (i+1);
    retval += vwgt[ncon-1-i] * multiplier;
  }

  return retval;
}