Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoMPVectorUnitTest_MaskTraits.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 
16 
17 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Create_8)
18 {
19  constexpr int ensemble_size = 8;
20 
21  typedef Kokkos::DefaultExecutionSpace execution_space;
23  typedef Sacado::MP::Vector<storage_type> scalar;
24 
25  scalar a = (scalar) 1.;
26  a[0] = 2.5;
27  a[2] = 2.5;
28  scalar b = (scalar) 2.;
29 
30  auto m1 = a>b;
31  std::cout << std::endl;
32  std::cout << a << std::endl;
33  std::cout << b << std::endl;
34  std::cout << m1 << std::endl;
35  TEST_EQUALITY( m1.getSize(), ensemble_size );
36  TEST_EQUALITY( m1.get(0), true );
37  TEST_EQUALITY( m1.get(1), false );
38  TEST_EQUALITY( m1.get(2), true );
39  for (auto i=3; i<ensemble_size; ++i)
40  TEST_EQUALITY( m1.get(i), false );
41 
42  TEST_EQUALITY( (double) m1, 2./ensemble_size );
43 }
44 
45 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Create_16)
46 {
47  constexpr int ensemble_size = 16;
48 
49  typedef Kokkos::DefaultExecutionSpace execution_space;
51  typedef Sacado::MP::Vector<storage_type> scalar;
52 
53  scalar a = (scalar) 1.;
54  a[0] = 2.5;
55  a[2] = 2.5;
56  scalar b = (scalar) 2.;
57 
58  auto m1 = a>b;
59  std::cout << std::endl;
60  std::cout << a << std::endl;
61  std::cout << b << std::endl;
62  std::cout << m1 << std::endl;
63  TEST_EQUALITY( m1.getSize(), ensemble_size );
64  TEST_EQUALITY( m1.get(0), true );
65  TEST_EQUALITY( m1.get(1), false );
66  TEST_EQUALITY( m1.get(2), true );
67  for (auto i=3; i<ensemble_size; ++i)
68  TEST_EQUALITY( m1.get(i), false );
69 
70  TEST_EQUALITY( (double) m1, 2./ensemble_size );
71 }
72 
73 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Not_8)
74 {
75  constexpr int ensemble_size = 8;
76 
77  typedef Kokkos::DefaultExecutionSpace execution_space;
79  typedef Sacado::MP::Vector<storage_type> scalar;
80 
81  scalar a = (scalar) 1.;
82  a[0] = 2.5;
83  a[2] = 2.5;
84  scalar b = (scalar) 2.;
85 
86  auto m1 = a>b;
87  auto m2 = !m1;
88  std::cout << m1 << std::endl;
89  std::cout << m2 << std::endl;
90  for (auto i=0; i<ensemble_size; ++i)
91  TEST_EQUALITY( m2.get(i), !m1.get(i) );
92 }
93 
94 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Multiplication_8)
95 {
96  constexpr int ensemble_size = 8;
97 
98  typedef Kokkos::DefaultExecutionSpace execution_space;
100  typedef Sacado::MP::Vector<storage_type> scalar;
101 
102  scalar a = (scalar) 1.;
103  a[0] = 2.5;
104  a[2] = 2.5;
105  scalar b = (scalar) 2.;
106 
107  auto m1 = a>b;
108  scalar mul = m1*a;
109 
110  scalar mul2 = m1*b;
111  scalar mul3 = b*m1;
112 
113  std::cout << m1 << std::endl;
114  std::cout << mul << std::endl;
115 
116  std::cout << mul2 << std::endl;
117  std::cout << mul3 << std::endl;
118 
119  TEST_EQUALITY( mul[0], 2.5 );
120  TEST_EQUALITY( mul[1], 0. );
121  TEST_EQUALITY( mul[2], 2.5 );
122  for (auto i=3; i<ensemble_size; ++i)
123  TEST_EQUALITY( mul[i], 0. );
124 
125  TEST_EQUALITY( mul2, mul3 );
126 }
127 
128 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Multiplication_16)
129 {
130  constexpr int ensemble_size = 16;
131 
132  typedef Kokkos::DefaultExecutionSpace execution_space;
134  typedef Sacado::MP::Vector<storage_type> scalar;
135 
136  scalar a = (scalar) 1.;
137  a[0] = 2.5;
138  a[2] = 2.5;
139  scalar b = (scalar) 2.;
140 
141  auto m1 = a>b;
142  scalar mul = m1*a;
143  std::cout << m1 << std::endl;
144  std::cout << mul << std::endl;
145 
146  TEST_EQUALITY( mul[0], 2.5 );
147  TEST_EQUALITY( mul[1], 0. );
148  TEST_EQUALITY( mul[2], 2.5 );
149  for (auto i=3; i<ensemble_size; ++i)
150  TEST_EQUALITY( mul[i], 0. );
151 }
152 
153 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mul_Add_8)
154 {
155  constexpr int ensemble_size = 8;
156 
157  typedef Kokkos::DefaultExecutionSpace execution_space;
159  typedef Sacado::MP::Vector<storage_type> scalar;
160 
161  scalar a = (scalar) 1.;
162  a[0] = 2.5;
163  a[2] = 2.5;
164  scalar b = (scalar) 2.;
165 
166  auto m1 = a>b;
167  scalar mul = m1*a + !m1*b;
168  scalar mul2 = a*m1 + !m1*b;
169  std::cout << m1 << std::endl;
170  std::cout << mul << std::endl;
171  std::cout << mul2 << std::endl;
172 
173  TEST_EQUALITY( mul[0], 2.5 );
174  TEST_EQUALITY( mul[1], 2. );
175  TEST_EQUALITY( mul[2], 2.5 );
176  for (auto i=3; i<ensemble_size; ++i)
177  TEST_EQUALITY( mul[i], 2. );
178 
179  TEST_EQUALITY( mul, mul2 );
180 }
181 
182 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_DEFAULT)
183 {
184  constexpr int ensemble_size = 8;
185 
186  typedef Kokkos::DefaultExecutionSpace execution_space;
188  typedef Sacado::MP::Vector<storage_type> scalar;
189 
190  using namespace MaskLogic;
191 
192  scalar a = (scalar) 1.;
193  a[1] = 2.5;
194  a[2] = 2.5;
195  scalar b = (scalar) 2.;
196 
197  auto m1 = a>b;
198  auto m2 = a>(scalar) 0.;
199  auto m3 = a> 0.;
200  auto m4 = 0.<a;
201  std::cout << m1 << std::endl;
202  std::cout << m2 << std::endl;
203  std::cout << m3<< std::endl;
204  std::cout << m4<< std::endl;
205 
206  if (m1)
207  {TEST_EQUALITY( true, false );}
208  else
209  {TEST_EQUALITY( true, true );}
210 
211  TEST_EQUALITY((bool) m1, false );
212  TEST_EQUALITY((bool) !m1, true );
213 
214  if (m2)
215  {TEST_EQUALITY( true, true );}
216  else
217  {TEST_EQUALITY( true, false );}
218 
219  TEST_EQUALITY((bool) m2, true );
220  TEST_EQUALITY((bool) !m2, false );
221 
222  TEST_EQUALITY( m2, m3 );
223  TEST_EQUALITY( m2, m4 );
224 }
225 
226 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND)
227 {
228  constexpr int ensemble_size = 8;
229 
230  typedef Kokkos::DefaultExecutionSpace execution_space;
232  typedef Sacado::MP::Vector<storage_type> scalar;
233 
234  using namespace MaskLogic;
235 
236  scalar a = (scalar) 1.;
237  a[0] = 2.5;
238  a[2] = 2.5;
239  scalar b = (scalar) 2.;
240 
241  auto m1 = a>b;
242  auto m2 = a>0.;
243  std::cout << m1 << std::endl;
244  std::cout << m2 << std::endl;
245 
246 
247  TEST_EQUALITY( AND(true), true );
248  TEST_EQUALITY( AND(false), false );
249  TEST_EQUALITY( AND(m1), false );
250  TEST_EQUALITY( AND(!m1), false );
251  TEST_EQUALITY( AND(m2), true );
252  TEST_EQUALITY( AND(!m2), false );
253 }
254 
255 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR)
256 {
257  constexpr int ensemble_size = 8;
258 
259  typedef Kokkos::DefaultExecutionSpace execution_space;
261  typedef Sacado::MP::Vector<storage_type> scalar;
262 
263  using namespace MaskLogic;
264 
265  scalar a = (scalar) 1.;
266  a[0] = 2.5;
267  a[2] = 2.5;
268  scalar b = (scalar) 2.;
269 
270  auto m1 = a>b;
271  auto m2 = a>0.;
272  std::cout << m1 << std::endl;
273  std::cout << m2 << std::endl;
274 
275 
276  TEST_EQUALITY( OR(true), true );
277  TEST_EQUALITY( OR(false), false );
278  TEST_EQUALITY( OR(m1), true );
279  TEST_EQUALITY( OR(!m1), true );
280  TEST_EQUALITY( OR(m2), true );
281  TEST_EQUALITY( OR(!m2), false );
282 }
283 
284 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_XOR)
285 {
286  constexpr int ensemble_size = 8;
287 
288  typedef Kokkos::DefaultExecutionSpace execution_space;
290  typedef Sacado::MP::Vector<storage_type> scalar;
291 
292  using namespace MaskLogic;
293 
294  scalar a = (scalar) 1.;
295  a[2] = 2.5;
296  scalar b = (scalar) 2.;
297 
298  auto m1 = a>b;
299  auto m2 = a>0.;
300  std::cout << m1 << std::endl;
301  std::cout << m2 << std::endl;
302 
303 
304  TEST_EQUALITY( XOR(true), true );
305  TEST_EQUALITY( XOR(false), false );
306  TEST_EQUALITY( XOR(m1), true );
307  TEST_EQUALITY( XOR(!m1), false );
308  TEST_EQUALITY( XOR(m2), false );
309  TEST_EQUALITY( XOR(!m2), false );
310 }
311 
312 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_compared_to_double)
313 {
314  constexpr int ensemble_size = 8;
315 
316  typedef Kokkos::DefaultExecutionSpace execution_space;
318  typedef Sacado::MP::Vector<storage_type> scalar;
319 
320  scalar a = (scalar) 1.;
321  a[2] = 2.5;
322  scalar b = (scalar) 2.;
323 
324  auto m1 = a>b;
325  auto m2 = a>0.;
326 
327  std::cout << m1 << std::endl;
328  std::cout << m2 << std::endl;
329 
330  TEST_EQUALITY((double) m1,1./ensemble_size);
331  TEST_EQUALITY((double) m2,1.);
332 
333  TEST_EQUALITY(m1==1.,false);
334  TEST_EQUALITY(m1!=1.,true);
335  TEST_EQUALITY(m1==0.,false);
336  TEST_EQUALITY(m1!=0.,true);
337 
338  TEST_EQUALITY(m1>=0.5,false);
339  TEST_EQUALITY(m1<=0.5,true);
340  TEST_EQUALITY(m1>0.5,false);
341  TEST_EQUALITY(m1<0.5,true);
342 
343  TEST_EQUALITY(m2==1.,true);
344  TEST_EQUALITY(m2!=1.,false);
345  TEST_EQUALITY(m2==0.,false);
346  TEST_EQUALITY(m2!=0.,true);
347 
348  TEST_EQUALITY(m2>=0.5,true);
349  TEST_EQUALITY(m2<=0.5,false);
350  TEST_EQUALITY(m2>0.5,true);
351  TEST_EQUALITY(m2<0.5,false);
352 }
353 
354 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND_Mask)
355 {
356  constexpr int ensemble_size = 8;
357 
358  typedef Kokkos::DefaultExecutionSpace execution_space;
360  typedef Sacado::MP::Vector<storage_type> scalar;
361 
362  scalar a = (scalar) 1.;
363  a[2] = 2.5;
364  scalar b = (scalar) 2.;
365 
366  auto m1 = a>b;
367  auto m2 = a>0.;
368 
369  auto m3 = m1 && m2;
370  std::cout << m1 << std::endl;
371  std::cout << m2 << std::endl;
372  std::cout << m3 << std::endl;
373  TEST_EQUALITY(m3,m1);
374 }
375 
376 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR_Mask)
377 {
378  constexpr int ensemble_size = 8;
379 
380  typedef Kokkos::DefaultExecutionSpace execution_space;
382  typedef Sacado::MP::Vector<storage_type> scalar;
383 
384  scalar a = (scalar) 1.;
385  a[2] = 2.5;
386  scalar b = (scalar) 2.;
387 
388  auto m1 = a>b;
389  auto m2 = a>0.;
390 
391  auto m3 = m1 || m2;
392  std::cout << m1 << std::endl;
393  std::cout << m2 << std::endl;
394  std::cout << m3 << std::endl;
395  TEST_EQUALITY(m3,m2);
396 }
397 
398 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_ADD_Mask)
399 {
400  constexpr int ensemble_size = 8;
401 
402  typedef Kokkos::DefaultExecutionSpace execution_space;
404  typedef Sacado::MP::Vector<storage_type> scalar;
405 
406  scalar a = (scalar) 1.;
407  a[2] = 2.5;
408  scalar b = (scalar) 2.;
409 
410  std::cout << a << std::endl;
411  std::cout << b << std::endl;
412 
413  auto m1 = a>b;
414  auto m2 = a>0.;
415  //std::cout << m1 << std::endl;
416  //std::cout << m2 << std::endl;
417  auto m3 = m1 + m2;
418 
419  std::cout << m3 << std::endl;
420  TEST_EQUALITY(m3,m2);
421 }
422 
423 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_SUB_Mask)
424 {
425  constexpr int ensemble_size = 8;
426 
427  typedef Kokkos::DefaultExecutionSpace execution_space;
429  typedef Sacado::MP::Vector<storage_type> scalar;
430 
431  scalar a = (scalar) 1.;
432  a[2] = 2.5;
433  scalar b = (scalar) 2.;
434 
435  auto m1 = a>b;
436  auto m2 = a>0.;
437  std::cout << m1 << std::endl;
438  std::cout << m2 << std::endl;
439  auto m3 = (a>0.) - (a>b);
440  std::cout << m3 << std::endl;
441  TEST_EQUALITY(m3,!m1);
442 }
443 
444 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_signbit_v)
445 {
446  constexpr int ensemble_size = 8;
447 
448  typedef Kokkos::DefaultExecutionSpace execution_space;
450  typedef Sacado::MP::Vector<storage_type> scalar;
451  typedef Mask<scalar> mask;
452 
453  scalar a = (scalar) 1.;
454  a[2] = -2.5;
455 
456  auto m1 = signbit_v(a);
457  mask m2;
458  m2.set(2,true);
459  TEST_EQUALITY(m1,m2);
460 }
461 
462 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_copysign)
463 {
464  constexpr int ensemble_size = 8;
465 
466  typedef Kokkos::DefaultExecutionSpace execution_space;
468  typedef Sacado::MP::Vector<storage_type> scalar;
469 
470  scalar a = (scalar) 1.;
471  a[2] = -2.5;
472 
473  scalar b = (scalar) 2.;
474 
475  using std::copysign;
476 
477  std::cout << std::endl;
478  std::cout << a << std::endl;
479  std::cout << b << std::endl;
480  b = copysign(b,a);
481  std::cout << a << std::endl;
482  std::cout << b << std::endl;
483  TEST_EQUALITY(b[2],-2.);
484 }
485 
486 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_assign)
487 {
488  constexpr int ensemble_size = 8;
489 
490  typedef Kokkos::DefaultExecutionSpace execution_space;
492  typedef Sacado::MP::Vector<storage_type> scalar;
493 
494  scalar a = (scalar) 1.;
495  a[2] = -2.5;
496 
497  mask_assign(a<=0.,a) = {0.,a};
498 
499  TEST_EQUALITY(a[1],1.);
500  TEST_EQUALITY(a[2],0.);
501 
502  double b = 1.;
503 
504  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
505 
506  TEST_EQUALITY(b,2.);
507 
508  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
509  TEST_EQUALITY(b,-1.);
510 
511 }
512 
513 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_pointer_assign)
514 {
515  constexpr int ensemble_size = 8;
516 
517  typedef Kokkos::DefaultExecutionSpace execution_space;
519  typedef Sacado::MP::Vector<storage_type> scalar;
520 
521  scalar a = (scalar) 1.;
522  a[2] = -2.5;
523  scalar *p = &a;
524 
525  mask_assign(a<=0.,*p) = {0.,a};
526 
527  TEST_EQUALITY(a[1],1.);
528  TEST_EQUALITY(a[2],0.);
529 }
530 
531 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_div)
532 {
533  constexpr int ensemble_size = 8;
534 
535  typedef Kokkos::DefaultExecutionSpace execution_space;
537  typedef Sacado::MP::Vector<storage_type> scalar;
538 
539  scalar a2 = {0.,2.,2.,2.,2.,2.,2.,2.};
540  std::cout << a2 << std::endl;
541 
542  scalar a = (scalar) 1.;
543  a[2] = -2.5;
544  auto m = (a>(scalar) 0.);
545  std::cout << "m is computed" << std::endl;
546  std::cout << m << std::endl;
547  m = a>0.;
548  std::cout << "m is computed" << std::endl;
549  std::cout << m << std::endl;
550 
551  std::cout << a << std::endl;
552  std::cout << m << std::endl;
553  std::cout << (a>=(scalar) 0. )<< std::endl;
554  std::cout << (a> 0. )<< std::endl;
555  std::cout << (a>= 0.) << std::endl;
556  std::cout << (0.<a )<< std::endl;
557  std::cout << (0.<=a) << std::endl;
558 
559  mask_assign<scalar>(m,a) /= {a, 2.,-1.};
560  TEST_EQUALITY(a[1],0.5);
561  TEST_EQUALITY(a[2],-1.);
562 
563  /*
564  This test is working only if c++ 14 is allowed due to the fact
565  that copy-list-initialization in the constructor of tuple is not allowed before
566  as it is an explicit one.
567 
568  mask_assign<scalar>(m,a) /= {(scalar) 4.,2.,-1.};
569  */
570  //std::tuple<scalar,scalar,scalar> ts {4.,2.,-1.};
571  mask_assign<scalar>(m,a) /= {4.,2.,-1.};
572  TEST_EQUALITY(a[1],2.);
573  TEST_EQUALITY(a[2],-1.);
574 
575 
576  double b = 1.;
577  mask_assign(b>0.5,b) /= {b, 2.,-1.};
578  TEST_EQUALITY(b,0.5);
579  mask_assign(b>0.5,b) /= {b, 2.,-1.};
580  TEST_EQUALITY(b,-1.);
581 
582 }
KOKKOS_INLINE_FUNCTION Vector< S > copysign(const Vector< S > &a1, const Vector< S > &a2)
Stokhos::StandardStorage< int, double > storage_type
Statically allocated storage class.
Kokkos::DefaultExecutionSpace execution_space
KOKKOS_INLINE_FUNCTION bool OR(Mask< T > m)
KOKKOS_INLINE_FUNCTION MaskedAssign< scalar > mask_assign(bool b, scalar *s)
KOKKOS_INLINE_FUNCTION bool AND(Mask< T > m)
KOKKOS_INLINE_FUNCTION bool XOR(Mask< T > m)
#define TEST_EQUALITY(v1, v2)
TEUCHOS_UNIT_TEST(tAdaptivityManager, test_interface)
KOKKOS_INLINE_FUNCTION Mask< Sacado::MP::Vector< S > > signbit_v(const Sacado::MP::Vector< S > &a1)