Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_MP_Vector_MaskTraits.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 #ifndef STOKHOS_MP_VECTOR_MASKTRAITS_HPP
43 #define STOKHOS_MP_VECTOR_MASKTRAITS_HPP
44 
46 #include <iostream>
47 #include <cmath>
48 
49 template <typename T>
51  static const int size = 1;
52  typedef T value_type;
53  static const value_type& coeff(const T& x, int i) { return x; }
54  static value_type& coeff(T& x, int i) { return x; }
55 };
56 
57 template <typename S>
58 struct EnsembleTraits_m< Sacado::MP::Vector<S> > {
59  static const int size = S::static_size;
60  typedef typename S::value_type value_type;
61  static const value_type& coeff(const Sacado::MP::Vector<S>& x, int i) {
62  return x.fastAccessCoeff(i);
63  }
64  static value_type& coeff(Sacado::MP::Vector<S>& x, int i) {
65  return x.fastAccessCoeff(i);
66  }
67 };
68 
69 template<typename scalar> class Mask
70 {
71  private:
73  bool data[size];
74 
75  public:
76  Mask(){
77  for(int i=0; i<size; ++i)
78  data[i]=false;
79  }
80 
81  Mask(bool a){
82  for(int i=0; i<size; ++i)
83  data[i]=a;
84  }
85 
86  int getSize() const {return size;}
87 
88  bool operator> (double v)
89  {
90  double sum = 0;
91  for(int i=0; i<size; ++i)
92  sum = sum + this->data[i];
93 
94  return sum > v*size;
95  }
96 
97  bool operator< (double v)
98  {
99  double sum = 0;
100  for(int i=0; i<size; ++i)
101  sum = sum + this->data[i];
102 
103  return sum < v*size;
104  }
105 
106  bool operator>= (double v)
107  {
108  double sum = 0;
109  for(int i=0; i<size; ++i)
110  sum = sum + this->data[i];
111 
112  return sum >= v*size;
113  }
114 
115  bool operator<= (double v)
116  {
117  double sum = 0;
118  for(int i=0; i<size; ++i)
119  sum = sum + this->data[i];
120 
121  return sum <= v*size;
122  }
123 
124  bool operator== (double v)
125  {
126  double sum = 0;
127  for(int i=0; i<size; ++i)
128  sum = sum + this->data[i];
129 
130  return sum == v*size;
131  }
132 
133  bool operator!= (double v)
134  {
135  double sum = 0;
136  for(int i=0; i<size; ++i)
137  sum = sum + this->data[i];
138 
139  return sum != v*size;
140  }
141 
142  bool operator== (const Mask<scalar> &m2)
143  {
144  bool all = true;
145  for (int i = 0; i < size; ++i) {
146  all = all && (this->data[i] == m2.data[i]);
147  }
148  return all;
149  }
150 
151  bool operator!= (const Mask<scalar> &m2)
152  {
153  return !(this==m2);
154  }
155 
157  {
158  Mask<scalar> m3;
159  for(int i=0; i<size; ++i)
160  m3.data[i] = (this->data[i] && m2.data[i]);
161 
162  return m3;
163  }
164 
166  {
167  Mask<scalar> m3;
168  for(int i=0; i<size; ++i)
169  m3.data[i] = (this->data[i] || m2.data[i]);
170 
171  return m3;
172  }
173 
175  {
176  Mask<scalar> m3;
177  for(int i=0; i<size; ++i)
178  m3.data[i] = (this->data[i] && m2);
179 
180  return m3;
181  }
182 
184  {
185  Mask<scalar> m3;
186  for(int i=0; i<size; ++i)
187  m3.data[i] = (this->data[i] || m2);
188 
189  return m3;
190  }
191 
193  {
194  Mask<scalar> m3;
195  for(int i=0; i<size; ++i)
196  m3.data[i] = (this->data[i] + m2.data[i]);
197 
198  return m3;
199  }
200 
202  {
203  Mask<scalar> m3;
204  for(int i=0; i<size; ++i)
205  m3.data[i] = (this->data[i] - m2.data[i]);
206 
207  return m3;
208  }
209 
210  scalar operator* (const scalar &v)
211  {
212  typedef EnsembleTraits_m<scalar> ET;
213  scalar v2;
214  for(int i=0; i<size; ++i)
215  ET::coeff(v2,i) = ET::coeff(v,i)*this->data[i];
216 
217  return v2;
218  }
219 
220  bool operator[] (int i) const
221  {
222  return this->data[i];
223  }
224 
225  bool & operator[] (int i)
226  {
227  return this->data[i];
228  }
229 
231  {
232  Mask<scalar> m2;
233  for(int i=0; i<size; ++i)
234  m2.data[i] = !(this->data[i]);
235 
236  return m2;
237  }
238 
239  operator bool() const
240  {
241  return this->data[0];
242  }
243 
244  operator double() const
245  {
246  double sum = 0;
247  for(int i=0; i<size; ++i)
248  sum = sum + this->data[i];
249 
250  return sum/size;
251  }
252 };
253 
254 template<typename scalar> std::ostream &operator<<(std::ostream &os, const Mask<scalar>& m) {
255  os << "[ ";
256  for(int i=0; i<m.getSize(); ++i)
257  os << m[i] << " ";
258  return os << "]";
259 }
260 
262 {
265  for(int i=0; i<ET::size; ++i){
266  ET::coeff(mul,i) = ET::coeff(a1,i)*m[i];
267  }
268  return mul;
269 }
270 
271 template<typename S> Sacado::MP::Vector<S> operator* (const typename S::value_type &a1, const Mask<Sacado::MP::Vector<S>> &m)
272 {
275  for(int i=0; i<ET::size; ++i){
276  ET::coeff(mul,i) = m[i]*a1;
277  }
278  return mul;
279 }
280 
281 template<typename S> Sacado::MP::Vector<S> operator* (const Mask<Sacado::MP::Vector<S>> &m, const typename S::value_type &a1)
282 {
285  for(int i=0; i<ET::size; ++i){
286  ET::coeff(mul,i) = m[i]*a1;
287  }
288  return mul;
289 }
290 
291 namespace Sacado {
292  namespace MP {
293  template <typename S> Vector<S> copysign(const Vector<S> &a1, const Vector<S> &a2)
294  {
295  typedef EnsembleTraits_m< Vector<S> > ET;
296 
297  Vector<S> a_out;
298 
299  using std::copysign;
300  for(int i=0; i<ET::size; ++i){
301  ET::coeff(a_out,i) = copysign(ET::coeff(a1,i),ET::coeff(a2,i));
302  }
303 
304  return a_out;
305  }
306  }
307 }
308 
309 
311 {
313  using std::signbit;
314 
316  for(int i=0; i<ET::size; ++i)
317  mask[i] = signbit(ET::coeff(a1,i));
318  return mask;
319 }
320 
321 // Vector - vector comparisons
322 
324 {
326 
328  for(int i=0; i<ET::size; ++i)
329  mask[i] = ET::coeff(a1,i) > ET::coeff(a2,i);
330  return mask;
331 }
332 
334 {
336 
338  for(int i=0; i<ET::size; ++i)
339  mask[i] = ET::coeff(a1,i) >= ET::coeff(a2,i);
340  return mask;
341 }
342 
343 template<typename S> Mask<Sacado::MP::Vector<S> > operator< (const Sacado::MP::Vector<S> &a1, const Sacado::MP::Vector<S> &a2)
344 {
346 
348  for(int i=0; i<ET::size; ++i)
349  mask[i] = ET::coeff(a1,i) < ET::coeff(a2,i);
350  return mask;
351 }
352 
353 template<typename S> Mask<Sacado::MP::Vector<S> > operator<= (const Sacado::MP::Vector<S> &a1, const Sacado::MP::Vector<S> &a2)
354 {
356 
358  for(int i=0; i<ET::size; ++i)
359  mask[i] = ET::coeff(a1,i) <= ET::coeff(a2,i);
360  return mask;
361 }
362 
363 // Vector - scalar comparisons
364 
365 template<typename S> Mask<Sacado::MP::Vector<S> > operator> (const Sacado::MP::Vector<S> &a1, const typename S::value_type &a2)
366 {
368 
370  for(int i=0; i<ET::size; ++i)
371  mask[i] = ET::coeff(a1,i) > a2;
372  return mask;
373 }
374 
375 template<typename S> Mask<Sacado::MP::Vector<S> > operator>= (const Sacado::MP::Vector<S> &a1, const typename S::value_type &a2)
376 {
378 
380  for(int i=0; i<ET::size; ++i)
381  mask[i] = ET::coeff(a1,i) >= a2;
382  return mask;
383 }
384 
385 template<typename S> Mask<Sacado::MP::Vector<S> > operator< (const Sacado::MP::Vector<S> &a1, const typename S::value_type &a2)
386 {
388 
390  for(int i=0; i<ET::size; ++i)
391  mask[i] = ET::coeff(a1,i) < a2;
392  return mask;
393 }
394 
395 template<typename S> Mask<Sacado::MP::Vector<S> > operator<= (const Sacado::MP::Vector<S> &a1, const typename S::value_type &a2)
396 {
398 
400  for(int i=0; i<ET::size; ++i)
401  mask[i] = ET::coeff(a1,i) <= a2;
402  return mask;
403 }
404 
405 // Scalar -vector comparisons
406 
407 template<typename S> Mask<Sacado::MP::Vector<S> > operator> (const typename S::value_type &a2, Sacado::MP::Vector<S> &a1)
408 {
410 
412  for(int i=0; i<ET::size; ++i)
413  mask[i] = a2 > ET::coeff(a1,i);
414  return mask;
415 }
416 
417 template<typename S> Mask<Sacado::MP::Vector<S> > operator>= (const typename S::value_type &a2, const Sacado::MP::Vector<S> &a1)
418 {
420 
422  for(int i=0; i<ET::size; ++i)
423  mask[i] = a2 >= ET::coeff(a1,i);
424  return mask;
425 }
426 
427 template<typename S> Mask<Sacado::MP::Vector<S> > operator< (const typename S::value_type &a2, const Sacado::MP::Vector<S> &a1)
428 {
430 
432  for(int i=0; i<ET::size; ++i)
433  mask[i] = a2 < ET::coeff(a1,i);
434  return mask;
435 }
436 
437 template<typename S> Mask<Sacado::MP::Vector<S> > operator<= (const typename S::value_type &a2, const Sacado::MP::Vector<S> &a1)
438 {
440 
442  for(int i=0; i<ET::size; ++i)
443  mask[i] = a2 <= ET::coeff(a1,i);
444  return mask;
445 }
446 
447 namespace MaskLogic{
448 
449 class OR
450 {
451 private:
452  bool value;
453 
454 public:
455  OR(){
456  value = false;
457  }
458 
459  OR(bool value_){
460  value = value_;
461  }
462 
463  template<typename T> OR(T m){
464  value = (((double) m)!=0.);
465  }
466 
467  operator bool() const
468  {
469  return value;
470  }
471 };
472 
473 class XOR
474 {
475 private:
476  bool value;
477 
478 public:
479  XOR(){
480  value = false;
481  }
482 
483  XOR(bool value_){
484  value = value_;
485  }
486 
487  template<typename T> XOR(T m){
488  value = (((double) m)==1./m.getSize());
489  }
490 
491  operator bool() const
492  {
493  return value;
494  }
495 };
496 
497 class AND
498 {
499 private:
500  bool value;
501 
502 public:
503  AND(){
504  value = false;
505  }
506 
507  AND(bool value_){
508  value = value_;
509  }
510 
511  template<typename T> AND(T m){
512  value = (((double) m)==1.);
513  }
514 
515  operator bool() const
516  {
517  return value;
518  }
519 };
520 }
521 #endif // STOKHOS_MP_VECTOR_MASKTRAITS_HPP
Vector< S > copysign(const Vector< S > &a1, const Vector< S > &a2)
static const int size
bool operator==(double v)
bool operator>=(double v)
bool operator<=(double v)
bool operator[](int i) const
bool operator<(double v)
Mask< scalar > operator!()
KOKKOS_INLINE_FUNCTION bool operator>(const PCE< Storage > &a, const PCE< Storage > &b)
bool operator!=(double v)
KOKKOS_INLINE_FUNCTION bool operator>=(const PCE< Storage > &a, const PCE< Storage > &b)
bool operator>(double v)
static value_type & coeff(T &x, int i)
static value_type & coeff(Sacado::MP::Vector< S > &x, int i)
Mask< scalar > operator&&(const Mask< scalar > &m2)
Mask< scalar > operator+(const Mask< scalar > &m2)
int getSize() const
Mask< scalar > operator||(const Mask< scalar > &m2)
static const value_type & coeff(const T &x, int i)
KOKKOS_INLINE_FUNCTION PCE< Storage > operator*(const PCE< Storage > &a, const PCE< Storage > &b)
static const value_type & coeff(const Sacado::MP::Vector< S > &x, int i)
scalar operator*(const scalar &v)
Mask< scalar > operator-(const Mask< scalar > &m2)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type sum(const Kokkos::View< RD, RP...> &r, const Kokkos::View< XD, XP...> &x)
Mask< Sacado::MP::Vector< S > > signbit_v(const Sacado::MP::Vector< S > &a1)