aboutsummaryrefslogtreecommitdiff
path: root/Source/Parser/wp_parser_c.h
blob: 75091704b9d59f50dd45b1015a591a8d93211a9d (plain) (blame)
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
#ifndef WP_PARSER_C_H_
#define WP_PARSER_C_H_

#include "wp_parser_y.h"

#include <AMReX_GpuQualifiers.H>
#include <AMReX_GpuPrint.H>
#include <AMReX_Extension.H>
#include <AMReX_Math.H>
#include <AMReX_REAL.H>
#include <AMReX_Print.H>
#include <AMReX.H>

#include <cassert>
#include <cmath>
#include <set>
#include <string>
#include <type_traits>

struct wp_parser* wp_c_parser_new (char const* function_body);

#ifdef AMREX_USE_GPU

template <int Depth, std::enable_if_t<(Depth<WARPX_PARSER_DEPTH), int> = 0>
AMREX_GPU_DEVICE AMREX_NO_INLINE
void wp_ast_update_device_ptr (struct wp_node* node, char* droot, char* hroot)
{
    switch (node->type)
    {
    case WP_NUMBER:
        break;
    case WP_SYMBOL:
    {
        auto p = (struct wp_symbol*)node;
        p->name = droot + (p->name - hroot);
        break;
    }
    case WP_ADD:
    case WP_SUB:
    case WP_MUL:
    case WP_DIV:
    {
        node->l = (wp_node*)(droot + ((char*)node->l - hroot));
        node->r = (wp_node*)(droot + ((char*)node->r - hroot));
        wp_ast_update_device_ptr<Depth+1>(node->l, droot, hroot);
        wp_ast_update_device_ptr<Depth+1>(node->r, droot, hroot);
        break;
    }
    case WP_NEG:
    {
        node->l = (wp_node*)(droot + ((char*)node->l - hroot));
        wp_ast_update_device_ptr<Depth+1>(node->l, droot, hroot);
        break;
    }
    case WP_F1:
    {
        auto p = (struct wp_f1*)node;
        p->l = (wp_node*)(droot + ((char*)p->l - hroot));
        wp_ast_update_device_ptr<Depth+1>(p->l, droot, hroot);
        break;
    }
    case WP_F2:
    {
        auto p = (struct wp_f2*)node;
        p->l = (wp_node*)(droot + ((char*)p->l - hroot));
        p->r = (wp_node*)(droot + ((char*)p->r - hroot));
        wp_ast_update_device_ptr<Depth+1>(p->l, droot, hroot);
        wp_ast_update_device_ptr<Depth+1>(p->r, droot, hroot);
        break;
    }
    case WP_ADD_VP:
    case WP_ADD_PP:
    case WP_SUB_VP:
    case WP_SUB_PP:
    case WP_MUL_VP:
    case WP_MUL_PP:
    case WP_DIV_VP:
    case WP_DIV_PP:
    case WP_NEG_P:
    {
        // No need to update node->l and node->r that contain a string for
        // variable name because we don't need them for device code.
        break;
    }
    default:
    {
#if AMREX_DEVICE_COMPILE
        AMREX_DEVICE_PRINTF("wp_ast_update_device_ptr: unknown node type %d\n",
                            static_cast<int>(node->type));
        amrex::Abort();
#endif
    }
    }
}

template <int Depth, std::enable_if_t<Depth == WARPX_PARSER_DEPTH,int> = 0>
AMREX_GPU_DEVICE AMREX_NO_INLINE
void wp_ast_update_device_ptr (struct wp_node*, char*, char*)
{
#if AMREX_DEVICE_COMPILE
    AMREX_DEVICE_PRINTF("wp_ast_update_device_ptr: WARPX_PARSER_DEPTH %d not big enough\n",
                        WARPX_PARSER_DEPTH);
    amrex::Abort();
#endif
}

#endif

template <int Depth, std::enable_if_t<(Depth<WARPX_PARSER_DEPTH), int> = 0>
AMREX_GPU_HOST_DEVICE
#ifdef AMREX_USE_GPU
AMREX_NO_INLINE
#endif
amrex::Real
wp_ast_eval (struct wp_node* node, amrex::Real const* x)
{
    amrex::Real result = 0.0;

    switch (node->type)
    {
    case WP_NUMBER:
    {
        result = ((struct wp_number*)node)->value;
        break;
    }
    case WP_SYMBOL:
    {
#if AMREX_DEVICE_COMPILE
        int i =((struct wp_symbol*)node)->ip.i;
        result = x[i];
#else
        result = *(((struct wp_symbol*)node)->ip.p);
#endif
        break;
    }
    case WP_ADD:
    {
        result = wp_ast_eval<Depth+1>(node->l,x) + wp_ast_eval<Depth+1>(node->r,x);
        break;
    }
    case WP_SUB:
    {
        result = wp_ast_eval<Depth+1>(node->l,x) - wp_ast_eval<Depth+1>(node->r,x);
        break;
    }
    case WP_MUL:
    {
        result = wp_ast_eval<Depth+1>(node->l,x) * wp_ast_eval<Depth+1>(node->r,x);
        break;
    }
    case WP_DIV:
    {
        result = wp_ast_eval<Depth+1>(node->l,x) / wp_ast_eval<Depth+1>(node->r,x);
        break;
    }
    case WP_NEG:
    {
        result = -wp_ast_eval<Depth+1>(node->l,x);
        break;
    }
    case WP_F1:
    {
        result = wp_call_f1(((struct wp_f1*)node)->ftype,
                wp_ast_eval<Depth+1>(((struct wp_f1*)node)->l,x));
        break;
    }
    case WP_F2:
    {
        result = wp_call_f2(((struct wp_f2*)node)->ftype,
                wp_ast_eval<Depth+1>(((struct wp_f2*)node)->l,x),
                wp_ast_eval<Depth+1>(((struct wp_f2*)node)->r,x));
        break;
    }
    case WP_ADD_VP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->rip.i;
        result = node->lvp.v + x[i];
#else
        result = node->lvp.v + *(node->rip.p);
#endif
        break;
    }
    case WP_ADD_PP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->lvp.ip.i;
        int j = node->rip.i;
        result = x[i] + x[j];
#else
        result = *(node->lvp.ip.p) + *(node->rip.p);
#endif
        break;
    }
    case WP_SUB_VP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->rip.i;
        result = node->lvp.v - x[i];
