File: xyzpart.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 (678 lines) | stat: -rw-r--r-- 20,874 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
 * Copyright 1997, Regents of the University of Minnesota
 *
 * xyzpart.c
 *
 * This file contains code that implements a coordinate based partitioning
 *
 * Started 7/11/97
 * George
 *
 * $Id: xyzpart.c 10755 2011-09-15 12:28:34Z karypis $
 *
 */

#include <parmetislib.h>


/*************************************************************************/
/*! This function implements a simple coordinate based partitioning
*/
/*************************************************************************/
void Coordinate_Partition(ctrl_t *ctrl, graph_t *graph, idx_t ndims, 
         real_t *xyz, idx_t setup)
{
  idx_t i, j, k, nvtxs, firstvtx, icoord, nbits; 
  idx_t *vtxdist, *bxyz;
  ikv_t *cand;

  WCOREPUSH;

  if (setup)
    CommSetup(ctrl, graph);
  else
    graph->nrecv = 0;

  nvtxs    = graph->nvtxs;
  vtxdist  = graph->vtxdist;
  firstvtx = vtxdist[ctrl->mype];

  cand = ikvwspacemalloc(ctrl, nvtxs);
  bxyz = iwspacemalloc(ctrl, nvtxs*ndims);

  /* Assign the coordinates into bins */
  nbits = 9;  /* 2^nbits # of bins */
  IRBinCoordinates(ctrl, graph, ndims, xyz, 1<<nbits, bxyz);

  /* Z-ordering */
  for (i=0; i<nvtxs; i++) {
    for (icoord=0, j=nbits-1; j>=0; j--) {
      for (k=0; k<ndims; k++)
        icoord = (icoord<<1) + (bxyz[i*ndims+k]&(1<<j) ? 1 : 0);
    }
    cand[i].key = icoord;
    cand[i].val = firstvtx+i;
  }

  /* Partition using sorting */
  PseudoSampleSort(ctrl, graph, cand);

  WCOREPOP;
}


/*************************************************************************/
/*! This function maps the coordinates into bin numbers.
    It starts with a uniform distribution of the max-min range and then
    performs a number of iterations that adjust the bucket boundaries based
    on the actual bucket counts.
*/
/*************************************************************************/
void IRBinCoordinates(ctrl_t *ctrl, graph_t *graph, idx_t ndims, real_t *xyz, 
         idx_t nbins, idx_t *bxyz)
{
  idx_t npes=ctrl->npes, mype=ctrl->mype; 
  idx_t i, j, k, l, gnvtxs, nvtxs;
  idx_t csize, psize;
  idx_t *vtxdist, *lcounts, *gcounts;
  real_t gmin, gmax, *emarkers, *nemarkers;
  rkv_t *cand;

  WCOREPUSH;

  gnvtxs   = graph->gnvtxs;
  nvtxs    = graph->nvtxs;

  cand      = rkvwspacemalloc(ctrl, nvtxs);
  lcounts   = iwspacemalloc(ctrl, nbins);
  gcounts   = iwspacemalloc(ctrl, nbins);
  emarkers  = rwspacemalloc(ctrl, nbins+1);
  nemarkers = rwspacemalloc(ctrl, nbins+1);


  /* Go over each dimension */
  for (k=0; k<ndims; k++) {
    for (i=0; i<nvtxs; i++) {
      cand[i].key = xyz[i*ndims+k];
      cand[i].val = i;
    }
    rkvsorti(nvtxs, cand);

    /* determine initial range */
    gkMPI_Allreduce((void *)&cand[0].key, (void *)&gmin, 1, REAL_T, MPI_MIN, ctrl->comm);
    gkMPI_Allreduce((void *)&cand[nvtxs-1].key, (void *)&gmax, 1, REAL_T, MPI_MAX, ctrl->comm);

    for (i=0; i<nbins; i++)
      emarkers[i] = gmin + (gmax-gmin)*i/nbins;
    emarkers[nbins] = gmax*(1.0+2.0*REAL_EPSILON);

    /* get into a iterative backet boundary refinement */
    for (l=0; l<5; l++) {
      /* determine bucket counts */
      iset(nbins, 0, lcounts);
      for (j=0, i=0; i<nvtxs;) {
        if (cand[i].key < emarkers[j+1]) {
          lcounts[j]++;
          i++;
        }
        else {
          j++;
        }
      }

      gkMPI_Allreduce((void *)lcounts, (void *)gcounts, nbins, IDX_T, MPI_SUM, ctrl->comm);

      /*
      if (mype == 0) {
        printf("Distribution [%"PRIDX"]...\n", l);
        for (i=0; i<nbins; i++)
          printf("\t%"PRREAL" - %"PRREAL" => %"PRIDX"\n", emarkers[i], emarkers[i+1], gcounts[i]);
      }
      */

      /* break-out if things look reasonably balanced */
      if (imax(nbins, gcounts) < 4*gnvtxs/nbins)
        break;

      /* refine buckets */
      rset(nbins, -1, nemarkers);
      for (j=0, i=0; i<nbins; i++) {
        for (csize=0; ; j++) {
          if (csize+gcounts[j] < gnvtxs/nbins) {
            csize += gcounts[j];
          }
          else {
            psize = gnvtxs/nbins-csize;
            emarkers[j] += (emarkers[j+1]-emarkers[j])*psize/gcounts[j];
            gcounts[j]  -= psize;

            nemarkers[i+1] = emarkers[j];
            break;
          }
        }
      }
      nemarkers[0]     = gmin;
      nemarkers[nbins] = gmax*(1.0+2.0*REAL_EPSILON);
      rcopy(nbins+1, nemarkers, emarkers);
    }

    /* assign the coordinate to the appropriate bin */
    for (j=0, i=0; i<nvtxs;) {
      if (cand[i].key < emarkers[j+1]) {
        bxyz[cand[i].val*ndims+k] = j;
        i++;
      }
      else {
        j++;
      }
    }
  }

  WCOREPOP;
}


