forked from uvahotspot/HotSpot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.c
596 lines (512 loc) · 11.6 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
#include <assert.h>
#include "util.h"
int eq(double x, double y)
{
return (fabs(x-y) < DELTA);
}
int le(double x, double y)
{
return ((x < y) || eq(x,y));
}
int ge(double x, double y)
{
return ((x > y) || eq(x,y));
}
void fatal(char *s)
{
fprintf(stderr, "error: %s", s);
exit(1);
}
void warning(char *s)
{
fprintf(stderr, "warning: %s", s);
}
void swap_ival (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap_dval (double *a, double *b)
{
double t = *a;
*a = *b;
*b = t;
}
int tolerant_ceil(double val)
{
double nearest = floor(val+0.5);
/* numbers close to integers */
if (eq(val, nearest))
return ((int) nearest);
/* all others */
else
return ((int) ceil(val));
}
int tolerant_floor(double val)
{
double nearest = floor(val+0.5);
/* numbers close to integers */
if (eq(val, nearest))
return ((int) nearest);
/* all others */
else
return ((int) floor(val));
}
double *dvector(int n)
{
double *v;
v=(double *)calloc(n, sizeof(double));
if (!v) fatal("allocation failure in dvector()\n");
return v;
}
void free_dvector(double *v)
{
free(v);
}
void dump_dvector (double *v, int n)
{
int i;
for (i=0; i < n; i++)
fprintf(stdout, "%.5f\t", v[i]);
fprintf(stdout, "\n");
}
void copy_dvector (double *dst, double *src, int n)
{
memmove(dst, src, sizeof(double) * n);
}
void zero_dvector (double *v, int n)
{
memset(v, 0, sizeof(double) * n);
}
/* sum of the elements */
double sum_dvector (double *v, int n)
{
double sum = 0;
int i;
for(i=0; i < n; i++)
sum += v[i];
return sum;
}
int *ivector(int n)
{
int *v;
v = (int *)calloc(n, sizeof(int));
if (!v) fatal("allocation failure in ivector()\n");
return v;
}
void free_ivector(int *v)
{
free(v);
}
void dump_ivector (int *v, int n)
{
int i;
for (i=0; i < n; i++)
fprintf(stdout, "%d\t", v[i]);
fprintf(stdout, "\n\n");
}
void copy_ivector (int *dst, int *src, int n)
{
memmove(dst, src, sizeof(int) * n);
}
void zero_ivector (int *v, int n)
{
memset(v, 0, sizeof(int) * n);
}
/*
* Thanks to Greg Link from Penn State University
* for these memory allocators/deallocators
*/
double **dmatrix(int nr, int nc)
{
int i;
double **m;
m = (double **) calloc (nr, sizeof(double *));
assert(m != NULL);
m[0] = (double *) calloc (nr * nc, sizeof(double));
assert(m[0] != NULL);
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
return m;
}
void free_dmatrix(double **m)
{
free(m[0]);
free(m);
}
int **imatrix(int nr, int nc)
{
int i;
int **m;
m = (int **) calloc (nr, sizeof(int *));
assert(m != NULL);
m[0] = (int *) calloc (nr * nc, sizeof(int));
assert(m[0] != NULL);
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
return m;
}
void free_imatrix(int **m)
{
free(m[0]);
free(m);
}
void dump_dmatrix (double **m, int nr, int nc)
{
int i;
for (i=0; i < nr; i++)
dump_dvector(m[i], nc);
fprintf(stdout, "\n");
}
void copy_dmatrix (double **dst, double **src, int nr, int nc)
{
memmove(dst[0], src[0], sizeof(double) * nr * nc);
}
void zero_dmatrix(double **m, int nr, int nc)
{
memset(m[0], 0, sizeof(double) * nr * nc);
}
void resize_dmatrix(double **m, int nr, int nc)
{
int i;
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
}
/* allocate 3-d matrix with 'nr' rows, 'nc' cols,
* 'nl' layers and a tail of 'xtra' elements
*/
double ***dcuboid_tail(int nr, int nc, int nl, int xtra)
{
int i, j;
double ***m;
/* 1-d array of pointers to the rows of the 2-d array below */
m = (double ***) calloc (nl, sizeof(double **));
assert(m != NULL);
/* 2-d array of pointers denoting (layer, row) */
m[0] = (double **) calloc (nl * nr, sizeof(double *));
assert(m[0] != NULL);
/* the actual 3-d data array */
m[0][0] = (double *) calloc (nl * nr * nc + xtra, sizeof(double));
assert(m[0][0] != NULL);
/* remaining pointers of the 1-d pointer array */
for (i = 1; i < nl; i++)
m[i] = m[0] + nr * i;
/* remaining pointers of the 2-d pointer array */
for (i = 0; i < nl; i++)
for (j = 0; j < nr; j++)
/* to reach the jth row in the ith layer,
* one has to cross i layers i.e., i*(nr*nc)
* values first and then j rows i.e., j*nc
* values next
*/
m[i][j] = m[0][0] + (nr * nc) * i + nc * j;
return m;
}
void free_dcuboid(double ***m)
{
free(m[0][0]);
free(m[0]);
free(m);
}
/* mirror the lower triangle to make 'm' fully symmetric */
void mirror_dmatrix(double **m, int n)
{
int i, j;
for(i=0; i < n; i++)
for(j=0; j < i; j++)
m[j][i] = m[i][j];
}
void dump_imatrix (int **m, int nr, int nc)
{
int i;
for (i=0; i < nr; i++)
dump_ivector(m[i], nc);
fprintf(stdout, "\n");
}
void copy_imatrix (int **dst, int **src, int nr, int nc)
{
memmove(dst[0], src[0], sizeof(int) * nr * nc);
}
void resize_imatrix(int **m, int nr, int nc)
{
int i;
for (i = 1; i < nr; i++)
m[i] = m[0] + nc * i;
}
/* initialize random number generator */
void init_rand(void)
{
srand(RAND_SEED);
}
/* random number within the range [0, max-1] */
int rand_upto(int max)
{
return (int) (max * (double) rand() / (RAND_MAX+1.0));
}
/* random number in the range [0, 1) */
double rand_fraction(void)
{
return ((double) rand() / (RAND_MAX+1.0));
}
/*
* reads tab-separated name-value pairs from file into
* a table of size max_entries and returns the number
* of entries read successfully
*/
int read_str_pairs(str_pair *table, int max_entries, char *file)
{
int i=0;
char str[LINE_SIZE], copy[LINE_SIZE];
char name[STR_SIZE];
char *ptr;
FILE *fp = fopen (file, "r");
if (!fp) {
sprintf (str,"error: %s could not be opened for reading\n", file);
fatal(str);
}
while(i < max_entries) {
fgets(str, LINE_SIZE, fp);
if (feof(fp))
break;
strcpy(copy, str);
/* ignore comments and empty lines */
ptr = strtok(str, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
if ((sscanf(copy, "%s%s", name, table[i].value) != 2) || (name[0] != '-'))
fatal("invalid file format\n");
/* ignore the leading "-" */
strcpy(table[i].name, &name[1]);
i++;
}
fclose(fp);
return i;
}
/*
* same as above but from command line instead of a file. the command
* line is of the form <prog_name> <name-value pairs> where
* <name-value pairs> is of the form -<variable> <value>
*/
int parse_cmdline(str_pair *table, int max_entries, int argc, char **argv)
{
int i, count;
for (i=1, count=0; i < argc && count < max_entries; i++) {
if (i % 2) { /* variable name */
if (argv[i][0] != '-')
fatal("invalid command line. check usage\n");
/* ignore the leading "-" */
strncpy(table[count].name, &argv[i][1], STR_SIZE-1);
table[count].name[STR_SIZE-1] = '\0';
} else { /* value */
strncpy(table[count].value, argv[i], STR_SIZE-1);
table[count].value[STR_SIZE-1] = '\0';
count++;
}
}
return count;
}
/* append the table onto a file */
void dump_str_pairs(str_pair *table, int size, char *file, char *prefix)
{
int i;
char str[STR_SIZE];
FILE *fp = fopen (file, "w");
if (!fp) {
sprintf (str,"error: %s could not be opened for writing\n", file);
fatal(str);
}
for(i=0; i < size; i++)
fprintf(fp, "%s%s\t%s\n", prefix, table[i].name, table[i].value);
fclose(fp);
}
/* table lookup for a name */
int get_str_index(str_pair *table, int size, char *str)
{
int i;
if (!table)
fatal("null pointer in get_str_index\n");
for (i = 0; i < size; i++)
if (!strcasecmp(str, table[i].name))
return i;
return -1;
}
/* delete entry at 'at' */
void delete_entry(str_pair *table, int size, int at)
{
int i;
/*
* overwrite this entry using the next and
* shift all later entries once
*/
for (i=at+1; i < size; i++) {
strcpy(table[i-1].name, table[i].name);
strcpy(table[i-1].value, table[i].value);
}
}
/*
* remove duplicate names in the table - the entries later
* in the table are discarded. returns the new size of the
* table
*/
int str_pairs_remove_duplicates(str_pair *table, int size)
{
int i, j;
for(i=0; i < size-1; i++)
for(j=i+1; j < size; j++)
if (!strcasecmp(table[i].name, table[j].name)) {
delete_entry(table, size, j);
size--;
j--;
}
return size;
}
/* debug */
void print_str_pairs(str_pair *table, int size)
{
int i;
fprintf(stdout, "printing string table\n");
for (i=0; i < size; i++)
fprintf(stdout, "%s\t%s\n", table[i].name, table[i].value);
}
/*
* binary search a sorted double array 'arr' of size 'n'. if found,
* the 'loc' pointer has the address of 'ele' and the return
* value is TRUE. otherwise, the return value is FALSE and 'loc'
* points to the 'should have been' location
*/
int bsearch_double(double *arr, int n, double ele, double **loc)
{
if(n < 0)
fatal("wrong index in binary search\n");
if(n == 0) {
*loc = arr;
return FALSE;
}
if(eq(arr[n/2], ele)) {
*loc = &arr[n/2];
return TRUE;
} else if (arr[n/2] < ele) {
return bsearch_double(&arr[n/2+1], (n-1)/2, ele, loc);
} else
return bsearch_double(arr, n/2, ele, loc);
}
/*
* binary search and insert an element into a partially sorted
* double array if not already present. returns FALSE if present
*/
int bsearch_insert_double(double *arr, int n, double ele)
{
double *loc;
int i;
/* element found - nothing more left to do */
if (bsearch_double(arr, n, ele, &loc))
return FALSE;
else {
for(i=n-1; i >= (loc-arr); i--)
arr[i+1] = arr[i];
arr[loc-arr] = ele;
}
return TRUE;
}
/*
* population count of an 8-bit integer - using pointers from
* http://aggregate.org/MAGIC/
*/
unsigned int ones8(register unsigned char n)
{
/* group the bits in two and compute the no. of 1's within a group
* this works because 00->00, 01->01, 10->01, 11->10 or
* n = n - (n >> 1). the 0x55 masking prevents bits flowing across
* group boundary
*/
n -= ((n >> 1) & 0x55);
/* add the 2-bit sums into nibbles */
n = ((n & 0x33) + ((n >> 2) & 0x33));
/* add both the nibbles */
n = ((n + (n >> 4)) & 0x0F);
return n;
}
/*
* find the number of non-empty, non-comment lines
* in a file open for reading
*/
int count_significant_lines(FILE *fp)
{
char str[LINE_SIZE], *ptr;
int count = 0;
fseek(fp, 0, SEEK_SET);
while(!feof(fp)) {
fgets(str, LINE_SIZE, fp);
if (feof(fp))
break;
/* ignore comments and empty lines */
ptr = strtok(str, " \r\t\n");
if (!ptr || ptr[0] == '#')
continue;
count++;
}
return count;
}
/* Ke's code: Coo2CSC */
struct coo_elem
{
int x;
int y;
double val;
};
int c2c_cmp( const void *a , const void *b )
{
struct coo_elem *c = (struct coo_elem *)a;
struct coo_elem *d = (struct coo_elem *)b;
if(c->y != d->y) return c->y - d->y;
else return c->x - d->x;
}
int coo2csc(int size, int nnz,
int *cooX, int *cooY, double *cooV, // input COO array
int *cscRowInd, int *cscColPtr, double *cscV) //output CSC array
{
int i, j;
int prev_x, prev_y;
// Init struct array
struct coo_elem *cooArray;
cooArray = (struct coo_elem *) calloc (nnz, sizeof(struct coo_elem));
// Copy in
for (i =0; i <nnz; i++) {
cooArray[i].x = cooX[i];
cooArray[i].y = cooY[i];
cooArray[i].val = cooV[i];
}
// Sort in col major
qsort(cooArray, nnz, sizeof(cooArray[0]), c2c_cmp);
// Copy out, check duplicate
j = -1;
prev_x = -1;
prev_y = -1;
for (i =0; i <nnz; i++) {
cscRowInd[i]=cooArray[i].x;
cscV[i]=cooArray[i].val;
while(j<cooArray[i].y){
j++;
cscColPtr[j]=i;
}
if((cooArray[i].x == prev_x) &&
(cooArray[i].y == prev_y))
printf("Warning: Duplicate elements in Matrix!\n");
prev_x = cooArray[i].x;
prev_y = cooArray[i].y;
}
cscColPtr[j+1]=i;
free(cooArray);
return 1;
}