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