Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoUQPCECommTests.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
45 #include "Teuchos_CommHelpers.hpp"
46 #include "Teuchos_DefaultComm.hpp"
47 #include "Teuchos_Array.hpp"
48 #include "Teuchos_Comm.hpp"
49 
50 #include "Sacado.hpp"
51 #include "Stokhos.hpp"
53 #include "Sacado_Fad_DFad.hpp"
54 #include "Sacado_mpl_apply.hpp"
55 #include "Sacado_Random.hpp"
56 
57 using Teuchos::RCP;
58 using Teuchos::rcp;
59 
60 // Common setup for unit tests
61 template <typename PCEType, typename FadType>
62 struct UnitTestSetup {
65 
66  typedef typename PCEType::cijk_type kokkos_cijk_type;
68 
71 
72  typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
75  int sz;
76 
78  const int d = 2;
79  const int p = 7;
80 
81  // Create product basis
83  for (int i=0; i<d; i++)
84  bases[i] =
86  basis =
88 
89  // Triple product tensor
92  kokkos_cijk =
93  Stokhos::create_product_tensor<execution_space>(*basis, *Cijk);
94 
95  // Serializers
97  rcp(new PCESerializerT(
100  fad_pce_serializer = rcp(new FadPCESerializerT(pce_serializer, 5));
101 
102  sz = basis->size();
103  }
104 };
105 
106 template <typename PCEType>
108  const Teuchos::Array<PCEType>& x2,
109  const std::string& tag,
110  Teuchos::FancyOStream& out) {
111 
112  // Check sizes match
113  bool success = (x.size() == x2.size());
114  out << tag << " PCE array size test";
115  if (success)
116  out << " passed";
117  else
118  out << " failed";
119  out << ": \n\tExpected: " << x.size() << ", \n\tGot: " << x2.size()
120  << "." << std::endl;
121 
122  // Check Fads match
123  for (int i=0; i<x.size(); i++) {
124  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
125  out << tag << " PCE array comparison test " << i;
126  if (success2)
127  out << " passed";
128  else
129  out << " failed";
130  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i] << "."
131  << std::endl;
132  success = success && success2;
133  }
134 
135  return success;
136 }
137 
138 template<typename Ordinal>
140  const Teuchos::Comm<Ordinal> &comm,
142  const bool result
143  )
144 {
145  out << "\nChecking that the above test passed in all processes ...";
146  int thisResult = ( result ? 1 : 0 );
147  int sumResult = -1;
148  Teuchos::reduceAll(comm,Teuchos::REDUCE_SUM,Ordinal(1),&thisResult,
149  &sumResult);
150  const bool passed = sumResult==Teuchos::size(comm);
151  if(passed)
152  out << " passed\n";
153  else
154  out << " (sumResult="<<sumResult<<"!=numProcs="<<Teuchos::size(comm)<<") failed\n";
155  return passed;
156 }
157 
158 typedef int Ordinal;
159 typedef Sacado::Fad::DFad<double> FadType;
160 typedef Kokkos::DefaultExecutionSpace execution_space;
163 
164 Sacado::Random<double> rnd;
165 
166 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_Broadcast ) {
170 
171  int n = 7;
172  Teuchos::Array<PCEType> x(n), x2(n);
173  for (int i=0; i<n; i++) {
174  x[i].reset(setup.kokkos_cijk);
175  for (int j=0; j<setup.sz; j++)
176  x[i].fastAccessCoeff(j) = rnd.number();
177  }
178  if (comm->getRank() == 0)
179  x2 = x;
180  Teuchos::broadcast(*comm, *setup.pce_serializer, 0, n, &x2[0]);
181  success = checkPCEArrays(x, x2, std::string("UQ::PCE")+" Broadcast", out);
182  success = checkResultOnAllProcs(*comm, out, success);
183 }
184 
185 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_GatherAll ) {
189 
190  int n = 7;
191  int size = comm->getSize();
192  int rank = comm->getRank();
193  int N = n*size;
194  Teuchos::Array<PCEType> x(n), x2(N), x3(N);
195  for (int i=0; i<n; i++) {
196  x[i].reset(setup.kokkos_cijk);
197  for (int j=0; j<setup.sz; j++)
198  x[i].fastAccessCoeff(j) = (rank+1)*(i+1)*(j+1);
199  }
200  for (int j=0; j<size; j++) {
201  for (int i=0; i<n; i++) {
202  x3[n*j+i].reset(setup.kokkos_cijk);
203  for (int k=0; k<setup.sz; k++)
204  x3[n*j+i].fastAccessCoeff(k) = (j+1)*(i+1)*(k+1);
205  }
206  }
207  Teuchos::gatherAll(*comm, *setup.pce_serializer,
208  n, &x[0], N, &x2[0]);
209  success = checkPCEArrays(x3, x2, std::string("UQ::PCE")+" Gather All", out);
210  success = checkResultOnAllProcs(*comm, out, success);
211 }
212 
213 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_SumAll ) {
217 
218  int n = 7;
219  int num_proc = comm->getSize();
220 
221  Teuchos::Array<PCEType> x(n), sums(n), sums2(n);
222  for (int i=0; i<n; i++) {
223  x[i].reset(setup.kokkos_cijk);
224  for (int j=0; j<setup.sz; j++)
225  x[i].fastAccessCoeff(j) = 2.0*(i+1);
226  }
227  for (int i=0; i<n; i++) {
228  sums[i].reset(setup.kokkos_cijk);
229  for (int j=0; j<setup.sz; j++)
230  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc;
231  }
232  Teuchos::reduceAll(*comm, *setup.pce_serializer,
233  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]);
234  success = checkPCEArrays(sums, sums2,
235  std::string("UQ::PCE")+" Sum All", out);
236  success = checkResultOnAllProcs(*comm, out, success);
237 }
238 
239 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_MaxAll ) {
243 
244  int n = 7;
245  int rank = comm->getRank();
246  int num_proc = comm->getSize();
247 
248  Teuchos::Array<PCEType> x(n), maxs(n), maxs2(n);
249  for (int i=0; i<n; i++) {
250  x[i].reset(setup.kokkos_cijk);
251  for (int j=0; j<setup.sz; j++)
252  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
253  }
254  for (int i=0; i<n; i++) {
255  maxs[i].reset(setup.kokkos_cijk);
256  for (int j=0; j<setup.sz; j++)
257  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc;
258  }
259  Teuchos::reduceAll(*comm, *setup.pce_serializer,
260  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]);
261  success = checkPCEArrays(maxs, maxs2,
262  std::string("UQ::PCE")+" Max All", out);
263  success = checkResultOnAllProcs(*comm, out, success);
264 }
265 
266 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_MinAll ) {
270 
271  int n = 7;
272  int rank = comm->getRank();
273 
274  Teuchos::Array<PCEType> x(n), mins(n), mins2(n);
275  for (int i=0; i<n; i++) {
276  x[i].reset(setup.kokkos_cijk);
277  for (int j=0; j<setup.sz; j++)
278  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
279  }
280  for (int i=0; i<n; i++) {
281  mins[i].reset(setup.kokkos_cijk);
282  for (int j=0; j<setup.sz; j++)
283  mins[i].fastAccessCoeff(j) = 2.0*(i+1);
284  }
285  Teuchos::reduceAll(*comm, *setup.pce_serializer,
286  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]);
287  success = checkPCEArrays(mins, mins2,
288  std::string("UQ::PCE")+" Min All", out);
289  success = checkResultOnAllProcs(*comm, out, success);
290 }
291 
292 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_ScanSum ) {
296 
297  int n = 7;
298  int rank = comm->getRank();
299 
300  Teuchos::Array<PCEType> x(n), sums(n), sums2(n);
301  for (int i=0; i<n; i++) {
302  x[i].reset(setup.kokkos_cijk);
303  for (int j=0; j<setup.sz; j++)
304  x[i].fastAccessCoeff(j) = 2.0*(i+1);
305  }
306  for (int i=0; i<n; i++) {
307  sums[i].reset(setup.kokkos_cijk);
308  for (int j=0; j<setup.sz; j++)
309  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
310  }
311  Teuchos::scan(*comm, *setup.pce_serializer,
312  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]);
313  success = checkPCEArrays(sums, sums2,
314  std::string("UQ::PCE")+" Scan Sum", out);
315  success = checkResultOnAllProcs(*comm, out, success);
316 }
317 
318 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_ScanMax ) {
322 
323  int n = 7;
324  int rank = comm->getRank();
325 
326  Teuchos::Array<PCEType> x(n), maxs(n), maxs2(n);
327  for (int i=0; i<n; i++) {
328  x[i].reset(setup.kokkos_cijk);
329  for (int j=0; j<setup.sz; j++)
330  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
331  }
332  for (int i=0; i<n; i++) {
333  maxs[i].reset(setup.kokkos_cijk);
334  for (int j=0; j<setup.sz; j++)
335  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
336  }
337  Teuchos::scan(*comm, *setup.pce_serializer,
338  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]);
339  success = checkPCEArrays(maxs, maxs2,
340  std::string("UQ::PCE")+" Scan Max", out);
341  success = checkResultOnAllProcs(*comm, out, success);
342 }
343 
344 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_ScanMin ) {
348 
349  int n = 7;
350  int rank = comm->getRank();
351 
352  Teuchos::Array<PCEType> x(n), mins(n), mins2(n);
353  for (int i=0; i<n; i++) {
354  x[i].reset(setup.kokkos_cijk);
355  for (int j=0; j<setup.sz; j++)
356  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1);
357  }
358  for (int i=0; i<n; i++) {
359  mins[i].reset(setup.kokkos_cijk);
360  for (int j=0; j<setup.sz; j++)
361  mins[i].fastAccessCoeff(j) = 2.0*(i+1);
362  }
363  Teuchos::scan(*comm, *setup.pce_serializer,
364  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]);
365  success = checkPCEArrays(mins, mins2,
366  std::string("UQ::PCE")+" Scan Min", out);
367  success = checkResultOnAllProcs(*comm, out, success);
368 }
369 
370 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, PCE_SendReceive ) {
374 
375  int num_proc = comm->getSize();
376  if (num_proc > 1) {
377  int rank = comm->getRank();
378  int n = 7;
379  Teuchos::Array<PCEType> x(n), x2(n);
380  for (int i=0; i<n; i++) {
381  x[i].reset(setup.kokkos_cijk);
382  for (int j=0; j<setup.sz; j++)
383  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(j+1);
384  }
385  if (rank != 1)
386  x2 = x;
387  if (rank == 0) Teuchos::send(*comm, *setup.pce_serializer,
388  n, &x[0], 1);
389  if (rank == 1) Teuchos::receive(*comm, *setup.pce_serializer,
390  0, n, &x2[0]);
391  success = checkPCEArrays(x, x2,
392  std::string("UQ::PCE")+" Send/Receive", out);
393  success = checkResultOnAllProcs(*comm, out, success);
394  }
395  else
396  success = true;
397 }
398 
399 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_Broadcast ) {
400  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
404 
405  int n = 7;
406  int p = 5;
407  Teuchos::Array<FadPCEType> x(n), x2(n);
408  for (int i=0; i<n; i++) {
409  PCEType f(setup.kokkos_cijk);
410  for (int k=0; k<setup.sz; k++)
411  f.fastAccessCoeff(k) = rnd.number();
412  x[i] = FadPCEType(p, f);
413  for (int j=0; j<p; j++) {
414  PCEType g(setup.kokkos_cijk);
415  for (int k=0; k<setup.sz; k++)
416  g.fastAccessCoeff(k) = rnd.number();
417  x[i].fastAccessDx(j) = g;
418  }
419  }
420  if (comm->getRank() == 0)
421  x2 = x;
422  Teuchos::broadcast(*comm, *setup.fad_pce_serializer, 0, n, &x2[0]);
423  success = checkPCEArrays(x, x2,
424  std::string("DFad")+"<"+"UQ::PCE"+"> Broadcast", out);
425  success = checkResultOnAllProcs(*comm, out, success);
426 }
427 
428 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_GatherAll ) {
429  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
433 
434  int n = 7;
435  int p = 5;
436  int size = comm->getSize();
437  int rank = comm->getRank();
438  int N = n*size;
439  Teuchos::Array<FadPCEType> x(n), x2(N), x3(N);
440  for (int i=0; i<n; i++) {
441  PCEType f(setup.kokkos_cijk);
442  for (int k=0; k<setup.sz; k++)
443  f.fastAccessCoeff(k) = (rank+1)*(i+1)*(k+1);
444  x[i] = FadPCEType(p, f);
445  for (int j=0; j<p; j++) {
446  x[i].fastAccessDx(j) = f;
447  }
448  }
449  for (int j=0; j<size; j++) {
450  for (int i=0; i<n; i++) {
451  PCEType f(setup.kokkos_cijk);
452  for (int k=0; k<setup.sz; k++)
453  f.fastAccessCoeff(k) = (j+1)*(i+1)*(k+1);
454  x3[n*j+i] = FadPCEType(p, f);
455  for (int k=0; k<p; k++)
456  x3[n*j+i].fastAccessDx(k) = f;
457  }
458  }
459  Teuchos::gatherAll(*comm, *setup.fad_pce_serializer,
460  n, &x[0], N, &x2[0]);
461  success = checkPCEArrays(x3, x2,
462  std::string("DFad")+"<"+"UQ::PCE"+"> Gather All", out);
463  success = checkResultOnAllProcs(*comm, out, success);
464 }
465 
466 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_SumAll ) {
467  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
471 
472  int n = 7;
473  int p = 5;
474  int num_proc = comm->getSize();
475 
476  Teuchos::Array<FadPCEType> x(n), sums(n), sums2(n);
477  for (int i=0; i<n; i++) {
478  PCEType f(setup.kokkos_cijk);
479  for (int k=0; k<setup.sz; k++)
480  f.fastAccessCoeff(k) = 2.0*(i+1);
481  x[i] = FadPCEType(p, f);
482  for (int j=0; j<p; j++) {
483  PCEType g(setup.kokkos_cijk);
484  for (int k=0; k<setup.sz; k++)
485  g.fastAccessCoeff(k) = 2.0*(i+1);
486  x[i].fastAccessDx(j) = g;
487  }
488  }
489  for (int i=0; i<n; i++) {
490  PCEType f(setup.kokkos_cijk);
491  for (int k=0; k<setup.sz; k++)
492  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc;
493  sums[i] = FadPCEType(p, f);
494  for (int j=0; j<p; j++) {
495  PCEType g(setup.kokkos_cijk);
496  for (int k=0; k<setup.sz; k++)
497  g.fastAccessCoeff(k) = 2.0*(i+1)*num_proc;
498  sums[i].fastAccessDx(j) = g;
499  }
500  }
501  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer,
502  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]);
503  success = checkPCEArrays(sums, sums2,
504  std::string("DFad")+"<"+"UQ::PCE"+"> Sum All", out);
505  success = checkResultOnAllProcs(*comm, out, success);
506 }
507 
508 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_MaxAll ) {
509  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
513 
514  int n = 8;
515  int p = 5;
516  int rank = comm->getRank();
517  int num_proc = comm->getSize();
518 
519  Teuchos::Array<FadPCEType> x(n), maxs(n), maxs2(n);
520  for (int i=0; i<n; i++) {
521  PCEType f(setup.kokkos_cijk);
522  for (int k=0; k<setup.sz; k++)
523  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
524  x[i] = FadPCEType(p, f);
525  for (int j=0; j<p; j++) {
526  x[i].fastAccessDx(j) = f;
527  }
528  }
529  for (int i=0; i<n; i++) {
530  PCEType f(setup.kokkos_cijk);
531  for (int k=0; k<setup.sz; k++)
532  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc;
533  maxs[i] = FadPCEType(p, f);
534  for (int j=0; j<p; j++)
535  maxs[i].fastAccessDx(j) = f;
536  }
537  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer,
538  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]);
539  success = checkPCEArrays(maxs, maxs2,
540  std::string("DFad")+"<"+"UQ::PCE"+"> Max All", out);
541  success = checkResultOnAllProcs(*comm, out, success);
542 }
543 
544 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_MinAll ) {
545  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
549 
550  int n = 8;
551  int p = 5;
552  int rank = comm->getRank();
553 
554  Teuchos::Array<FadPCEType> x(n), mins(n), mins2(n);
555  for (int i=0; i<n; i++) {
556  PCEType f(setup.kokkos_cijk);
557  for (int k=0; k<setup.sz; k++)
558  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
559  x[i] = FadPCEType(p, f);
560  for (int j=0; j<p; j++) {
561  x[i].fastAccessDx(j) = f;
562  }
563  }
564  for (int i=0; i<n; i++) {
565  PCEType f(setup.kokkos_cijk);
566  for (int k=0; k<setup.sz; k++)
567  f.fastAccessCoeff(k) = 2.0*(i+1);
568  mins[i] = FadPCEType(p, f);
569  for (int j=0; j<p; j++)
570  mins[i].fastAccessDx(j) = f;
571  }
572  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer,
573  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]);
574  success = checkPCEArrays(mins, mins2,
575  std::string("DFad")+"<"+"UQ::PCE"+"> Min All", out);
576  success = checkResultOnAllProcs(*comm, out, success);
577 }
578 
579 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_ScanSum ) {
580  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
584 
585  int n = 7;
586  int p = 5;
587  int rank = comm->getRank();
588 
589  Teuchos::Array<FadPCEType> x(n), sums(n), sums2(n);
590  for (int i=0; i<n; i++) {
591  PCEType f(setup.kokkos_cijk);
592  for (int k=0; k<setup.sz; k++)
593  f.fastAccessCoeff(k) = 2.0*(i+1);
594  x[i] = FadPCEType(p, f);
595  for (int j=0; j<p; j++) {
596  x[i].fastAccessDx(j) = f;
597  }
598  }
599  for (int i=0; i<n; i++) {
600  PCEType f(setup.kokkos_cijk);
601  for (int k=0; k<setup.sz; k++)
602  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
603  sums[i] = FadPCEType(p, f);
604  for (int j=0; j<p; j++)
605  sums[i].fastAccessDx(j) = f;
606  }
607  Teuchos::scan(*comm, *setup.fad_pce_serializer,
608  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]);
609  success = checkPCEArrays(sums, sums2,
610  std::string("DFad")+"<"+"UQ::PCE"+"> Scan Sum", out);
611  success = checkResultOnAllProcs(*comm, out, success);
612 }
613 
614 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_ScanMax ) {
615  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
619 
620  int n = 7;
621  int p = 5;
622  int rank = comm->getRank();
623 
624  Teuchos::Array<FadPCEType> x(n), maxs(n), maxs2(n);
625  for (int i=0; i<n; i++) {
626  PCEType f(setup.kokkos_cijk);
627  for (int k=0; k<setup.sz; k++)
628  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
629  x[i] = FadPCEType(p, f);
630  for (int j=0; j<p; j++) {
631  x[i].fastAccessDx(j) = f;
632  }
633  }
634  for (int i=0; i<n; i++) {
635  PCEType f(setup.kokkos_cijk);
636  for (int k=0; k<setup.sz; k++)
637  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
638  maxs[i] = FadPCEType(p, f);
639  for (int j=0; j<p; j++)
640  maxs[i].fastAccessDx(j) = f;
641  }
642  Teuchos::scan(*comm, *setup.fad_pce_serializer,
643  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]);
644  success = checkPCEArrays(maxs, maxs2,
645  std::string("DFad")+"<"+"UQ::PCE"+"> Scan Max", out);
646  success = checkResultOnAllProcs(*comm, out, success);
647 }
648 
649 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_ScanMin ) {
650  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
654 
655  int n = 7;
656  int p = 5;
657  int rank = comm->getRank();
658 
659  Teuchos::Array<FadPCEType> x(n), mins(n), mins2(n);
660  for (int i=0; i<n; i++) {
661  PCEType f(setup.kokkos_cijk);
662  for (int k=0; k<setup.sz; k++)
663  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1);
664  x[i] = FadPCEType(p, f);
665  for (int j=0; j<p; j++) {
666  x[i].fastAccessDx(j) = f;
667  }
668  }
669  for (int i=0; i<n; i++) {
670  PCEType f(setup.kokkos_cijk);
671  for (int k=0; k<setup.sz; k++)
672  f.fastAccessCoeff(k) = 2.0*(i+1);
673  mins[i] = FadPCEType(p, f);
674  for (int j=0; j<p; j++)
675  mins[i].fastAccessDx(j) = f;
676  }
677  Teuchos::scan(*comm, *setup.fad_pce_serializer,
678  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]);
679  success = checkPCEArrays(mins, mins2,
680  std::string("DFad")+"<"+"UQ::PCE"+"> Scan Min", out);
681  success = checkResultOnAllProcs(*comm, out, success);
682 }
683 
684 TEUCHOS_UNIT_TEST( UQ_PCE_Comm, FadPCE_SendReceive ) {
685  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
689 
690  int num_proc = comm->getSize();
691  if (num_proc > 1) {
692  int rank = comm->getRank();
693  int n = 7;
694  int p = 5;
695  Teuchos::Array<FadPCEType> x(n), x2(n);
696  for (int i=0; i<n; i++) {
697  PCEType f(setup.kokkos_cijk);
698  for (int k=0; k<setup.sz; k++)
699  f.fastAccessCoeff(k) = 2.0*(i+1)*(k+1);
700  x[i] = FadPCEType(p, f);
701  for (int j=0; j<p; j++)
702  x[i].fastAccessDx(j) = f;
703  }
704  if (rank != 1)
705  x2 = x;
706  if (rank == 0) Teuchos::send(*comm, *setup.fad_pce_serializer,
707  n, &x[0], 1);
708  if (rank == 1) Teuchos::receive(*comm, *setup.fad_pce_serializer,
709  0, n, &x2[0]);
710  success = checkPCEArrays(x, x2,
711  std::string("DFad")+"<"+"UQ::PCE"+"> Send/Receive", out);
712  success = checkResultOnAllProcs(*comm, out, success);
713  }
714  else
715  success = true;
716 }
717 
718 int main( int argc, char* argv[] ) {
719  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
720 
721  Kokkos::initialize();
722 // Kokkos::HostSpace::execution_space::initialize();
723 // if (!Kokkos::DefaultExecutionSpace::is_initialized())
724 // Kokkos::DefaultExecutionSpace::initialize();
725 
727 
728  Kokkos::finalize();
729 // Kokkos::HostSpace::execution_space::finalize();
730 // if (Kokkos::DefaultExecutionSpace::is_initialized())
731 // Kokkos::DefaultExecutionSpace::finalize();
732 
733  return res;
734 }
Stokhos::StandardStorage< int, double > storage_type
RCP< FadPCESerializerT > fad_pce_serializer
Kokkos::DefaultExecutionSpace execution_space
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
static Teuchos::RCP< const Comm< OrdinalType > > getComm()
bool checkPCEArrays(const Teuchos::Array< PCEType > &x, const Teuchos::Array< PCEType > &x2, const std::string &tag, Teuchos::FancyOStream &out)
Teuchos::ValueTypeSerializer< int, FadPCEType > FadPCESerializerT
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Sacado::Fad::DFad< double > FadType
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
expr1 expr1 expr1 expr2 expr1 expr1 c expr2 expr1 c fastAccessCoeff(j)-expr2.val(j)
Sacado::Random< double > rnd
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
int main(int argc, char **argv)
Sacado::UQ::PCE< storage_type > PCEType
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
size_type size() const
bool checkResultOnAllProcs(const Teuchos::Comm< Ordinal > &comm, Teuchos::FancyOStream &out, const bool result)
TEUCHOS_UNIT_TEST(tAdaptivityManager, test_interface)
PCEType::cijk_type kokkos_cijk_type
int n
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT