/* * Copyright 1997, Regents of the University of Minnesota * * util.c * * This function contains various utility routines * * Started 9/28/95 * George * * $Id: util.c 10057 2011-06-02 13:44:44Z karypis $ */ #include /************************************************************************* * This function prints an error message and exits **************************************************************************/ void myprintf(ctrl_t *ctrl, char *f_str,...) { va_list argp; fprintf(stdout, "[%2"PRIDX"] ", ctrl->mype); va_start(argp, f_str); vfprintf(stdout, f_str, argp); va_end(argp); if (strlen(f_str) == 0 || f_str[strlen(f_str)-1] != '\n') fprintf(stdout,"\n"); fflush(stdout); } /************************************************************************* * This function prints an error message and exits **************************************************************************/ void rprintf(ctrl_t *ctrl, char *f_str,...) { va_list argp; if (ctrl->mype == 0) { va_start(argp, f_str); vfprintf(stdout, f_str, argp); va_end(argp); } fflush(stdout); gkMPI_Barrier(ctrl->comm); } /************************************************************************* * This function does a binary search on an array for a key and returns * the index **************************************************************************/ idx_t BSearch(idx_t n, idx_t *array, idx_t key) { idx_t a=0, b=n, c; while (b-a > 8) { c = (a+b)>>1; if (array[c] > key) b = c; else a = c; } for (c=a; c>1); return (a > 1 ? 0 : 1); } /************************************************************************* * This function returns the log2(x) **************************************************************************/ idx_t log2Int(idx_t a) { idx_t i; for (i=1; a > 1; i++, a = a>>1); return i-1; } /************************************************************************* * These functions return the index of the maximum element in a vector **************************************************************************/ size_t rargmax_strd(size_t n, real_t *x, size_t incx) { size_t i, max=0; n *= incx; for (i=incx; i x[max] ? i : max); return max/incx; } /************************************************************************* * These functions return the index of the maximum element in a vector **************************************************************************/ size_t rargmin_strd(size_t n, real_t *x, size_t incx) { size_t i, min=0; n *= incx; for (i=incx; i x[1]) { max1 = 0; max2 = 1; } else { max1 = 1; max2 = 0; } for (i=2; i x[max1]) { max2 = max1; max1 = i; } else if (x[i] > x[max2]) max2 = i; } return max2; } /************************************************************************* * This function returns the average value of an array **************************************************************************/ real_t ravg(size_t n, real_t *x) { size_t i; real_t retval = 0.0; for (i=0; i m11) return 0; if (m22 < m12) return 1; if (m22 > m12) return 0; return sm2 < sm1; } /************************************************************************* * This function computes the top three values of a real_t array **************************************************************************/ void GetThreeMax(idx_t n, real_t *x, idx_t *first, idx_t *second, idx_t *third) { idx_t i; if (n <= 0) { *first = *second = *third = -1; return; } *second = *third = -1; *first = 0; for (i=1; i x[*first]) { *third = *second; *second = *first; *first = i; continue; } if (*second == -1 || x[i] > x[*second]) { *third = *second; *second = i; continue; } if (*third == -1 || x[i] > x[*third]) *third = i; } return; }