[2051] | 1 | /* |
---|
| 2 | * Copyright 1997, Regents of the University of Minnesota |
---|
| 3 | * |
---|
| 4 | * util.c |
---|
| 5 | * |
---|
| 6 | * This function contains various utility routines |
---|
| 7 | * |
---|
| 8 | * Started 9/28/95 |
---|
| 9 | * George |
---|
| 10 | * |
---|
| 11 | * $Id: util.c,v 1.1 1998/11/27 17:59:32 karypis Exp $ |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #include <metis.h> |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /************************************************************************* |
---|
| 18 | * This function prints an error message and exits |
---|
| 19 | **************************************************************************/ |
---|
| 20 | void errexit(char *f_str,...) |
---|
| 21 | { |
---|
| 22 | va_list argp; |
---|
| 23 | char out1[256], out2[256]; |
---|
| 24 | |
---|
| 25 | va_start(argp, f_str); |
---|
| 26 | vsprintf(out1, f_str, argp); |
---|
| 27 | va_end(argp); |
---|
| 28 | |
---|
| 29 | sprintf(out2, "Error! %s", out1); |
---|
| 30 | |
---|
| 31 | fprintf(stdout, out2); |
---|
| 32 | fflush(stdout); |
---|
| 33 | |
---|
| 34 | abort(); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | #ifndef DMALLOC |
---|
| 40 | /************************************************************************* |
---|
| 41 | * The following function allocates an array of integers |
---|
| 42 | **************************************************************************/ |
---|
| 43 | int *imalloc(int n, char *msg) |
---|
| 44 | { |
---|
| 45 | if (n == 0) |
---|
| 46 | return NULL; |
---|
| 47 | |
---|
| 48 | return (int *)GKmalloc(sizeof(int)*n, msg); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | /************************************************************************* |
---|
| 53 | * The following function allocates an array of integers |
---|
| 54 | **************************************************************************/ |
---|
| 55 | idxtype *idxmalloc(int n, char *msg) |
---|
| 56 | { |
---|
| 57 | if (n == 0) |
---|
| 58 | return NULL; |
---|
| 59 | |
---|
| 60 | return (idxtype *)GKmalloc(sizeof(idxtype)*n, msg); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | /************************************************************************* |
---|
| 65 | * The following function allocates an array of float |
---|
| 66 | **************************************************************************/ |
---|
| 67 | float *fmalloc(int n, char *msg) |
---|
| 68 | { |
---|
| 69 | if (n == 0) |
---|
| 70 | return NULL; |
---|
| 71 | |
---|
| 72 | return (float *)GKmalloc(sizeof(float)*n, msg); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | /************************************************************************* |
---|
| 77 | * The follwoing function allocates an array of integers |
---|
| 78 | **************************************************************************/ |
---|
| 79 | int *ismalloc(int n, int ival, char *msg) |
---|
| 80 | { |
---|
| 81 | if (n == 0) |
---|
| 82 | return NULL; |
---|
| 83 | |
---|
| 84 | return iset(n, ival, (int *)GKmalloc(sizeof(int)*n, msg)); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | /************************************************************************* |
---|
| 90 | * The follwoing function allocates an array of integers |
---|
| 91 | **************************************************************************/ |
---|
| 92 | idxtype *idxsmalloc(int n, idxtype ival, char *msg) |
---|
| 93 | { |
---|
| 94 | if (n == 0) |
---|
| 95 | return NULL; |
---|
| 96 | |
---|
| 97 | return idxset(n, ival, (idxtype *)GKmalloc(sizeof(idxtype)*n, msg)); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | /************************************************************************* |
---|
| 102 | * This function is my wrapper around malloc |
---|
| 103 | **************************************************************************/ |
---|
| 104 | void *GKmalloc(int nbytes, char *msg) |
---|
| 105 | { |
---|
| 106 | void *ptr; |
---|
| 107 | |
---|
| 108 | if (nbytes == 0) |
---|
| 109 | return NULL; |
---|
| 110 | |
---|
| 111 | ptr = (void *)malloc(nbytes); |
---|
| 112 | if (ptr == NULL) |
---|
| 113 | errexit("***Memory allocation failed for %s. Requested size: %d bytes", msg, nbytes); |
---|
| 114 | |
---|
| 115 | return ptr; |
---|
| 116 | } |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | /************************************************************************* |
---|
| 120 | * This function is my wrapper around free, allows multiple pointers |
---|
| 121 | **************************************************************************/ |
---|
| 122 | void GKfree(void **ptr1,...) |
---|
| 123 | { |
---|
| 124 | va_list plist; |
---|
| 125 | void **ptr; |
---|
| 126 | |
---|
| 127 | if (*ptr1 != NULL) |
---|
| 128 | free(*ptr1); |
---|
| 129 | *ptr1 = NULL; |
---|
| 130 | |
---|
| 131 | va_start(plist, ptr1); |
---|
| 132 | |
---|
| 133 | /* while ((int)(ptr = va_arg(plist, void **)) != -1) { */ |
---|
| 134 | while ((ptr = va_arg(plist, void **)) != LTERM) { |
---|
| 135 | if (*ptr != NULL) |
---|
| 136 | free(*ptr); |
---|
| 137 | *ptr = NULL; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | va_end(plist); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | /************************************************************************* |
---|
| 145 | * These functions set the values of a vector |
---|
| 146 | **************************************************************************/ |
---|
| 147 | int *iset(int n, int val, int *x) |
---|
| 148 | { |
---|
| 149 | int i; |
---|
| 150 | |
---|
| 151 | for (i=0; i<n; i++) |
---|
| 152 | x[i] = val; |
---|
| 153 | |
---|
| 154 | return x; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | /************************************************************************* |
---|
| 159 | * These functions set the values of a vector |
---|
| 160 | **************************************************************************/ |
---|
| 161 | idxtype *idxset(int n, idxtype val, idxtype *x) |
---|
| 162 | { |
---|
| 163 | int i; |
---|
| 164 | |
---|
| 165 | for (i=0; i<n; i++) |
---|
| 166 | x[i] = val; |
---|
| 167 | |
---|
| 168 | return x; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | |
---|
| 172 | /************************************************************************* |
---|
| 173 | * These functions set the values of a vector |
---|
| 174 | **************************************************************************/ |
---|
| 175 | float *sset(int n, float val, float *x) |
---|
| 176 | { |
---|
| 177 | int i; |
---|
| 178 | |
---|
| 179 | for (i=0; i<n; i++) |
---|
| 180 | x[i] = val; |
---|
| 181 | |
---|
| 182 | return x; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | /************************************************************************* |
---|
| 188 | * These functions return the index of the maximum element in a vector |
---|
| 189 | **************************************************************************/ |
---|
| 190 | int iamax(int n, int *x) |
---|
| 191 | { |
---|
| 192 | int i, max=0; |
---|
| 193 | |
---|
| 194 | for (i=1; i<n; i++) |
---|
| 195 | max = (x[i] > x[max] ? i : max); |
---|
| 196 | |
---|
| 197 | return max; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | /************************************************************************* |
---|
| 202 | * These functions return the index of the maximum element in a vector |
---|
| 203 | **************************************************************************/ |
---|
| 204 | int idxamax(int n, idxtype *x) |
---|
| 205 | { |
---|
| 206 | int i, max=0; |
---|
| 207 | |
---|
| 208 | for (i=1; i<n; i++) |
---|
| 209 | max = (x[i] > x[max] ? i : max); |
---|
| 210 | |
---|
| 211 | return max; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /************************************************************************* |
---|
| 215 | * These functions return the index of the maximum element in a vector |
---|
| 216 | **************************************************************************/ |
---|
| 217 | int idxamax_strd(int n, idxtype *x, int incx) |
---|
| 218 | { |
---|
| 219 | int i, max=0; |
---|
| 220 | |
---|
| 221 | n *= incx; |
---|
| 222 | for (i=incx; i<n; i+=incx) |
---|
| 223 | max = (x[i] > x[max] ? i : max); |
---|
| 224 | |
---|
| 225 | return max/incx; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | /************************************************************************* |
---|
| 231 | * These functions return the index of the maximum element in a vector |
---|
| 232 | **************************************************************************/ |
---|
| 233 | int samax(int n, float *x) |
---|
| 234 | { |
---|
| 235 | int i, max=0; |
---|
| 236 | |
---|
| 237 | for (i=1; i<n; i++) |
---|
| 238 | max = (x[i] > x[max] ? i : max); |
---|
| 239 | |
---|
| 240 | return max; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /************************************************************************* |
---|
| 244 | * These functions return the index of the almost maximum element in a vector |
---|
| 245 | **************************************************************************/ |
---|
| 246 | int samax2(int n, float *x) |
---|
| 247 | { |
---|
| 248 | int i, max1, max2; |
---|
| 249 | |
---|
| 250 | if (x[0] > x[1]) { |
---|
| 251 | max1 = 0; |
---|
| 252 | max2 = 1; |
---|
| 253 | } |
---|
| 254 | else { |
---|
| 255 | max1 = 1; |
---|
| 256 | max2 = 0; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | for (i=2; i<n; i++) { |
---|
| 260 | if (x[i] > x[max1]) { |
---|
| 261 | max2 = max1; |
---|
| 262 | max1 = i; |
---|
| 263 | } |
---|
| 264 | else if (x[i] > x[max2]) |
---|
| 265 | max2 = i; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | return max2; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | /************************************************************************* |
---|
| 273 | * These functions return the index of the minimum element in a vector |
---|
| 274 | **************************************************************************/ |
---|
| 275 | int idxamin(int n, idxtype *x) |
---|
| 276 | { |
---|
| 277 | int i, min=0; |
---|
| 278 | |
---|
| 279 | for (i=1; i<n; i++) |
---|
| 280 | min = (x[i] < x[min] ? i : min); |
---|
| 281 | |
---|
| 282 | return min; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | /************************************************************************* |
---|
| 287 | * These functions return the index of the minimum element in a vector |
---|
| 288 | **************************************************************************/ |
---|
| 289 | int samin(int n, float *x) |
---|
| 290 | { |
---|
| 291 | int i, min=0; |
---|
| 292 | |
---|
| 293 | for (i=1; i<n; i++) |
---|
| 294 | min = (x[i] < x[min] ? i : min); |
---|
| 295 | |
---|
| 296 | return min; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
| 300 | /************************************************************************* |
---|
| 301 | * This function sums the entries in an array |
---|
| 302 | **************************************************************************/ |
---|
| 303 | int idxsum(int n, idxtype *x) |
---|
| 304 | { |
---|
| 305 | int i, sum = 0; |
---|
| 306 | |
---|
| 307 | for (i=0; i<n; i++) |
---|
| 308 | sum += x[i]; |
---|
| 309 | |
---|
| 310 | return sum; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | /************************************************************************* |
---|
| 315 | * This function sums the entries in an array |
---|
| 316 | **************************************************************************/ |
---|
| 317 | int idxsum_strd(int n, idxtype *x, int incx) |
---|
| 318 | { |
---|
| 319 | int i, sum = 0; |
---|
| 320 | |
---|
| 321 | for (i=0; i<n; i++, x+=incx) { |
---|
| 322 | sum += *x; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | return sum; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | /************************************************************************* |
---|
| 330 | * This function sums the entries in an array |
---|
| 331 | **************************************************************************/ |
---|
| 332 | void idxadd(int n, idxtype *x, idxtype *y) |
---|
| 333 | { |
---|
| 334 | for (n--; n>=0; n--) |
---|
| 335 | y[n] += x[n]; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | /************************************************************************* |
---|
| 340 | * This function sums the entries in an array |
---|
| 341 | **************************************************************************/ |
---|
| 342 | int charsum(int n, char *x) |
---|
| 343 | { |
---|
| 344 | int i, sum = 0; |
---|
| 345 | |
---|
| 346 | for (i=0; i<n; i++) |
---|
| 347 | sum += x[i]; |
---|
| 348 | |
---|
| 349 | return sum; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /************************************************************************* |
---|
| 353 | * This function sums the entries in an array |
---|
| 354 | **************************************************************************/ |
---|
| 355 | int isum(int n, int *x) |
---|
| 356 | { |
---|
| 357 | int i, sum = 0; |
---|
| 358 | |
---|
| 359 | for (i=0; i<n; i++) |
---|
| 360 | sum += x[i]; |
---|
| 361 | |
---|
| 362 | return sum; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /************************************************************************* |
---|
| 366 | * This function sums the entries in an array |
---|
| 367 | **************************************************************************/ |
---|
| 368 | float ssum(int n, float *x) |
---|
| 369 | { |
---|
| 370 | int i; |
---|
| 371 | float sum = 0.0; |
---|
| 372 | |
---|
| 373 | for (i=0; i<n; i++) |
---|
| 374 | sum += x[i]; |
---|
| 375 | |
---|
| 376 | return sum; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /************************************************************************* |
---|
| 380 | * This function sums the entries in an array |
---|
| 381 | **************************************************************************/ |
---|
| 382 | float ssum_strd(int n, float *x, int incx) |
---|
| 383 | { |
---|
| 384 | int i; |
---|
| 385 | float sum = 0.0; |
---|
| 386 | |
---|
| 387 | for (i=0; i<n; i++, x+=incx) |
---|
| 388 | sum += *x; |
---|
| 389 | |
---|
| 390 | return sum; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | /************************************************************************* |
---|
| 394 | * This function sums the entries in an array |
---|
| 395 | **************************************************************************/ |
---|
| 396 | void sscale(int n, float alpha, float *x) |
---|
| 397 | { |
---|
| 398 | int i; |
---|
| 399 | |
---|
| 400 | for (i=0; i<n; i++) |
---|
| 401 | x[i] *= alpha; |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | |
---|
| 405 | /************************************************************************* |
---|
| 406 | * This function computes a 2-norm |
---|
| 407 | **************************************************************************/ |
---|
| 408 | float snorm2(int n, float *v) |
---|
| 409 | { |
---|
| 410 | int i; |
---|
| 411 | float partial = 0; |
---|
| 412 | |
---|
| 413 | for (i = 0; i<n; i++) |
---|
| 414 | partial += v[i] * v[i]; |
---|
| 415 | |
---|
| 416 | return sqrt(partial); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | |
---|
| 420 | |
---|
| 421 | /************************************************************************* |
---|
| 422 | * This function computes a 2-norm |
---|
| 423 | **************************************************************************/ |
---|
| 424 | float sdot(int n, float *x, float *y) |
---|
| 425 | { |
---|
| 426 | int i; |
---|
| 427 | float partial = 0; |
---|
| 428 | |
---|
| 429 | for (i = 0; i<n; i++) |
---|
| 430 | partial += x[i] * y[i]; |
---|
| 431 | |
---|
| 432 | return partial; |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | |
---|
| 436 | /************************************************************************* |
---|
| 437 | * This function computes a 2-norm |
---|
| 438 | **************************************************************************/ |
---|
| 439 | void saxpy(int n, float alpha, float *x, int incx, float *y, int incy) |
---|
| 440 | { |
---|
| 441 | int i; |
---|
| 442 | |
---|
| 443 | for (i=0; i<n; i++, x+=incx, y+=incy) |
---|
| 444 | *y += alpha*(*x); |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | |
---|
| 450 | /************************************************************************* |
---|
| 451 | * This file randomly permutes the contents of an array. |
---|
| 452 | * flag == 0, don't initialize perm |
---|
| 453 | * flag == 1, set p[i] = i |
---|
| 454 | **************************************************************************/ |
---|
| 455 | void RandomPermute(int n, idxtype *p, int flag) |
---|
| 456 | { |
---|
| 457 | int i, u, v; |
---|
| 458 | idxtype tmp; |
---|
| 459 | |
---|
| 460 | if (flag == 1) { |
---|
| 461 | for (i=0; i<n; i++) |
---|
| 462 | p[i] = i; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | if (n <= 4) |
---|
| 466 | return; |
---|
| 467 | |
---|
| 468 | for (i=0; i<n; i+=16) { |
---|
| 469 | u = RandomInRangeFast(n-4); |
---|
| 470 | v = RandomInRangeFast(n-4); |
---|
| 471 | SWAP(p[v], p[u], tmp); |
---|
| 472 | SWAP(p[v+1], p[u+1], tmp); |
---|
| 473 | SWAP(p[v+2], p[u+2], tmp); |
---|
| 474 | SWAP(p[v+3], p[u+3], tmp); |
---|
| 475 | } |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | |
---|
| 479 | |
---|
| 480 | /************************************************************************* |
---|
| 481 | * This function returns true if the a is a power of 2 |
---|
| 482 | **************************************************************************/ |
---|
| 483 | int ispow2(int a) |
---|
| 484 | { |
---|
| 485 | for (; a%2 != 1; a = a>>1); |
---|
| 486 | return (a > 1 ? 0 : 1); |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | |
---|
| 490 | /************************************************************************* |
---|
| 491 | * This function initializes the random number generator |
---|
| 492 | **************************************************************************/ |
---|
| 493 | void InitRandom(int seed) |
---|
| 494 | { |
---|
| 495 | if (seed == -1) { |
---|
| 496 | #ifndef __VC__ |
---|
| 497 | srand48(7654321L); |
---|
| 498 | #endif |
---|
| 499 | srand(4321); |
---|
| 500 | } |
---|
| 501 | else { |
---|
| 502 | #ifndef __VC__ |
---|
| 503 | srand48(seed); |
---|
| 504 | #endif |
---|
| 505 | srand(seed); |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | /************************************************************************* |
---|
| 510 | * This function returns the log2(x) |
---|
| 511 | **************************************************************************/ |
---|
| 512 | int log2(int a) |
---|
| 513 | { |
---|
| 514 | int i; |
---|
| 515 | |
---|
| 516 | for (i=1; a > 1; i++, a = a>>1); |
---|
| 517 | return i-1; |
---|
| 518 | } |
---|
| 519 | |
---|