WarpX
wp_parser_y.h
Go to the documentation of this file.
1 #ifndef WP_PARSER_Y_H_
2 #define WP_PARSER_Y_H_
3 
4 #include <AMReX_GpuQualifiers.H>
5 #include <AMReX_GpuPrint.H>
6 #include <AMReX_REAL.H>
7 #include <AMReX_Math.H>
8 #include <AMReX_Print.H>
9 
10 #include <cstdlib>
11 #include <cstdio>
12 #include <cstring>
13 #include <iostream>
14 #include <type_traits>
15 
16 enum wp_f1_t { // Bulit-in functions with one argument
17  WP_SQRT = 1,
37 };
38 
39 enum wp_f2_t { // Built-in functions with two arguments
40  WP_POW = 1,
52 };
53 
54 enum wp_node_t {
55  WP_NUMBER = 1,
64  WP_ADD_VP, /* types below are generated by optimization */
73 };
74 
75 /* In C, the address of the first member of a struct is the same as
76  * the address of the struct itself. Because of this, all struct wp_*
77  * pointers can be passed around as struct wp_node pointer and enum
78  * wp_node_t type can be safely checked to determine their real type.
79  */
80 
81 union wp_ip {
82  int i;
83  amrex_real* p;
84 };
85 
86 union wp_vp {
87  amrex_real v;
88  union wp_ip ip;
89 };
90 
91 struct wp_node {
93  struct wp_node* l;
94  struct wp_node* r;
95  union wp_vp lvp; // After optimization, this may store left value/pointer.
96  union wp_ip rip; // this may store right pointer.
97 };
98 
99 struct wp_number {
101  amrex_real value;
102 };
103 
104 struct wp_symbol {
106  char* name;
107  union wp_ip ip;
108 };
109 
110 struct wp_f1 { /* Builtin functions with one argument */
112  struct wp_node* l;
113  enum wp_f1_t ftype;
114 };
115 
116 struct wp_f2 { /* Builtin functions with two arguments */
118  struct wp_node* l;
119  struct wp_node* r;
120  enum wp_f2_t ftype;
121 };
122 
123 /*******************************************************************/
124 
125 /* These functions are used in bison rules to generate the original
126  * AST. */
127 void wp_defexpr (struct wp_node* body);
128 struct wp_node* wp_newnumber (amrex_real d);
129 struct wp_symbol* wp_makesymbol (char* name);
130 struct wp_node* wp_newsymbol (struct wp_symbol* sym);
131 struct wp_node* wp_newnode (enum wp_node_t type, struct wp_node* l,
132  struct wp_node* r);
133 struct wp_node* wp_newf1 (enum wp_f1_t ftype, struct wp_node* l);
134 struct wp_node* wp_newf2 (enum wp_f2_t ftype, struct wp_node* l,
135  struct wp_node* r);
136 
137 void yyerror (char const *s, ...);
138 
139 /*******************************************************************/
140 
141 /* This is our struct for storing AST in a more packed way. The whole
142  * tree is stored in a contiguous chunk of memory starting from void*
143  * p_root with a size of sz_mempool.
144  */
145 struct wp_parser {
146  void* p_root;
147  void* p_free;
148  struct wp_node* ast;
149  size_t sz_mempool;
150 };
151 
152 struct wp_parser* wp_parser_new (void);
153 void wp_parser_delete (struct wp_parser* parser);
154 
155 struct wp_parser* wp_parser_dup (struct wp_parser* source);
156 struct wp_node* wp_parser_ast_dup (struct wp_parser* parser, struct wp_node* src, int move);
157 
158 void wp_parser_regvar (struct wp_parser* parser, char const* name, amrex_real* p);
159 void wp_parser_regvar_gpu (struct wp_parser* parser, char const* name, int i);
160 void wp_parser_setconst (struct wp_parser* parser, char const* name, amrex_real c);
161 
162 /* We need to walk the tree in these functions */
163 void wp_ast_optimize (struct wp_node* node);
164 size_t wp_ast_size (struct wp_node* node);
165 void wp_ast_print (struct wp_node* node);
166 void wp_ast_depth (struct wp_node* node, int* n);
167 void wp_ast_regvar (struct wp_node* node, char const* name, amrex_real* p);
168 void wp_ast_regvar_gpu (struct wp_node* node, char const* name, int i);
169 void wp_ast_setconst (struct wp_node* node, char const* name, amrex_real c);
170 
171 template <typename T, std::enable_if_t<std::is_floating_point<T>::value,int> = 0>
172 AMREX_GPU_HOST_DEVICE
173 #ifdef AMREX_USE_GPU
174 AMREX_NO_INLINE
175 #endif
176 T
178 {
179  switch (type) {
180  case WP_SQRT: return std::sqrt(a);
181  case WP_EXP: return std::exp(a);
182  case WP_LOG: return std::log(a);
183  case WP_LOG10: return std::log10(a);
184  case WP_SIN: return std::sin(a);
185  case WP_COS: return std::cos(a);
186  case WP_TAN: return std::tan(a);
187  case WP_ASIN: return std::asin(a);
188  case WP_ACOS: return std::acos(a);
189  case WP_ATAN: return std::atan(a);
190  case WP_SINH: return std::sinh(a);
191  case WP_COSH: return std::cosh(a);
192  case WP_TANH: return std::tanh(a);
193  case WP_ABS: return amrex::Math::abs(a);
194  case WP_POW_M3: return 1.0/(a*a*a);
195  case WP_POW_M2: return 1.0/(a*a);
196  case WP_POW_M1: return 1.0/a;
197  case WP_POW_P1: return a;
198  case WP_POW_P2: return a*a;
199  case WP_POW_P3: return a*a*a;
200  default:
201 #if AMREX_DEVICE_COMPILE
202  AMREX_DEVICE_PRINTF("wp_call_f1: Unknown function %d\n", type);
203 #else
204  amrex::AllPrint() << "wp_call_f1: Unknown function " << type << "\n";
205 #endif
206  return 0.0;
207  }
208 }
209 
210 
211 template <typename T, std::enable_if_t<std::is_floating_point<T>::value,int> = 0>
212 AMREX_GPU_HOST_DEVICE
213 #ifdef AMREX_USE_GPU
214 AMREX_NO_INLINE
215 #endif
216 T
217 wp_call_f2 (enum wp_f2_t type, T a, T b)
218 {
219  switch (type) {
220  case WP_POW:
221  return std::pow(a,b);
222  case WP_GT:
223  return (a > b) ? 1.0 : 0.0;
224  case WP_LT:
225  return (a < b) ? 1.0 : 0.0;
226  case WP_GEQ:
227  return (a >= b) ? 1.0 : 0.0;
228  case WP_LEQ:
229  return (a <= b) ? 1.0 : 0.0;
230  case WP_EQ:
231  return (a == b) ? 1.0 : 0.0;
232  case WP_NEQ:
233  return (a != b) ? 1.0 : 0.0;
234  case WP_AND:
235  return ((a != T(0)) && (b != T(0))) ? 1.0 : 0.0;
236  case WP_OR:
237  return ((a != T(0)) || (b != T(0))) ? 1.0 : 0.0;
238  case WP_HEAVISIDE:
239  return (a < 0.0) ? 0.0 : ((a > 0.0) ? 1.0 : b);
240  case WP_MIN:
241  return (a < b) ? a : b;
242  case WP_MAX:
243  return (a > b) ? a : b;
244  default:
245 #if AMREX_DEVICE_COMPILE
246  AMREX_DEVICE_PRINTF("wp_call_f2: Unknown function %d\n", type);
247 #else
248  amrex::AllPrint() << "wp_call_f2: Unknown function " << type << "\n";
249 #endif
250  amrex::Abort();
251  return 0.0;
252  }
253 }
254 
255 #endif
void wp_ast_regvar(struct wp_node *node, char const *name, amrex_real *p)
Definition: wp_parser_y.cpp:926
Definition: wp_parser_y.h:25
Definition: wp_parser_y.h:69
Definition: wp_parser_y.h:71
Definition: wp_parser_y.h:72
Definition: wp_parser_y.h:19
Definition: wp_parser_y.h:58
Definition: wp_parser_y.h:47
struct wp_node * wp_newf2(enum wp_f2_t ftype, struct wp_node *l, struct wp_node *r)
Definition: wp_parser_y.cpp:63
Definition: wp_parser_y.h:60
Definition: wp_parser_y.h:65
parser
Definition: run_alltests.py:107
struct wp_node * l
Definition: wp_parser_y.h:118
Definition: wp_parser_y.h:45
Definition: wp_parser_y.h:44
void wp_ast_regvar_gpu(struct wp_node *node, char const *name, int i)
Definition: wp_parser_y.cpp:981
Definition: wp_parser_y.h:30
struct wp_parser * wp_parser_new(void)
Definition: wp_parser_y.cpp:86
struct wp_node * wp_newnumber(amrex_real d)
Definition: wp_parser_y.cpp:18
Definition: wp_parser_y.h:67
Definition: wp_parser_y.h:68
Definition: wp_parser_y.h:55
Definition: wp_parser_y.h:35
Definition: wp_parser_y.h:31
Definition: wp_parser_y.h:26
Definition: wp_parser_y.h:40
Definition: wp_parser_y.h:49
void wp_parser_delete(struct wp_parser *parser)
Definition: wp_parser_y.cpp:106
Definition: wp_parser_y.h:29
Definition: wp_parser_y.h:41
struct wp_node * l
Definition: wp_parser_y.h:93
Definition: wp_parser_y.h:20
Definition: wp_parser_y.h:70
amrex_real v
Definition: wp_parser_y.h:87
void wp_ast_depth(struct wp_node *node, int *n)
Definition: wp_parser_y.cpp:862
Definition: wp_parser_y.h:43
Definition: wp_parser_y.h:145
void wp_parser_regvar(struct wp_parser *parser, char const *name, amrex_real *p)
Definition: wp_parser_y.cpp:1087
size_t sz_mempool
Definition: wp_parser_y.h:149
Definition: wp_parser_y.h:32
Definition: wp_parser_y.h:104
Definition: wp_parser_y.h:36
size_t wp_ast_size(struct wp_node *node)
Definition: wp_parser_y.cpp:144
Definition: wp_parser_y.h:56
Definition: wp_parser_y.h:46
wp_f2_t
Definition: wp_parser_y.h:39
AMREX_GPU_HOST_DEVICE T wp_call_f2(enum wp_f2_t type, T a, T b)
Definition: wp_parser_y.h:217
struct wp_node * ast
Definition: wp_parser_y.h:148
Definition: wp_parser_y.h:27
struct wp_node * wp_newf1(enum wp_f1_t ftype, struct wp_node *l)
Definition: wp_parser_y.cpp:53
Definition: wp_parser_y.h:33
struct wp_symbol * wp_makesymbol(char *name)
Definition: wp_parser_y.cpp:27
struct wp_node * wp_parser_ast_dup(struct wp_parser *parser, struct wp_node *src, int move)
Definition: wp_parser_y.cpp:200
int n
Definition: run_libensemble_on_warpx.py:68
void wp_parser_regvar_gpu(struct wp_parser *parser, char const *name, int i)
Definition: wp_parser_y.cpp:1093
Definition: wp_parser_y.h:61
amrex_real * p
Definition: wp_parser_y.h:83
struct wp_node * wp_newsymbol(struct wp_symbol *sym)
Definition: wp_parser_y.cpp:37
Definition: wp_parser_y.h:66
char * name
Definition: wp_parser_y.h:106
Definition: wp_parser_y.h:91
Definition: wp_parser_y.h:59
type
Definition: run_alltests_1node.py:67
Definition: wp_parser_y.h:62
void yyerror(char const *s,...)
Definition: wp_parser_y.cpp:74
void wp_parser_setconst(struct wp_parser *parser, char const *name, amrex_real c)
Definition: wp_parser_y.cpp:1099
struct wp_node * l
Definition: wp_parser_y.h:112
void wp_ast_setconst(struct wp_node *node, char const *name, amrex_real c)
Definition: wp_parser_y.cpp:1035
struct wp_node * r
Definition: wp_parser_y.h:119
name
Definition: run_automated.py:204
Definition: wp_parser_y.h:86
AMREX_GPU_HOST_DEVICE T wp_call_f1(enum wp_f1_t type, T a)
Definition: wp_parser_y.h:177
Definition: wp_parser_y.h:64
int i
Definition: wp_parser_y.h:82
Definition: wp_parser_y.h:24
wp_node_t
Definition: wp_parser_y.h:54
Definition: wp_parser_y.h:50
Definition: wp_parser_y.h:21
struct wp_node * r
Definition: wp_parser_y.h:94
Definition: wp_parser_y.h:48
Definition: wp_parser_y.h:116
Definition: wp_parser_y.h:57
Definition: wp_parser_y.h:42
Definition: wp_parser_y.h:63
struct wp_parser * wp_parser_dup(struct wp_parser *source)
Definition: wp_parser_y.cpp:131
Definition: wp_parser_y.h:34
void * p_free
Definition: wp_parser_y.h:147
s
Definition: plot_results.py:103
wp_f1_t
Definition: wp_parser_y.h:16
amrex_real value
Definition: wp_parser_y.h:101
void * p_root
Definition: wp_parser_y.h:146
struct wp_node * wp_newnode(enum wp_node_t type, struct wp_node *l, struct wp_node *r)
Definition: wp_parser_y.cpp:43
Definition: wp_parser_y.h:99
Definition: wp_parser_y.h:28
void wp_ast_print(struct wp_node *node)
Definition: wp_parser_y.cpp:784
Definition: wp_parser_y.h:23
void wp_defexpr(struct wp_node *body)
Definition: wp_parser_y.cpp:12
Definition: wp_parser_y.h:22
Definition: wp_parser_y.h:81
Definition: wp_parser_y.h:51
Definition: wp_parser_y.h:110
Definition: wp_parser_y.h:18
Definition: wp_parser_y.h:17
void wp_ast_optimize(struct wp_node *node)
Definition: wp_parser_y.cpp:299