Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tay_CommTests.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_CommHelpers.hpp"
12 #include "Teuchos_DefaultComm.hpp"
13 #include "Teuchos_Array.hpp"
14 #include "Teuchos_Comm.hpp"
15 
16 #include "Sacado_mpl_apply.hpp"
17 #include "Sacado_Random.hpp"
18 
19 using Teuchos::RCP;
20 using Teuchos::rcp;
22 
23 template <typename TayType>
25  const Teuchos::Array<TayType>& x2,
26  const std::string& tag,
27  Teuchos::FancyOStream& out) {
28 
29  // Check sizes match
30  bool success = (x.size() == x2.size());
31  out << tag << " Taylor array size test";
32  if (success)
33  out << " passed";
34  else
35  out << " failed";
36  out << ": \n\tExpected: " << x.size() << ", \n\tGot: " << x2.size()
37  << "." << std::endl;
38 
39  // Check coefficients match
40  for (int i=0; i<x.size(); i++) {
41  bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
42  out << tag << " Taylor array comparison test " << i;
43  if (success2)
44  out << " passed";
45  else
46  out << " failed";
47  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i] << "."
48  << std::endl;
49  success = success && success2;
50  }
51 
52  return success;
53 }
54 
55 template<typename Ordinal>
57  const Teuchos::Comm<Ordinal> &comm,
59  const bool result
60  )
61 {
62  out << "\nChecking that the above test passed in all processes ...";
63  int thisResult = ( result ? 1 : 0 );
64  int sumResult = -1;
65  Teuchos::reduceAll(comm,Teuchos::REDUCE_SUM,Ordinal(1),&thisResult,
66  &sumResult);
67  const bool passed = sumResult==Teuchos::size(comm);
68  if(passed)
69  out << " passed\n";
70  else
71  out << " (sumResult="<<sumResult<<"!=numProcs="<<Teuchos::size(comm)<<") failed\n";
72  return passed;
73 }
74 
75 #define TAY_COMM_TESTS(TayType, TAY) \
76 TEUCHOS_UNIT_TEST( TAY##_Comm, Broadcast ) { \
77  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
78  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
79  \
80  int n = 7; \
81  int p = 5; \
82  ValueTypeSerializer<int,TayType> tts( \
83  rcp(new ValueTypeSerializer<int,double>), p+1); \
84  \
85  Teuchos::Array<TayType> x(n), x2(n), x3(n); \
86  for (int i=0; i<n; i++) { \
87  x[i] = TayType(p, rnd.number()); \
88  for (int j=0; j<=p; j++) \
89  x[i].fastAccessCoeff(j) = rnd.number(); \
90  } \
91  for (int i=0; i<n; i++) { \
92  x2[i] = TayType(p, 0.0); \
93  } \
94  if (comm->getRank() == 0) { \
95  x2 = x; \
96  x3 = x; \
97  } \
98  \
99  Teuchos::broadcast(*comm, 0, n, &x2[0]); \
100  bool success1 = checkFadArrays( \
101  x, x2, std::string(#TAY)+" Broadcast", out); \
102  success1 = checkResultOnAllProcs(*comm, out, success1); \
103  \
104  Teuchos::broadcast(*comm, tts, 0, n, &x3[0]); \
105  bool success2 = checkFadArrays( \
106  x, x3, std::string(#TAY)+" Broadcast TTS", out); \
107  success2 = checkResultOnAllProcs(*comm, out, success2); \
108  \
109  success = success1 && success2; \
110 } \
111  \
112 TEUCHOS_UNIT_TEST( TAY##_Comm, GatherAll ) { \
113  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
114  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
115  \
116  int n = 7; \
117  int p = 5; \
118  int size = comm->getSize(); \
119  int rank = comm->getRank(); \
120  int N = n*size; \
121  ValueTypeSerializer<int,TayType> tts( \
122  rcp(new ValueTypeSerializer<int,double>), p+1); \
123  \
124  Teuchos::Array<TayType> x(n), x2(N), x3(N), x4(N); \
125  for (int i=0; i<n; i++) { \
126  x[i] = TayType(p, (rank+1)*(i+1)); \
127  for (int j=0; j<=p; j++) \
128  x[i].fastAccessCoeff(j) = (rank+1)*(i+1)*(j+1); \
129  } \
130  for (int i=0; i<N; i++) { \
131  x2[i] = TayType(p, 0.0); \
132  } \
133  for (int j=0; j<size; j++) { \
134  for (int i=0; i<n; i++) { \
135  x3[n*j+i] = TayType(p, (j+1)*(i+1)); \
136  for (int k=0; k<=p; k++) \
137  x3[n*j+i].fastAccessCoeff(k) = (j+1)*(i+1)*(k+1); \
138  } \
139  } \
140  \
141  Teuchos::gatherAll(*comm, n, &x[0], N, &x2[0]); \
142  bool success1 = checkFadArrays( \
143  x3, x2, std::string(#TAY)+" Gather All", out); \
144  success1 = checkResultOnAllProcs(*comm, out, success1); \
145  \
146  Teuchos::gatherAll(*comm, tts, n, &x[0], N, &x4[0]); \
147  bool success2 = checkFadArrays( \
148  x3, x4, std::string(#TAY)+" Gather All TTS", out); \
149  success2 = checkResultOnAllProcs(*comm, out, success2); \
150  \
151  success = success1 && success2; \
152 } \
153  \
154 TEUCHOS_UNIT_TEST( TAY##_Comm, SumAll ) { \
155  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
156  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
157  \
158  int n = 7; \
159  int p = 5; \
160  int num_proc = comm->getSize(); \
161  ValueTypeSerializer<int,TayType> tts( \
162  rcp(new ValueTypeSerializer<int,double>), p+1); \
163  \
164  Teuchos::Array<TayType> x(n), sums(n), sums2(n), sums3(n); \
165  for (int i=0; i<n; i++) { \
166  x[i] = TayType(p, 1.0*(i+1)); \
167  for (int j=0; j<=p; j++) \
168  x[i].fastAccessCoeff(j) = 2.0*(i+1); \
169  } \
170  for (int i=0; i<n; i++) { \
171  sums[i] = TayType(p, 1.0*(i+1)*num_proc); \
172  for (int j=0; j<=p; j++) \
173  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc; \
174  } \
175  for (int i=0; i<n; i++) { \
176  sums2[i] = TayType(p, 0.0); \
177  } \
178  \
179  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
180  bool success1 = checkFadArrays( \
181  sums, sums2, std::string(#TAY)+" Sum All", out); \
182  success1 = checkResultOnAllProcs(*comm, out, success1); \
183  \
184  Teuchos::reduceAll(*comm, tts, Teuchos::REDUCE_SUM, n, &x[0], &sums3[0]); \
185  bool success2 = checkFadArrays( \
186  sums, sums3, std::string(#TAY)+" Sum All TTS", out); \
187  success2 = checkResultOnAllProcs(*comm, out, success2); \
188  \
189  success = success1 && success2; \
190 } \
191  \
192 TEUCHOS_UNIT_TEST( TAY##_Comm, MaxAll ) { \
193  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
194  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
195  \
196  int n = 7; \
197  int p = 5; \
198  int rank = comm->getRank(); \
199  int num_proc = comm->getSize(); \
200  ValueTypeSerializer<int,TayType> tts( \
201  rcp(new ValueTypeSerializer<int,double>), p+1); \
202  \
203  Teuchos::Array<TayType> x(n), maxs(n), maxs2(n), maxs3(n); \
204  for (int i=0; i<n; i++) { \
205  x[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
206  for (int j=0; j<=p; j++) \
207  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
208  } \
209  for (int i=0; i<n; i++) { \
210  maxs[i] = TayType(p, 1.0*(i+1)*num_proc); \
211  for (int j=0; j<=p; j++) \
212  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*num_proc; \
213  } \
214  for (int i=0; i<n; i++) { \
215  maxs2[i] = TayType(p, 0.0); \
216  } \
217  \
218  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
219  bool success1 = checkFadArrays( \
220  maxs, maxs2, std::string(#TAY)+" Max All", out); \
221  success1 = checkResultOnAllProcs(*comm, out, success1); \
222  \
223  Teuchos::reduceAll(*comm, tts, Teuchos::REDUCE_MAX, n, &x[0], &maxs3[0]); \
224  bool success2 = checkFadArrays( \
225  maxs, maxs3, std::string(#TAY)+" Max All TTS", out); \
226  success2 = checkResultOnAllProcs(*comm, out, success2); \
227  \
228  success = success1 && success2; \
229 } \
230  \
231 TEUCHOS_UNIT_TEST( TAY##_Comm, MinAll ) { \
232  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
233  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
234  \
235  int n = 7; \
236  int p = 5; \
237  int rank = comm->getRank(); \
238  ValueTypeSerializer<int,TayType> tts( \
239  rcp(new ValueTypeSerializer<int,double>), p+1); \
240  \
241  Teuchos::Array<TayType> x(n), mins(n), mins2(n), mins3(n); \
242  for (int i=0; i<n; i++) { \
243  x[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
244  for (int j=0; j<=p; j++) \
245  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
246  } \
247  for (int i=0; i<n; i++) { \
248  mins[i] = TayType(p, 1.0*(i+1)); \
249  for (int j=0; j<=p; j++) \
250  mins[i].fastAccessCoeff(j) = 2.0*(i+1); \
251  } \
252  for (int i=0; i<n; i++) { \
253  mins2[i] = TayType(p, 0.0); \
254  } \
255  \
256  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
257  bool success1 = checkFadArrays( \
258  mins, mins2, std::string(#TAY)+" Min All", out); \
259  success1 = checkResultOnAllProcs(*comm, out, success1); \
260  \
261  Teuchos::reduceAll(*comm, tts, Teuchos::REDUCE_MIN, n, &x[0], &mins3[0]); \
262  bool success2 = checkFadArrays( \
263  mins, mins3, std::string(#TAY)+" Min All TTS", out); \
264  success2 = checkResultOnAllProcs(*comm, out, success2); \
265  \
266  success = success1 && success2; \
267 } \
268  \
269 TEUCHOS_UNIT_TEST( TAY##_Comm, ScanSum ) { \
270  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
271  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
272  \
273  int n = 7; \
274  int p = 5; \
275  int rank = comm->getRank(); \
276  ValueTypeSerializer<int,TayType> tts( \
277  rcp(new ValueTypeSerializer<int,double>), p+1); \
278  \
279  Teuchos::Array<TayType> x(n), sums(n), sums2(n), sums3(n); \
280  for (int i=0; i<n; i++) { \
281  x[i] = TayType(p, 1.0*(i+1)); \
282  for (int j=0; j<=p; j++) \
283  x[i].fastAccessCoeff(j) = 2.0*(i+1); \
284  } \
285  for (int i=0; i<n; i++) { \
286  sums[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
287  for (int j=0; j<=p; j++) \
288  sums[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
289  } \
290  for (int i=0; i<n; i++) { \
291  sums2[i] = TayType(p, 0.0); \
292  } \
293  \
294  Teuchos::scan(*comm, Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
295  bool success1 = checkFadArrays( \
296  sums, sums2, std::string(#TAY)+" Scan Sum", out); \
297  success1 = checkResultOnAllProcs(*comm, out, success1); \
298  \
299  Teuchos::scan(*comm, tts, Teuchos::REDUCE_SUM, n, &x[0], &sums3[0]); \
300  bool success2 = checkFadArrays( \
301  sums, sums3, std::string(#TAY)+" Scan Sum TTS", out); \
302  success2 = checkResultOnAllProcs(*comm, out, success2); \
303  \
304  success = success1 && success2; \
305 } \
306  \
307 TEUCHOS_UNIT_TEST( TAY##_Comm, ScanMax ) { \
308  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
309  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
310  \
311  int n = 7; \
312  int p = 5; \
313  int rank = comm->getRank(); \
314  ValueTypeSerializer<int,TayType> tts( \
315  rcp(new ValueTypeSerializer<int,double>), p+1); \
316  \
317  Teuchos::Array<TayType> x(n), maxs(n), maxs2(n), maxs3(n); \
318  for (int i=0; i<n; i++) { \
319  x[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
320  for (int j=0; j<=p; j++) \
321  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
322  } \
323  for (int i=0; i<n; i++) { \
324  maxs[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
325  for (int j=0; j<=p; j++) \
326  maxs[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
327  } \
328  for (int i=0; i<n; i++) { \
329  maxs2[i] = TayType(p, 0.0); \
330  } \
331  \
332  Teuchos::scan(*comm, Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
333  bool success1 = checkFadArrays( \
334  maxs, maxs2, std::string(#TAY)+" Scan Max", out); \
335  success1 = checkResultOnAllProcs(*comm, out, success1); \
336  \
337  Teuchos::scan(*comm, tts, Teuchos::REDUCE_MAX, n, &x[0], &maxs3[0]); \
338  bool success2 = checkFadArrays( \
339  maxs, maxs3, std::string(#TAY)+" Scan Max TTS", out); \
340  success2 = checkResultOnAllProcs(*comm, out, success2); \
341  \
342  success = success1 && success2; \
343 } \
344  \
345 TEUCHOS_UNIT_TEST( TAY##_Comm, ScanMin ) { \
346  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
347  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
348  \
349  int n = 7; \
350  int p = 5; \
351  int rank = comm->getRank(); \
352  ValueTypeSerializer<int,TayType> tts( \
353  rcp(new ValueTypeSerializer<int,double>), p+1); \
354  \
355  Teuchos::Array<TayType> x(n), mins(n), mins2(n), mins3(n); \
356  for (int i=0; i<n; i++) { \
357  x[i] = TayType(p, 1.0*(i+1)*(rank+1)); \
358  for (int j=0; j<=p; j++) \
359  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(rank+1); \
360  } \
361  for (int i=0; i<n; i++) { \
362  mins[i] = TayType(p, 1.0*(i+1)); \
363  for (int j=0; j<=p; j++) \
364  mins[i].fastAccessCoeff(j) = 2.0*(i+1); \
365  } \
366  for (int i=0; i<n; i++) { \
367  mins2[i] = TayType(p, 0.0); \
368  } \
369  \
370  Teuchos::scan(*comm, Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
371  bool success1 = checkFadArrays( \
372  mins, mins2, std::string(#TAY)+" Scan Min", out); \
373  success1 = checkResultOnAllProcs(*comm, out, success1); \
374  \
375  Teuchos::scan(*comm, tts, Teuchos::REDUCE_MIN, n, &x[0], &mins3[0]); \
376  bool success2 = checkFadArrays( \
377  mins, mins3, std::string(#TAY)+" Scan Min TTS", out); \
378  success2 = checkResultOnAllProcs(*comm, out, success2); \
379  \
380  success = success1 && success2; \
381 } \
382  \
383 TEUCHOS_UNIT_TEST( TAY##_Comm, SendReceive ) { \
384  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
385  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
386  \
387  int num_proc = comm->getSize(); \
388  if (num_proc > 1) { \
389  int rank = comm->getRank(); \
390  int n = 7; \
391  int p = 5; \
392  ValueTypeSerializer<int,TayType> tts( \
393  rcp(new ValueTypeSerializer<int,double>), p+1); \
394  \
395  Teuchos::Array<TayType> x(n), x2(n), x3(n); \
396  for (int i=0; i<n; i++) { \
397  x[i] = TayType(p, 1.0*(i+1)); \
398  for (int j=0; j<=p; j++) \
399  x[i].fastAccessCoeff(j) = 2.0*(i+1)*(j+1); \
400  } \
401  for (int i=0; i<n; i++) { \
402  x2[i] = TayType(p, 0.0); \
403  } \
404  if (rank != 1) { \
405  x2 = x; \
406  x3 = x2; \
407  } \
408  \
409  if (rank == 0) Teuchos::send(*comm, n, &x[0], 1); \
410  if (rank == 1) Teuchos::receive(*comm, 0, n, &x2[0]); \
411  bool success1 = checkFadArrays( \
412  x, x2, std::string(#TAY)+" Send/Receive", out); \
413  success1 = checkResultOnAllProcs(*comm, out, success1); \
414  \
415  if (rank == 0) Teuchos::send(*comm, tts, n, &x[0], 1); \
416  if (rank == 1) Teuchos::receive(*comm, tts, 0, n, &x3[0]); \
417  bool success2 = checkFadArrays( \
418  x, x3, std::string(#TAY)+" Send/Receive TTS", out); \
419  success2 = checkResultOnAllProcs(*comm, out, success2); \
420  \
421  success = success1 && success2; \
422  } \
423  else \
424  success = true; \
425 } \
426  \
427 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedBroadcast ) { \
428  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
429  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
430  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
431  \
432  int n = 7; \
433  int p1 = 5; \
434  int p2 = 5; \
435  RCP< ValueTypeSerializer<int,TayType> > tts = \
436  rcp(new ValueTypeSerializer<int,TayType>( \
437  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
438  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
439  \
440  Teuchos::Array<TayTayType> x(n), x2(n), x3(n); \
441  for (int i=0; i<n; i++) { \
442  TayType f(p1, rnd.number()); \
443  for (int k=0; k<=p1; k++) \
444  f.fastAccessCoeff(k) = rnd.number(); \
445  x[i] = TayTayType(p2, f); \
446  for (int j=0; j<=p2; j++) { \
447  TayType g(p1, rnd.number()); \
448  for (int k=0; k<=p1; k++) \
449  g.fastAccessCoeff(k) = rnd.number(); \
450  x[i].fastAccessCoeff(j) = g; \
451  } \
452  } \
453  for (int i=0; i<n; i++) { \
454  x2[i] = TayTayType(p2, TayType(p1, 0.0)); \
455  for (int j=0; j<=p2; j++) \
456  x2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
457  } \
458  if (comm->getRank() == 0) { \
459  x2 = x; \
460  x3 = x; \
461  } \
462  \
463  Teuchos::broadcast(*comm, 0, n, &x2[0]); \
464  bool success1 = checkFadArrays( \
465  x, x2, std::string(#TAY)+"<"+#TAY+"> Broadcast", out); \
466  success1 = checkResultOnAllProcs(*comm, out, success1); \
467  \
468  Teuchos::broadcast(*comm, ttts, 0, n, &x3[0]); \
469  bool success2 = checkFadArrays( \
470  x, x3, std::string(#TAY)+"<"+#TAY+"> Broadcast TTS", out); \
471  success2 = checkResultOnAllProcs(*comm, out, success2); \
472  \
473  success = success1 && success2; \
474 } \
475  \
476 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedGatherAll ) { \
477  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
478  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
479  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
480  \
481  int n = 7; \
482  int p1 = 5; \
483  int p2 = 5; \
484  RCP< ValueTypeSerializer<int,TayType> > tts = \
485  rcp(new ValueTypeSerializer<int,TayType>( \
486  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
487  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
488  \
489  int size = comm->getSize(); \
490  int rank = comm->getRank(); \
491  int N = n*size; \
492  Teuchos::Array<TayTayType> x(n), x2(N), x3(N), x4(N); \
493  for (int i=0; i<n; i++) { \
494  TayType f(p1, (rank+1)*(i+1)); \
495  for (int k=0; k<=p1; k++) \
496  f.fastAccessCoeff(k) = (rank+1)*(i+1)*(k+1); \
497  x[i] = TayTayType(p2, f); \
498  for (int j=0; j<=p2; j++) { \
499  x[i].fastAccessCoeff(j) = f; \
500  } \
501  } \
502  for (int i=0; i<N; i++) { \
503  x2[i] = TayTayType(p2, TayType(p1, 0.0)); \
504  for (int j=0; j<=p2; j++) \
505  x2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
506  } \
507  for (int j=0; j<size; j++) { \
508  for (int i=0; i<n; i++) { \
509  TayType f(p1, (j+1)*(i+1)); \
510  for (int k=0; k<=p1; k++) \
511  f.fastAccessCoeff(k) = (j+1)*(i+1)*(k+1); \
512  x3[n*j+i] = TayTayType(p2, f); \
513  for (int k=0; k<=p2; k++) \
514  x3[n*j+i].fastAccessCoeff(k) = f; \
515  } \
516  } \
517  \
518  Teuchos::gatherAll(*comm, n, &x[0], N, &x2[0]); \
519  bool success1 = checkFadArrays( \
520  x3, x2, std::string(#TAY)+"<"+#TAY+"> Gather All", out); \
521  success1 = checkResultOnAllProcs(*comm, out, success1); \
522  \
523  Teuchos::gatherAll(*comm, ttts, n, &x[0], N, &x4[0]); \
524  bool success2 = checkFadArrays( \
525  x3, x4, std::string(#TAY)+"<"+#TAY+"> Gather All FTS", out); \
526  success2 = checkResultOnAllProcs(*comm, out, success2); \
527  \
528  success = success1 && success2; \
529 } \
530  \
531 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedSumAll ) { \
532  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
533  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
534  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
535  \
536  int n = 7; \
537  int p1 = 5; \
538  int p2 = 5; \
539  int num_proc = comm->getSize(); \
540  RCP< ValueTypeSerializer<int,TayType> > tts = \
541  rcp(new ValueTypeSerializer<int,TayType>( \
542  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
543  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
544  \
545  Teuchos::Array<TayTayType> x(n), sums(n), sums2(n), sums3(n); \
546  for (int i=0; i<n; i++) { \
547  TayType f(p1, 1.0*(i+1)); \
548  for (int k=0; k<=p1; k++) \
549  f.fastAccessCoeff(k) = 2.0*(i+1); \
550  x[i] = TayTayType(p2, f); \
551  for (int j=0; j<=p2; j++) { \
552  x[i].fastAccessCoeff(j) = f; \
553  } \
554  } \
555  for (int i=0; i<n; i++) { \
556  TayType f(p1, 1.0*(i+1)*num_proc); \
557  for (int k=0; k<=p1; k++) \
558  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc; \
559  sums[i] = TayTayType(p2, f); \
560  for (int j=0; j<=p2; j++) \
561  sums[i].fastAccessCoeff(j) = f; \
562  } \
563  for (int i=0; i<n; i++) { \
564  sums2[i] = TayTayType(p2, TayType(p1, 0.0)); \
565  for (int j=0; j<=p2; j++) \
566  sums2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
567  } \
568  \
569  Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
570  bool success1 = checkFadArrays( \
571  sums, sums2, std::string(#TAY)+"<"+#TAY+"> Sum All", out); \
572  success1 = checkResultOnAllProcs(*comm, out, success1); \
573  \
574  Teuchos::reduceAll(*comm, ttts, Teuchos::REDUCE_SUM, n, &x[0], &sums3[0]); \
575  bool success2 = checkFadArrays( \
576  sums, sums3, std::string(#TAY)+"<"+#TAY+"> Sum All", out); \
577  success2 = checkResultOnAllProcs(*comm, out, success2); \
578  \
579  success = success1 && success2; \
580 } \
581  \
582 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedMaxAll ) { \
583  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
584  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
585  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
586  \
587  int n = 7; \
588  int p1 = 5; \
589  int p2 = 5; \
590  int rank = comm->getRank(); \
591  int num_proc = comm->getSize(); \
592  RCP< ValueTypeSerializer<int,TayType> > tts = \
593  rcp(new ValueTypeSerializer<int,TayType>( \
594  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
595  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
596  \
597  Teuchos::Array<TayTayType> x(n), maxs(n), maxs2(n), maxs3(n); \
598  for (int i=0; i<n; i++) { \
599  TayType f(p1, 1.0*(i+1)*(rank+1)); \
600  for (int k=0; k<=p1; k++) \
601  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
602  x[i] = TayTayType(p2, f); \
603  for (int j=0; j<=p2; j++) { \
604  x[i].fastAccessCoeff(j) = f; \
605  } \
606  } \
607  for (int i=0; i<n; i++) { \
608  TayType f(p1, 1.0*(i+1)*num_proc); \
609  for (int k=0; k<=p1; k++) \
610  f.fastAccessCoeff(k) = 2.0*(i+1)*num_proc; \
611  maxs[i] = TayTayType(p2, f); \
612  for (int j=0; j<=p2; j++) \
613  maxs[i].fastAccessCoeff(j) = f; \
614  } \
615  for (int i=0; i<n; i++) { \
616  maxs2[i] = TayTayType(p2, TayType(p1, 0.0)); \
617  for (int j=0; j<=p2; j++) \
618  maxs2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
619  } \
620  \
621  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
622  bool success1 = checkFadArrays( \
623  maxs, maxs2, std::string(#TAY)+"<"+#TAY+"> Max All", out); \
624  success1 = checkResultOnAllProcs(*comm, out, success1); \
625  \
626  Teuchos::reduceAll(*comm, ttts, Teuchos::REDUCE_MAX, n, &x[0], &maxs3[0]); \
627  bool success2 = checkFadArrays( \
628  maxs, maxs3, std::string(#TAY)+"<"+#TAY+"> Max All FTS", out); \
629  success2 = checkResultOnAllProcs(*comm, out, success2); \
630  \
631  success = success1 && success2; \
632 } \
633  \
634 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedMinAll ) { \
635  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
636  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
637  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
638  \
639  int n = 7; \
640  int p1 = 5; \
641  int p2 = 5; \
642  int rank = comm->getRank(); \
643  RCP< ValueTypeSerializer<int,TayType> > tts = \
644  rcp(new ValueTypeSerializer<int,TayType>( \
645  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
646  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
647  \
648  Teuchos::Array<TayTayType> x(n), mins(n), mins2(n), mins3(n); \
649  for (int i=0; i<n; i++) { \
650  TayType f(p1, 1.0*(i+1)*(rank+1)); \
651  for (int k=0; k<=p1; k++) \
652  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
653  x[i] = TayTayType(p2, f); \
654  for (int j=0; j<=p2; j++) { \
655  x[i].fastAccessCoeff(j) = f; \
656  } \
657  } \
658  for (int i=0; i<n; i++) { \
659  TayType f(p1, 1.0*(i+1)); \
660  for (int k=0; k<=p1; k++) \
661  f.fastAccessCoeff(k) = 2.0*(i+1); \
662  mins[i] = TayTayType(p2, f); \
663  for (int j=0; j<=p2; j++) \
664  mins[i].fastAccessCoeff(j) = f; \
665  } \
666  for (int i=0; i<n; i++) { \
667  mins2[i] = TayTayType(p2, TayType(p1, 0.0)); \
668  for (int j=0; j<=p2; j++) \
669  mins2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
670  } \
671  \
672  Teuchos::reduceAll(*comm, Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
673  bool success1 = checkFadArrays( \
674  mins, mins2, std::string(#TAY)+"<"+#TAY+"> Min All", out); \
675  success1 = checkResultOnAllProcs(*comm, out, success1); \
676  \
677  Teuchos::reduceAll(*comm, ttts, Teuchos::REDUCE_MIN, n, &x[0], &mins3[0]); \
678  bool success2 = checkFadArrays( \
679  mins, mins3, std::string(#TAY)+"<"+#TAY+"> Min All FTS", out); \
680  success2 = checkResultOnAllProcs(*comm, out, success2); \
681  \
682  success = success1 && success2; \
683 } \
684  \
685 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedScanSum ) { \
686  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
687  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
688  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
689  \
690  int n = 7; \
691  int p1 = 5; \
692  int p2 = 5; \
693  int rank = comm->getRank(); \
694  RCP< ValueTypeSerializer<int,TayType> > tts = \
695  rcp(new ValueTypeSerializer<int,TayType>( \
696  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
697  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
698  \
699  Teuchos::Array<TayTayType> x(n), sums(n), sums2(n), sums3(n); \
700  for (int i=0; i<n; i++) { \
701  TayType f(p1, 1.0*(i+1)); \
702  for (int k=0; k<=p1; k++) \
703  f.fastAccessCoeff(k) = 2.0*(i+1); \
704  x[i] = TayTayType(p2, f); \
705  for (int j=0; j<=p2; j++) { \
706  x[i].fastAccessCoeff(j) = f; \
707  } \
708  } \
709  for (int i=0; i<n; i++) { \
710  TayType f(p1, 1.0*(i+1)*(rank+1)); \
711  for (int k=0; k<=p1; k++) \
712  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
713  sums[i] = TayTayType(p2, f); \
714  for (int j=0; j<=p2; j++) \
715  sums[i].fastAccessCoeff(j) = f; \
716  } \
717  for (int i=0; i<n; i++) { \
718  sums2[i] = TayTayType(p2, TayType(p1, 0.0)); \
719  for (int j=0; j<=p2; j++) \
720  sums2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
721  } \
722  \
723  Teuchos::scan(*comm, Teuchos::REDUCE_SUM, n, &x[0], &sums2[0]); \
724  bool success1 = checkFadArrays( \
725  sums, sums2, std::string(#TAY)+"<"+#TAY+"> Scan Sum", out); \
726  success1 = checkResultOnAllProcs(*comm, out, success1); \
727  \
728  Teuchos::scan(*comm, ttts, Teuchos::REDUCE_SUM, n, &x[0], &sums3[0]); \
729  bool success2 = checkFadArrays( \
730  sums, sums3, std::string(#TAY)+"<"+#TAY+"> Scan Sum FTS", out); \
731  success2 = checkResultOnAllProcs(*comm, out, success2); \
732  \
733  success = success1 && success2; \
734 } \
735  \
736 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedScanMax ) { \
737  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
738  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
739  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
740  \
741  int n = 7; \
742  int p1 = 5; \
743  int p2 = 5; \
744  int rank = comm->getRank(); \
745  RCP< ValueTypeSerializer<int,TayType> > tts = \
746  rcp(new ValueTypeSerializer<int,TayType>( \
747  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
748  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
749  \
750  Teuchos::Array<TayTayType> x(n), maxs(n), maxs2(n), maxs3(n); \
751  for (int i=0; i<n; i++) { \
752  TayType f(p1, 1.0*(i+1)*(rank+1)); \
753  for (int k=0; k<=p1; k++) \
754  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
755  x[i] = TayTayType(p2, f); \
756  for (int j=0; j<=p2; j++) { \
757  x[i].fastAccessCoeff(j) = f; \
758  } \
759  } \
760  for (int i=0; i<n; i++) { \
761  TayType f(p1, 1.0*(i+1)*(rank+1)); \
762  for (int k=0; k<=p1; k++) \
763  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
764  maxs[i] = TayTayType(p2, f); \
765  for (int j=0; j<=p2; j++) \
766  maxs[i].fastAccessCoeff(j) = f; \
767  } \
768  for (int i=0; i<n; i++) { \
769  maxs2[i] = TayTayType(p2, TayType(p1, 0.0)); \
770  for (int j=0; j<=p2; j++) \
771  maxs2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
772  } \
773  \
774  Teuchos::scan(*comm, Teuchos::REDUCE_MAX, n, &x[0], &maxs2[0]); \
775  bool success1 = checkFadArrays( \
776  maxs, maxs2, std::string(#TAY)+"<"+#TAY+"> Scan Max", out); \
777  success1 = checkResultOnAllProcs(*comm, out, success1); \
778  \
779  Teuchos::scan(*comm, ttts, Teuchos::REDUCE_MAX, n, &x[0], &maxs3[0]); \
780  bool success2 = checkFadArrays( \
781  maxs, maxs3, std::string(#TAY)+"<"+#TAY+"> Scan Max FTS", out); \
782  success2 = checkResultOnAllProcs(*comm, out, success2); \
783  \
784  success = success1 && success2; \
785 } \
786  \
787 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedScanMin ) { \
788  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
789  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
790  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
791  \
792  int n = 7; \
793  int p1 = 5; \
794  int p2 = 5; \
795  int rank = comm->getRank(); \
796  RCP< ValueTypeSerializer<int,TayType> > tts = \
797  rcp(new ValueTypeSerializer<int,TayType>( \
798  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
799  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
800  \
801  Teuchos::Array<TayTayType> x(n), mins(n), mins2(n), mins3(n); \
802  for (int i=0; i<n; i++) { \
803  TayType f(p1, 1.0*(i+1)*(rank+1)); \
804  for (int k=0; k<=p1; k++) \
805  f.fastAccessCoeff(k) = 2.0*(i+1)*(rank+1); \
806  x[i] = TayTayType(p2, f); \
807  for (int j=0; j<=p2; j++) { \
808  x[i].fastAccessCoeff(j) = f; \
809  } \
810  } \
811  for (int i=0; i<n; i++) { \
812  TayType f(p1, 1.0*(i+1)); \
813  for (int k=0; k<=p1; k++) \
814  f.fastAccessCoeff(k) = 2.0*(i+1); \
815  mins[i] = TayTayType(p2, f); \
816  for (int j=0; j<=p2; j++) \
817  mins[i].fastAccessCoeff(j) = f; \
818  } \
819  for (int i=0; i<n; i++) { \
820  mins2[i] = TayTayType(p2, TayType(p1, 0.0)); \
821  for (int j=0; j<=p2; j++) \
822  mins2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
823  } \
824  \
825  Teuchos::scan(*comm, Teuchos::REDUCE_MIN, n, &x[0], &mins2[0]); \
826  bool success1 = checkFadArrays( \
827  mins, mins2, std::string(#TAY)+"<"+#TAY+"> Scan Min", out); \
828  success1 = checkResultOnAllProcs(*comm, out, success1); \
829  \
830  Teuchos::scan(*comm, ttts, Teuchos::REDUCE_MIN, n, &x[0], &mins3[0]); \
831  bool success2 = checkFadArrays( \
832  mins, mins3, std::string(#TAY)+"<"+#TAY+"> Scan Min FTS", out); \
833  success2 = checkResultOnAllProcs(*comm, out, success2); \
834  \
835  success = success1 && success2; \
836 } \
837  \
838 TEUCHOS_UNIT_TEST( TAY##_Comm, NestedSendReceive ) { \
839  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
840  Teuchos::RCP<const Teuchos::Comm<Ordinal> > \
841  comm = Teuchos::DefaultComm<Ordinal>::getComm(); \
842  \
843  int num_proc = comm->getSize(); \
844  if (num_proc > 1) { \
845  int rank = comm->getRank(); \
846  int n = 7; \
847  int p1 = 5; \
848  int p2 = 5; \
849  RCP< ValueTypeSerializer<int,TayType> > tts = \
850  rcp(new ValueTypeSerializer<int,TayType>( \
851  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
852  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
853  \
854  Teuchos::Array<TayTayType> x(n), x2(n), x3(n); \
855  for (int i=0; i<n; i++) { \
856  TayType f(p1, 1.0*(i+1)); \
857  for (int k=0; k<=p1; k++) \
858  f.fastAccessCoeff(k) = 2.0*(i+1)*(k+1); \
859  x[i] = TayTayType(p2, f); \
860  for (int j=0; j<=p2; j++) \
861  x[i].fastAccessCoeff(j) = f; \
862  } \
863  for (int i=0; i<n; i++) { \
864  x2[i] = TayTayType(p2, TayType(p1, 0.0)); \
865  for (int j=0; j<=p2; j++) \
866  x2[i].fastAccessCoeff(j) = TayType(p1, 0.0); \
867  } \
868  if (rank != 1) { \
869  x2 = x; \
870  x3 = x2; \
871  } \
872  \
873  if (rank == 0) Teuchos::send(*comm, n, &x[0], 1); \
874  if (rank == 1) Teuchos::receive(*comm, 0, n, &x2[0]); \
875  bool success1 = checkFadArrays( \
876  x, x2, std::string(#TAY)+"<"+#TAY+"> Send/Receive", out); \
877  success1 = checkResultOnAllProcs(*comm, out, success1); \
878  \
879  if (rank == 0) Teuchos::send(*comm, ttts, n, &x[0], 1); \
880  if (rank == 1) Teuchos::receive(*comm, ttts, 0, n, &x3[0]); \
881  bool success2 = checkFadArrays( \
882  x, x3, std::string(#TAY)+"<"+#TAY+"> Send/Receive FTS", out); \
883  success2 = checkResultOnAllProcs(*comm, out, success2); \
884  \
885  success = success1 && success2; \
886  } \
887  else \
888  success = true; \
889 }
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool checkFadArrays(const ArrayType &x, const ArrayType &x2, const std::string &tag, Teuchos::FancyOStream &out)
bool checkResultOnAllProcs(const Teuchos::Comm< Ordinal > &comm, Teuchos::FancyOStream &out, const bool result)
static SACADO_INLINE_FUNCTION bool eval(const T &x, const T &y)
int Ordinal
size_type size() const