/*************************************************************************/
/*! This function maps the coordinates into bin numbers. It uses a per
    dimension recursive center-of mass bisection approach.
*/
/*************************************************************************/
void RBBinCoordinates(ctrl_t *ctrl, graph_t *graph, idx_t ndims, real_t *xyz, 
         idx_t nbins, idx_t *bxyz)
{
  idx_t npes=ctrl->npes, mype=ctrl->mype; 
  idx_t i, j, k, l, gnvtxs, nvtxs, cnbins;
  idx_t *vtxdist, *lcounts, *gcounts;
  real_t sum, gmin, gmax, gsum, *emarkers, *nemarkers, *lsums, *gsums;
  rkv_t *cand;
  ikv_t *buckets;

  WCOREPUSH;

  gnvtxs   = graph->gnvtxs;
  nvtxs    = graph->nvtxs;

  buckets   = ikvwspacemalloc(ctrl, nbins);
  cand      = rkvwspacemalloc(ctrl, nvtxs);
  lcounts   = iwspacemalloc(ctrl, nbins);
  gcounts   = iwspacemalloc(ctrl, nbins);
  lsums     = rwspacemalloc(ctrl, nbins);
  gsums     = rwspacemalloc(ctrl, nbins);
  emarkers  = rwspacemalloc(ctrl, nbins+1);
  nemarkers = rwspacemalloc(ctrl, nbins+1);


  /* Go over each dimension */
  for (k=0; k<ndims; k++) {
    for (sum=0.0, i=0; i<nvtxs; i++) {
      cand[i].key = xyz[i*ndims+k];
      cand[i].val = i;
      sum += cand[i].key;
    }
    rkvsorti(nvtxs, cand);

    /* determine initial stats */
    gkMPI_Allreduce((void *)&cand[0].key, (void *)&gmin, 1, REAL_T, MPI_MIN, ctrl->comm);
    gkMPI_Allreduce((void *)&cand[nvtxs-1].key, (void *)&gmax, 1, REAL_T, MPI_MAX, ctrl->comm);
    gkMPI_Allreduce((void *)&sum, (void *)&gsum, 1, REAL_T, MPI_MAX, ctrl->comm);

    emarkers[0] = gmin;
    emarkers[1] = gsum/gnvtxs;
    emarkers[2] = gmax*(1.0+2.0*REAL_EPSILON);
    cnbins = 2;

    /* get into a iterative backet boundary refinement */
    while (cnbins < nbins) {
      /* determine bucket counts */
      iset(cnbins, 0, lcounts);
      rset(cnbins, 0, lsums);
      for (j=0, i=0; i<nvtxs;) {
        if (cand[i].key < emarkers[j+1]) {
          lcounts[j]++;
          lsums[j] += cand[i].key;
          i++;
        }
        else {
          j++;
        }
      }

      gkMPI_Allreduce((void *)lcounts, (void *)gcounts, cnbins, IDX_T, MPI_SUM, ctrl->comm);
      gkMPI_Allreduce((void *)lsums, (void *)gsums, cnbins, REAL_T, MPI_SUM, ctrl->comm);

      /*
      if (mype == 0) {
        printf("Distribution [%"PRIDX"]...\n", cnbins);
        for (i=0; i<cnbins; i++)
          printf("\t%"PRREAL" - %"PRREAL" => %"PRIDX"\n", emarkers[i], emarkers[i+1], gcounts[i]);
      }
      */


      /* split over-weight buckets */
      for (i=0; i<cnbins; i++) {
        buckets[i].key = gcounts[i];
        buckets[i].val = i;
      }
      ikvsorti(cnbins, buckets);

      for (j=0, i=cnbins-1; i>=0; i--, j++) {
        l = buckets[i].val;
        if (buckets[i].key > gnvtxs/nbins && cnbins < nbins) {
          /*
          if (mype == 0)
            printf("\t\t %f %f\n", (float)emarkers[l], (float)emarkers[l+1]);
          */
          nemarkers[j++] = (emarkers[l]+emarkers[l+1])/2;
          cnbins++;
        }
        nemarkers[j] = emarkers[l];
      }
      PASSERT(ctrl, cnbins == j);
      
      rsorti(cnbins, nemarkers);
      rcopy(cnbins, nemarkers, emarkers);
      emarkers[cnbins] = gmax*(1.0+2.0*REAL_EPSILON);
    }

    /* assign the coordinate to the appropriate bin */
    for (j=0, i=0; i<nvtxs;) {
      if (cand[i].key < emarkers[j+1]) {
        bxyz[cand[i].val*ndims+k] = j;
        i++;
      }
      else {
        j++;
      }
    }
  }

  WCOREPOP;
}