#else
        result = node->lvp.v - *(node->rip.p);
#endif
        break;
    }
    case WP_SUB_PP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->lvp.ip.i;
        int j = node->rip.i;
        result = x[i] - x[j];
#else
        result = *(node->lvp.ip.p) - *(node->rip.p);
#endif
        break;
    }
    case WP_MUL_VP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->rip.i;
        result = node->lvp.v * x[i];
#else
        result = node->lvp.v * *(node->rip.p);
#endif
        break;
    }
    case WP_MUL_PP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->lvp.ip.i;
        int j = node->rip.i;
        result = x[i] * x[j];
#else
        result = *(node->lvp.ip.p) * *(node->rip.p);
#endif
        break;
    }
    case WP_DIV_VP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->rip.i;
        result = node->lvp.v / x[i];
#else
        result = node->lvp.v / *(node->rip.p);
#endif
        break;
    }
    case WP_DIV_PP:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->lvp.ip.i;
        int j = node->rip.i;
        result = x[i] / x[j];
#else
        result = *(node->lvp.ip.p) / *(node->rip.p);
#endif
        break;
    }
    case WP_NEG_P:
    {
#if AMREX_DEVICE_COMPILE
        int i = node->rip.i;
        result = -x[i];
#else
        result = -*(node->lvp.ip.p);
#endif
        break;
    }
    default:
    {
#if AMREX_DEVICE_COMPILE
        AMREX_DEVICE_PRINTF("wp_ast_eval: unknown node type %d\n", node->type);
#else
        amrex::AllPrint() << "wp_ast_eval: unknown node type " << node->type << "\n";
#endif
    }
    }

    // check for NaN & Infs, e.g. if individual terms invalidate the whole
    // expression in piecewise constructed functions, etc.
    if
#if __cplusplus >= 201703L
    constexpr
#endif
    (Depth == 0)
    {
        if (!amrex::Math::isfinite(result))
        {
            constexpr char const * const err_msg =
                "wp_ast_eval: function parser encountered an invalid result value (NaN or Inf)!";
#if AMREX_DEVICE_COMPILE
            AMREX_DEVICE_PRINTF("%s\n", err_msg);
#else
            amrex::AllPrint() << err_msg << "\n";
#endif
            amrex::Abort(err_msg);
        }
    }

    return result;
}

template <int Depth, std::enable_if_t<Depth == WARPX_PARSER_DEPTH,int> = 0>
AMREX_GPU_HOST_DEVICE
#ifdef AMREX_USE_GPU
AMREX_NO_INLINE
#endif
amrex::Real
wp_ast_eval (struct wp_node* node, amrex::Real const* x)
{
#if AMREX_DEVICE_COMPILE
    AMREX_DEVICE_PRINTF("wp_ast_eval: WARPX_PARSER_DEPTH %d not big enough\n",
                        WARPX_PARSER_DEPTH);
#else
    amrex::AllPrint() << "wp_ast_eval: WARPX_PARSER_DEPTH" << WARPX_PARSER_DEPTH
                      << "not big enough\n";
#endif
    amrex::ignore_unused(node, x);
    return 0.;
}

inline
void
wp_ast_get_symbols (struct wp_node* node, std::set<std::string>& symbols)
{
    switch (node->type)
    {
    case WP_NUMBER:
        break;
    case WP_SYMBOL:
        symbols.emplace(((struct wp_symbol*)node)->name);
        break;
    case WP_ADD:
    case WP_SUB:
    case WP_MUL:
    case WP_DIV:
    case WP_ADD_PP:
    case WP_SUB_PP:
    case WP_MUL_PP:
    case WP_DIV_PP:
        wp_ast_get_symbols(node->l, symbols);
        wp_ast_get_symbols(node->r, symbols);
        break;
    case WP_NEG:
    case WP_NEG_P:
        wp_ast_get_symbols(node->l, symbols);
        break;
    case WP_F1:
        wp_ast_get_symbols(((struct wp_f1*)node)->l, symbols);
        break;
    case WP_F2:
        wp_ast_get_symbols(((struct wp_f2*)node)->l, symbols);
        wp_ast_get_symbols(((struct wp_f2*)node)->r, symbols);
        break;
    case WP_ADD_VP:
    case WP_SUB_VP:
    case WP_MUL_VP:
    case WP_DIV_VP:
        wp_ast_get_symbols(node->r, symbols);
        break;
    default:
        amrex::AllPrint() << "wp_ast_get_symbols: unknown node type " << node->type << "\n";
    }
}

#endif