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  typedef Mask<scalar> mask;
190 
191  using namespace MaskLogic;
192 
193  scalar a = (scalar) 1.;
194  a[1] = 2.5;
195  a[2] = 2.5;
196  scalar b = (scalar) 2.;
197 
198  auto m1 = a>b;
199  auto m2 = a>(scalar) 0.;
200  auto m3 = a> 0.;
201  auto m4 = 0.<a;
202  std::cout << m1 << std::endl;
203  std::cout << m2 << std::endl;
204  std::cout << m3<< std::endl;
205  std::cout << m4<< std::endl;
206 
207  if (m1)
208  {TEST_EQUALITY( true, false );}
209  else
210  {TEST_EQUALITY( true, true );}
211 
212  TEST_EQUALITY((bool) m1, false );
213  TEST_EQUALITY((bool) !m1, true );
214 
215  if (m2)
216  {TEST_EQUALITY( true, true );}
217  else
218  {TEST_EQUALITY( true, false );}
219 
220  TEST_EQUALITY((bool) m2, true );
221  TEST_EQUALITY((bool) !m2, false );
222 
223  TEST_EQUALITY( m2, (const mask)m3 );
224  TEST_EQUALITY( m2, (const mask)m4 );
225 }
226 
227 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND)
228 {
229  constexpr int ensemble_size = 8;
230 
231  typedef Kokkos::DefaultExecutionSpace execution_space;
233  typedef Sacado::MP::Vector<storage_type> scalar;
234 
235  using namespace MaskLogic;
236 
237  scalar a = (scalar) 1.;
238  a[0] = 2.5;
239  a[2] = 2.5;
240  scalar b = (scalar) 2.;
241 
242  auto m1 = a>b;
243  auto m2 = a>0.;
244  std::cout << m1 << std::endl;
245  std::cout << m2 << std::endl;
246 
247 
248  TEST_EQUALITY( AND(true), true );
249  TEST_EQUALITY( AND(false), false );
250  TEST_EQUALITY( AND(m1), false );
251  TEST_EQUALITY( AND(!m1), false );
252  TEST_EQUALITY( AND(m2), true );
253  TEST_EQUALITY( AND(!m2), false );
254 }
255 
256 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR)
257 {
258  constexpr int ensemble_size = 8;
259 
260  typedef Kokkos::DefaultExecutionSpace execution_space;
262  typedef Sacado::MP::Vector<storage_type> scalar;
263 
264  using namespace MaskLogic;
265 
266  scalar a = (scalar) 1.;
267  a[0] = 2.5;
268  a[2] = 2.5;
269  scalar b = (scalar) 2.;
270 
271  auto m1 = a>b;
272  auto m2 = a>0.;
273  std::cout << m1 << std::endl;
274  std::cout << m2 << std::endl;
275 
276 
277  TEST_EQUALITY( OR(true), true );
278  TEST_EQUALITY( OR(false), false );
279  TEST_EQUALITY( OR(m1), true );
280  TEST_EQUALITY( OR(!m1), true );
281  TEST_EQUALITY( OR(m2), true );
282  TEST_EQUALITY( OR(!m2), false );
283 }
284 
285 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_XOR)
286 {
287  constexpr int ensemble_size = 8;
288 
289  typedef Kokkos::DefaultExecutionSpace execution_space;
291  typedef Sacado::MP::Vector<storage_type> scalar;
292 
293  using namespace MaskLogic;
294 
295  scalar a = (scalar) 1.;
296  a[2] = 2.5;
297  scalar b = (scalar) 2.;
298 
299  auto m1 = a>b;
300  auto m2 = a>0.;
301  std::cout << m1 << std::endl;
302  std::cout << m2 << std::endl;
303 
304 
305  TEST_EQUALITY( XOR(true), true );
306  TEST_EQUALITY( XOR(false), false );
307  TEST_EQUALITY( XOR(m1), true );
308  TEST_EQUALITY( XOR(!m1), false );
309  TEST_EQUALITY( XOR(m2), false );
310  TEST_EQUALITY( XOR(!m2), false );
311 }
312 
313 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_compared_to_double)
314 {
315  constexpr int ensemble_size = 8;
316 
317  typedef Kokkos::DefaultExecutionSpace execution_space;
319  typedef Sacado::MP::Vector<storage_type> scalar;
320 
321  scalar a = (scalar) 1.;
322  a[2] = 2.5;
323  scalar b = (scalar) 2.;
324 
325  auto m1 = a>b;
326  auto m2 = a>0.;
327 
328  std::cout << m1 << std::endl;
329  std::cout << m2 << std::endl;
330 
331  TEST_EQUALITY((double) m1,1./ensemble_size);
332  TEST_EQUALITY((double) m2,1.);
333 
334  TEST_EQUALITY(m1==1.,false);
335  TEST_EQUALITY(m1!=1.,true);
336  TEST_EQUALITY(m1==0.,false);
337  TEST_EQUALITY(m1!=0.,true);
338 
339  TEST_EQUALITY(m1>=0.5,false);
340  TEST_EQUALITY(m1<=0.5,true);
341  TEST_EQUALITY(m1>0.5,false);
342  TEST_EQUALITY(m1<0.5,true);
343 
344  TEST_EQUALITY(m2==1.,true);
345  TEST_EQUALITY(m2!=1.,false);
346  TEST_EQUALITY(m2==0.,false);
347  TEST_EQUALITY(m2!=0.,true);
348 
349  TEST_EQUALITY(m2>=0.5,true);
350  TEST_EQUALITY(m2<=0.5,false);
351  TEST_EQUALITY(m2>0.5,true);
352  TEST_EQUALITY(m2<0.5,false);
353 }
354 
355 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND_Mask)
356 {
357  constexpr int ensemble_size = 8;
358 
359  typedef Kokkos::DefaultExecutionSpace execution_space;
361  typedef Sacado::MP::Vector<storage_type> scalar;
362  typedef Mask<scalar> mask;
363 
364  scalar a = (scalar) 1.;
365  a[2] = 2.5;
366  scalar b = (scalar) 2.;
367 
368  auto m1 = a>b;
369  auto m2 = a>0.;
370 
371  auto m3 = m1 && m2;
372  std::cout << m1 << std::endl;
373  std::cout << m2 << std::endl;
374  std::cout << m3 << std::endl;
375  TEST_EQUALITY(m3,(const mask)m1);
376 }
377 
378 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR_Mask)
379 {
380  constexpr int ensemble_size = 8;
381 
382  typedef Kokkos::DefaultExecutionSpace execution_space;
384  typedef Sacado::MP::Vector<storage_type> scalar;
385  typedef Mask<scalar> mask;
386 
387  scalar a = (scalar) 1.;
388  a[2] = 2.5;
389  scalar b = (scalar) 2.;
390 
391  auto m1 = a>b;
392  auto m2 = a>0.;
393 
394  auto m3 = m1 || m2;
395  std::cout << m1 << std::endl;
396  std::cout << m2 << std::endl;
397  std::cout << m3 << std::endl;
398  TEST_EQUALITY(m3,(const mask)m2);
399 }
400 
401 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_ADD_Mask)
402 {
403  constexpr int ensemble_size = 8;
404 
405  typedef Kokkos::DefaultExecutionSpace execution_space;
407  typedef Sacado::MP::Vector<storage_type> scalar;
408  typedef Mask<scalar> mask;
409 
410  scalar a = (scalar) 1.;
411  a[2] = 2.5;
412  scalar b = (scalar) 2.;
413 
414  std::cout << a << std::endl;
415  std::cout << b << std::endl;
416 
417  auto m1 = a>b;
418  auto m2 = a>0.;
419  //std::cout << m1 << std::endl;
420  //std::cout << m2 << std::endl;
421  auto m3 = m1 + m2;
422 
423  std::cout << m3 << std::endl;
424  TEST_EQUALITY(m3,(const mask)m2);
425 }
426 
427 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_SUB_Mask)
428 {
429  constexpr int ensemble_size = 8;
430 
431  typedef Kokkos::DefaultExecutionSpace execution_space;
433  typedef Sacado::MP::Vector<storage_type> scalar;
434  typedef Mask<scalar> mask;
435 
436  scalar a = (scalar) 1.;
437  a[2] = 2.5;
438  scalar b = (scalar) 2.;
439 
440  auto m1 = a>b;
441  auto m2 = a>0.;
442  std::cout << m1 << std::endl;
443  std::cout << m2 << std::endl;
444  auto m3 = (a>0.) - (a>b);
445  std::cout << m3 << std::endl;
446  TEST_EQUALITY(m3,(const mask)!m1);
447 }
448 
449 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_signbit_v)
450 {
451  constexpr int ensemble_size = 8;
452 
453  typedef Kokkos::DefaultExecutionSpace execution_space;
455  typedef Sacado::MP::Vector<storage_type> scalar;
456  typedef Mask<scalar> mask;
457 
458  scalar a = (scalar) 1.;
459  a[2] = -2.5;
460 
461  auto m1 = signbit_v(a);
462  mask m2;
463  m2.set(2,true);
464  TEST_EQUALITY(m1,(const mask)m2);
465 }
466 
467 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_copysign)
468 {
469  constexpr int ensemble_size = 8;
470 
471  typedef Kokkos::DefaultExecutionSpace execution_space;
473  typedef Sacado::MP::Vector<storage_type> scalar;
474 
475  scalar a = (scalar) 1.;
476  a[2] = -2.5;
477 
478  scalar b = (scalar) 2.;
479 
480  using std::copysign;
481 
482  std::cout << std::endl;
483  std::cout << a << std::endl;
484  std::cout << b << std::endl;
485  b = copysign(b,a);
486  std::cout << a << std::endl;
487  std::cout << b << std::endl;
488  TEST_EQUALITY(b[2],-2.);
489 }
490 
491 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_assign)
492 {
493  constexpr int ensemble_size = 8;
494 
495  typedef Kokkos::DefaultExecutionSpace execution_space;
497  typedef Sacado::MP::Vector<storage_type> scalar;
498 
499  scalar a = (scalar) 1.;
500  a[2] = -2.5;
501 
502  mask_assign(a<=0.,a) = {0.,a};
503 
504  TEST_EQUALITY(a[1],1.);
505  TEST_EQUALITY(a[2],0.);
506 
507  double b = 1.;
508 
509  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
510 
511  TEST_EQUALITY(b,2.);
512 
513  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
514  TEST_EQUALITY(b,-1.);
515 
516 }
517 
518 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_pointer_assign)
519 {
520  constexpr int ensemble_size = 8;
521 
522  typedef Kokkos::DefaultExecutionSpace execution_space;
524  typedef Sacado::MP::Vector<storage_type> scalar;
525 
526  scalar a = (scalar) 1.;
527  a[2] = -2.5;
528  scalar *p = &a;
529 
530  mask_assign(a<=0.,*p) = {0.,a};
531 
532  TEST_EQUALITY(a[1],1.);
533  TEST_EQUALITY(a[2],0.);
534 }
535 
536 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_div)
537 {
538  constexpr int ensemble_size = 8;
539 
540  typedef Kokkos::DefaultExecutionSpace execution_space;
542  typedef Sacado::MP::Vector<storage_type> scalar;
543 
544  scalar a2 = {0.,2.,2.,2.,2.,2.,2.,2.};
545  std::cout << a2 << std::endl;
546 
547  scalar a = (scalar) 1.;
548  a[2] = -2.5;
549  auto m = (a>(scalar) 0.);
550  std::cout << "m is computed" << std::endl;
551  std::cout << m << std::endl;
552  m = a>0.;
553  std::cout << "m is computed" << std::endl;
554  std::cout << m << std::endl;
555 
556  std::cout << a << std::endl;
557  std::cout << m << std::endl;
558  std::cout << (a>=(scalar) 0. )<< std::endl;
559  std::cout << (a> 0. )<< std::endl;
560  std::cout << (a>= 0.) << std::endl;
561  std::cout << (0.<a )<< std::endl;
562  std::cout << (0.<=a) << std::endl;
563 
564  mask_assign<scalar>(m,a) /= {a, 2.,-1.};
565  TEST_EQUALITY(a[1],0.5);
566  TEST_EQUALITY(a[2],-1.);
567 
568  /*
569  This test is working only if c++ 14 is allowed due to the fact
570  that copy-list-initialization in the constructor of tuple is not allowed before
571  as it is an explicit one.
572 
573  mask_assign<scalar>(m,a) /= {(scalar) 4.,2.,-1.};
574  */
575  //std::tuple<scalar,scalar,scalar> ts {4.,2.,-1.};
576  mask_assign<scalar>(m,a) /= {4.,2.,-1.};
577  TEST_EQUALITY(a[1],2.);
578  TEST_EQUALITY(a[2],-1.);
579 
580 
581  double b = 1.;
582  mask_assign(b>0.5,b) /= {b, 2.,-1.};
583  TEST_EQUALITY(b,0.5);
584  mask_assign(b>0.5,b) /= {b, 2.,-1.};
585  TEST_EQUALITY(b,-1.);
586 
587 }
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)