Discontinuous Galerkin Library
#include "dg/algorithm.h"
subroutines.h
Go to the documentation of this file.
1#pragma once
3#include "dg/backend/config.h"
4
5namespace dg{
11{
12 template<class T>
13 DG_DEVICE T operator()(T x)const{return x;}
14};
15
16
19
21struct equals
22{
23 template< class T1, class T2>
24DG_DEVICE void operator()( T1 x, T2& y) const
25 {
26 y = x;
27 }
28};
31{
32 template< class T1, class T2>
33DG_DEVICE void operator()( T1 x, T2& y) const
34 {
35 y += x;
36 }
37};
40{
41 template< class T1, class T2>
42DG_DEVICE void operator()( T1 x, T2& y) const
43 {
44 y -= x;
45 }
46};
49{
50 template< class T1, class T2>
51DG_DEVICE void operator()( T1 x, T2& y) const
52 {
53 y *= x;
54 }
55};
58{
59 template< class T1, class T2>
60DG_DEVICE void operator()( T1 x, T2& y) const
61 {
62 y /= x;
63 }
64};
66
69
71struct divides
72{
73 template< class T1, class T2>
74DG_DEVICE T1 operator()( T1 x1, T2 x2) const
75 {
76 return x1/x2;
77 }
78};
79
81struct Sum
82{
84 template< class T1, class ...Ts>
85DG_DEVICE T1 operator()( T1 x, Ts... rest) const
86 {
87 T1 tmp = T1{0};
88 sum( tmp, x, rest...);
89 return tmp;
90 }
91 private:
92 template<class T, class ...Ts>
93DG_DEVICE void sum( T& tmp, T x, Ts... rest) const
94 {
95 tmp += x;
96 sum( tmp, rest...);
97 }
98
99 template<class T>
100DG_DEVICE void sum( T& tmp, T x) const
101 {
102 tmp += x;
103 }
104};
105
108{
110 template< class T, class ...Ts>
111DG_DEVICE T operator()( T a, T x, Ts... rest) const
112 {
113 T tmp = T{0};
114 sum( tmp, a, x, rest...);
115 return tmp;
116 }
117 private:
118 template<class T, class ...Ts>
119DG_DEVICE void sum( T& tmp, T alpha, T x, Ts... rest) const
120 {
121 tmp = DG_FMA( alpha, x, tmp);
122 sum( tmp, rest...);
123 }
124
125 template<class T>
126DG_DEVICE void sum( T& tmp, T alpha, T x) const
127 {
128 tmp = DG_FMA(alpha, x, tmp);
129 }
130};
133{
135 template< class T1, class ...Ts>
136DG_DEVICE T1 operator()( T1 a, T1 x1, T1 y1, Ts... rest) const
137 {
138 T1 tmp = T1{0};
139 sum( tmp, a, x1, y1, rest...);
140 return tmp;
141 }
142 private:
143 template<class T, class ...Ts>
144DG_DEVICE void sum( T& tmp, T alpha, T x, T y, Ts... rest) const
145 {
146 tmp = DG_FMA( alpha*x, y, tmp);
147 sum( tmp, rest...);
148 }
149
150 template<class T>
151DG_DEVICE void sum( T& tmp, T alpha, T x, T y) const
152 {
153 tmp = DG_FMA(alpha*x, y, tmp);
154 }
155};
156
158
161
164{
166 template< class T1, class ...Ts>
167DG_DEVICE void operator()( T1& y, T1& yt, T1 b, T1 bt, Ts... rest) const
168 {
169 y = b*y;
170 yt = bt*yt;
171 sum( y, yt, rest...);
172 }
173 private:
174 template< class T1, class ...Ts>
175DG_DEVICE void sum( T1& y_1, T1& yt_1, T1 b, T1 bt, T1 k, Ts... rest) const
176 {
177 y_1 = DG_FMA( b, k, y_1);
178 yt_1 = DG_FMA( bt, k, yt_1);
179 sum( y_1, yt_1, rest...);
180 }
181
182 template< class T1>
183DG_DEVICE void sum( T1& y_1, T1& yt_1, T1 b, T1 bt, T1 k) const
184 {
185 y_1 = DG_FMA( b, k, y_1);
186 yt_1 = DG_FMA( bt, k, yt_1);
187 }
188};
189
191template<class BinarySub, class Functor>
193{
194 Evaluate( BinarySub sub, Functor g):
195 m_f( sub),
196 m_g( g) {}
197#ifdef __CUDACC__
198// cuda compiler spits out a lot of warnings if
199// e.g. dg::transform is used on host vectors with host function
200// hd_warning_disable is unfortunately undocumented, but let's try
201// If it ever causes trouble we can remove it again
202// it just suppresses compiler warnings:
203// https://stackoverflow.com/questions/55481202/how-to-disable-cuda-host-device-warning-for-just-one-function
204#pragma hd_warning_disable
205#endif
206 template< class T, class... Ts>
207DG_DEVICE void operator() ( T& y, Ts... xs){
208 m_f(m_g(xs...), y);
209 }
210 private:
211 BinarySub m_f;
212 Functor m_g;
213};
214
216template<class T>
217struct Scal
218{
219 Scal( T a): m_a(a){}
221 void operator()( T& y)const{
222 y *= m_a;
223 }
224 private:
225 T m_a;
226};
227
229template<class T>
230struct Plus
231{
232 Plus( T a): m_a(a){}
234 void operator()( T& y) const{
235 y += m_a;
236 }
237 private:
238 T m_a;
239};
240
243template<class T>
244struct Axpby
245{
246 Axpby( T a, T b): m_a(a), m_b(b){}
248 void operator()( T x, T& y)const {
249 T temp = y*m_b;
250 y = DG_FMA( m_a, x, temp);
251 }
252 private:
253 T m_a, m_b;
254};
257template<class T>
258struct AxyPby
259{
260 AxyPby( T a, T b): m_a(a), m_b(b){}
262 void operator()( T x, T& y)const {
263 T temp = y*m_b;
264 y = DG_FMA( m_a*x, y, temp);
265 }
266 private:
267 T m_a, m_b;
268};
269
271template<class T>
273{
274 Axpbypgz( T a, T b, T g): m_a(a), m_b(b), m_g(g){}
276 void operator()( T x, T y, T& z)const{
277 T temp = z*m_g;
278 temp = DG_FMA( m_a, x, temp);
279 temp = DG_FMA( m_b, y, temp);
280 z = temp;
281 }
282 private:
283 T m_a, m_b, m_g;
284};
285
287template<class T>
289{
290 PointwiseDot( T a, T b, T g = (T)0): m_a(a), m_b(b), m_g(g) {}
292DG_DEVICE void operator()( T x, T y, T& z)const{
293 T temp = z*m_b;
294 z = DG_FMA( m_a*x, y, temp);
295 }
298 void operator()( T x1, T x2, T x3, T& y)const{
299 T temp = y*m_b;
300 y = DG_FMA( m_a*x1, x2*x3, temp);
301 }
304 void operator()( T x1, T y1, T x2, T y2, T& z)const{
305 T temp = z*m_g;
306 temp = DG_FMA( m_a*x1, y1, temp);
307 temp = DG_FMA( m_b*x2, y2, temp);
308 z = temp;
309 }
310 private:
311 T m_a, m_b, m_g;
312};
313
315template<class T>
317{
318 PointwiseDivide( T a, T b): m_a(a), m_b(b){}
321 void operator()( T y, T& z)const{
322 T temp = z*m_b;
323 z = DG_FMA( m_a, z/y, temp);
324 }
326 void operator()( T x, T y, T& z)const{
327 T temp = z*m_b;
328 z = DG_FMA( m_a, x/y, temp);
329 }
330 private:
331 T m_a, m_b;
332};
334
336namespace detail
337{
338template<class F, class G>
339struct Compose
340{
341 Compose( F f, G g):m_f(f), m_g(g){}
342 template<class ...Xs>
343 auto operator() ( Xs&& ... xs){
344 return m_f(m_g(std::forward<Xs>(xs)...));
345 }
346 template<class ...Xs>
347 auto operator() ( Xs&& ... xs) const {
348 return m_f(m_g(std::forward<Xs>(xs)...));
349 }
350 private:
351 F m_f;
352 G m_g;
353};
354}//namespace detail
356
359
379template <class UnaryOp, class Functor>
380auto compose( UnaryOp f, Functor g) {
381 return detail::Compose<UnaryOp,Functor>( f, g);
382 //a C++-14 way of generating a generic lambda with a parameter pack. Taken from:
383 //https://stackoverflow.com/questions/19071268/function-composition-in-c-c11
384 //return [f,g](auto&&... xs){ return f(g(std::forward<decltype(xs)>(xs)...));};
385}
391template <class UnaryOp, typename... Functors>
392auto compose(UnaryOp f0, Functors... fs) {
393 return compose( f0 , compose(fs...));
394}
396
397}//namespace dg
Some utility functions for the dg::evaluate routines.
#define DG_DEVICE
Expands to __host__ __device__ if compiled with nvcc else is empty.
Definition: functions.h:9
auto compose(UnaryOp f, Functor g)
Create Composition functor .
Definition: subroutines.h:380
@ z
z direction
@ y
y direction
@ x
x direction
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Definition: subroutines.h:245
DG_DEVICE void operator()(T x, T &y) const
Definition: subroutines.h:248
Axpby(T a, T b)
Definition: subroutines.h:246
Definition: subroutines.h:273
Axpbypgz(T a, T b, T g)
Definition: subroutines.h:274
DG_DEVICE void operator()(T x, T y, T &z) const
Definition: subroutines.h:276
Definition: subroutines.h:259
DG_DEVICE void operator()(T x, T &y) const
Definition: subroutines.h:262
AxyPby(T a, T b)
Definition: subroutines.h:260
Definition: subroutines.h:164
DG_DEVICE void operator()(T1 &y, T1 &yt, T1 b, T1 bt, Ts... rest) const
Definition: subroutines.h:167
Definition: subroutines.h:193
DG_DEVICE void operator()(T &y, Ts... xs)
Definition: subroutines.h:207
Evaluate(BinarySub sub, Functor g)
Definition: subroutines.h:194
Definition: subroutines.h:11
DG_DEVICE T operator()(T x) const
Definition: subroutines.h:13
Definition: subroutines.h:108
DG_DEVICE T operator()(T a, T x, Ts... rest) const
Definition: subroutines.h:111
Definition: subroutines.h:231
DG_DEVICE void operator()(T &y) const
Definition: subroutines.h:234
Plus(T a)
Definition: subroutines.h:232
Definition: subroutines.h:317
DG_DEVICE void operator()(T x, T y, T &z) const
Definition: subroutines.h:326
DG_DEVICE void operator()(T y, T &z) const
Definition: subroutines.h:321
PointwiseDivide(T a, T b)
Definition: subroutines.h:318
Definition: subroutines.h:289
DG_DEVICE void operator()(T x1, T x2, T x3, T &y) const
Definition: subroutines.h:298
DG_DEVICE void operator()(T x, T y, T &z) const
Definition: subroutines.h:292
PointwiseDot(T a, T b, T g=(T) 0)
Definition: subroutines.h:290
DG_DEVICE void operator()(T x1, T y1, T x2, T y2, T &z) const
Definition: subroutines.h:304
Definition: subroutines.h:218
DG_DEVICE void operator()(T &y) const
Definition: subroutines.h:221
Scal(T a)
Definition: subroutines.h:219
Definition: subroutines.h:82
DG_DEVICE T1 operator()(T1 x, Ts... rest) const
Definition: subroutines.h:85
Definition: subroutines.h:133
DG_DEVICE T1 operator()(T1 a, T1 x1, T1 y1, Ts... rest) const
Definition: subroutines.h:136
Definition: subroutines.h:58
DG_DEVICE void operator()(T1 x, T2 &y) const
Definition: subroutines.h:60
Definition: subroutines.h:72
DG_DEVICE T1 operator()(T1 x1, T2 x2) const
Definition: subroutines.h:74
Definition: subroutines.h:22
DG_DEVICE void operator()(T1 x, T2 &y) const
Definition: subroutines.h:24
Definition: subroutines.h:40
DG_DEVICE void operator()(T1 x, T2 &y) const
Definition: subroutines.h:42
Definition: subroutines.h:31
DG_DEVICE void operator()(T1 x, T2 &y) const
Definition: subroutines.h:33
Definition: subroutines.h:49
DG_DEVICE void operator()(T1 x, T2 &y) const
Definition: subroutines.h:51