1#ifndef AMREX_IPARSER_H_
2#define AMREX_IPARSER_H_
12#include <AMReX_IParser_Exe.H>
34 template <
int M=N, std::enable_if_t<M==0,
int> = 0>
47 template <
typename... Ts>
49 std::enable_if_t<
sizeof...(Ts) == N && std::conjunction_v<std::is_integral<Ts>...>,
51 operator() (Ts... var)
const noexcept
71 explicit operator bool ()
const {
97 IParser (std::string
const& func_body);
104 void define (std::string
const& func_body);
107 explicit operator bool ()
const;
114 void setConstant (std::string
const& name,
long long c);
130 [[nodiscard]]
int depth ()
const;
141 [[nodiscard]] std::string
expr ()
const;
147 [[nodiscard]] std::set<std::string>
symbols ()
const;
165 std::string m_expression;
166 struct amrex_iparser* m_iparser =
nullptr;
168 bool m_use_arena =
true;
169 char* m_host_executor =
nullptr;
171 char* m_device_executor =
nullptr;
173 int m_max_stack_size = 0;
177 Data (Data
const&) =
delete;
178 Data (Data &&) =
delete;
179 Data& operator= (Data
const&) =
delete;
180 Data& operator= (Data &&) =
delete;
184 std::shared_ptr<Data> m_data;
191 if (m_data && m_data->m_iparser) {
194 if (!(m_data->m_host_executor)) {
196 m_data->m_exe_size =
static_cast<int>
197 (iparser_exe_size(m_data->m_iparser, m_data->m_max_stack_size,
200 if (m_data->m_max_stack_size > AMREX_IPARSER_STACK_SIZE) {
201 amrex::Abort(
"amrex::IParser: AMREX_IPARSER_STACK_SIZE, "
202 + std::to_string(AMREX_IPARSER_STACK_SIZE) +
", is too small for "
203 + m_data->m_expression);
205 if (stack_size != 0) {
206 amrex::Abort(
"amrex::IParser: something went wrong with iparser stack! "
207 + std::to_string(stack_size));
211 if (m_data->m_host_executor ==
nullptr) {
212 m_data->m_host_executor = (
char*) std::malloc(m_data->m_exe_size);
213 m_data->m_use_arena =
false;
217 iparser_compile(m_data->m_iparser, m_data->m_host_executor);
218 }
catch (
const std::runtime_error& e) {
219 throw std::runtime_error(std::string(e.what()) +
" in IParser expression \""
220 + m_data->m_expression +
"\"");
238 auto exe = compileHost<N>();
241 if (m_data && m_data->m_iparser && !(m_data->m_device_executor)
242 && m_data->m_use_arena)
244 m_data->m_device_executor = (
char*)
The_Arena()->
alloc(m_data->m_exe_size);
248 exe.m_device_executor = m_data->m_device_executor;
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#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
virtual void * alloc(std::size_t sz)=0
Integer-only variant of amrex::Parser.
Definition AMReX_IParser.H:91
void print() const
Print the parse tree to stdout for debugging.
Definition AMReX_IParser.cpp:86
std::set< std::string > symbols() const
Return the set of symbols referenced by the expression.
Definition AMReX_IParser.cpp:124
void setConstant(std::string const &name, long long c)
Bind a named integer constant.
Definition AMReX_IParser.cpp:67
IParserExecutor< N > compileHost() const
Compile the expression into a host-only executor.
Definition AMReX_IParser.H:189
std::string expr() const
Return the sanitized expression string.
Definition AMReX_IParser.cpp:114
IParserExecutor< N > compile() const
Compile the expression into a host/device IParserExecutor.
Definition AMReX_IParser.H:236
void registerVariables(Vector< std::string > const &vars)
Register the ordered list of integer variables referenced by the expression.
Definition AMReX_IParser.cpp:75
int depth() const
Return the maximum parse-tree depth.
Definition AMReX_IParser.cpp:94
void define(std::string const &func_body)
Parse and own a new integer expression, replacing any previous state.
Definition AMReX_IParser.cpp:18
IParser()=default
Default-construct; call define() before compile().
int maxStackSize() const
Return the maximum parser stack usage.
Definition AMReX_IParser.cpp:104
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:845
Arena * The_Arena()
Definition AMReX_Arena.cpp:805
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:421
Definition AMReX_Amr.cpp:49
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:240
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:43
Callable wrapper around an integer parser expression with N variables.
Definition AMReX_IParser.H:30
char * m_device_executor
Pointer to device bytecode (if copied).
Definition AMReX_IParser.H:78
__host__ __device__ long long operator()() const noexcept
Evaluate constant expressions (N == 0) and return the integer result.
Definition AMReX_IParser.H:36
char * m_host_executor
Pointer to host bytecode.
Definition AMReX_IParser.H:76