ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
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  when command line fails, print out help info
18 ************************************************************************/
19 void print_help(char *program_name){
20  printf("\nHelp\nTo cluster a graph into a given number of clusters:\n %s [options] graph_file number_of_clusters\n", program_name);
21  printf(" options: -o ncut|rassoc\n");
22  printf(" \t\t ncut --- normalized cut (default)\n\t\t rassoc --- ratio association\n");
23  printf(" -l number_of_local_search_steps (default is 0)\n");
24  printf(" -b use only boundary points (default is to use all points)\n");
25  // printf(" -s use spectral at coarsest level (default is METIS init.)\n");
26  printf("\nTo compute objective function value for a given clustering:\n %s [options] -e clustering_file graph_file\n\n", program_name);
27 }
28 
29 
30 /************************************************************************
31  find out the cluster size
32 ************************************************************************/
33 void clusterSize(GraphType * graph, int *clustersize){
34  idxtype *where, i, nvtxs;
35  where = graph->where;
36  nvtxs = graph->nvtxs;
37 
38  for (i=0; i<nvtxs; i++)
39  clustersize[where[i]] ++;
40 }
41 
42 /*************************************************************************
43 * This function transform Metis graph matrix into a dense matrix,
44 * which is represented as an array initialized to be 0
45 **************************************************************************/
46 void sparse2dense(GraphType * graph, double * dense, float *m_adjwgt)
47 {
48  int nvtxs, i, j;
49  idxtype *adjwgt, *adjncy, *xadj;
50 
51  nvtxs = graph->nvtxs;
52  xadj = graph->xadj;
53  adjncy = graph->adjncy;
54  adjwgt = graph->adjwgt;
55 
56  for (i=0; i<nvtxs; i++)
57  for (j=0; j<nvtxs; j++)
58  dense[i*nvtxs+j] =0;
59 
60  if (adjwgt == NULL)
61  for (i=0; i<nvtxs; i++)
62  for (j=xadj[i]; j<xadj[i+1]; j++)
63  dense[i* nvtxs+adjncy[j]] = 1;
64  else
65  for (i=0; i<nvtxs; i++)
66  for (j=xadj[i]; j<xadj[i+1]; j++)
67  dense[i* nvtxs+adjncy[j]] = m_adjwgt[j];
68 }
69 
70 /*************************************************************************
71 * This function extracts file name from a path
72 **************************************************************************/
73 void extractfilename(char *path, char *name)
74 {
75  int length, i, j;
76  length = strlen(path);
77  for(i= length-1; i>=0; i--)
78  if ((path[i] == '/') || (path[i] == '\\'))
79  {
80  i++;
81  for (j=i; j<length; j++)
82  name[j-i]=path[j];
83  name[j-i] = '\0';
84  break;
85  }
86  else if (i==0)
87  {
88  for (j=i; j<length; j++)
89  name[j-i]=path[j];
90  name[j] = '\0';
91  break;
92  }
93 }
94 
95 /*************************************************************************
96 * This function prints an error message and exits
97 **************************************************************************/
98 void graclus_errexit(char *f_str,...)
99 {
100  va_list argp;
101  char out1[256], out2[256];
102 
103  va_start(argp, f_str);
104  vsprintf(out1, f_str, argp);
105  va_end(argp);
106 
107  sprintf(out2, "Error! %s", out1);
108 
109  fprintf(stdout, out2);
110  fflush(stdout);
111 
112  abort();
113 }
114 
115 
116 
117 #ifndef DMALLOC
118 /*************************************************************************
119 * The following function allocates an array of Chains
120 **************************************************************************/
121 Chains *chainmalloc(int n, char *msg)
122 {
123  if (n == 0)
124  return NULL;
125 
126  return (Chains *)GKmalloc(sizeof(Chains)*n, msg);
127 }
128 
129 /*************************************************************************
130 * The following function allocates an 2-D array of floats
131 **************************************************************************/
132 float **f2malloc(int n, int m, char *msg)
133 {
134  float ** temp;
135  int i;
136  if ((n == 0) || (m==0))
137  return NULL;
138 
139  temp = (float **)GKmalloc(sizeof(float *)*n, msg);
140  for (i=0; i<n; i++)
141  temp[i] = (float *)GKmalloc(sizeof(float)*m, msg);
142  return temp;
143 }
144 
145 /*************************************************************************
146 * The following function allocates an 2-D array of ints
147 **************************************************************************/
148 int **i2malloc(int n, int m, char *msg)
149 {
150  int ** temp;
151  int i;
152  if ((n == 0) || (m==0))
153  return NULL;
154 
155  temp = (int **)GKmalloc(sizeof(int *)*n, msg);
156  for (i=0; i<n; i++)
157  temp[i] = (int *)GKmalloc(sizeof(int)*m, msg);
158  return temp;
159 }
160 
161 /*************************************************************************
162 * The following function allocates an array of integers
163 **************************************************************************/
164 int *imalloc(int n, char *msg)
165 {
166  if (n == 0)
167  return NULL;
168 
169  return (int *)GKmalloc(sizeof(int)*n, msg);
170 }
171 
172 
173 /*************************************************************************
174 * The following function allocates an array of integers
175 **************************************************************************/
176 idxtype *idxmalloc(int n, char *msg)
177 {
178  if (n == 0)
179  return NULL;
180 
181  return (idxtype *)GKmalloc(sizeof(idxtype)*n, msg);
182 }
183 
184 
185 /*************************************************************************
186 * The following function allocates an array of float
187 **************************************************************************/
188 float *fmalloc(int n, char *msg)
189 {
190  if (n == 0)
191  return NULL;
192 
193  return (float *)GKmalloc(sizeof(float)*n, msg);
194 }
195 
196 
197 /*************************************************************************
198 * The follwoing function allocates an array of integers
199 **************************************************************************/
200 int *ismalloc(int n, int ival, char *msg)
201 {
202  if (n == 0)
203  return NULL;
204 
205  return iset(n, ival, (int *)GKmalloc(sizeof(int)*n, msg));
206 }
207 
208 
209 
210 /*************************************************************************
211 * The follwoing function allocates an array of integers
212 **************************************************************************/
213 idxtype *idxsmalloc(int n, idxtype ival, char *msg)
214 {
215  if (n == 0)
216  return NULL;
217 
218  return idxset(n, ival, (idxtype *)GKmalloc(sizeof(idxtype)*n, msg));
219 }
220 
221 
222 /*************************************************************************
223 * This function is my wrapper around malloc
224 **************************************************************************/
225 void *GKmalloc(int nbytes, char *msg)
226 {
227  void *ptr;
228 
229  if (nbytes == 0)
230  return NULL;
231 
232  ptr = (void *)malloc(nbytes);
233  if (ptr == NULL)
234  graclus_errexit("***Memory allocation failed for %s. Requested size: %d bytes", msg, nbytes);
235 
236  return ptr;
237 }
238 #endif
239 
240 /*************************************************************************
241 * This function is my wrapper around free, allows multiple pointers
242 **************************************************************************/
243 void GKfree(void **ptr1,...)
244 {
245  va_list plist;
246  void **ptr;
247 
248  if (*ptr1 != NULL)
249  free(*ptr1);
250  *ptr1 = NULL;
251 
252  va_start(plist, ptr1);
253 
254  /* while ((int)(ptr = va_arg(plist, void **)) != -1) { */
255  while ((ptr = va_arg(plist, void **)) != LTERM) {
256  if (*ptr != NULL)
257  free(*ptr);
258  *ptr = NULL;
259  }
260 
261  va_end(plist);
262 }
263 
264 
265 /*************************************************************************
266 * These functions set the values of a vector
267 **************************************************************************/
268 int *iset(int n, int val, int *x)
269 {
270  int i;
271 
272  for (i=0; i<n; i++)
273  x[i] = val;
274 
275  return x;
276 }
277 
278 
279 /*************************************************************************
280 * These functions set the values of a vector
281 **************************************************************************/
282 idxtype *idxset(int n, idxtype val, idxtype *x)
283 {
284  int i;
285 
286  for (i=0; i<n; i++)
287  x[i] = val;
288 
289  return x;
290 }
291 
292 
293 /*************************************************************************
294 * These functions set the values of a vector
295 **************************************************************************/
296 float *sset(int n, float val, float *x)
297 {
298  int i;
299 
300  for (i=0; i<n; i++)
301  x[i] = val;
302 
303  return x;
304 }
305 
306 
307 
308 /*************************************************************************
309 * These functions return the index of the maximum element in a vector
310 **************************************************************************/
311 int iamax(int n, int *x)
312 {
313  int i, max=0;
314 
315  for (i=1; i<n; i++)
316  max = (x[i] > x[max] ? i : max);
317 
318  return max;
319 }
320 
321 
322 /*************************************************************************
323 * These functions return the index of the maximum element in a vector
324 **************************************************************************/
325 int idxamax(int n, idxtype *x)
326 {
327  int i, max=0;
328 
329  for (i=1; i<n; i++)
330  max = (x[i] > x[max] ? i : max);
331 
332  return max;
333 }
334 
335 /*************************************************************************
336 * These functions return the index of the maximum element in a vector
337 **************************************************************************/
338 int idxamax_strd(int n, idxtype *x, int incx)
339 {
340  int i, max=0;
341 
342  n *= incx;
343  for (i=incx; i<n; i+=incx)
344  max = (x[i] > x[max] ? i : max);
345 
346  return max/incx;
347 }
348 
349 
350 
351 /*************************************************************************
352 * These functions return the index of the maximum element in a vector
353 **************************************************************************/
354 int samax(int n, float *x)
355 {
356  int i, max=0;
357 
358  for (i=1; i<n; i++)
359  max = (x[i] > x[max] ? i : max);
360 
361  return max;
362 }
363 
364 /*************************************************************************
365 * These functions return the index of the almost maximum element in a vector
366 **************************************************************************/
367 int samax2(int n, float *x)
368 {
369  int i, max1, max2;
370 
371  if (x[0] > x[1]) {
372  max1 = 0;
373  max2 = 1;
374  }
375  else {
376  max1 = 1;
377  max2 = 0;
378  }
379 
380  for (i=2; i<n; i++) {
381  if (x[i] > x[max1]) {
382  max2 = max1;
383  max1 = i;
384  }
385  else if (x[i] > x[max2])
386  max2 = i;
387  }
388 
389  return max2;
390 }
391 
392 
393 /*************************************************************************
394 * These functions return the index of the minimum element in a vector
395 **************************************************************************/
396 int idxamin(int n, idxtype *x)
397 {
398  int i, min=0;
399 
400  for (i=1; i<n; i++)
401  min = (x[i] < x[min] ? i : min);
402 
403  return min;
404 }
405 
406 
407 /*************************************************************************
408 * These functions return the index of the minimum element in a vector
409 **************************************************************************/
410 int samin(int n, float *x)
411 {
412  int i, min=0;
413 
414  for (i=1; i<n; i++)
415  min = (x[i] < x[min] ? i : min);
416 
417  return min;
418 }
419 
420 
421 /*************************************************************************
422 * This function sums the entries in an array
423 **************************************************************************/
424 int idxsum(int n, idxtype *x)
425 {
426  int i, sum = 0;
427 
428  for (i=0; i<n; i++)
429  sum += x[i];
430 
431  return sum;
432 }
433 
434 
435 /*************************************************************************
436 * This function sums the entries in an array
437 **************************************************************************/
438 int idxsum_strd(int n, idxtype *x, int incx)
439 {
440  int i, sum = 0;
441 
442  for (i=0; i<n; i++, x+=incx) {
443  sum += *x;
444  }
445 
446  return sum;
447 }
448 
449 
450 /*************************************************************************
451 * This function sums the entries in an array
452 **************************************************************************/
453 void idxadd(int n, idxtype *x, idxtype *y)
454 {
455  for (n--; n>=0; n--)
456  y[n] += x[n];
457 }
458 
459 
460 /*************************************************************************
461 * This function sums the entries in an array
462 **************************************************************************/
463 int charsum(int n, char *x)
464 {
465  int i, sum = 0;
466 
467  for (i=0; i<n; i++)
468  sum += x[i];
469 
470  return sum;
471 }
472 
473 /*************************************************************************
474 * This function sums the entries in an array
475 **************************************************************************/
476 int isum(int n, int *x)
477 {
478  int i, sum = 0;
479 
480  for (i=0; i<n; i++)
481  sum += x[i];
482 
483  return sum;
484 }
485 
486 /*************************************************************************
487 * This function sums the entries in an array
488 **************************************************************************/
489 float ssum(int n, float *x)
490 {
491  int i;
492  float sum = 0.0;
493 
494  for (i=0; i<n; i++)
495  sum += x[i];
496 
497  return sum;
498 }
499 
500 /*************************************************************************
501 * This function sums the entries in an array
502 **************************************************************************/
503 float ssum_strd(int n, float *x, int incx)
504 {
505  int i;
506  float sum = 0.0;
507 
508  for (i=0; i<n; i++, x+=incx)
509  sum += *x;
510 
511  return sum;
512 }
513 
514 /*************************************************************************
515 * This function sums the entries in an array
516 **************************************************************************/
517 void sscale(int n, float alpha, float *x)
518 {
519  int i;
520 
521  for (i=0; i<n; i++)
522  x[i] *= alpha;
523 }
524 
525 
526 /*************************************************************************
527 * This function computes a 2-norm
528 **************************************************************************/
529 float snorm2(int n, float *v)
530 {
531  int i;
532  float partial = 0;
533 
534  for (i = 0; i<n; i++)
535  partial += v[i] * v[i];
536 
537  return sqrt(partial);
538 }
539 
540 
541 
542 /*************************************************************************
543 * This function computes a 2-norm
544 **************************************************************************/
545 float sdot(int n, float *x, float *y)
546 {
547  int i;
548  float partial = 0;
549 
550  for (i = 0; i<n; i++)
551  partial += x[i] * y[i];
552 
553  return partial;
554 }
555 
556 
557 /*************************************************************************
558 * This function computes a 2-norm
559 **************************************************************************/
560 void saxpy(int n, float alpha, float *x, int incx, float *y, int incy)
561 {
562  int i;
563 
564  for (i=0; i<n; i++, x+=incx, y+=incy)
565  *y += alpha*(*x);
566 }
567 
568 
569 
570 
571 /*************************************************************************
572 * This file randomly permutes the contents of an array.
573 * flag == 0, don't initialize perm
574 * flag == 1, set p[i] = i
575 **************************************************************************/
576 void RandomPermute(int n, idxtype *p, int flag)
577 {
578  int i, j, u, v;
579  idxtype tmp;
580 
581  if (flag == 1) {
582  for (i=0; i<n; i++)
583  p[i] = i;
584  }
585 
586  for(i = 1; i < n; i++)
587  {
588  j = rand() % (i+1);
589  tmp = p[i];
590  p[i] = p[j];
591  p[j] = tmp;
592  }
593 
594 /*
595  if (flag == 1) {
596  for (i=0; i<n; i++)
597  p[i] = i;
598  }
599 
600  if (n <= 4)
601  return;
602 
603  for (i=0; i<n; i+=16) {
604  u = RandomInRangeFast(n-4);
605  v = RandomInRangeFast(n-4);
606  SWAP(p[v], p[u], tmp);
607  SWAP(p[v+1], p[u+1], tmp);
608  SWAP(p[v+2], p[u+2], tmp);
609  SWAP(p[v+3], p[u+3], tmp);
610  }
611 */
612 }
613 
614 
615 /*************************************************************************
616 * This function generates random initialization
617 **************************************************************************/
618 void RandomInit(int n, int k, idxtype *label)
619 {
620  int i, chunksize, j;
621  idxtype tmp;
622  idxtype *p= idxmalloc(n, "Util: RandomInit\n");
623 
624  RandomPermute(n, p, 1);
625  chunksize = n / k +1;
626  j=0;
627  for (i=0; i<n; i++){
628  label[p[i]] = j;
629  if ((i+1)% chunksize ==0)
630  j++;
631  }
632  free (p);
633 }
634 
635 
636 
637 /*************************************************************************
638 * This function returns true if the a is a power of 2
639 **************************************************************************/
640 int ispow2(int a)
641 {
642  for (; a%2 != 1; a = a>>1);
643  return (a > 1 ? 0 : 1);
644 }
645 
646 
647 /*************************************************************************
648 * This function initializes the random number generator
649 **************************************************************************/
650 void InitRandom(int seed)
651 {
652  if (seed == -1) {
653  srand(4321);
654  }
655  else {
656  srand(seed);
657  }
658 }
659 
660 /*************************************************************************
661 * This function returns the log2(x)
662 **************************************************************************/
663 int log2_metis(int a)
664 {
665  int i;
666 
667  for (i=1; a > 1; i++, a = a>>1);
668  return i-1;
669 }
670 
std::string name
#define NULL
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
int min(int a, int b)
Definition: cutil_math.h:53
int max(int a, int b)
Definition: cutil_math.h:48
#define LTERM
Definition: defs.h:23
@ partial
Definition: lz4.c:365
static const std::string path
Definition: PointCloud.cpp:59
int idxtype
Definition: struct.h:17
idxtype * adjncy
Definition: struct.h:172
idxtype * where
Definition: struct.h:183
int nvtxs
Definition: struct.h:168
idxtype * adjwgt
Definition: struct.h:173
idxtype * xadj
Definition: struct.h:169
int idxamax(int n, idxtype *x)
Definition: util.c:325
Chains * chainmalloc(int n, char *msg)
Definition: util.c:121
void sparse2dense(GraphType *graph, double *dense, float *m_adjwgt)
Definition: util.c:46
void graclus_errexit(char *f_str,...)
Definition: util.c:98
void RandomPermute(int n, idxtype *p, int flag)
Definition: util.c:576
int idxamin(int n, idxtype *x)
Definition: util.c:396
int * ismalloc(int n, int ival, char *msg)
Definition: util.c:200
void sscale(int n, float alpha, float *x)
Definition: util.c:517
void idxadd(int n, idxtype *x, idxtype *y)
Definition: util.c:453
int iamax(int n, int *x)
Definition: util.c:311
int samax(int n, float *x)
Definition: util.c:354
float sdot(int n, float *x, float *y)
Definition: util.c:545
idxtype * idxmalloc(int n, char *msg)
Definition: util.c:176
int idxsum_strd(int n, idxtype *x, int incx)
Definition: util.c:438
int idxamax_strd(int n, idxtype *x, int incx)
Definition: util.c:338
int ** i2malloc(int n, int m, char *msg)
Definition: util.c:148
int idxsum(int n, idxtype *x)
Definition: util.c:424
int * iset(int n, int val, int *x)
Definition: util.c:268
void GKfree(void **ptr1,...)
Definition: util.c:243
idxtype * idxset(int n, idxtype val, idxtype *x)
Definition: util.c:282
int charsum(int n, char *x)
Definition: util.c:463
void extractfilename(char *path, char *name)
Definition: util.c:73
void RandomInit(int n, int k, idxtype *label)
Definition: util.c:618
int samax2(int n, float *x)
Definition: util.c:367
void print_help(char *program_name)
Definition: util.c:19
float * sset(int n, float val, float *x)
Definition: util.c:296
int samin(int n, float *x)
Definition: util.c:410
int isum(int n, int *x)
Definition: util.c:476
void * GKmalloc(int nbytes, char *msg)
Definition: util.c:225
int ispow2(int a)
Definition: util.c:640
float ssum_strd(int n, float *x, int incx)
Definition: util.c:503
void InitRandom(int seed)
Definition: util.c:650
idxtype * idxsmalloc(int n, idxtype ival, char *msg)
Definition: util.c:213
float * fmalloc(int n, char *msg)
Definition: util.c:188
float snorm2(int n, float *v)
Definition: util.c:529
void clusterSize(GraphType *graph, int *clustersize)
Definition: util.c:33
float ** f2malloc(int n, int m, char *msg)
Definition: util.c:132
float ssum(int n, float *x)
Definition: util.c:489
int log2_metis(int a)
Definition: util.c:663
int * imalloc(int n, char *msg)
Definition: util.c:164
void saxpy(int n, float alpha, float *x, int incx, float *y, int incy)
Definition: util.c:560