Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
AMReX_IParser.H
Go to the documentation of this file.
1#ifndef AMREX_IPARSER_H_
2#define AMREX_IPARSER_H_
3
4#include <AMReX_Arena.H>
5#include <AMReX_Array.H>
6#include <AMReX_GpuDevice.H>
7#include <AMReX_IParser_Exe.H>
8#include <AMReX_Vector.H>
9
10#include <memory>
11#include <string>
12#include <set>
13
14namespace amrex {
15
16template <int N>
18{
19 template <int M=N, std::enable_if_t<M==0,int> = 0>
21 long long operator() () const noexcept
22 {
25 }
26
27 template <typename... Ts>
29 std::enable_if_t<sizeof...(Ts) == N && std::conjunction_v<std::is_integral<Ts>...>,
30 long long>
31 operator() (Ts... var) const noexcept
32 {
33 amrex::GpuArray<long long,N> l_var{var...};
35 AMREX_IF_ON_HOST((return iparser_exe_eval(m_host_executor, l_var.data());))
36 }
37
39 long long operator() (GpuArray<long long, N> const& var) const noexcept
40 {
43 }
44
46 explicit operator bool () const {
47 AMREX_IF_ON_DEVICE((return m_device_executor != nullptr;))
48 AMREX_IF_ON_HOST((return m_host_executor != nullptr;))
49 }
50
51 char* m_host_executor = nullptr;
52#ifdef AMREX_USE_GPU
53 char* m_device_executor = nullptr;
54#endif
55};
56
58{
59public:
60 IParser (std::string const& func_body);
61 IParser () = default;
62 void define (std::string const& func_body);
63
64 explicit operator bool () const;
65
66 void setConstant (std::string const& name, long long c);
67
68 void registerVariables (Vector<std::string> const& vars);
69
70 void print () const;
71
72 [[nodiscard]] int depth () const;
73 [[nodiscard]] int maxStackSize () const;
74
75 [[nodiscard]] std::string expr () const;
76
77 [[nodiscard]] std::set<std::string> symbols () const;
78
80 template <int N> [[nodiscard]] IParserExecutor<N> compile () const;
81
83 template <int N> [[nodiscard]] IParserExecutor<N> compileHost () const;
84
85private:
86
87 struct Data {
88 std::string m_expression;
89 struct amrex_iparser* m_iparser = nullptr;
90 int m_nvars = 0;
91 bool m_use_arena = true;
92 char* m_host_executor = nullptr;
93#ifdef AMREX_USE_GPU
94 char* m_device_executor = nullptr;
95#endif
97 int m_exe_size = 0;
98 Data () = default;
99 ~Data ();
100 Data (Data const&) = delete;
101 Data (Data &&) = delete;
102 Data& operator= (Data const&) = delete;
103 Data& operator= (Data &&) = delete;
104 };
105
106 std::shared_ptr<Data> m_data;
107};
108
109template <int N>
112{
113 if (m_data && m_data->m_iparser) {
114 AMREX_ASSERT(N == m_data->m_nvars);
115
116 if (!(m_data->m_host_executor)) {
117 int stack_size;
118 m_data->m_exe_size = static_cast<int>
119 (iparser_exe_size(m_data->m_iparser, m_data->m_max_stack_size,
120 stack_size));
121
122 if (m_data->m_max_stack_size > AMREX_IPARSER_STACK_SIZE) {
123 amrex::Abort("amrex::IParser: AMREX_IPARSER_STACK_SIZE, "
124 + std::to_string(AMREX_IPARSER_STACK_SIZE) + ", is too small for "
125 + m_data->m_expression);
126 }
127 if (stack_size != 0) {
128 amrex::Abort("amrex::IParser: something went wrong with iparser stack! "
129 + std::to_string(stack_size));
130 }
131
132 m_data->m_host_executor = (char*)The_Pinned_Arena()->alloc(m_data->m_exe_size);
133 if (m_data->m_host_executor == nullptr) { // Arena is not ready yet
134 m_data->m_host_executor = (char*) std::malloc(m_data->m_exe_size);
135 m_data->m_use_arena = false;
136 }
137
138 try {
139 iparser_compile(m_data->m_iparser, m_data->m_host_executor);
140 } catch (const std::runtime_error& e) {
141 throw std::runtime_error(std::string(e.what()) + " in IParser expression \""
142 + m_data->m_expression + "\"");
143 }
144 }
145
146#ifdef AMREX_USE_GPU
147 return IParserExecutor<N>{m_data->m_host_executor, m_data->m_device_executor};
148#else
150#endif
151 } else {
152 return IParserExecutor<N>{};
153 }
154}
155
156template <int N>
159{
160 auto exe = compileHost<N>();
161
162#ifdef AMREX_USE_GPU
163 if (m_data && m_data->m_iparser && !(m_data->m_device_executor)
164 && m_data->m_use_arena)
165 {
166 m_data->m_device_executor = (char*)The_Arena()->alloc(m_data->m_exe_size);
167 Gpu::htod_memcpy_async(m_data->m_device_executor, m_data->m_host_executor,
168 m_data->m_exe_size);
170 exe.m_device_executor = m_data->m_device_executor;
171 }
172#endif
173
174 return exe;
175}
176
177}
178
179#endif
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
#define AMREX_IPARSER_STACK_SIZE
Definition AMReX_IParser_Exe.H:12
virtual void * alloc(std::size_t sz)=0
Definition AMReX_IParser.H:58
void print() const
Definition AMReX_IParser.cpp:82
std::set< std::string > symbols() const
Definition AMReX_IParser.cpp:120
void setConstant(std::string const &name, long long c)
Definition AMReX_IParser.cpp:63
IParserExecutor< N > compileHost() const
This compiles for CPU only.
Definition AMReX_IParser.H:111
std::string expr() const
Definition AMReX_IParser.cpp:110
IParserExecutor< N > compile() const
This compiles for both GPU and CPU.
Definition AMReX_IParser.H:158
void registerVariables(Vector< std::string > const &vars)
Definition AMReX_IParser.cpp:71
int depth() const
Definition AMReX_IParser.cpp:90
void define(std::string const &func_body)
Definition AMReX_IParser.cpp:18
std::shared_ptr< Data > m_data
Definition AMReX_IParser.H:106
IParser()=default
int maxStackSize() const
Definition AMReX_IParser.cpp:100
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:27
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:237
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:251
Definition AMReX_Amr.cpp:49
void iparser_compile(struct amrex_iparser *parser, char *p)
Definition AMReX_IParser_Exe.H:515
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE long long iparser_exe_eval(const char *p, long long const *x)
Definition AMReX_IParser_Exe.H:231
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:656
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
Arena * The_Arena()
Definition AMReX_Arena.cpp:616
std::size_t iparser_exe_size(struct amrex_iparser *parser, int &max_stack_size, int &stack_size)
Definition AMReX_IParser_Exe.H:502
Definition AMReX_Array.H:34
Definition AMReX_IParser.H:18
char * m_device_executor
Definition AMReX_IParser.H:53
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE long long operator()() const noexcept
Definition AMReX_IParser.H:21
char * m_host_executor
Definition AMReX_IParser.H:51
Definition AMReX_IParser.H:87
Data & operator=(Data const &)=delete
int m_exe_size
Definition AMReX_IParser.H:97
Data(Data &&)=delete
~Data()
Definition AMReX_IParser.cpp:41
int m_max_stack_size
Definition AMReX_IParser.H:96
int m_nvars
Definition AMReX_IParser.H:90
char * m_device_executor
Definition AMReX_IParser.H:94
std::string m_expression
Definition AMReX_IParser.H:88
char * m_host_executor
Definition AMReX_IParser.H:92
Data(Data const &)=delete
struct amrex_iparser * m_iparser
Definition AMReX_IParser.H:89
bool m_use_arena
Definition AMReX_IParser.H:91
Definition AMReX_IParser_Y.H:153