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 //
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 
46 
48 
49 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Create_8)
50 {
51  constexpr int ensemble_size = 8;
52 
53  typedef Kokkos::DefaultExecutionSpace execution_space;
55  typedef Sacado::MP::Vector<storage_type> scalar;
56 
57  scalar a = (scalar) 1.;
58  a[0] = 2.5;
59  a[2] = 2.5;
60  scalar b = (scalar) 2.;
61 
62  auto m1 = a>b;
63  std::cout << std::endl;
64  std::cout << a << std::endl;
65  std::cout << b << std::endl;
66  std::cout << m1 << std::endl;
67  TEST_EQUALITY( m1.getSize(), ensemble_size );
68  TEST_EQUALITY( m1.get(0), true );
69  TEST_EQUALITY( m1.get(1), false );
70  TEST_EQUALITY( m1.get(2), true );
71  for (auto i=3; i<ensemble_size; ++i)
72  TEST_EQUALITY( m1.get(i), false );
73 
74  TEST_EQUALITY( (double) m1, 2./ensemble_size );
75 }
76 
77 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Create_16)
78 {
79  constexpr int ensemble_size = 16;
80 
81  typedef Kokkos::DefaultExecutionSpace execution_space;
83  typedef Sacado::MP::Vector<storage_type> scalar;
84 
85  scalar a = (scalar) 1.;
86  a[0] = 2.5;
87  a[2] = 2.5;
88  scalar b = (scalar) 2.;
89 
90  auto m1 = a>b;
91  std::cout << std::endl;
92  std::cout << a << std::endl;
93  std::cout << b << std::endl;
94  std::cout << m1 << std::endl;
95  TEST_EQUALITY( m1.getSize(), ensemble_size );
96  TEST_EQUALITY( m1.get(0), true );
97  TEST_EQUALITY( m1.get(1), false );
98  TEST_EQUALITY( m1.get(2), true );
99  for (auto i=3; i<ensemble_size; ++i)
100  TEST_EQUALITY( m1.get(i), false );
101 
102  TEST_EQUALITY( (double) m1, 2./ensemble_size );
103 }
104 
105 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Not_8)
106 {
107  constexpr int ensemble_size = 8;
108 
109  typedef Kokkos::DefaultExecutionSpace execution_space;
111  typedef Sacado::MP::Vector<storage_type> scalar;
112 
113  scalar a = (scalar) 1.;
114  a[0] = 2.5;
115  a[2] = 2.5;
116  scalar b = (scalar) 2.;
117 
118  auto m1 = a>b;
119  auto m2 = !m1;
120  std::cout << m1 << std::endl;
121  std::cout << m2 << std::endl;
122  for (auto i=0; i<ensemble_size; ++i)
123  TEST_EQUALITY( m2.get(i), !m1.get(i) );
124 }
125 
126 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Multiplication_8)
127 {
128  constexpr int ensemble_size = 8;
129 
130  typedef Kokkos::DefaultExecutionSpace execution_space;
132  typedef Sacado::MP::Vector<storage_type> scalar;
133 
134  scalar a = (scalar) 1.;
135  a[0] = 2.5;
136  a[2] = 2.5;
137  scalar b = (scalar) 2.;
138 
139  auto m1 = a>b;
140  scalar mul = m1*a;
141 
142  scalar mul2 = m1*b;
143  scalar mul3 = b*m1;
144 
145  std::cout << m1 << std::endl;
146  std::cout << mul << std::endl;
147 
148  std::cout << mul2 << std::endl;
149  std::cout << mul3 << std::endl;
150 
151  TEST_EQUALITY( mul[0], 2.5 );
152  TEST_EQUALITY( mul[1], 0. );
153  TEST_EQUALITY( mul[2], 2.5 );
154  for (auto i=3; i<ensemble_size; ++i)
155  TEST_EQUALITY( mul[i], 0. );
156 
157  TEST_EQUALITY( mul2, mul3 );
158 }
159 
160 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Multiplication_16)
161 {
162  constexpr int ensemble_size = 16;
163 
164  typedef Kokkos::DefaultExecutionSpace execution_space;
166  typedef Sacado::MP::Vector<storage_type> scalar;
167 
168  scalar a = (scalar) 1.;
169  a[0] = 2.5;
170  a[2] = 2.5;
171  scalar b = (scalar) 2.;
172 
173  auto m1 = a>b;
174  scalar mul = m1*a;
175  std::cout << m1 << std::endl;
176  std::cout << mul << std::endl;
177 
178  TEST_EQUALITY( mul[0], 2.5 );
179  TEST_EQUALITY( mul[1], 0. );
180  TEST_EQUALITY( mul[2], 2.5 );
181  for (auto i=3; i<ensemble_size; ++i)
182  TEST_EQUALITY( mul[i], 0. );
183 }
184 
185 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mul_Add_8)
186 {
187  constexpr int ensemble_size = 8;
188 
189  typedef Kokkos::DefaultExecutionSpace execution_space;
191  typedef Sacado::MP::Vector<storage_type> scalar;
192 
193  scalar a = (scalar) 1.;
194  a[0] = 2.5;
195  a[2] = 2.5;
196  scalar b = (scalar) 2.;
197 
198  auto m1 = a>b;
199  scalar mul = m1*a + !m1*b;
200  scalar mul2 = a*m1 + !m1*b;
201  std::cout << m1 << std::endl;
202  std::cout << mul << std::endl;
203  std::cout << mul2 << std::endl;
204 
205  TEST_EQUALITY( mul[0], 2.5 );
206  TEST_EQUALITY( mul[1], 2. );
207  TEST_EQUALITY( mul[2], 2.5 );
208  for (auto i=3; i<ensemble_size; ++i)
209  TEST_EQUALITY( mul[i], 2. );
210 
211  TEST_EQUALITY( mul, mul2 );
212 }
213 
214 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_DEFAULT)
215 {
216  constexpr int ensemble_size = 8;
217 
218  typedef Kokkos::DefaultExecutionSpace execution_space;
220  typedef Sacado::MP::Vector<storage_type> scalar;
221 
222  using namespace MaskLogic;
223 
224  scalar a = (scalar) 1.;
225  a[1] = 2.5;
226  a[2] = 2.5;
227  scalar b = (scalar) 2.;
228 
229  auto m1 = a>b;
230  auto m2 = a>(scalar) 0.;
231  auto m3 = a> 0.;
232  auto m4 = 0.<a;
233  std::cout << m1 << std::endl;
234  std::cout << m2 << std::endl;
235  std::cout << m3<< std::endl;
236  std::cout << m4<< std::endl;
237 
238  if (m1)
239  {TEST_EQUALITY( true, false );}
240  else
241  {TEST_EQUALITY( true, true );}
242 
243  TEST_EQUALITY((bool) m1, false );
244  TEST_EQUALITY((bool) !m1, true );
245 
246  if (m2)
247  {TEST_EQUALITY( true, true );}
248  else
249  {TEST_EQUALITY( true, false );}
250 
251  TEST_EQUALITY((bool) m2, true );
252  TEST_EQUALITY((bool) !m2, false );
253 
254  TEST_EQUALITY( m2, m3 );
255  TEST_EQUALITY( m2, m4 );
256 }
257 
258 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND)
259 {
260  constexpr int ensemble_size = 8;
261 
262  typedef Kokkos::DefaultExecutionSpace execution_space;
264  typedef Sacado::MP::Vector<storage_type> scalar;
265 
266  using namespace MaskLogic;
267 
268  scalar a = (scalar) 1.;
269  a[0] = 2.5;
270  a[2] = 2.5;
271  scalar b = (scalar) 2.;
272 
273  auto m1 = a>b;
274  auto m2 = a>0.;
275  std::cout << m1 << std::endl;
276  std::cout << m2 << std::endl;
277 
278 
279  TEST_EQUALITY( AND(true), true );
280  TEST_EQUALITY( AND(false), false );
281  TEST_EQUALITY( AND(m1), false );
282  TEST_EQUALITY( AND(!m1), false );
283  TEST_EQUALITY( AND(m2), true );
284  TEST_EQUALITY( AND(!m2), false );
285 }
286 
287 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR)
288 {
289  constexpr int ensemble_size = 8;
290 
291  typedef Kokkos::DefaultExecutionSpace execution_space;
293  typedef Sacado::MP::Vector<storage_type> scalar;
294 
295  using namespace MaskLogic;
296 
297  scalar a = (scalar) 1.;
298  a[0] = 2.5;
299  a[2] = 2.5;
300  scalar b = (scalar) 2.;
301 
302  auto m1 = a>b;
303  auto m2 = a>0.;
304  std::cout << m1 << std::endl;
305  std::cout << m2 << std::endl;
306 
307 
308  TEST_EQUALITY( OR(true), true );
309  TEST_EQUALITY( OR(false), false );
310  TEST_EQUALITY( OR(m1), true );
311  TEST_EQUALITY( OR(!m1), true );
312  TEST_EQUALITY( OR(m2), true );
313  TEST_EQUALITY( OR(!m2), false );
314 }
315 
316 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_XOR)
317 {
318  constexpr int ensemble_size = 8;
319 
320  typedef Kokkos::DefaultExecutionSpace execution_space;
322  typedef Sacado::MP::Vector<storage_type> scalar;
323 
324  using namespace MaskLogic;
325 
326  scalar a = (scalar) 1.;
327  a[2] = 2.5;
328  scalar b = (scalar) 2.;
329 
330  auto m1 = a>b;
331  auto m2 = a>0.;
332  std::cout << m1 << std::endl;
333  std::cout << m2 << std::endl;
334 
335 
336  TEST_EQUALITY( XOR(true), true );
337  TEST_EQUALITY( XOR(false), false );
338  TEST_EQUALITY( XOR(m1), true );
339  TEST_EQUALITY( XOR(!m1), false );
340  TEST_EQUALITY( XOR(m2), false );
341  TEST_EQUALITY( XOR(!m2), false );
342 }
343 
344 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_compared_to_double)
345 {
346  constexpr int ensemble_size = 8;
347 
348  typedef Kokkos::DefaultExecutionSpace execution_space;
350  typedef Sacado::MP::Vector<storage_type> scalar;
351 
352  scalar a = (scalar) 1.;
353  a[2] = 2.5;
354  scalar b = (scalar) 2.;
355 
356  auto m1 = a>b;
357  auto m2 = a>0.;
358 
359  std::cout << m1 << std::endl;
360  std::cout << m2 << std::endl;
361 
362  TEST_EQUALITY((double) m1,1./ensemble_size);
363  TEST_EQUALITY((double) m2,1.);
364 
365  TEST_EQUALITY(m1==1.,false);
366  TEST_EQUALITY(m1!=1.,true);
367  TEST_EQUALITY(m1==0.,false);
368  TEST_EQUALITY(m1!=0.,true);
369 
370  TEST_EQUALITY(m1>=0.5,false);
371  TEST_EQUALITY(m1<=0.5,true);
372  TEST_EQUALITY(m1>0.5,false);
373  TEST_EQUALITY(m1<0.5,true);
374 
375  TEST_EQUALITY(m2==1.,true);
376  TEST_EQUALITY(m2!=1.,false);
377  TEST_EQUALITY(m2==0.,false);
378  TEST_EQUALITY(m2!=0.,true);
379 
380  TEST_EQUALITY(m2>=0.5,true);
381  TEST_EQUALITY(m2<=0.5,false);
382  TEST_EQUALITY(m2>0.5,true);
383  TEST_EQUALITY(m2<0.5,false);
384 }
385 
386 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_AND_Mask)
387 {
388  constexpr int ensemble_size = 8;
389 
390  typedef Kokkos::DefaultExecutionSpace execution_space;
392  typedef Sacado::MP::Vector<storage_type> scalar;
393 
394  scalar a = (scalar) 1.;
395  a[2] = 2.5;
396  scalar b = (scalar) 2.;
397 
398  auto m1 = a>b;
399  auto m2 = a>0.;
400 
401  auto m3 = m1 && m2;
402  std::cout << m1 << std::endl;
403  std::cout << m2 << std::endl;
404  std::cout << m3 << std::endl;
405  TEST_EQUALITY(m3,m1);
406 }
407 
408 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_OR_Mask)
409 {
410  constexpr int ensemble_size = 8;
411 
412  typedef Kokkos::DefaultExecutionSpace execution_space;
414  typedef Sacado::MP::Vector<storage_type> scalar;
415 
416  scalar a = (scalar) 1.;
417  a[2] = 2.5;
418  scalar b = (scalar) 2.;
419 
420  auto m1 = a>b;
421  auto m2 = a>0.;
422 
423  auto m3 = m1 || m2;
424  std::cout << m1 << std::endl;
425  std::cout << m2 << std::endl;
426  std::cout << m3 << std::endl;
427  TEST_EQUALITY(m3,m2);
428 }
429 
430 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_ADD_Mask)
431 {
432  constexpr int ensemble_size = 8;
433 
434  typedef Kokkos::DefaultExecutionSpace execution_space;
436  typedef Sacado::MP::Vector<storage_type> scalar;
437 
438  scalar a = (scalar) 1.;
439  a[2] = 2.5;
440  scalar b = (scalar) 2.;
441 
442  std::cout << a << std::endl;
443  std::cout << b << std::endl;
444 
445  auto m1 = a>b;
446  auto m2 = a>0.;
447  //std::cout << m1 << std::endl;
448  //std::cout << m2 << std::endl;
449  auto m3 = m1 + m2;
450 
451  std::cout << m3 << std::endl;
452  TEST_EQUALITY(m3,m2);
453 }
454 
455 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_SUB_Mask)
456 {
457  constexpr int ensemble_size = 8;
458 
459  typedef Kokkos::DefaultExecutionSpace execution_space;
461  typedef Sacado::MP::Vector<storage_type> scalar;
462 
463  scalar a = (scalar) 1.;
464  a[2] = 2.5;
465  scalar b = (scalar) 2.;
466 
467  auto m1 = a>b;
468  auto m2 = a>0.;
469  std::cout << m1 << std::endl;
470  std::cout << m2 << std::endl;
471  auto m3 = (a>0.) - (a>b);
472  std::cout << m3 << std::endl;
473  TEST_EQUALITY(m3,!m1);
474 }
475 
476 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_signbit_v)
477 {
478  constexpr int ensemble_size = 8;
479 
480  typedef Kokkos::DefaultExecutionSpace execution_space;
482  typedef Sacado::MP::Vector<storage_type> scalar;
483  typedef Mask<scalar> mask;
484 
485  scalar a = (scalar) 1.;
486  a[2] = -2.5;
487 
488  auto m1 = signbit_v(a);
489  mask m2;
490  m2.set(2,true);
491  TEST_EQUALITY(m1,m2);
492 }
493 
494 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_copysign)
495 {
496  constexpr int ensemble_size = 8;
497 
498  typedef Kokkos::DefaultExecutionSpace execution_space;
500  typedef Sacado::MP::Vector<storage_type> scalar;
501 
502  scalar a = (scalar) 1.;
503  a[2] = -2.5;
504 
505  scalar b = (scalar) 2.;
506 
507  using std::copysign;
508 
509  std::cout << std::endl;
510  std::cout << a << std::endl;
511  std::cout << b << std::endl;
512  b = copysign(b,a);
513  std::cout << a << std::endl;
514  std::cout << b << std::endl;
515  TEST_EQUALITY(b[2],-2.);
516 }
517 
518 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_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 
529  mask_assign(a<=0.,a) = {0.,a};
530 
531  TEST_EQUALITY(a[1],1.);
532  TEST_EQUALITY(a[2],0.);
533 
534  double b = 1.;
535 
536  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
537 
538  TEST_EQUALITY(b,2.);
539 
540  mask_assign(b>0.5 && b<2.,b) = {2.*b,-1.};
541  TEST_EQUALITY(b,-1.);
542 
543 }
544 
545 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_pointer_assign)
546 {
547  constexpr int ensemble_size = 8;
548 
549  typedef Kokkos::DefaultExecutionSpace execution_space;
551  typedef Sacado::MP::Vector<storage_type> scalar;
552 
553  scalar a = (scalar) 1.;
554  a[2] = -2.5;
555  scalar *p = &a;
556 
557  mask_assign(a<=0.,*p) = {0.,a};
558 
559  TEST_EQUALITY(a[1],1.);
560  TEST_EQUALITY(a[2],0.);
561 }
562 
563 TEUCHOS_UNIT_TEST( MP_Vector_MaskTraits, Mask_div)
564 {
565  constexpr int ensemble_size = 8;
566 
567  typedef Kokkos::DefaultExecutionSpace execution_space;
569  typedef Sacado::MP::Vector<storage_type> scalar;
570 
571  scalar a2 = {0.,2.};
572  std::cout << a2 << std::endl;
573 
574  scalar a = (scalar) 1.;
575  a[2] = -2.5;
576  auto m = (a>(scalar) 0.);
577  std::cout << "m is computed" << std::endl;
578  std::cout << m << std::endl;
579  m = a>0.;
580  std::cout << "m is computed" << std::endl;
581  std::cout << m << std::endl;
582 
583  std::cout << a << std::endl;
584  std::cout << m << std::endl;
585  std::cout << (a>=(scalar) 0. )<< std::endl;
586  std::cout << (a> 0. )<< std::endl;
587  std::cout << (a>= 0.) << std::endl;
588  std::cout << (0.<a )<< std::endl;
589  std::cout << (0.<=a) << std::endl;
590 
591  mask_assign<scalar>(m,a) /= {a, 2.,-1.};
592  TEST_EQUALITY(a[1],0.5);
593  TEST_EQUALITY(a[2],-1.);
594 
595  /*
596  This test is working only if c++ 14 is allowed due to the fact
597  that copy-list-initialization in the constructor of tuple is not allowed before
598  as it is an explicit one.
599 
600  mask_assign<scalar>(m,a) /= {(scalar) 4.,2.,-1.};
601  */
602  //std::tuple<scalar,scalar,scalar> ts {4.,2.,-1.};
603  mask_assign<scalar>(m,a) /= {4.,2.,-1.};
604  TEST_EQUALITY(a[1],2.);
605  TEST_EQUALITY(a[2],-1.);
606 
607 
608  double b = 1.;
609  mask_assign(b>0.5,b) /= {b, 2.,-1.};
610  TEST_EQUALITY(b,0.5);
611  mask_assign(b>0.5,b) /= {b, 2.,-1.};
612  TEST_EQUALITY(b,-1.);
613 
614 }
615 
616 int main( int argc, char* argv[] ) {
617  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
618 
619  Kokkos::initialize();
620 // Kokkos::HostSpace::execution_space::initialize();
621 // if (!Kokkos::DefaultExecutionSpace::is_initialized())
622 // Kokkos::DefaultExecutionSpace::initialize();
623 
625 
626  Kokkos::finalize();
627 // Kokkos::HostSpace::execution_space::finalize();
628 // if (Kokkos::DefaultExecutionSpace::is_initialized())
629 // Kokkos::DefaultExecutionSpace::finalize();
630 
631  return res;
632 }
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)
static int runUnitTestsFromMain(int argc, char *argv[])
int main(int argc, char **argv)
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)