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