/**************************************************************************/
/*! This function sorts a distributed list of ikv_t in increasing 
    order, and uses it to compute a partition. It uses samplesort. 

    This function is poorly implemented and makes the assumption that the
    number of vertices in each processor is greater than npes. 
    This constraint is currently enforced by the calling functions. 
    \todo fix it in 4.0.
*/
/**************************************************************************/
void SampleSort(ctrl_t *ctrl, graph_t *graph, ikv_t *elmnts)
{
  idx_t i, j, k, nvtxs, nrecv, npes=ctrl->npes, mype=ctrl->mype, 
        firstvtx, lastvtx;
  idx_t *scounts, *rcounts, *vtxdist, *perm;
  ikv_t *relmnts, *mypicks, *allpicks;

  WCOREPUSH;

  CommUpdateNnbrs(ctrl, npes);

  nvtxs   = graph->nvtxs;
  vtxdist = graph->vtxdist;

  /* get memory for the counts */
  scounts = iwspacemalloc(ctrl, npes+1);
  rcounts = iwspacemalloc(ctrl, npes+1);

  /* get memory for the splitters */
  mypicks  = ikvwspacemalloc(ctrl, npes+1);
  WCOREPUSH; /* for freeing allpicks */
  allpicks = ikvwspacemalloc(ctrl, npes*npes);

  /* Sort the local elements */
  ikvsorti(nvtxs, elmnts);

  /* Select the local npes-1 equally spaced elements */
  for (i=1; i<npes; i++) { 
    mypicks[i-1].key = elmnts[i*(nvtxs/npes)].key;
    mypicks[i-1].val = elmnts[i*(nvtxs/npes)].val;
  }

  /* PrintPairs(ctrl, npes-1, mypicks, "Mypicks"); */

  /* Gather the picks to all the processors */
  gkMPI_Allgather((void *)mypicks, 2*(npes-1), IDX_T, (void *)allpicks, 
      2*(npes-1), IDX_T, ctrl->comm);

  /* PrintPairs(ctrl, npes*(npes-1), allpicks, "Allpicks"); */

  /* Sort all the picks */
  ikvsortii(npes*(npes-1), allpicks);

  /* PrintPairs(ctrl, npes*(npes-1), allpicks, "Allpicks"); */

  /* Select the final splitters. Set the boundaries to simplify coding */
  for (i=1; i<npes; i++)
    mypicks[i] = allpicks[i*(npes-1)];
  mypicks[0].key    = IDX_MIN;
  mypicks[npes].key = IDX_MAX;

  /* PrintPairs(ctrl, npes+1, mypicks, "Mypicks"); */

  WCOREPOP;  /* free allpicks */

  /* Compute the number of elements that belong to each bucket */
  iset(npes, 0, scounts);
  for (j=i=0; i<nvtxs; i++) {
    if (elmnts[i].key < mypicks[j+1].key || 
        (elmnts[i].key == mypicks[j+1].key && elmnts[i].val < mypicks[j+1].val))
      scounts[j]++;
    else
      scounts[++j]++;
  }
  gkMPI_Alltoall(scounts, 1, IDX_T, rcounts, 1, IDX_T, ctrl->comm);

  MAKECSR(i, npes, scounts);
  MAKECSR(i, npes, rcounts);

/*
  PrintVector(ctrl, npes+1, 0, scounts, "Scounts");
  PrintVector(ctrl, npes+1, 0, rcounts, "Rcounts");
*/

  /* Allocate memory for sorted elements and receive them */
  nrecv   = rcounts[npes];
  relmnts = ikvwspacemalloc(ctrl, nrecv);

  /* Issue the receives first */
  for (i=0; i<npes; i++) 
    gkMPI_Irecv((void *)(relmnts+rcounts[i]), 2*(rcounts[i+1]-rcounts[i]), 
        IDX_T, i, 1, ctrl->comm, ctrl->rreq+i);

  /* Issue the sends next */
  for (i=0; i<npes; i++) 
    gkMPI_Isend((void *)(elmnts+scounts[i]), 2*(scounts[i+1]-scounts[i]), 
        IDX_T, i, 1, ctrl->comm, ctrl->sreq+i);

  gkMPI_Waitall(npes, ctrl->rreq, ctrl->statuses);
  gkMPI_Waitall(npes, ctrl->sreq, ctrl->statuses);


  /* OK, now do the local sort of the relmnts. Use perm to keep track original order */
  perm = iwspacemalloc(ctrl, nrecv);
  for (i=0; i<nrecv; i++) {
    perm[i]        = relmnts[i].val;
    relmnts[i].val = i;
  }
  ikvsorti(nrecv, relmnts);


  /* Compute what needs to be shifted */
  gkMPI_Scan((void *)(&nrecv), (void *)(&lastvtx), 1, IDX_T, MPI_SUM, ctrl->comm);
  firstvtx = lastvtx-nrecv;  

  /*myprintf(ctrl, "first, last: %"PRIDX" %"PRIDX"\n", firstvtx, lastvtx); */

  for (j=0, i=0; i<npes; i++) {
    if (vtxdist[i+1] > firstvtx) {  /* Found the first PE that is passed me */
      if (vtxdist[i+1] >= lastvtx) {
        /* myprintf(ctrl, "Shifting %"PRIDX" elements to processor %"PRIDX"\n", lastvtx-firstvtx, i); */
        for (k=0; k<lastvtx-firstvtx; k++, j++) 
          relmnts[relmnts[j].val].key = i;
      }
      else {
        /* myprintf(ctrl, "Shifting %"PRIDX" elements to processor %"PRIDX"\n", vtxdist[i+1]-firstvtx, i); */
        for (k=0; k<vtxdist[i+1]-firstvtx; k++, j++) 
          relmnts[relmnts[j].val].key = i;

        firstvtx = vtxdist[i+1];
      }
    }
    if (vtxdist[i+1] >= lastvtx)
      break;
  }

  /* Reverse the ordering on the relmnts[].val */
  for (i=0; i<nrecv; i++) {
    PASSERTP(ctrl, relmnts[i].key>=0 && relmnts[i].key<npes, 
            (ctrl, "%"PRIDX" %"PRIDX"\n", i, relmnts[i].key));
    relmnts[i].val = perm[i];
  }

  /* OK, now sent it back */
  /* Issue the receives first */
  for (i=0; i<npes; i++) 
    gkMPI_Irecv((void *)(elmnts+scounts[i]), 2*(scounts[i+1]-scounts[i]), IDX_T, 
        i, 1, ctrl->comm, ctrl->rreq+i);

  /* Issue the sends next */
  for (i=0; i<npes; i++) 
    gkMPI_Isend((void *)(relmnts+rcounts[i]), 2*(rcounts[i+1]-rcounts[i]), IDX_T, 
        i, 1, ctrl->comm, ctrl->sreq+i);

  gkMPI_Waitall(npes, ctrl->rreq, ctrl->statuses);
  gkMPI_Waitall(npes, ctrl->sreq, ctrl->statuses);


  /* Construct a partition for the graph */
  graph->where = imalloc(graph->nvtxs+graph->nrecv, "PartSort: graph->where");
  firstvtx = vtxdist[mype];
  for (i=0; i<nvtxs; i++) {
    PASSERTP(ctrl, elmnts[i].key>=0 && elmnts[i].key<npes, 
        (ctrl, "%"PRIDX" %"PRIDX"\n", i, elmnts[i].key));
    PASSERTP(ctrl, elmnts[i].val>=vtxdist[mype] && elmnts[i].val<vtxdist[mype+1], 
        (ctrl, "%"PRIDX" %"PRIDX" %"PRIDX" %"PRIDX"\n", i, vtxdist[mype], vtxdist[mype+1], elmnts[i].val));
    graph->where[elmnts[i].val-firstvtx] = elmnts[i].key;
  }

  WCOREPOP;
}


