Line data Source code
1 : //
2 : // File IpplExpressions.h
3 : // Expression Templates classes.
4 : //
5 : #ifndef IPPL_EXPRESSIONS_H
6 : #define IPPL_EXPRESSIONS_H
7 :
8 : #include <type_traits>
9 :
10 : namespace ippl {
11 : namespace detail {
12 : /*!
13 : * @file IpplExpressions.h
14 : *
15 : * Expression class which should be Kokkos-aware
16 : * need to inherit from Expression.
17 : */
18 :
19 : /*!
20 : * Basic expression class for BareField, Vector and Scalar.
21 : * Expression classes need to inherit from this with the
22 : * CRTP (curiously recursive template pattern) design
23 : * pattern.
24 : */
25 : template <typename E, size_t N = sizeof(E)>
26 : struct Expression {
27 : constexpr static unsigned dim = E::dim;
28 :
29 : /*!
30 : * Access single element of the expression
31 : */
32 73116022 : KOKKOS_INLINE_FUNCTION auto operator[](size_t i) const {
33 73116022 : return static_cast<const E&>(*this)[i];
34 : }
35 : };
36 :
37 : /*!
38 : * This expression is only used to allocate
39 : * enough memory for the kernel on the device.
40 : * It is instantiated in the assignment operator
41 : * of the BareField class.
42 : */
43 : template <typename E, size_t N = sizeof(E)>
44 : struct CapturedExpression {
45 : constexpr static unsigned dim = E::dim;
46 :
47 : template <typename... Args>
48 5019968 : KOKKOS_INLINE_FUNCTION auto operator()(Args... args) const {
49 : static_assert(sizeof...(Args) == dim || dim == 0);
50 5019968 : return reinterpret_cast<const E&>(*this)(args...);
51 : }
52 :
53 : char buffer[N];
54 : };
55 :
56 : /*!
57 : * Expression for intrinsic data types. They are both regular expressions
58 : * and field expressions.
59 : */
60 : template <typename T>
61 : struct Scalar : public Expression<Scalar<T>, sizeof(T)> {
62 : typedef T value_type;
63 : constexpr static unsigned dim = 0;
64 :
65 : KOKKOS_FUNCTION
66 20525504 : Scalar(value_type val)
67 20525504 : : val_m(val) {}
68 :
69 : /*!
70 : * Access the scalar value with single index.
71 : * This is used for binary operations between
72 : * Scalar and Vector.
73 : */
74 114282334 : KOKKOS_INLINE_FUNCTION value_type operator[](size_t /*i*/) const { return val_m; }
75 :
76 : /*!
77 : * Access the scalar value with multiple indices.
78 : * This is used for binary operations between
79 : * Scalar and BareField, Scalar and BareField,
80 : * and Scalar and Field.
81 : */
82 : template <typename... Args>
83 2863872 : KOKKOS_INLINE_FUNCTION auto operator()(Args... /*args*/) const {
84 2863872 : return val_m;
85 : }
86 :
87 : private:
88 : value_type val_m;
89 : };
90 :
91 : template <typename T>
92 : struct isExpression : std::false_type {};
93 :
94 : template <typename T>
95 : struct isExpression<Scalar<T>> : std::true_type {};
96 :
97 : } // namespace detail
98 : } // namespace ippl
99 :
100 : #include "Expression/IpplOperations.h"
101 :
102 : #endif
|