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