/**************************************************************************/
/*! This function sorts a distributed list of ikv_t in increasing 
    order, and uses it to compute a partition. It uses a 
    samplesort variant whose number of local samples can potentially
    be smaller than npes. 
*/
/**************************************************************************/
void PseudoSampleSort(ctrl_t *ctrl, graph_t *graph, ikv_t *elmnts)
{
  idx_t npes=ctrl->npes, mype=ctrl->mype; 
  idx_t i, j, k, nlsamples, ntsamples, nvtxs, nrecv, firstvtx, lastvtx;
  idx_t *scounts, *rcounts, *sdispls, *rdispls, *vtxdist, *perm;
  ikv_t *relmnts, *mypicks, *allpicks;

STARTTIMER(ctrl, ctrl->AuxTmr1);

  WCOREPUSH;

  nvtxs   = graph->nvtxs;
  vtxdist = graph->vtxdist;

  /* determine the number of local samples */
  //nlsamples = (GlobalSESum(ctrl, graph->nedges) + graph->gnvtxs)/(npes*npes);
  nlsamples = graph->gnvtxs/(npes*npes);
  if (nlsamples > npes)
    nlsamples = npes;
  else if (nlsamples < 75)
    nlsamples = gk_min(75, npes); /* the 'npes' in the min is to account for small graphs */


  IFSET(ctrl->dbglvl, DBG_INFO, 
      rprintf(ctrl, "PseudoSampleSort: nlsamples=%"PRIDX" of %"PRIDX"\n", nlsamples, npes));

  /* get memory for the counts and displacements */
  scounts = iwspacemalloc(ctrl, npes+1);
  rcounts = iwspacemalloc(ctrl, npes+1);
  sdispls = iwspacemalloc(ctrl, npes+1);
  rdispls = iwspacemalloc(ctrl, npes+1);

  /* get memory for the splitters */
  mypicks  = ikvwspacemalloc(ctrl, npes+1);

  WCOREPUSH; /* for freeing allpicks */
  allpicks = ikvwspacemalloc(ctrl, npes*nlsamples);

  /* Sort the local elements */
  ikvsorti(nvtxs, elmnts);

  /* Select the local nlsamples-1 equally spaced elements */
  for (i=0; i<nlsamples-1; i++) { 
    if (nvtxs > 0) {  
      k = (nvtxs/(3*nlsamples)            /* initial offset */
           + i*nvtxs/nlsamples            /* increament */
           + mype*nvtxs/(npes*nlsamples)  /* per-pe shift for nlsamples<npes */
          )%nvtxs;
      mypicks[i].key = elmnts[k].key;
      mypicks[i].val = elmnts[k].val;
    }
    else {
      /* Take care the case in which a processor has no elements, at which
         point we still select nlsamples-1, but we set their .val to -1 to be 
         removed later prior to sorting */
      mypicks[i].val = -1;
    }
  }

  /* PrintPairs(ctrl, nlsamples-1, mypicks, "Mypicks"); */

STOPTIMER(ctrl, ctrl->AuxTmr1);
STARTTIMER(ctrl, ctrl->AuxTmr2);

  /* Gather the picks to all the processors */
  gkMPI_Allgather((void *)mypicks, 2*(nlsamples-1), IDX_T, (void *)allpicks, 
      2*(nlsamples-1), IDX_T, ctrl->comm);

  /* PrintPairs(ctrl, npes*(nlsamples-1), allpicks, "Allpicks"); */

  /* Remove any samples that have .val == -1 */
  for (ntsamples=0, i=0; i<npes*(nlsamples-1); i++) {
    if (allpicks[i].val != -1) 
      allpicks[ntsamples++] = allpicks[i];
  }

  /* Sort all the picks */
  ikvsortii(ntsamples, allpicks);


  /* Select the final splitters. Set the boundaries to simplify coding */
  for (i=1; i<npes; i++)
    mypicks[i] = allpicks[i*ntsamples/npes];
  mypicks[0].key    = IDX_MIN;
  mypicks[npes].key = IDX_MAX;


  WCOREPOP;  /* free allpicks */

STOPTIMER(ctrl, ctrl->AuxTmr2);
STARTTIMER(ctrl, ctrl->AuxTmr3);

  /* Compute the number of elements that belong to each bucket */
  iset(npes, 0, scounts);
  for (j=i=0; i<nvtxs; i++) {
    if (elmnts[i].key < mypicks[j+1].key || 
        (elmnts[i].key == mypicks[j+1].key && elmnts[i].val < mypicks[j+1].val))
      scounts[j]++;
    else
      scounts[++j]++;
  }
  PASSERT(ctrl, j < npes);
  gkMPI_Alltoall(scounts, 1, IDX_T, rcounts, 1, IDX_T, ctrl->comm);

  /* multiply raw counts by 2 to account for the ikv_t type */
  sdispls[0] = rdispls[0] = 0;
  for (i=0; i<npes; i++) {
    scounts[i] *= 2;
    rcounts[i] *= 2;
    sdispls[i+1] = sdispls[i] + scounts[i];
    rdispls[i+1] = rdispls[i] + rcounts[i];
  }

STOPTIMER(ctrl, ctrl->AuxTmr3);
STARTTIMER(ctrl, ctrl->AuxTmr4);

  /*
  PrintVector(ctrl, npes+1, 0, scounts, "Scounts");
  PrintVector(ctrl, npes+1, 0, rcounts, "Rcounts");
  */

  /* Allocate memory for sorted elements and receive them */
  nrecv   = rdispls[npes]/2;  /* The divide by 2 is to get the # of ikv_t elements */
  relmnts = ikvwspacemalloc(ctrl, nrecv);

  IFSET(ctrl->dbglvl, DBG_INFO, 
      rprintf(ctrl, "PseudoSampleSort: max_nrecv: %"PRIDX" of %"PRIDX"\n", 
        GlobalSEMax(ctrl, nrecv), graph->gnvtxs/npes));
  if (mype == 0 || mype == npes-1)
    IFSET(ctrl->dbglvl, DBG_INFO, 
        myprintf(ctrl, "PseudoSampleSort: nrecv: %"PRIDX" of %"PRIDX"\n", 
          nrecv, graph->gnvtxs/npes));


  gkMPI_Alltoallv((void *)elmnts,  scounts, sdispls, IDX_T,
                  (void *)relmnts, rcounts, rdispls, IDX_T, 
                  ctrl->comm);

STOPTIMER(ctrl, ctrl->AuxTmr4);
STARTTIMER(ctrl, ctrl->AuxTmr5);

  /* OK, now do the local sort of the relmnts. Use perm to keep track original order */
  perm = iwspacemalloc(ctrl, nrecv);
  for (i=0; i<nrecv; i++) {
    perm[i]        = relmnts[i].val;
    relmnts[i].val = i;
  }
  ikvsorti(nrecv, relmnts);

  /* Compute what needs to be shifted */
  gkMPI_Scan((void *)(&nrecv), (void *)(&lastvtx), 1, IDX_T, MPI_SUM, ctrl->comm);
  firstvtx = lastvtx-nrecv;  

  for (j=0, i=0; i<npes; i++) {
    if (vtxdist[i+1] > firstvtx) {  /* Found the first PE that is passed me */
      if (vtxdist[i+1] >= lastvtx) {
        /* myprintf(ctrl, "Shifting %"PRIDX" elements to processor %"PRIDX"\n", lastvtx-firstvtx, i); */
        for (k=0; k<lastvtx-firstvtx; k++, j++) 
          relmnts[relmnts[j].val].key = i;
      }
      else {
        /* myprintf(ctrl, "Shifting %"PRIDX" elements to processor %"PRIDX"\n", vtxdist[i+1]-firstvtx, i); */
        for (k=0; k<vtxdist[i+1]-firstvtx; k++, j++) 
          relmnts[relmnts[j].val].key = i;

        firstvtx = vtxdist[i+1];
      }
    }
    if (vtxdist[i+1] >= lastvtx)
      break;
  }

  /* Reverse the ordering on the relmnts[].val */
  for (i=0; i<nrecv; i++) {
    PASSERTP(ctrl, relmnts[i].key>=0 && relmnts[i].key<npes, 
            (ctrl, "%"PRIDX" %"PRIDX"\n", i, relmnts[i].key));
    relmnts[i].val = perm[i];
  }

STOPTIMER(ctrl, ctrl->AuxTmr5);
STARTTIMER(ctrl, ctrl->AuxTmr6);

  /* OK, now sent it back. The role of send/recv arrays is now reversed. */
  gkMPI_Alltoallv((void *)relmnts, rcounts, rdispls, IDX_T,
                  (void *)elmnts,  scounts, sdispls, IDX_T, 
                  ctrl->comm);


  /* Construct a partition for the graph */
  graph->where = imalloc(graph->nvtxs+graph->nrecv, "PartSort: graph->where");
  firstvtx = vtxdist[mype];
  for (i=0; i<nvtxs; i++) {
    PASSERTP(ctrl, elmnts[i].key>=0 && elmnts[i].key<npes, 
        (ctrl, "%"PRIDX" %"PRIDX"\n", i, elmnts[i].key));
    PASSERTP(ctrl, elmnts[i].val>=vtxdist[mype] && elmnts[i].val<vtxdist[mype+1], 
        (ctrl, "%"PRIDX" %"PRIDX" %"PRIDX" %"PRIDX"\n", i, vtxdist[mype], vtxdist[mype+1], elmnts[i].val));
    graph->where[elmnts[i].val-firstvtx] = elmnts[i].key;
  }

  WCOREPOP;

STOPTIMER(ctrl, ctrl->AuxTmr6);
}