00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <parmetislib.h>
00015
00016
00017
00018
00019
00020 void errexit(char *f_str,...)
00021 {
00022 va_list argp;
00023 char out1[256], out2[256];
00024
00025 va_start(argp, f_str);
00026 vsprintf(out1, f_str, argp);
00027 va_end(argp);
00028
00029 sprintf(out2, "Error! %s", out1);
00030
00031 fputs(out2, stdout);
00032 fflush(stdout);
00033
00034 abort();
00035 }
00036
00037
00038
00039
00040
00041 void myprintf(CtrlType *ctrl, char *f_str,...)
00042 {
00043 va_list argp;
00044 char out1[256], out2[256];
00045
00046 va_start(argp, f_str);
00047 vsprintf(out1, f_str, argp);
00048 va_end(argp);
00049
00050 sprintf(out2, "[%2d] %s", ctrl->mype, out1);
00051
00052 fputs(out2, stdout);
00053 fflush(stdout);
00054
00055 }
00056
00057
00058
00059
00060
00061
00062 void rprintf(CtrlType *ctrl, char *f_str,...)
00063 {
00064 va_list argp;
00065
00066 if (ctrl->mype == 0) {
00067 va_start(argp, f_str);
00068 vfprintf(stdout, f_str, argp);
00069 va_end(argp);
00070 }
00071
00072 fflush(stdout);
00073
00074 MPI_Barrier(ctrl->comm);
00075
00076 }
00077
00078
00079 #ifndef DMALLOC
00080
00081
00082
00083 int *imalloc(int n, char *msg)
00084 {
00085 if (n == 0)
00086 return NULL;
00087
00088 return (int *)GKmalloc(sizeof(int)*n, msg);
00089 }
00090
00091
00092
00093
00094
00095 idxtype *idxmalloc(int n, char *msg)
00096 {
00097 if (n == 0)
00098 return NULL;
00099
00100 return (idxtype *)GKmalloc(sizeof(idxtype)*n, msg);
00101 }
00102
00103
00104
00105
00106
00107 floattype *fmalloc(int n, char *msg)
00108 {
00109 if (n == 0)
00110 return NULL;
00111
00112 return (floattype *)GKmalloc(sizeof(floattype)*n, msg);
00113 }
00114
00115
00116
00117
00118
00119 int *ismalloc(int n, int ival, char *msg)
00120 {
00121 if (n == 0)
00122 return NULL;
00123
00124 return iset(n, ival, (int *)GKmalloc(sizeof(int)*n, msg));
00125 }
00126
00127
00128
00129
00130
00131
00132 idxtype *idxsmalloc(int n, idxtype ival, char *msg)
00133 {
00134 if (n == 0)
00135 return NULL;
00136
00137 return idxset(n, ival, (idxtype *)GKmalloc(sizeof(idxtype)*n, msg));
00138 }
00139
00140
00141
00142
00143
00144 void *GKmalloc(int nbytes, char *msg)
00145 {
00146 void *ptr;
00147
00148 if (nbytes == 0)
00149 return NULL;
00150
00151 ptr = (void *)malloc(nbytes);
00152 if (ptr == NULL)
00153 errexit("***Memory allocation failed for %s. Requested size: %d bytes", msg, nbytes);
00154
00155 return ptr;
00156 }
00157 #endif
00158
00159
00160
00161
00162 void GKfree(void **ptr1,...)
00163 {
00164 va_list plist;
00165 void **ptr;
00166
00167 if (*ptr1 != NULL)
00168 free(*ptr1);
00169 *ptr1 = NULL;
00170
00171 va_start(plist, ptr1);
00172
00173 while ((ptr = va_arg(plist, void **)) != LTERM) {
00174 if (*ptr != NULL)
00175 free(*ptr);
00176 *ptr = NULL;
00177 }
00178
00179 va_end(plist);
00180 }
00181
00182
00183
00184
00185
00186 int *iset(int n, int val, int *x)
00187 {
00188 int i;
00189
00190 for (i=0; i<n; i++)
00191 x[i] = val;
00192
00193 return x;
00194 }
00195
00196
00197
00198
00199
00200 idxtype *idxset(int n, idxtype val, idxtype *x)
00201 {
00202 int i;
00203
00204 for (i=0; i<n; i++)
00205 x[i] = val;
00206
00207 return x;
00208 }
00209
00210
00211
00212
00213
00214
00215 int idxamax(int n, idxtype *x)
00216 {
00217 int i, max=0;
00218
00219 for (i=1; i<n; i++)
00220 max = (x[i] > x[max] ? i : max);
00221
00222 return max;
00223 }
00224
00225
00226
00227
00228
00229 int idxamin(int n, idxtype *x)
00230 {
00231 int i, min=0;
00232
00233 for (i=1; i<n; i++)
00234 min = (x[i] < x[min] ? i : min);
00235
00236 return min;
00237 }
00238
00239
00240
00241
00242
00243 int idxsum(int n, idxtype *x)
00244 {
00245 int i, sum = 0;
00246
00247 for (i=0; i<n; i++)
00248 sum += x[i];
00249
00250 return sum;
00251 }
00252
00253
00254
00255
00256
00257 int charsum(int n, char *x)
00258 {
00259 int i, sum = 0;
00260
00261 for (i=0; i<n; i++)
00262 sum += x[i];
00263
00264 return sum;
00265 }
00266
00267
00268
00269
00270 int isum(int n, int *x)
00271 {
00272 int i, sum = 0;
00273
00274 for (i=0; i<n; i++)
00275 sum += x[i];
00276
00277 return sum;
00278 }
00279
00280
00281
00282
00283
00284 floattype snorm2(int n, floattype *v)
00285 {
00286 int i;
00287 floattype partial = 0;
00288
00289 for (i = 0; i<n; i++)
00290 partial += v[i] * v[i];
00291
00292 return sqrt(partial);
00293 }
00294
00295
00296
00297
00298
00299
00300 floattype sdot(int n, floattype *x, floattype *y)
00301 {
00302 int i;
00303 floattype partial = 0;
00304
00305 for (i = 0; i<n; i++)
00306 partial += x[i] * y[i];
00307
00308 return partial;
00309 }
00310
00311
00312
00313
00314
00315 void saxpy(int n, floattype alpha, floattype *x, floattype *y)
00316 {
00317 int i;
00318
00319 for (i=0; i<n; i++)
00320 y[i] += alpha*x[i];
00321 }
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 void ikeyvalsort_org(int n, KeyValueType *nodes)
00332 {
00333 qsort((void *)nodes, (size_t)n, (size_t)sizeof(KeyValueType), IncKeyValueCmp);
00334 }
00335
00336
00337
00338
00339
00340 int IncKeyValueCmp(const void *v1, const void *v2)
00341 {
00342 KeyValueType *n1, *n2;
00343
00344 n1 = (KeyValueType *)v1;
00345 n2 = (KeyValueType *)v2;
00346
00347 return (n1->key != n2->key ? n1->key - n2->key : n1->val - n2->val);
00348 }
00349
00350
00351
00352
00353
00354
00355 void dkeyvalsort(int n, KeyValueType *nodes)
00356 {
00357 qsort((void *)nodes, (size_t)n, (size_t)sizeof(KeyValueType), DecKeyValueCmp);
00358 }
00359
00360
00361
00362
00363
00364 int DecKeyValueCmp(const void *v1, const void *v2)
00365 {
00366 KeyValueType *n1, *n2;
00367
00368 n1 = (KeyValueType *)v1;
00369 n2 = (KeyValueType *)v2;
00370
00371 return n2->key - n1->key;
00372
00373 }
00374
00375
00376
00377
00378
00379
00380
00381 int BSearch(int n, idxtype *array, int key)
00382 {
00383 int a=0, b=n, c;
00384
00385 while (b-a > 8) {
00386 c = (a+b)>>1;
00387 if (array[c] > key)
00388 b = c;
00389 else
00390 a = c;
00391 }
00392
00393 for (c=a; c<b; c++) {
00394 if (array[c] == key)
00395 return c;
00396 }
00397
00398 errexit("Key %d not found!\n", key);
00399
00400 return 0;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410 void RandomPermute(int n, idxtype *p, int flag)
00411 {
00412 int i, u, v;
00413 idxtype tmp;
00414
00415 if (flag == 1) {
00416 for (i=0; i<n; i++)
00417 p[i] = i;
00418 }
00419
00420 for (i=0; i<n; i++) {
00421 v = RandomInRange(n);
00422 u = RandomInRange(n);
00423 SWAP(p[v], p[u], tmp);
00424 }
00425 }
00426
00427
00428
00429
00430
00431
00432
00433 void FastRandomPermute(int n, idxtype *p, int flag)
00434 {
00435 int i, u, v;
00436 idxtype tmp;
00437
00438
00439 if (n < 25) {
00440 RandomPermute(n, p, flag);
00441 return;
00442 }
00443
00444 if (flag == 1) {
00445 for (i=0; i<n; i++)
00446 p[i] = i;
00447 }
00448
00449 for (i=0; i<n; i+=8) {
00450 v = RandomInRange(n-4);
00451 u = RandomInRange(n-4);
00452 SWAP(p[v], p[u], tmp);
00453 SWAP(p[v+1], p[u+1], tmp);
00454 SWAP(p[v+2], p[u+2], tmp);
00455 SWAP(p[v+3], p[u+3], tmp);
00456 }
00457 }
00458
00459
00460
00461
00462 int ispow2(int a)
00463 {
00464 for (; a%2 != 1; a = a>>1);
00465 return (a > 1 ? 0 : 1);
00466 }
00467
00468
00469
00470
00471 int log2Int(int a)
00472 {
00473 int i;
00474
00475 for (i=1; a > 1; i++, a = a>>1);
00476 return i-1;
00477 }
00478
00479
00480
00481
00482
00483 floattype *sset(int n, floattype val, floattype *x)
00484 {
00485 int i;
00486
00487 for (i=0; i<n; i++)
00488 x[i] = val;
00489
00490 return x;
00491 }
00492
00493
00494
00495
00496
00497
00498 int iamax(int n, int *x)
00499 {
00500 int i, max=0;
00501
00502 for (i=1; i<n; i++)
00503 max = (x[i] > x[max] ? i : max);
00504
00505 return max;
00506 }
00507
00508
00509
00510
00511
00512 int samax_strd(int n, floattype *x, int incx)
00513 {
00514 int i;
00515 int max=0;
00516
00517 n *= incx;
00518 for (i=incx; i<n; i+=incx)
00519 max = (x[i] > x[max] ? i : max);
00520
00521 return max/incx;
00522 }
00523
00524
00525
00526
00527
00528 int sfamax(int n, floattype *x)
00529 {
00530 int i;
00531 int max=0;
00532
00533 for (i=1; i<n; i++)
00534 max = (fabs(x[i]) > fabs(x[max]) ? i : max);
00535
00536 return max;
00537 }
00538
00539
00540
00541
00542
00543
00544 int samin_strd(int n, floattype *x, int incx)
00545 {
00546 int i;
00547 int min=0;
00548
00549 n *= incx;
00550 for (i=incx; i<n; i+=incx)
00551 min = (x[i] < x[min] ? i : min);
00552
00553 return min/incx;
00554 }
00555
00556
00557
00558
00559
00560 int idxamax_strd(int n, idxtype *x, int incx)
00561 {
00562 int i, max=0;
00563
00564 n *= incx;
00565 for (i=incx; i<n; i+=incx)
00566 max = (x[i] > x[max] ? i : max);
00567
00568 return max/incx;
00569 }
00570
00571
00572
00573
00574
00575 int idxamin_strd(int n, idxtype *x, int incx)
00576 {
00577 int i, min=0;
00578
00579 n *= incx;
00580 for (i=incx; i<n; i+=incx)
00581 min = (x[i] < x[min] ? i : min);
00582
00583 return min/incx;
00584 }
00585
00586
00587
00588
00589
00590 floattype idxavg(int n, idxtype *x)
00591 {
00592 int i;
00593 floattype retval = 0.0;
00594
00595 for (i=0; i<n; i++)
00596 retval += (floattype)(x[i]);
00597
00598 return retval / (floattype)(n);
00599 }
00600
00601
00602
00603
00604
00605 floattype savg(int n, floattype *x)
00606 {
00607 int i;
00608 floattype retval = 0.0;
00609
00610 for (i=0; i<n; i++)
00611 retval += x[i];
00612
00613 return retval / (floattype)(n);
00614 }
00615
00616
00617
00618
00619
00620 int samax(int n, floattype *x)
00621 {
00622 int i, max=0;
00623
00624 for (i=1; i<n; i++)
00625 max = (x[i] > x[max] ? i : max);
00626
00627 return max;
00628 }
00629
00630
00631
00632
00633
00634 int sfavg(int n, floattype *x)
00635 {
00636 int i;
00637 floattype total = 0.0;
00638
00639 if (n == 0)
00640 return 0.0;
00641
00642 for (i=0; i<n; i++)
00643 total += fabs(x[i]);
00644
00645 return total / (floattype) n;
00646 }
00647
00648
00649
00650
00651
00652 int samax2(int n, floattype *x)
00653 {
00654 int i, max1, max2;
00655
00656 if (x[0] > x[1]) {
00657 max1 = 0;
00658 max2 = 1;
00659 }
00660 else {
00661 max1 = 1;
00662 max2 = 0;
00663 }
00664
00665 for (i=2; i<n; i++) {
00666 if (x[i] > x[max1]) {
00667 max2 = max1;
00668 max1 = i;
00669 }
00670 else if (x[i] > x[max2])
00671 max2 = i;
00672 }
00673
00674 return max2;
00675 }
00676
00677
00678
00679
00680
00681 int samin(int n, floattype *x)
00682 {
00683 int i, min=0;
00684
00685 for (i=1; i<n; i++)
00686 min = (x[i] < x[min] ? i : min);
00687
00688 return min;
00689 }
00690
00691
00692
00693
00694
00695 int idxsum_strd(int n, idxtype *x, int incx)
00696 {
00697 int i, sum = 0;
00698
00699 for (i=0; i<n; i++, x+=incx) {
00700 sum += *x;
00701 }
00702
00703 return sum;
00704 }
00705
00706
00707
00708
00709
00710 void idxadd(int n, idxtype *x, idxtype *y)
00711 {
00712 for (n--; n>=0; n--)
00713 y[n] += x[n];
00714 }
00715
00716
00717
00718
00719
00720 floattype ssum(int n, floattype *x)
00721 {
00722 int i;
00723 floattype sum = 0.0;
00724
00725 for (i=0; i<n; i++)
00726 sum += x[i];
00727
00728 return sum;
00729 }
00730
00731
00732
00733
00734 floattype ssum_strd(int n, floattype *x, int incx)
00735 {
00736 int i;
00737 floattype sum = 0.0;
00738
00739 for (i=0; i<n; i++, x+=incx)
00740 sum += *x;
00741
00742 return sum;
00743 }
00744
00745
00746
00747
00748 void sscale(int n, floattype alpha, floattype *x)
00749 {
00750 int i;
00751
00752 for (i=0; i<n; i++)
00753 x[i] *= alpha;
00754 }
00755
00756
00757
00758
00759
00760 void saneg(int n, floattype *x)
00761 {
00762 int i;
00763
00764 for (i=0; i<n; i++)
00765 x[i] = -1.0*x[i];
00766 }
00767
00768
00769
00770
00771
00772
00773
00774 floattype BetterVBalance(int ncon, floattype *vwgt, floattype *u1wgt, floattype *u2wgt)
00775 {
00776 int i;
00777 floattype sum1, sum2, diff1, diff2;
00778
00779 if (ncon == 1)
00780 return u1wgt[0] - u1wgt[0];
00781
00782 sum1 = sum2 = 0.0;
00783 for (i=0; i<ncon; i++) {
00784 sum1 += vwgt[i]+u1wgt[i];
00785 sum2 += vwgt[i]+u2wgt[i];
00786 }
00787 sum1 = sum1/(1.0*ncon);
00788 sum2 = sum2/(1.0*ncon);
00789
00790 diff1 = diff2 = 0.0;
00791 for (i=0; i<ncon; i++) {
00792 diff1 += fabs(sum1 - (vwgt[i]+u1wgt[i]));
00793 diff2 += fabs(sum2 - (vwgt[i]+u2wgt[i]));
00794 }
00795
00796 return diff1 - diff2;
00797
00798 }
00799
00800
00801
00802
00803
00804
00805
00806 int IsHBalanceBetterFT(int ncon, floattype *pfrom, floattype *pto, floattype *nvwgt, floattype *ubvec)
00807 {
00808 int i;
00809 floattype blb1=0.0, alb1=0.0, sblb=0.0, salb=0.0;
00810 floattype blb2=0.0, alb2=0.0;
00811 floattype temp;
00812
00813 for (i=0; i<ncon; i++) {
00814 temp = amax(pfrom[i], pto[i])/ubvec[i];
00815 if (blb1 < temp) {
00816 blb2 = blb1;
00817 blb1 = temp;
00818 }
00819 else if (blb2 < temp)
00820 blb2 = temp;
00821 sblb += temp;
00822
00823 temp = amax(pfrom[i]-nvwgt[i], pto[i]+nvwgt[i])/ubvec[i];
00824 if (alb1 < temp) {
00825 alb2 = alb1;
00826 alb1 = temp;
00827 }
00828 else if (alb2 < temp)
00829 alb2 = temp;
00830 salb += temp;
00831 }
00832
00833 if (alb1 < blb1)
00834 return 1;
00835 if (blb1 < alb1)
00836 return 0;
00837 if (alb2 < blb2)
00838 return 1;
00839 if (blb2 < alb2)
00840 return 0;
00841
00842 return salb < sblb;
00843
00844 }
00845
00846
00847
00848
00849
00850
00851 int IsHBalanceBetterTT(int ncon, floattype *pt1, floattype *pt2, floattype *nvwgt, floattype *ubvec)
00852 {
00853 int i;
00854 floattype m11=0.0, m12=0.0, m21=0.0, m22=0.0, sm1=0.0, sm2=0.0, temp;
00855
00856 for (i=0; i<ncon; i++) {
00857 temp = (pt1[i]+nvwgt[i])/ubvec[i];
00858 if (m11 < temp) {
00859 m12 = m11;
00860 m11 = temp;
00861 }
00862 else if (m12 < temp)
00863 m12 = temp;
00864 sm1 += temp;
00865 temp = (pt2[i]+nvwgt[i])/ubvec[i];
00866 if (m21 < temp) {
00867 m22 = m21;
00868 m21 = temp;
00869 }
00870 else if (m22 < temp)
00871 m22 = temp;
00872 sm2 += temp;
00873 }
00874 if (m21 < m11)
00875 return 1;
00876 if (m21 > m11)
00877 return 0;
00878 if (m22 < m12)
00879 return 1;
00880 if (m22 > m12)
00881 return 0;
00882
00883 return sm2 < sm1;
00884 }
00885
00886
00887
00888
00889 int myvalkeycompare(const void *fptr, const void *sptr)
00890 {
00891 KVType *first, *second;
00892
00893 first = (KVType *)(fptr);
00894 second = (KVType *)(sptr);
00895
00896 if (first->val > second->val)
00897 return 1;
00898
00899 if (first->val < second->val)
00900 return -1;
00901
00902 return 0;
00903 }
00904
00905
00906
00907
00908 int imyvalkeycompare(const void *fptr, const void *sptr)
00909 {
00910 KVType *first, *second;
00911
00912 first = (KVType *)(fptr);
00913 second = (KVType *)(sptr);
00914
00915 if (first->val > second->val)
00916 return -1;
00917
00918 if (first->val < second->val)
00919 return 1;
00920
00921 return 0;
00922 }
00923
00924
00925
00926
00927
00928 floattype *fsmalloc(int n, floattype fval, char *msg)
00929 {
00930 if (n == 0)
00931 return NULL;
00932
00933 return sset(n, fval, (floattype *)GKmalloc(sizeof(floattype)*n, msg));
00934 }
00935
00936
00937
00938
00939
00940 void saxpy2(int n, floattype alpha, floattype *x, int incx, floattype *y, int incy)
00941 {
00942 int i;
00943
00944 for (i=0; i<n; i++, x+=incx, y+=incy)
00945 *y += alpha*(*x);
00946 }
00947
00948
00949
00950
00951
00952 void GetThreeMax(int n, floattype *x, int *first, int *second, int *third)
00953 {
00954 int i;
00955
00956 if (n <= 0) {
00957 *first = *second = *third = -1;
00958 return;
00959 }
00960
00961 *second = *third = -1;
00962 *first = 0;
00963
00964 for (i=1; i<n; i++) {
00965 if (x[i] > x[*first]) {
00966 *third = *second;
00967 *second = *first;
00968 *first = i;
00969 continue;
00970 }
00971
00972 if (*second == -1 || x[i] > x[*second]) {
00973 *third = *second;
00974 *second = i;
00975 continue;
00976 }
00977
00978 if (*third == -1 || x[i] > x[*third])
00979 *third = i;
00980 }
00981
00982 return;
00983 }