00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <metis.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 #ifndef DMALLOC
00040
00041
00042
00043 int *imalloc(size_t n, char *msg)
00044 {
00045 if (n == 0)
00046 return NULL;
00047
00048 return (int *)GKmalloc(sizeof(int)*n, msg);
00049 }
00050
00051
00052
00053
00054
00055 idxtype *idxmalloc(size_t n, char *msg)
00056 {
00057 if (n == 0)
00058 return NULL;
00059
00060 return (idxtype *)GKmalloc(sizeof(idxtype)*n, msg);
00061 }
00062
00063
00064
00065
00066
00067 floattype *fmalloc(size_t n, char *msg)
00068 {
00069 if (n == 0)
00070 return NULL;
00071
00072 return (floattype *)GKmalloc(sizeof(floattype)*n, msg);
00073 }
00074
00075
00076
00077
00078
00079 int *ismalloc(size_t n, int ival, char *msg)
00080 {
00081 if (n == 0)
00082 return NULL;
00083
00084 return iset(n, ival, (int *)GKmalloc(sizeof(int)*n, msg));
00085 }
00086
00087
00088
00089
00090
00091
00092 idxtype *idxsmalloc(size_t n, idxtype ival, char *msg)
00093 {
00094 if (n == 0)
00095 return NULL;
00096
00097 return idxset(n, ival, (idxtype *)GKmalloc(sizeof(idxtype)*n, msg));
00098 }
00099
00100
00101
00102
00103
00104 void *GKmalloc(size_t nbytes, char *msg)
00105 {
00106 void *ptr;
00107
00108 if (nbytes == 0)
00109 return NULL;
00110
00111 ptr = (void *)malloc(nbytes);
00112 if (ptr == NULL)
00113 errexit("***Memory allocation failed for %s. Requested size: %d bytes", msg, nbytes);
00114
00115 return ptr;
00116 }
00117 #endif
00118
00119
00120
00121
00122 void GKfree(void **ptr1,...)
00123 {
00124 va_list plist;
00125 void **ptr;
00126
00127 if (*ptr1 != NULL)
00128 free(*ptr1);
00129 *ptr1 = NULL;
00130
00131 va_start(plist, ptr1);
00132
00133
00134 while ((ptr = va_arg(plist, void **)) != LTERM) {
00135 if (*ptr != NULL)
00136 free(*ptr);
00137 *ptr = NULL;
00138 }
00139
00140 va_end(plist);
00141 }
00142
00143
00144
00145
00146
00147 int *iset(int n, int val, int *x)
00148 {
00149 int i;
00150
00151 for (i=0; i<n; i++)
00152 x[i] = val;
00153
00154 return x;
00155 }
00156
00157
00158
00159
00160
00161 idxtype *idxset(int n, idxtype val, idxtype *x)
00162 {
00163 int i;
00164
00165 for (i=0; i<n; i++)
00166 x[i] = val;
00167
00168 return x;
00169 }
00170
00171
00172
00173
00174
00175 floattype *sset(int n, floattype val, floattype *x)
00176 {
00177 int i;
00178
00179 for (i=0; i<n; i++)
00180 x[i] = val;
00181
00182 return x;
00183 }
00184
00185
00186
00187
00188
00189
00190 int iamax(int n, int *x)
00191 {
00192 int i, max=0;
00193
00194 for (i=1; i<n; i++)
00195 max = (x[i] > x[max] ? i : max);
00196
00197 return max;
00198 }
00199
00200
00201
00202
00203
00204 int idxamax(int n, idxtype *x)
00205 {
00206 int i, max=0;
00207
00208 for (i=1; i<n; i++)
00209 max = (x[i] > x[max] ? i : max);
00210
00211 return max;
00212 }
00213
00214
00215
00216
00217 int idxamax_strd(int n, idxtype *x, int incx)
00218 {
00219 int i, max=0;
00220
00221 n *= incx;
00222 for (i=incx; i<n; i+=incx)
00223 max = (x[i] > x[max] ? i : max);
00224
00225 return max/incx;
00226 }
00227
00228
00229
00230
00231
00232
00233 int samax(int n, floattype *x)
00234 {
00235 int i, max=0;
00236
00237 for (i=1; i<n; i++)
00238 max = (x[i] > x[max] ? i : max);
00239
00240 return max;
00241 }
00242
00243
00244
00245
00246 int samax2(int n, floattype *x)
00247 {
00248 int i, max1, max2;
00249
00250 if (x[0] > x[1]) {
00251 max1 = 0;
00252 max2 = 1;
00253 }
00254 else {
00255 max1 = 1;
00256 max2 = 0;
00257 }
00258
00259 for (i=2; i<n; i++) {
00260 if (x[i] > x[max1]) {
00261 max2 = max1;
00262 max1 = i;
00263 }
00264 else if (x[i] > x[max2])
00265 max2 = i;
00266 }
00267
00268 return max2;
00269 }
00270
00271
00272
00273
00274
00275 int idxamin(int n, idxtype *x)
00276 {
00277 int i, min=0;
00278
00279 for (i=1; i<n; i++)
00280 min = (x[i] < x[min] ? i : min);
00281
00282 return min;
00283 }
00284
00285
00286
00287
00288
00289 int samin(int n, floattype *x)
00290 {
00291 int i, min=0;
00292
00293 for (i=1; i<n; i++)
00294 min = (x[i] < x[min] ? i : min);
00295
00296 return min;
00297 }
00298
00299
00300
00301
00302
00303 int idxsum(int n, idxtype *x)
00304 {
00305 int i, sum = 0;
00306
00307 for (i=0; i<n; i++)
00308 sum += x[i];
00309
00310 return sum;
00311 }
00312
00313
00314
00315
00316
00317 int idxsum_strd(int n, idxtype *x, int incx)
00318 {
00319 int i, sum = 0;
00320
00321 for (i=0; i<n; i++, x+=incx) {
00322 sum += *x;
00323 }
00324
00325 return sum;
00326 }
00327
00328
00329
00330
00331
00332 void idxadd(int n, idxtype *x, idxtype *y)
00333 {
00334 for (n--; n>=0; n--)
00335 y[n] += x[n];
00336 }
00337
00338
00339
00340
00341
00342 int charsum(int n, char *x)
00343 {
00344 int i, sum = 0;
00345
00346 for (i=0; i<n; i++)
00347 sum += x[i];
00348
00349 return sum;
00350 }
00351
00352
00353
00354
00355 int isum(int n, int *x)
00356 {
00357 int i, sum = 0;
00358
00359 for (i=0; i<n; i++)
00360 sum += x[i];
00361
00362 return sum;
00363 }
00364
00365
00366
00367
00368 floattype ssum(int n, floattype *x)
00369 {
00370 int i;
00371 floattype sum = 0.0;
00372
00373 for (i=0; i<n; i++)
00374 sum += x[i];
00375
00376 return sum;
00377 }
00378
00379
00380
00381
00382 floattype ssum_strd(int n, floattype *x, int incx)
00383 {
00384 int i;
00385 floattype sum = 0.0;
00386
00387 for (i=0; i<n; i++, x+=incx)
00388 sum += *x;
00389
00390 return sum;
00391 }
00392
00393
00394
00395
00396 void sscale(int n, floattype alpha, floattype *x)
00397 {
00398 int i;
00399
00400 for (i=0; i<n; i++)
00401 x[i] *= alpha;
00402 }
00403
00404
00405
00406
00407
00408 floattype snorm2(int n, floattype *v)
00409 {
00410 int i;
00411 floattype partial = 0;
00412
00413 for (i = 0; i<n; i++)
00414 partial += v[i] * v[i];
00415
00416 return sqrt(partial);
00417 }
00418
00419
00420
00421
00422
00423
00424 floattype sdot(int n, floattype *x, floattype *y)
00425 {
00426 int i;
00427 floattype partial = 0;
00428
00429 for (i = 0; i<n; i++)
00430 partial += x[i] * y[i];
00431
00432 return partial;
00433 }
00434
00435
00436
00437
00438
00439 void saxpy(int n, floattype alpha, floattype *x, int incx, floattype *y, int incy)
00440 {
00441 int i;
00442
00443 for (i=0; i<n; i++, x+=incx, y+=incy)
00444 *y += alpha*(*x);
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455 void RandomPermute(int n, idxtype *p, int flag)
00456 {
00457 int i, u, v;
00458 idxtype tmp;
00459
00460 if (flag == 1) {
00461 for (i=0; i<n; i++)
00462 p[i] = i;
00463 }
00464
00465 if (n <= 4)
00466 return;
00467
00468 for (i=0; i<n; i+=16) {
00469 u = RandomInRange(n-4);
00470 v = RandomInRange(n-4);
00471 SWAP(p[v], p[u], tmp);
00472 SWAP(p[v+1], p[u+1], tmp);
00473 SWAP(p[v+2], p[u+2], tmp);
00474 SWAP(p[v+3], p[u+3], tmp);
00475 }
00476 }
00477
00478
00479
00480
00481
00482
00483 int ispow2(int a)
00484 {
00485 for (; a%2 != 1; a = a>>1);
00486 return (a > 1 ? 0 : 1);
00487 }
00488
00489
00490
00491
00492
00493 void InitRandom(int seed)
00494 {
00495 if (seed == -1)
00496 srand(4321);
00497 else
00498 srand(seed);
00499 }
00500
00501
00502
00503
00504 int log2Int(int a)
00505 {
00506 int i;
00507
00508 for (i=1; a > 1; i++, a = a>>1);
00509 return i-1;
00510 }
00511