Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
uvahotspot
GitHub Repository: uvahotspot/HotSpot
Path: blob/master/util.h
612 views
1
#ifndef __UTIL_H
2
#define __UTIL_H
3
4
#include <stdio.h>
5
6
#if SUPERLU > 0
7
/* Lib for SuperLU */
8
#include "slu_ddefs.h"
9
#endif
10
11
#define MAX(x,y) (((x)>(y))?(x):(y))
12
#define MIN(x,y) (((x)<(y))?(x):(y))
13
#define MAX3(a,b,c) MAX(MAX(a,b),c)
14
#define MIN3(a,b,c) MIN(MIN(a,b),c)
15
#define MID3(a,b,c) ((MIN(a,b)<(c))?(MIN(MAX(a,b),c)):(MAX(MIN(a,b),c)))
16
#define MAX4(a,b,c,d) MAX(MAX(MAX(a,b),c),d)
17
#define MIN4(a,b,c,d) MIN(MIN(MIN(a,b),c),d)
18
#define DELTA 1.0e-6
19
#define LARGENUM 1.0e100
20
#define NULLFILE "(null)"
21
22
23
#define TRUE 1
24
#define FALSE 0
25
26
#define RAND_SEED 1500450271
27
28
#define STR_SIZE 512
29
#define LINE_SIZE 65536
30
#define MAX_ENTRIES 512
31
32
int eq(double x, double y);
33
int le(double x, double y);
34
int ge(double x, double y);
35
void fatal (char *s);
36
void warning (char *s);
37
void swap_ival (int *a, int *b);
38
void swap_dval (double *a, double *b);
39
int tolerant_ceil(double val);
40
int tolerant_floor(double val);
41
42
/* vector routines */
43
double *dvector(int n);
44
void free_dvector(double *v);
45
void dump_dvector(double *v, int n);
46
void copy_dvector (double *dst, double *src, int n);
47
void zero_dvector (double *v, int n);
48
49
int *ivector(int n);
50
void free_ivector(int *v);
51
void dump_ivector(int *v, int n);
52
void copy_ivector (int *dst, int *src, int n);
53
void zero_ivector (int *v, int n);
54
/* sum of the elements */
55
double sum_dvector (double *v, int n);
56
57
/* matrix routines - Thanks to Greg Link
58
* from Penn State University for the
59
* memory allocators/deallocators
60
*/
61
double **dmatrix(int nr, int nc);
62
void free_dmatrix(double **m);
63
void dump_dmatrix(double **m, int nr, int nc);
64
void copy_dmatrix(double **dst, double **src, int nr, int nc);
65
void zero_dmatrix(double **m, int nr, int nc);
66
void resize_dmatrix(double **m, int nr, int nc);
67
/* mirror the lower triangle to make 'm' fully symmetric */
68
void mirror_dmatrix(double **m, int n);
69
70
int **imatrix(int nr, int nc);
71
void free_imatrix(int **m);
72
void dump_imatrix(int **m, int nr, int nc);
73
void copy_imatrix(int **dst, int **src, int nr, int nc);
74
void resize_imatrix(int **m, int nr, int nc);
75
76
/* routines for 3-d matrix with tail */
77
/* allocate 3-d matrix with 'nr' rows, 'nc' cols,
78
* 'nl' layers and a tail of 'xtra' elements
79
*/
80
double ***dcuboid_tail(int nr, int nc, int nl, int xtra);
81
/* destructor */
82
void free_dcuboid(double ***m);
83
84
/* initialize random number generator */
85
void init_rand(void);
86
/* random number within the range [0, max-1] */
87
int rand_upto(int max);
88
/* random number in the range [0, 1) */
89
double rand_fraction(void);
90
91
/* a table of name value pairs */
92
typedef struct str_pair_st
93
{
94
char name[STR_SIZE];
95
char value[STR_SIZE];
96
}str_pair;
97
/*
98
* reads tab-separated name-value pairs from file into
99
* a table of size max_entries and returns the number
100
* of entries read successfully
101
*/
102
int read_str_pairs(str_pair *table, int max_entries, char *file);
103
/* same as above but from command line instead of a file */
104
int parse_cmdline(str_pair *table, int max_entries, int argc, char **argv);
105
/* append the table onto a file */
106
void dump_str_pairs(str_pair *table, int size, char *file, char *prefix);
107
/* table lookup for a name */
108
int get_str_index(str_pair *table, int size, char *str);
109
/*
110
* remove duplicate names in the table - the entries later
111
* in the table are discarded. returns the new size of the
112
* table
113
*/
114
int str_pairs_remove_duplicates(str_pair *table, int size);
115
/*
116
* binary search a sorted double array 'arr' of size 'n'. if found,
117
* the 'loc' pointer has the address of 'ele' and the return
118
* value is TRUE. otherwise, the return value is FALSE and 'loc'
119
* points to the 'should have been' location
120
*/
121
int bsearch_double(double *arr, int n, double ele, double **loc);
122
/*
123
* binary search and insert an element into a partially sorted
124
* double array if not already present. returns FALSE if present
125
*/
126
int bsearch_insert_double(double *arr, int n, double ele);
127
128
/* search if an array contains a value
129
* return the index if so and -1 otherwise
130
*/
131
int contains(int *array, int size, int value);
132
133
/*
134
* population count of an 8-bit integer - using pointers from
135
* http://aggregate.org/MAGIC/
136
*/
137
unsigned int ones8(register unsigned char n);
138
/*
139
* find the number of non-empty, non-comment lines
140
* in a file open for reading
141
*/
142
int count_significant_lines(FILE *fp);
143
144
/* For Matrix format conversion */
145
int coo2csc(int size, int nnz,
146
int *cooX, int *cooY, double *cooV,
147
int *cscRowInd, int *cscColPtr, double *cscV);
148
int c2c_cmp( const void *a , const void *b);
149
150
void gaussj(double **a, int n, double *b);
151
152
#if SUPERLU > 0
153
/* Some Basic Matrix Data Structures and Operations */
154
155
/*
156
* Structure to store a diagonal matrix
157
*/
158
typedef struct diagonal_matrix_t_st
159
{
160
int n;
161
double *vals;
162
} diagonal_matrix_t;
163
164
/*
165
* computes A = c*diag + A
166
*/
167
int diagonal_add_SparseMatrix(double c, diagonal_matrix_t *diag, SuperMatrix *A);
168
169
/*
170
* computes vector = c*diag*vector
171
*/
172
int diagonal_mul_vector(double c, diagonal_matrix_t *diag, double **vector);
173
174
/*
175
* computes vector2 = c1*vector[1] + c2*vector[2]
176
*/
177
int vector_add_vector(int n, double c1, double *vector1, double c2, double *vector2);
178
179
/*
180
* computes vector = A*vector
181
*/
182
int SparseMatrix_mul_vector(SuperMatrix *A, double *vector);
183
184
void cooTocsv(char *filename, int size, int nnz, int *cooX, int *cooY, double *cooV);
185
void diagTocsv(char *filename, diagonal_matrix_t *diag);
186
void vectorTocsv(char *filename, int size, double *vector);
187
#endif
188
189
#endif
190
191