Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_MP_Vector_SFS_ops.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Sacado_cmath.hpp"
43 #include <ostream> // for std::ostream
44 
45 #ifdef __CUDACC__
46 #include <math_functions.h>
47 #endif
48 
49 #define MP_UNARYOP_MACRO(OPNAME,OPER) \
50 namespace Sacado { \
51  namespace MP { \
52  \
53  template <typename O, typename T, int N, typename D> \
54  KOKKOS_INLINE_FUNCTION \
55  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
56  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a) \
57  { \
58  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
59  for (O i=0; i<a.size(); ++i) \
60  c.fastAccessCoeff(i) = OPER(a.fastAccessCoeff(i)); \
61  return c; \
62  } \
63  \
64  template <typename O, typename T, int N, typename D> \
65  KOKKOS_INLINE_FUNCTION \
66  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
67  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a) \
68  { \
69  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
70  for (O i=0; i<a.size(); ++i) \
71  c.fastAccessCoeff(i) = OPER(a.fastAccessCoeff(i)); \
72  return c; \
73  } \
74  \
75  } \
76 }
77 
78 MP_UNARYOP_MACRO(operator+, +)
79 MP_UNARYOP_MACRO(operator-, -)
99 
100 #undef MP_UNARYOP_MACRO
101 
102 #define MP_BINARYOP_MACRO(OPNAME,OPER) \
103 namespace Sacado { \
104  namespace MP { \
105  \
106  template <typename O, typename T, int N, typename D> \
107  KOKKOS_INLINE_FUNCTION \
108  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
109  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
110  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
111  { \
112  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
113  for (O i=0; i<a.size(); ++i) \
114  c.fastAccessCoeff(i) = \
115  a.fastAccessCoeff(i) OPER b.fastAccessCoeff(i); \
116  return c; \
117  } \
118  \
119  template <typename O, typename T, int N, typename D> \
120  KOKKOS_INLINE_FUNCTION \
121  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
122  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
123  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
124  { \
125  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
126  for (O i=0; i<a.size(); ++i) \
127  c.fastAccessCoeff(i) = \
128  a.fastAccessCoeff(i) OPER b.fastAccessCoeff(i); \
129  return c; \
130  } \
131  \
132  template <typename O, typename T, int N, typename D> \
133  KOKKOS_INLINE_FUNCTION \
134  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
135  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
136  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
137  { \
138  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
139  for (O i=0; i<a.size(); ++i) \
140  c.fastAccessCoeff(i) = \
141  a.fastAccessCoeff(i) OPER b.fastAccessCoeff(i); \
142  return c; \
143  } \
144  \
145  template <typename O, typename T, int N, typename D> \
146  KOKKOS_INLINE_FUNCTION \
147  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
148  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
149  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
150  { \
151  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
152  for (O i=0; i<a.size(); ++i) \
153  c.fastAccessCoeff(i) = \
154  a.fastAccessCoeff(i) OPER b.fastAccessCoeff(i); \
155  return c; \
156  } \
157  \
158  template <typename O, typename T, int N, typename D> \
159  KOKKOS_INLINE_FUNCTION \
160  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
161  OPNAME (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
162  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
163  { \
164  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
165  for (O i=0; i<b.size(); ++i) \
166  c.fastAccessCoeff(i) = \
167  a OPER b.fastAccessCoeff(i); \
168  return c; \
169  } \
170  \
171  template <typename O, typename T, int N, typename D> \
172  KOKKOS_INLINE_FUNCTION \
173  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
174  OPNAME (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
175  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
176  { \
177  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
178  for (O i=0; i<b.size(); ++i) \
179  c.fastAccessCoeff(i) = \
180  a OPER b.fastAccessCoeff(i); \
181  return c; \
182  } \
183  \
184  template <typename O, typename T, int N, typename D> \
185  KOKKOS_INLINE_FUNCTION \
186  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
187  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
188  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
189  { \
190  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
191  for (O i=0; i<a.size(); ++i) \
192  c.fastAccessCoeff(i) = \
193  a.fastAccessCoeff(i) OPER b; \
194  return c; \
195  } \
196  \
197  template <typename O, typename T, int N, typename D> \
198  KOKKOS_INLINE_FUNCTION \
199  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
200  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
201  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
202  { \
203  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
204  for (O i=0; i<a.size(); ++i) \
205  c.fastAccessCoeff(i) = \
206  a.fastAccessCoeff(i) OPER b; \
207  return c; \
208  } \
209  } \
210 }
211 
212 MP_BINARYOP_MACRO(operator+, +)
213 MP_BINARYOP_MACRO(operator-, -)
214 MP_BINARYOP_MACRO(operator*, *)
215 MP_BINARYOP_MACRO(operator/, /)
216 
217 #undef MP_BINARYOP_MACRO
218 
219 #define MP_BINARYOP_MACRO(OPNAME,OPER) \
220 namespace Sacado { \
221  namespace MP { \
222  \
223  template <typename O, typename T, int N, typename D> \
224  KOKKOS_INLINE_FUNCTION \
225  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
226  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
227  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
228  { \
229  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
230  for (O i=0; i<a.size(); ++i) \
231  c.fastAccessCoeff(i) = \
232  OPER( a.fastAccessCoeff(i) , b.fastAccessCoeff(i) ); \
233  return c; \
234  } \
235  \
236  template <typename O, typename T, int N, typename D> \
237  KOKKOS_INLINE_FUNCTION \
238  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
239  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
240  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
241  { \
242  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
243  for (O i=0; i<a.size(); ++i) \
244  c.fastAccessCoeff(i) = \
245  OPER( a.fastAccessCoeff(i) , b.fastAccessCoeff(i) ); \
246  return c; \
247  } \
248  \
249  template <typename O, typename T, int N, typename D> \
250  KOKKOS_INLINE_FUNCTION \
251  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
252  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
253  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
254  { \
255  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
256  for (O i=0; i<a.size(); ++i) \
257  c.fastAccessCoeff(i) = \
258  OPER( a.fastAccessCoeff(i) , b.fastAccessCoeff(i) ); \
259  return c; \
260  } \
261  \
262  template <typename O, typename T, int N, typename D> \
263  KOKKOS_INLINE_FUNCTION \
264  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
265  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
266  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
267  { \
268  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
269  for (O i=0; i<a.size(); ++i) \
270  c.fastAccessCoeff(i) = \
271  OPER( a.fastAccessCoeff(i) , b.fastAccessCoeff(i) ); \
272  return c; \
273  } \
274  \
275  template <typename O, typename T, int N, typename D> \
276  KOKKOS_INLINE_FUNCTION \
277  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
278  OPNAME (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
279  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
280  { \
281  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
282  for (O i=0; i<b.size(); ++i) \
283  c.fastAccessCoeff(i) = \
284  OPER( a , b.fastAccessCoeff(i) ); \
285  return c; \
286  } \
287  \
288  template <typename O, typename T, int N, typename D> \
289  KOKKOS_INLINE_FUNCTION \
290  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
291  OPNAME (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
292  const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
293  { \
294  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
295  for (O i=0; i<b.size(); ++i) \
296  c.fastAccessCoeff(i) = \
297  OPER( a , b.fastAccessCoeff(i) ); \
298  return c; \
299  } \
300  \
301  template <typename O, typename T, int N, typename D> \
302  KOKKOS_INLINE_FUNCTION \
303  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
304  OPNAME (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
305  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
306  { \
307  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
308  for (O i=0; i<a.size(); ++i) \
309  c.fastAccessCoeff(i) = \
310  OPER( a.fastAccessCoeff(i) , b ); \
311  return c; \
312  } \
313  \
314  template <typename O, typename T, int N, typename D> \
315  KOKKOS_INLINE_FUNCTION \
316  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > \
317  OPNAME (const volatile Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
318  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
319  { \
320  Vector< Stokhos::StaticFixedStorage<O,T,N,D> > c; \
321  for (O i=0; i<a.size(); ++i) \
322  c.fastAccessCoeff(i) = \
323  OPER( a.fastAccessCoeff(i) , b ); \
324  return c; \
325  } \
326  } \
327 }
328 
329 MP_BINARYOP_MACRO(atan2, std::atan2)
331 #ifdef __CUDACC__
334 #else
337 #endif
338 
339 #undef MP_BINARYOP_MACRO
340 
341 //-------------------------- Relational Operators -----------------------
342 
343 #define MP_RELOP_MACRO(OP) \
344 namespace Sacado { \
345  namespace MP { \
346  \
347  template <typename O, typename T, int N, typename D> \
348  KOKKOS_INLINE_FUNCTION \
349  bool \
350  operator OP (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
351  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
352  { \
353  return a.val() OP b.val(); \
354  } \
355  \
356  template <typename O, typename T, int N, typename D> \
357  KOKKOS_INLINE_FUNCTION \
358  bool \
359  operator OP (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
360  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
361  { \
362  return a OP b.val(); \
363  } \
364  \
365  template <typename O, typename T, int N, typename D> \
366  KOKKOS_INLINE_FUNCTION \
367  bool \
368  operator OP (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
369  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
370  { \
371  return a.val() OP b; \
372  } \
373  } \
374 }
375 
376 MP_RELOP_MACRO(==)
377 MP_RELOP_MACRO(!=)
380 MP_RELOP_MACRO(<=)
381 MP_RELOP_MACRO(>=)
382 MP_RELOP_MACRO(<<=)
383 MP_RELOP_MACRO(>>=)
386 
387 #undef MP_RELOP_MACRO
388 
389 namespace Sacado {
390 
391  namespace MP {
392 
393  template <typename O, typename T, int N, typename D>
394  KOKKOS_INLINE_FUNCTION
396  {
397  return ! a.val();
398  }
399 
400  } // namespace MP
401 
402 } // namespace Sacado
403 
404 
405 //-------------------------- Boolean Operators -----------------------
406 namespace Sacado {
407 
408  namespace MP {
409 
410  template <typename O, typename T, int N, typename D>
411  KOKKOS_INLINE_FUNCTION
413  bool is_zero = true;
414  for (int i=0; i<x.size(); i++)
415  is_zero = is_zero && (x.coeff(i) == 0.0);
416  return !is_zero;
417  }
418 
419  } // namespace MP
420 
421 } // namespace Sacado
422 
423 #define PCE_BOOL_MACRO(OP) \
424 namespace Sacado { \
425  namespace MP { \
426  \
427  template <typename O, typename T, int N, typename D> \
428  KOKKOS_INLINE_FUNCTION \
429  bool \
430  operator OP (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
431  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
432  { \
433  return toBool(a) OP toBool(b); \
434  } \
435  \
436  template <typename O, typename T, int N, typename D> \
437  KOKKOS_INLINE_FUNCTION \
438  bool \
439  operator OP (const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& a, \
440  const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& b) \
441  { \
442  return a OP toBool(b); \
443  } \
444  \
445  template <typename O, typename T, int N, typename D> \
446  KOKKOS_INLINE_FUNCTION \
447  bool \
448  operator OP (const Vector< Stokhos::StaticFixedStorage<O,T,N,D> >& a, \
449  const typename Vector< Stokhos::StaticFixedStorage<O,T,N,D> >::value_type& b) \
450  { \
451  return toBool(a) OP b; \
452  } \
453  } \
454 }
455 
456 PCE_BOOL_MACRO(&&)
457 PCE_BOOL_MACRO(||)
458 
459 #undef PCE_BOOL_MACRO
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > fabs(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > tan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > sinh(const PCE< Storage > &a)
Statically allocated storage class.
KOKKOS_INLINE_FUNCTION PCE< Storage > pow(const PCE< Storage > &a, const PCE< Storage > &b)
OrthogPoly< T, Storage > atanh(const OrthogPoly< T, Storage > &a)
KOKKOS_INLINE_FUNCTION bool toBool(const Expr< T > &xx)
KOKKOS_INLINE_FUNCTION PCE< Storage > tanh(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > cbrt(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > acos(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
KOKKOS_INLINE_FUNCTION bool operator!(const Expr< T > &expr)
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
OrthogPoly< T, Storage > acosh(const OrthogPoly< T, Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > max(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
KOKKOS_INLINE_FUNCTION PCE< Storage > cosh(const PCE< Storage > &a)
#define MP_UNARYOP_MACRO(OPNAME, OPER)
KOKKOS_INLINE_FUNCTION PCE< Storage > abs(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > atan(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > exp(const PCE< Storage > &a)
OrthogPoly< T, Storage > asinh(const OrthogPoly< T, Storage > &a)
#define MP_RELOP_MACRO(OP)
KOKKOS_INLINE_FUNCTION PCE< Storage > sin(const PCE< Storage > &a)
#define PCE_BOOL_MACRO(OP)
KOKKOS_INLINE_FUNCTION PCE< Storage > log(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > log10(const PCE< Storage > &a)
#define MP_BINARYOP_MACRO(OPNAME, OPER)
KOKKOS_INLINE_FUNCTION PCE< Storage > asin(const PCE< Storage > &a)
KOKKOS_INLINE_FUNCTION PCE< Storage > cos(const PCE< Storage > &a)