Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoPCECommTests.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 #include "Teuchos_CommHelpers.hpp"
15 #include "Teuchos_DefaultComm.hpp"
16 #include "Teuchos_Array.hpp"
17 #include "Teuchos_Comm.hpp"
18 
19 #include "Sacado.hpp"
20 #include "Stokhos_Sacado.hpp"
21 #include "Sacado_Fad_DFad.hpp"
22 #include "Sacado_mpl_apply.hpp"
23 #include "Sacado_Random.hpp"
24 
25 using Teuchos::RCP;
26 using Teuchos::rcp;
27 
28 // Common setup for unit tests
29 template <typename PCEType, typename FadType>
30 struct UnitTestSetup {
34 
37 
38  typedef typename Sacado::mpl::apply<FadType,PCEType>::type FadPCEType;
41  int sz;
42 
44  const int d = 2;
45  const int p = 7;
46 
47  // Create product basis
49  for (int i=0; i<d; i++)
50  bases[i] =
52  basis =
54 
55  // Triple product tensor
57 
58  // Expansion
59  exp =
61 
62  // Serializers
64  rcp(new PCESerializerT(
65  exp,
68 
69  sz = basis->size();
70  }
71 };
72 
73 template <typename PCEType>
75  const Teuchos::Array<PCEType>& x2,
76  const std::string& tag,
77  Teuchos::FancyOStream& out) {
78 
79  // Check sizes match
80  bool success = (x.size() == x2.size());
81  out << tag << " PCE array size test";
82  if (success)
83  out << " passed";
84  else
85  out << " failed";
86  out << ": \n\tExpected: " << x.size() << ", \n\tGot: " << x2.size()
87  << "." << std::endl;
88 
89  // Check Fads match
90  for (int i=0; i<x.size(); i++) {
91  bool success2 = Sacado::IsEqual<PCEType>::eval(x[i], x2[i]);
92  out << tag << " PCE array comparison test " << i;
93  if (success2)
94  out << " passed";
95  else
96  out << " failed";
97  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i] << "."
98  << std::endl;
99  success = success && success2;
100  }
101 
102  return success;
103 }
104 
105 template<typename Ordinal>
107  const Teuchos::Comm<Ordinal> &comm,
109  const bool result
110  )
111 {
112  out << "\nChecking that the above test passed in all processes ...";
113  int thisResult = ( result ? 1 : 0 );
114  int sumResult = -1;
115  Teuchos::reduceAll(comm,Teuchos::REDUCE_SUM,Ordinal(1),&thisResult,
116  &sumResult);
117  const bool passed = sumResult==Teuchos::size(comm);
118  if(passed)
119  out << " passed\n";
120  else
121  out << " (sumResult="<<sumResult<<"!=numProcs="<<Teuchos::size(comm)<<") failed\n";
122  return passed;
123 }
124 
125 #define PCE_COMM_TESTS(PCEType, FadType, PCE, FAD) \
126 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_Broadcast ) { \
127  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
128  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
129  \
130  int n = 7; \
131  Teuchos::Array<PCEType> x(n), x2(n); \
132  for (int i=0; i<n; i++) { \
133  x[i] = PCEType(setup.exp); \
134  for (int j=0; j<setup.sz; j++) \
135  x[i].fastAccessCoeff(j) = rnd.number(); \
136  } \
137  if (comm->getRank() == 0) \
138  x2 = x; \
139  Teuchos::broadcast(*comm, *setup.pce_serializer, 0, n, &x2[0]); \
140  success = checkPCEArrays(x, x2, std::string(#PCE)+" Broadcast", out); \
141  success = checkResultOnAllProcs(*comm, out, success); \
142 } \
143  \
144 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_GatherAll ) { \
145  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
146  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
147  \
148  int n = 7; \
149  int size = comm->getSize(); \
150  int rank = comm->getRank(); \
151  int N = n*size; \
152  Teuchos::Array<PCEType> x(n), x2(N), x3(N); \
153  for (int i=0; i<n; i++) { \
154  x[i] = PCEType(setup.exp); \
155  for (int j=0; j<setup.sz; j++) \
156  x[i].fastAccessCoeff(j) = (rank+1)*(i+1)*(j+1); \
157  } \
158  for (int j=0; j<size; j++) { \
159  for (int i=0; i<n; i++) { \
160  x3[n*j+i] = PCEType(setup.exp); \
161  for (int k=0; k<setup.sz; k++) \
162  x3[n*j+i].fastAccessCoeff(k) = (j+1)*(i+1)*(k+1); \
163  } \
164  } \
165  Teuchos::gatherAll(*comm, *setup.pce_serializer, \
166  n, &x[0], N, &x2[0]); \
167  success = checkPCEArrays(x3, x2, std::string(#PCE)+" Gather All", out); \
168  success = checkResultOnAllProcs(*comm, out, success); \
169 } \
170  \
171 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_SumAll ) { \
172  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
173  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
174  \
175  int n = 7; \
176  int num_proc = comm->getSize(); \
177  \
178  Teuchos::Array<PCEType> x(n), sums(n), sums2(n); \
179  for (int i=0; i<n; i++) { \
180  x[i] = PCEType(setup.exp); \
181  for (int j=0; j<setup.sz; j++) \
182  x[i].fastAccessCoeff(j) = 2.0*(i+1); \
183  } \
184  for (int i=0; i<n; i++) { \
185  sums[i] = PCEType(setup.exp); \
186  for (int j=0; j<setup.sz; j++) \
187  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc; \
188  } \
189  Teuchos::reduceAll(*comm, *setup.pce_serializer, \
190  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
191  success = checkPCEArrays(sums, sums2, \
192  std::string(#PCE)+" Sum All", out); \
193  success = checkResultOnAllProcs(*comm, out, success); \
194 } \
195  \
196 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_MaxAll ) { \
197  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
198  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
199  \
200  int n = 7; \
201  int rank = comm->getRank(); \
202  int num_proc = comm->getSize(); \
203  \
204  Teuchos::Array<PCEType> x(n), maxs(n), maxs2(n); \
205  for (int i=0; i<n; i++) { \
206  x[i] = PCEType(setup.exp); \
207  for (int j=0; j<setup.sz; j++) \
208  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
209  } \
210  for (int i=0; i<n; i++) { \
211  maxs[i] = PCEType(setup.exp); \
212  for (int j=0; j<setup.sz; j++) \
213  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc; \
214  } \
215  Teuchos::reduceAll(*comm, *setup.pce_serializer, \
216  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
217  success = checkPCEArrays(maxs, maxs2, \
218  std::string(#PCE)+" Max All", out); \
219  success = checkResultOnAllProcs(*comm, out, success); \
220 } \
221  \
222 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_MinAll ) { \
223  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
224  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
225  \
226  int n = 7; \
227  int rank = comm->getRank(); \
228  \
229  Teuchos::Array<PCEType> x(n), mins(n), mins2(n); \
230  for (int i=0; i<n; i++) { \
231  x[i] = PCEType(setup.exp); \
232  for (int j=0; j<setup.sz; j++) \
233  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
234  } \
235  for (int i=0; i<n; i++) { \
236  mins[i] = PCEType(setup.exp); \
237  for (int j=0; j<setup.sz; j++) \
238  mins[i].fastAccessCoeff(j) = 2.0*(i+1); \
239  } \
240  Teuchos::reduceAll(*comm, *setup.pce_serializer, \
241  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
242  success = checkPCEArrays(mins, mins2, \
243  std::string(#PCE)+" Min All", out); \
244  success = checkResultOnAllProcs(*comm, out, success); \
245 } \
246  \
247 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_ScanSum ) { \
248  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
249  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
250  \
251  int n = 7; \
252  int rank = comm->getRank(); \
253  \
254  Teuchos::Array<PCEType> x(n), sums(n), sums2(n); \
255  for (int i=0; i<n; i++) { \
256  x[i] = PCEType(setup.exp); \
257  for (int j=0; j<setup.sz; j++) \
258  x[i].fastAccessCoeff(j) = 2.0*(i+1); \
259  } \
260  for (int i=0; i<n; i++) { \
261  sums[i] = PCEType(setup.exp); \
262  for (int j=0; j<setup.sz; j++) \
263  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
264  } \
265  Teuchos::scan(*comm, *setup.pce_serializer, \
266  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
267  success = checkPCEArrays(sums, sums2, \
268  std::string(#PCE)+" Scan Sum", out); \
269  success = checkResultOnAllProcs(*comm, out, success); \
270 } \
271  \
272 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_ScanMax ) { \
273  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
274  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
275  \
276  int n = 7; \
277  int rank = comm->getRank(); \
278  \
279  Teuchos::Array<PCEType> x(n), maxs(n), maxs2(n); \
280  for (int i=0; i<n; i++) { \
281  x[i] = PCEType(setup.exp); \
282  for (int j=0; j<setup.sz; j++) \
283  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
284  } \
285  for (int i=0; i<n; i++) { \
286  maxs[i] = PCEType(setup.exp); \
287  for (int j=0; j<setup.sz; j++) \
288  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
289  } \
290  Teuchos::scan(*comm, *setup.pce_serializer, \
291  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
292  success = checkPCEArrays(maxs, maxs2, \
293  std::string(#PCE)+" Scan Max", out); \
294  success = checkResultOnAllProcs(*comm, out, success); \
295 } \
296  \
297 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_ScanMin ) { \
298  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
299  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
300  \
301  int n = 7; \
302  int rank = comm->getRank(); \
303  \
304  Teuchos::Array<PCEType> x(n), mins(n), mins2(n); \
305  for (int i=0; i<n; i++) { \
306  x[i] = PCEType(setup.exp); \
307  for (int j=0; j<setup.sz; j++) \
308  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
309  } \
310  for (int i=0; i<n; i++) { \
311  mins[i] = PCEType(setup.exp); \
312  for (int j=0; j<setup.sz; j++) \
313  mins[i].fastAccessCoeff(j) = 2.0*(i+1); \
314  } \
315  Teuchos::scan(*comm, *setup.pce_serializer, \
316  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
317  success = checkPCEArrays(mins, mins2, \
318  std::string(#PCE)+" Scan Min", out); \
319  success = checkResultOnAllProcs(*comm, out, success); \
320 } \
321  \
322 TEUCHOS_UNIT_TEST( PCE##_Comm, PCE_SendReceive ) { \
323  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
324  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
325  \
326  int num_proc = comm->getSize(); \
327  if (num_proc > 1) { \
328  int rank = comm->getRank(); \
329  int n = 7; \
330  Teuchos::Array<PCEType> x(n), x2(n); \
331  for (int i=0; i<n; i++) { \
332  x[i] = PCEType(setup.exp); \
333  for (int j=0; j<setup.sz; j++) \
334  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(j+1); \
335  } \
336  if (rank != 1) \
337  x2 = x; \
338  if (rank == 0) Teuchos::send(*comm, *setup.pce_serializer, \
339  n, &x[0], 1); \
340  if (rank == 1) Teuchos::receive(*comm, *setup.pce_serializer, \
341  0, n, &x2[0]); \
342  success = checkPCEArrays(x, x2, \
343  std::string(#PCE)+" Send/Receive", out); \
344  success = checkResultOnAllProcs(*comm, out, success); \
345  } \
346  else \
347  success = true; \
348 } \
349  \
350 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_Broadcast ) { \
351  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
352  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
353  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
354  \
355  int n = 7; \
356  int p = 5; \
357  Teuchos::Array<FadPCEType> x(n), x2(n); \
358  for (int i=0; i<n; i++) { \
359  PCEType f(setup.exp); \
360  for (int k=0; k<setup.sz; k++) \
361  f.fastAccessCoeff(k) = rnd.number(); \
362  x[i] = FadPCEType(p, f); \
363  for (int j=0; j<p; j++) { \
364  PCEType g(setup.exp); \
365  for (int k=0; k<setup.sz; k++) \
366  g.fastAccessCoeff(k) = rnd.number(); \
367  x[i].fastAccessDx(j) = g; \
368  } \
369  } \
370  if (comm->getRank() == 0) \
371  x2 = x; \
372  Teuchos::broadcast(*comm, *setup.fad_pce_serializer, 0, n, &x2[0]); \
373  success = checkPCEArrays(x, x2, \
374  std::string(#FAD)+"<"+#PCE+"> Broadcast", out); \
375  success = checkResultOnAllProcs(*comm, out, success); \
376 } \
377  \
378 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_GatherAll ) { \
379  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
380  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
381  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
382  \
383  int n = 7; \
384  int p = 5; \
385  int size = comm->getSize(); \
386  int rank = comm->getRank(); \
387  int N = n*size; \
388  Teuchos::Array<FadPCEType> x(n), x2(N), x3(N); \
389  for (int i=0; i<n; i++) { \
390  PCEType f(setup.exp); \
391  for (int k=0; k<setup.sz; k++) \
392  f.fastAccessCoeff(k) = (rank+1)*(i+1)*(k+1); \
393  x[i] = FadPCEType(p, f); \
394  for (int j=0; j<p; j++) { \
395  x[i].fastAccessDx(j) = f; \
396  } \
397  } \
398  for (int j=0; j<size; j++) { \
399  for (int i=0; i<n; i++) { \
400  PCEType f(setup.exp); \
401  for (int k=0; k<setup.sz; k++) \
402  f.fastAccessCoeff(k) = (j+1)*(i+1)*(k+1); \
403  x3[n*j+i] = FadPCEType(p, f); \
404  for (int k=0; k<p; k++) \
405  x3[n*j+i].fastAccessDx(k) = f; \
406  } \
407  } \
408  Teuchos::gatherAll(*comm, *setup.fad_pce_serializer, \
409  n, &x[0], N, &x2[0]); \
410  success = checkPCEArrays(x3, x2, \
411  std::string(#FAD)+"<"+#PCE+"> Gather All", out); \
412  success = checkResultOnAllProcs(*comm, out, success); \
413 } \
414  \
415 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_SumAll ) { \
416  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
417  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
418  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
419  \
420  int n = 7; \
421  int p = 5; \
422  int num_proc = comm->getSize(); \
423  \
424  Teuchos::Array<FadPCEType> x(n), sums(n), sums2(n); \
425  for (int i=0; i<n; i++) { \
426  PCEType f(setup.exp); \
427  for (int k=0; k<setup.sz; k++) \
428  f.fastAccessCoeff(k) = 2.0*(i+1); \
429  x[i] = FadPCEType(p, f); \
430  for (int j=0; j<p; j++) { \
431  PCEType g(setup.exp); \
432  for (int k=0; k<setup.sz; k++) \
433  g.fastAccessCoeff(k) = 2.0*(i+1); \
434  x[i].fastAccessDx(j) = g; \
435  } \
436  } \
437  for (int i=0; i<n; i++) { \
438  PCEType f(setup.exp); \
439  for (int k=0; k<setup.sz; k++) \
440  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc; \
441  sums[i] = FadPCEType(p, f); \
442  for (int j=0; j<p; j++) { \
443  PCEType g(setup.exp); \
444  for (int k=0; k<setup.sz; k++) \
445  g.fastAccessCoeff(k) = 2.0*(i+1)*num_proc; \
446  sums[i].fastAccessDx(j) = g; \
447  } \
448  } \
449  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer, \
450  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
451  success = checkPCEArrays(sums, sums2, \
452  std::string(#FAD)+"<"+#PCE+"> Sum All", out); \
453  success = checkResultOnAllProcs(*comm, out, success); \
454 } \
455  \
456 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_MaxAll ) { \
457  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
458  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
459  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
460  \
461  int n = 8; \
462  int p = 5; \
463  int rank = comm->getRank(); \
464  int num_proc = comm->getSize(); \
465  \
466  Teuchos::Array<FadPCEType> x(n), maxs(n), maxs2(n); \
467  for (int i=0; i<n; i++) { \
468  PCEType f(setup.exp); \
469  for (int k=0; k<setup.sz; k++) \
470  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
471  x[i] = FadPCEType(p, f); \
472  for (int j=0; j<p; j++) { \
473  x[i].fastAccessDx(j) = f; \
474  } \
475  } \
476  for (int i=0; i<n; i++) { \
477  PCEType f(setup.exp); \
478  for (int k=0; k<setup.sz; k++) \
479  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc; \
480  maxs[i] = FadPCEType(p, f); \
481  for (int j=0; j<p; j++) \
482  maxs[i].fastAccessDx(j) = f; \
483  } \
484  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer, \
485  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
486  success = checkPCEArrays(maxs, maxs2, \
487  std::string(#FAD)+"<"+#PCE+"> Max All", out); \
488  success = checkResultOnAllProcs(*comm, out, success); \
489 } \
490  \
491 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_MinAll ) { \
492  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
493  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
494  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
495  \
496  int n = 8; \
497  int p = 5; \
498  int rank = comm->getRank(); \
499  \
500  Teuchos::Array<FadPCEType> x(n), mins(n), mins2(n); \
501  for (int i=0; i<n; i++) { \
502  PCEType f(setup.exp); \
503  for (int k=0; k<setup.sz; k++) \
504  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
505  x[i] = FadPCEType(p, f); \
506  for (int j=0; j<p; j++) { \
507  x[i].fastAccessDx(j) = f; \
508  } \
509  } \
510  for (int i=0; i<n; i++) { \
511  PCEType f(setup.exp); \
512  for (int k=0; k<setup.sz; k++) \
513  f.fastAccessCoeff(k) = 2.0*(i+1); \
514  mins[i] = FadPCEType(p, f); \
515  for (int j=0; j<p; j++) \
516  mins[i].fastAccessDx(j) = f; \
517  } \
518  Teuchos::reduceAll(*comm, *setup.fad_pce_serializer, \
519  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
520  success = checkPCEArrays(mins, mins2, \
521  std::string(#FAD)+"<"+#PCE+"> Min All", out); \
522  success = checkResultOnAllProcs(*comm, out, success); \
523 } \
524  \
525 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_ScanSum ) { \
526  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
527  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
528  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
529  \
530  int n = 7; \
531  int p = 5; \
532  int rank = comm->getRank(); \
533  \
534  Teuchos::Array<FadPCEType> x(n), sums(n), sums2(n); \
535  for (int i=0; i<n; i++) { \
536  PCEType f(setup.exp); \
537  for (int k=0; k<setup.sz; k++) \
538  f.fastAccessCoeff(k) = 2.0*(i+1); \
539  x[i] = FadPCEType(p, f); \
540  for (int j=0; j<p; j++) { \
541  x[i].fastAccessDx(j) = f; \
542  } \
543  } \
544  for (int i=0; i<n; i++) { \
545  PCEType f(setup.exp); \
546  for (int k=0; k<setup.sz; k++) \
547  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
548  sums[i] = FadPCEType(p, f); \
549  for (int j=0; j<p; j++) \
550  sums[i].fastAccessDx(j) = f; \
551  } \
552  Teuchos::scan(*comm, *setup.fad_pce_serializer, \
553  Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
554  success = checkPCEArrays(sums, sums2, \
555  std::string(#FAD)+"<"+#PCE+"> Scan Sum", out); \
556  success = checkResultOnAllProcs(*comm, out, success); \
557 } \
558  \
559 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_ScanMax ) { \
560  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
561  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
562  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
563  \
564  int n = 7; \
565  int p = 5; \
566  int rank = comm->getRank(); \
567  \
568  Teuchos::Array<FadPCEType> x(n), maxs(n), maxs2(n); \
569  for (int i=0; i<n; i++) { \
570  PCEType f(setup.exp); \
571  for (int k=0; k<setup.sz; k++) \
572  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
573  x[i] = FadPCEType(p, f); \
574  for (int j=0; j<p; j++) { \
575  x[i].fastAccessDx(j) = f; \
576  } \
577  } \
578  for (int i=0; i<n; i++) { \
579  PCEType f(setup.exp); \
580  for (int k=0; k<setup.sz; k++) \
581  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
582  maxs[i] = FadPCEType(p, f); \
583  for (int j=0; j<p; j++) \
584  maxs[i].fastAccessDx(j) = f; \
585  } \
586  Teuchos::scan(*comm, *setup.fad_pce_serializer, \
587  Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
588  success = checkPCEArrays(maxs, maxs2, \
589  std::string(#FAD)+"<"+#PCE+"> Scan Max", out); \
590  success = checkResultOnAllProcs(*comm, out, success); \
591 } \
592  \
593 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_ScanMin ) { \
594  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
595  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
596  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
597  \
598  int n = 7; \
599  int p = 5; \
600  int rank = comm->getRank(); \
601  \
602  Teuchos::Array<FadPCEType> x(n), mins(n), mins2(n); \
603  for (int i=0; i<n; i++) { \
604  PCEType f(setup.exp); \
605  for (int k=0; k<setup.sz; k++) \
606  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
607  x[i] = FadPCEType(p, f); \
608  for (int j=0; j<p; j++) { \
609  x[i].fastAccessDx(j) = f; \
610  } \
611  } \
612  for (int i=0; i<n; i++) { \
613  PCEType f(setup.exp); \
614  for (int k=0; k<setup.sz; k++) \
615  f.fastAccessCoeff(k) = 2.0*(i+1); \
616  mins[i] = FadPCEType(p, f); \
617  for (int j=0; j<p; j++) \
618  mins[i].fastAccessDx(j) = f; \
619  } \
620  Teuchos::scan(*comm, *setup.fad_pce_serializer, \
621  Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
622  success = checkPCEArrays(mins, mins2, \
623  std::string(#FAD)+"<"+#PCE+"> Scan Min", out); \
624  success = checkResultOnAllProcs(*comm, out, success); \
625 } \
626  \
627 TEUCHOS_UNIT_TEST( PCE##_Comm, FadPCE_SendReceive ) { \
628  typedef Sacado::mpl::apply<FadType,PCEType>::type FadPCEType; \
629  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
630  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
631  \
632  int num_proc = comm->getSize(); \
633  if (num_proc > 1) { \
634  int rank = comm->getRank(); \
635  int n = 7; \
636  int p = 5; \
637  Teuchos::Array<FadPCEType> x(n), x2(n); \
638  for (int i=0; i<n; i++) { \
639  PCEType f(setup.exp); \
640  for (int k=0; k<setup.sz; k++) \
641  f.fastAccessCoeff(k) = 2.0*(i+1)*(k+1); \
642  x[i] = FadPCEType(p, f); \
643  for (int j=0; j<p; j++) \
644  x[i].fastAccessDx(j) = f; \
645  } \
646  if (rank != 1) \
647  x2 = x; \
648  if (rank == 0) Teuchos::send(*comm, *setup.fad_pce_serializer, \
649  n, &x[0], 1); \
650  if (rank == 1) Teuchos::receive(*comm, *setup.fad_pce_serializer, \
651  0, n, &x2[0]); \
652  success = checkPCEArrays(x, x2, \
653  std::string(#FAD)+"<"+#PCE+"> Send/Receive", out); \
654  success = checkResultOnAllProcs(*comm, out, success); \
655  } \
656  else \
657  success = true; \
658 }
659 
660 typedef int Ordinal;
662 typedef Sacado::Fad::DFad<double> fad_type;
663 namespace PCETest {
664  Sacado::Random<double> rnd;
667  PCE_COMM_TESTS(pce_type, fad_type, OrthogPoly, DFad)
668 }
669 
670 namespace ETPCETest {
671  Sacado::Random<double> rnd;
674  PCE_COMM_TESTS(pce_type, fad_type, ETOrthogPoly, DFad)
675 }
676 
677 int main( int argc, char* argv[] ) {
678  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
680 }
Stokhos::StandardStorage< int, double > storage_type
Sacado::ETPCE::OrthogPoly< double, storage_type > pce_type
RCP< FadPCESerializerT > fad_pce_serializer
UnitTestSetup< pce_type, fad_type > setup
RCP< const Stokhos::CompletePolynomialBasis< int, double > > basis
RCP< PCESerializerT > pce_serializer
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[])
#define PCE_COMM_TESTS(PCEType, FadType, PCE, FAD)
Sacado::Fad::DFad< double > fad_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Sacado::Random< double > rnd
virtual Teuchos::RCP< Stokhos::Sparse3Tensor< ordinal_type, value_type > > computeTripleProductTensor() const
Compute triple product tensor.
UnitTestSetup< pce_type, fad_type > setup
Sacado::mpl::apply< FadType, PCEType >::type FadPCEType
Legendre polynomial basis.
Sacado::PCE::OrthogPoly< double, storage_type > pce_type
int main(int argc, char **argv)
size_type size() const
bool checkResultOnAllProcs(const Teuchos::Comm< Ordinal > &comm, Teuchos::FancyOStream &out, const bool result)
virtual ordinal_type size() const
Return total size of basis.
Sacado::Random< double > rnd
RCP< Stokhos::AlgebraicOrthogPolyExpansion< int, double > > exp
Sacado::Fad::DFad< double > fad_type
RCP< Stokhos::Sparse3Tensor< int, double > > Cijk
Teuchos::ValueTypeSerializer< int, PCEType > PCESerializerT