WarpX
WarpXParserWrapper.H
Go to the documentation of this file.
1 /* Copyright 2020 Revathi Jambunathan, Weiqun Zhang
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 #ifndef WARPX_PARSER_WRAPPER_H_
8 #define WARPX_PARSER_WRAPPER_H_
9 
10 #include "Parser/WarpXParser.H"
11 #include "Parser/GpuParser.H"
12 
13 #include <AMReX_Gpu.H>
14 
15 #include <memory>
16 
23 template <int N>
25 {
26  template <typename... Ts>
27  AMREX_GPU_HOST_DEVICE
28  std::enable_if_t<sizeof...(Ts) == N
29  and amrex::Same<amrex::Real,Ts...>::value,
30  amrex::Real>
31  operator() (Ts... var) const noexcept
32  {
33 #ifdef AMREX_USE_GPU
34 #if AMREX_DEVICE_COMPILE
35 // WarpX compiled for GPU, function compiled for __device__
36  amrex::GpuArray<amrex::Real,N> l_var{var...};
37  return wp_ast_eval<0>(m_gpu_parser_ast, l_var.data());
38 #else
39 // WarpX compiled for GPU, function compiled for __host__
40  return (*m_gpu_parser)(var...);
41 #endif
42 #else
43 // WarpX compiled for CPU
44  return (*m_gpu_parser)(var...);
45 #endif
46  }
47 
48 #ifdef AMREX_USE_GPU
49  struct wp_node * m_gpu_parser_ast = nullptr;
50 #endif
51  GpuParser<N> const* m_gpu_parser = nullptr;
52 };
53 
62 template <int N>
64  : public GpuParser<N>
65 {
67 
68  ParserWrapper (ParserWrapper<N> const&) = delete;
69  void operator= (ParserWrapper<N> const&) = delete;
70 
72 
73  void operator() () const = delete; // Hide GpuParser<N>::operator()
74 
76 #ifdef AMREX_USE_GPU
77  return HostDeviceParser<N>{this->m_gpu_parser_ast, static_cast<GpuParser<N> const*>(this)};
78 #else
79  return HostDeviceParser<N>{static_cast<GpuParser<N> const*>(this)};
80 #endif
81  }
82 };
83 
84 template <int N>
86 {
87  return parser_wrapper ? parser_wrapper->getParser() : HostDeviceParser<N>{};
88 }
89 
90 template <int N>
91 HostDeviceParser<N> getParser (std::unique_ptr<ParserWrapper<N> > const& parser_wrapper)
92 {
93  return parser_wrapper ? parser_wrapper->getParser() : HostDeviceParser<N>{};
94 }
95 
96 #endif
~ParserWrapper()
Definition: WarpXParserWrapper.H:71
The ParserWrapper struct is constructed to safely use the GpuParser class that can essentially be tho...
Definition: WarpXParserWrapper.H:63
HostDeviceParser< N > getParser(ParserWrapper< N > const *parser_wrapper)
Definition: WarpXParserWrapper.H:85
HostDeviceParser< N > getParser() const
Definition: WarpXParserWrapper.H:75
AMREX_GPU_HOST_DEVICE std::enable_if_t< sizeof...(Ts)==N and amrex::Same< amrex::Real, Ts... >::value, amrex::Real > operator()(Ts... var) const noexcept
Definition: WarpXParserWrapper.H:31
Definition: wp_parser_y.h:91
void clear()
Definition: GpuParser.H:163
GpuParser< N > const * m_gpu_parser
Definition: WarpXParserWrapper.H:51
Definition: GpuParser.H:23
The HostDeviceParser struct is non-owning and is suitable for being value captured by device lamba...
Definition: WarpXParserWrapper.H:24