/contrib/Metis/util.c

https://bitbucket.org/lge/gmsh · C · 525 lines · 278 code · 127 blank · 120 comment · 59 complexity · 0407c646bab5fa04944260d3fcdf9f3a MD5 · raw file

  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.3 2006-02-24 22:07:08 geuzaine Exp $
  12. */
  13. #include <metis.h>
  14. /*************************************************************************
  15. * This function prints an error message and exits
  16. **************************************************************************/
  17. void errexit(char *f_str,...)
  18. {
  19. va_list argp;
  20. char out1[256], out2[256];
  21. va_start(argp, f_str);
  22. vsprintf(out1, f_str, argp);
  23. va_end(argp);
  24. sprintf(out2, "Error! %s", out1);
  25. fprintf(stdout, out2);
  26. fflush(stdout);
  27. abort();
  28. }
  29. #ifndef DMALLOC
  30. /*************************************************************************
  31. * The following function allocates an array of integers
  32. **************************************************************************/
  33. int *imalloc(int n, char *msg)
  34. {
  35. if (n == 0)
  36. return NULL;
  37. return (int *)GKmalloc(sizeof(int)*n, msg);
  38. }
  39. /*************************************************************************
  40. * The following function allocates an array of integers
  41. **************************************************************************/
  42. idxtype *idxmalloc(int n, char *msg)
  43. {
  44. if (n == 0)
  45. return NULL;
  46. return (idxtype *)GKmalloc(sizeof(idxtype)*n, msg);
  47. }
  48. /*************************************************************************
  49. * The following function allocates an array of float
  50. **************************************************************************/
  51. float *fmalloc(int n, char *msg)
  52. {
  53. if (n == 0)
  54. return NULL;
  55. return (float *)GKmalloc(sizeof(float)*n, msg);
  56. }
  57. /*************************************************************************
  58. * The follwoing function allocates an array of integers
  59. **************************************************************************/
  60. int *ismalloc(int n, int ival, char *msg)
  61. {
  62. if (n == 0)
  63. return NULL;
  64. return iset(n, ival, (int *)GKmalloc(sizeof(int)*n, msg));
  65. }
  66. /*************************************************************************
  67. * The follwoing function allocates an array of integers
  68. **************************************************************************/
  69. idxtype *idxsmalloc(int n, idxtype ival, char *msg)
  70. {
  71. if (n == 0)
  72. return NULL;
  73. return idxset(n, ival, (idxtype *)GKmalloc(sizeof(idxtype)*n, msg));
  74. }
  75. /*************************************************************************
  76. * This function is my wrapper around malloc
  77. **************************************************************************/
  78. void *GKmalloc(int nbytes, char *msg)
  79. {
  80. void *ptr;
  81. if (nbytes == 0)
  82. return NULL;
  83. ptr = (void *)malloc(nbytes);
  84. if (ptr == NULL)
  85. errexit("***Memory allocation failed for %s. Requested size: %d bytes", msg, nbytes);
  86. return ptr;
  87. }
  88. #endif
  89. /*************************************************************************
  90. * This function is my wrapper around free, allows multiple pointers
  91. **************************************************************************/
  92. void GKfree(void **ptr1,...)
  93. {
  94. va_list plist;
  95. void **ptr;
  96. if (*ptr1 != NULL)
  97. free(*ptr1);
  98. *ptr1 = NULL;
  99. va_start(plist, ptr1);
  100. /* while ((int)(ptr = va_arg(plist, void **)) != -1) { */
  101. while ((ptr = va_arg(plist, void **)) != LTERM) {
  102. if (*ptr != NULL)
  103. free(*ptr);
  104. *ptr = NULL;
  105. }
  106. va_end(plist);
  107. }
  108. /*************************************************************************
  109. * These functions set the values of a vector
  110. **************************************************************************/
  111. int *iset(int n, int val, int *x)
  112. {
  113. int i;
  114. for (i=0; i<n; i++)
  115. x[i] = val;
  116. return x;
  117. }
  118. /*************************************************************************
  119. * These functions set the values of a vector
  120. **************************************************************************/
  121. idxtype *idxset(int n, idxtype val, idxtype *x)
  122. {
  123. int i;
  124. for (i=0; i<n; i++)
  125. x[i] = val;
  126. return x;
  127. }
  128. /*************************************************************************
  129. * These functions set the values of a vector
  130. **************************************************************************/
  131. float *sset(int n, float val, float *x)
  132. {
  133. int i;
  134. for (i=0; i<n; i++)
  135. x[i] = val;
  136. return x;
  137. }
  138. /*************************************************************************
  139. * These functions return the index of the maximum element in a vector
  140. **************************************************************************/
  141. int iamax(int n, int *x)
  142. {
  143. int i, max=0;
  144. for (i=1; i<n; i++)
  145. max = (x[i] > x[max] ? i : max);
  146. return max;
  147. }
  148. /*************************************************************************
  149. * These functions return the index of the maximum element in a vector
  150. **************************************************************************/
  151. int idxamax(int n, idxtype *x)
  152. {
  153. int i, max=0;
  154. for (i=1; i<n; i++)
  155. max = (x[i] > x[max] ? i : max);
  156. return max;
  157. }
  158. /*************************************************************************
  159. * These functions return the index of the maximum element in a vector
  160. **************************************************************************/
  161. int idxamax_strd(int n, idxtype *x, int incx)
  162. {
  163. int i, max=0;
  164. n *= incx;
  165. for (i=incx; i<n; i+=incx)
  166. max = (x[i] > x[max] ? i : max);
  167. return max/incx;
  168. }
  169. /*************************************************************************
  170. * These functions return the index of the maximum element in a vector
  171. **************************************************************************/
  172. int samax(int n, float *x)
  173. {
  174. int i, max=0;
  175. for (i=1; i<n; i++)
  176. max = (x[i] > x[max] ? i : max);
  177. return max;
  178. }
  179. /*************************************************************************
  180. * These functions return the index of the almost maximum element in a vector
  181. **************************************************************************/
  182. int samax2(int n, float *x)
  183. {
  184. int i, max1, max2;
  185. if (x[0] > x[1]) {
  186. max1 = 0;
  187. max2 = 1;
  188. }
  189. else {
  190. max1 = 1;
  191. max2 = 0;
  192. }
  193. for (i=2; i<n; i++) {
  194. if (x[i] > x[max1]) {
  195. max2 = max1;
  196. max1 = i;
  197. }
  198. else if (x[i] > x[max2])
  199. max2 = i;
  200. }
  201. return max2;
  202. }
  203. /*************************************************************************
  204. * These functions return the index of the minimum element in a vector
  205. **************************************************************************/
  206. int idxamin(int n, idxtype *x)
  207. {
  208. int i, min=0;
  209. for (i=1; i<n; i++)
  210. min = (x[i] < x[min] ? i : min);
  211. return min;
  212. }
  213. /*************************************************************************
  214. * These functions return the index of the minimum element in a vector
  215. **************************************************************************/
  216. int samin(int n, float *x)
  217. {
  218. int i, min=0;
  219. for (i=1; i<n; i++)
  220. min = (x[i] < x[min] ? i : min);
  221. return min;
  222. }
  223. /*************************************************************************
  224. * This function sums the entries in an array
  225. **************************************************************************/
  226. int idxsum(int n, idxtype *x)
  227. {
  228. int i, sum = 0;
  229. for (i=0; i<n; i++)
  230. sum += x[i];
  231. return sum;
  232. }
  233. /*************************************************************************
  234. * This function sums the entries in an array
  235. **************************************************************************/
  236. int idxsum_strd(int n, idxtype *x, int incx)
  237. {
  238. int i, sum = 0;
  239. for (i=0; i<n; i++, x+=incx) {
  240. sum += *x;
  241. }
  242. return sum;
  243. }
  244. /*************************************************************************
  245. * This function sums the entries in an array
  246. **************************************************************************/
  247. void idxadd(int n, idxtype *x, idxtype *y)
  248. {
  249. for (n--; n>=0; n--)
  250. y[n] += x[n];
  251. }
  252. /*************************************************************************
  253. * This function sums the entries in an array
  254. **************************************************************************/
  255. int charsum(int n, char *x)
  256. {
  257. int i, sum = 0;
  258. for (i=0; i<n; i++)
  259. sum += x[i];
  260. return sum;
  261. }
  262. /*************************************************************************
  263. * This function sums the entries in an array
  264. **************************************************************************/
  265. int isum(int n, int *x)
  266. {
  267. int i, sum = 0;
  268. for (i=0; i<n; i++)
  269. sum += x[i];
  270. return sum;
  271. }
  272. /*************************************************************************
  273. * This function sums the entries in an array
  274. **************************************************************************/
  275. float ssum(int n, float *x)
  276. {
  277. int i;
  278. float sum = 0.0;
  279. for (i=0; i<n; i++)
  280. sum += x[i];
  281. return sum;
  282. }
  283. /*************************************************************************
  284. * This function sums the entries in an array
  285. **************************************************************************/
  286. float ssum_strd(int n, float *x, int incx)
  287. {
  288. int i;
  289. float sum = 0.0;
  290. for (i=0; i<n; i++, x+=incx)
  291. sum += *x;
  292. return sum;
  293. }
  294. /*************************************************************************
  295. * This function sums the entries in an array
  296. **************************************************************************/
  297. void sscale(int n, float alpha, float *x)
  298. {
  299. int i;
  300. for (i=0; i<n; i++)
  301. x[i] *= alpha;
  302. }
  303. /*************************************************************************
  304. * This function computes a 2-norm
  305. **************************************************************************/
  306. float snorm2(int n, float *v)
  307. {
  308. int i;
  309. float partial = 0;
  310. for (i = 0; i<n; i++)
  311. partial += v[i] * v[i];
  312. return sqrt(partial);
  313. }
  314. /*************************************************************************
  315. * This function computes a 2-norm
  316. **************************************************************************/
  317. float sdot(int n, float *x, float *y)
  318. {
  319. int i;
  320. float partial = 0;
  321. for (i = 0; i<n; i++)
  322. partial += x[i] * y[i];
  323. return partial;
  324. }
  325. /*************************************************************************
  326. * This function computes a 2-norm
  327. **************************************************************************/
  328. void saxpy(int n, float alpha, float *x, int incx, float *y, int incy)
  329. {
  330. int i;
  331. for (i=0; i<n; i++, x+=incx, y+=incy)
  332. *y += alpha*(*x);
  333. }
  334. /*************************************************************************
  335. * This file randomly permutes the contents of an array.
  336. * flag == 0, don't initialize perm
  337. * flag == 1, set p[i] = i
  338. **************************************************************************/
  339. void RandomPermute(int n, idxtype *p, int flag)
  340. {
  341. int i, u, v;
  342. idxtype tmp;
  343. if (flag == 1) {
  344. for (i=0; i<n; i++)
  345. p[i] = i;
  346. }
  347. if (n <= 4)
  348. return;
  349. for (i=0; i<n; i+=16) {
  350. u = RandomInRangeFast(n-4);
  351. v = RandomInRangeFast(n-4);
  352. SWAP(p[v], p[u], tmp);
  353. SWAP(p[v+1], p[u+1], tmp);
  354. SWAP(p[v+2], p[u+2], tmp);
  355. SWAP(p[v+3], p[u+3], tmp);
  356. }
  357. }
  358. /*************************************************************************
  359. * This function returns true if the a is a power of 2
  360. **************************************************************************/
  361. int ispow2(int a)
  362. {
  363. for (; a%2 != 1; a = a>>1);
  364. return (a > 1 ? 0 : 1);
  365. }
  366. /*************************************************************************
  367. * This function initializes the random number generator
  368. **************************************************************************/
  369. void InitRandom(int seed)
  370. {
  371. if (seed == -1) {
  372. /* Gmsh
  373. #ifndef __VC__
  374. */
  375. #if !defined(WIN32) || defined(__CYGWIN__)
  376. srand48(7654321L);
  377. #endif
  378. srand(4321);
  379. }
  380. else {
  381. /* Gmsh
  382. #ifndef __VC__
  383. */
  384. #if !defined(WIN32) || defined(__CYGWIN__)
  385. srand48(seed);
  386. #endif
  387. srand(seed);
  388. }
  389. }
  390. /*************************************************************************
  391. * This function returns the log2(x)
  392. **************************************************************************/
  393. int METISlog2(int a) /* Gmsh: changed from log2 */
  394. {
  395. int i;
  396. for (i=1; a > 1; i++, a = a>>1);
  397. return i-1;
  398. }