-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstd.h
executable file
·184 lines (155 loc) · 4.04 KB
/
cstd.h
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
#ifndef _cstd_h_
#define _cstd_h_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <complex.h>
#include <limits.h>
#include <cblas.h>
#ifndef PI
#define PI 3.1415927
#endif
#ifndef PI2
#define PI2 9.8696044
#endif
#ifndef PI_1
#define PI_1 0.3183099
#endif
#ifndef PI_2
#define PI_2 0.1013212
#endif
#ifndef TOL
#define TOL 1e-12
#endif
#ifndef EPS
#define EPS FLT_EPSILON
#endif
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef SGN
#define SGN(x) ((x) < 0 ? -1.0 : 1.0)
#endif
#ifndef ABS
#define ABS(x) ((x) < 0 ? -(x) : (x))
#endif
#ifndef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#ifndef NINT
#define NINT(x) ((int)((x) > 0.0 ? (x) + 0.5 : (x) - 0.5))
#endif
/* complex data type */
typedef float cpx[2];
typedef double zpx[2];
/* interface of optimization algorithm*/
typedef float (*opt_fg)(float *x, float *g);
/* allocate and free multi-dimensional arrays */
void *alloc1(size_t n1, size_t size);
void *realloc1(void *v, size_t n1, size_t size);
void **alloc2(size_t n1, size_t n2, size_t size);
void free1(void *p);
void free2(void **p);
int *alloc1int(size_t n1);
int *realloc1int(int *v, size_t n1);
int **alloc2int(size_t n1, size_t n2);
void free1int(int *p);
void free2int(int **p);
float *alloc1float(size_t n1);
float *realloc1float(float *v, size_t n1);
float **alloc2float(size_t n1, size_t n2);
void free1float(float *p);
void free2float(float **p);
double *alloc1double(size_t n1);
double *realloc1double(double *v, size_t n1);
double **alloc2double(size_t n1, size_t n2);
void free1double(double *p);
void free2double(double **p);
cpx *alloc1complexf(size_t n1);
cpx *realloc1complexf(cpx *v, size_t n1);
cpx **alloc2complexf(size_t n1, size_t n2);
void free1complexf(cpx *p);
void free2complexf(cpx **p);
zpx *alloc1complex(size_t n1);
zpx *realloc1complex(zpx *v, size_t n1);
zpx **alloc2complex(size_t n1, size_t n2);
void free1complex(zpx *p);
void free2complex(zpx **p);
char *alloc1char(size_t n1);
char *realloc1char(char *v, size_t n1);
void free1char(char *p);
/* string to numeric conversion with error checking */
short eatoh(char *s);
unsigned short eatou(char *s);
int eatoi(char *s);
unsigned int eatop(char *s);
long eatol(char *s);
unsigned long eatov(char *s);
float eatof(char *s);
double eatod(char *s);
/* string to numeric conversion with error checking */
short eatoh(char *s);
unsigned short eatou(char *s);
int eatoi(char *s);
unsigned int eatop(char *s);
long eatol(char *s);
unsigned long eatov(char *s);
float eatof(char *s);
double eatod(char *s);
void err(char *fmt, ...);
void warn(char *fmt, ...);
/* pading for 2D model */
void pad2(const float *x, float *xx, const int nz, const int nx, const int lft, const int rht, const int top, const int bot);
/* blas function used openblas*/
/* blas2 */
void sscale(int n, float alpha, float *a, float *b);
float sdot(int n, float *a, float *b);
double ddot(int n, double *a, double *b);
void saxpy(int n, float alpha, float *a, float *b);
/* blas3 */
void sgemm(int m, int n, int k, float *A, float *B, float *C);
void dgemm(int m, int n, int k, double *A, double *B, double *C);
/* hash map */
typedef struct HashNode
{
char *key;
char *value;
struct HashNode *next;
} *HashNode;
typedef struct HashTable
{
HashNode *table;
int size;
} *HashTable;
HashTable hash_creat(int size);
void hash_add(HashTable table, char *s /* key=value */);
void hash_close(HashTable tab);
/*init function*/
void init(int argc, char **argv);
bool getint(const char *key, int *n);
bool getfloat(const char *key, float *n);
bool getdouble(const char *key, double *n);
bool getstring(const char *key, char **n);
bool getbool(const char *key, bool *n);
/* file struct */
typedef struct file
{
char *headname, *dataname;
FILE *head, *data;
bool write;
HashTable tb;
} *file;
file input(const char *tag);
file output(const char *tag);
static void file_close(file f);
#endif