Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SacadoMPVectorSerializationTests.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 
15 #include "Teuchos_Array.hpp"
16 #include "Stokhos_Sacado.hpp"
18 #include "Sacado_Fad_DFad.hpp"
19 #include "Sacado_mpl_apply.hpp"
20 #include "Sacado_Random.hpp"
21 
22 #include <Kokkos_Core.hpp>
23 
24 //
25 // Currently this doesn't test:
26 // * the device
27 // * threaded storage (needs the device)
28 // * strided storage with non-trivial stride
29 //
30 
31 using Teuchos::RCP;
32 using Teuchos::rcp;
33 
34 // Common setup for unit tests
35 template <typename VecType, typename FadType>
36 struct UnitTestSetup {
37  int sz;
38 
41 
42  typedef typename Sacado::mpl::apply<FadType,VecType>::type FadVecType;
45 
47  sz = 8;
48 
49  // Serializers
51  rcp(new VecSerializerT(
54  }
55 };
56 
57 template <typename VecType>
59  const std::string& tag,
60  Teuchos::FancyOStream& out) {
61 
62  typedef int Ordinal;
64 
65  // Serialize
66  Ordinal count = x.size();
67  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
68  char *charBuffer = new char[bytes];
69  SerT::serialize(count, &x[0], bytes, charBuffer);
70  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
71 
72  // Check counts match
73  bool success = (count == count2);
74  out << tag << " serialize/deserialize count test";
75  if (success)
76  out << " passed";
77  else
78  out << " failed";
79  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
80  << std::endl;
81 
82  // Deserialize
83  Teuchos::Array<VecType> x2(count2);
84  for (Ordinal i=0; i<count2; i++)
85  x2[i].reset(x[i].size());
86  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
87 
88  delete [] charBuffer;
89 
90  // Check coefficients match
91  for (Ordinal i=0; i<count; i++) {
92  bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
93  out << tag << " serialize/deserialize vec test " << i;
94  if (success2)
95  out << " passed";
96  else
97  out << " failed";
98  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
99  << "." << std::endl;
100  success = success && success2;
101  }
102 
103  return success;
104 }
105 
106 template <typename VecType, typename Serializer>
108  const Serializer& serializer,
109  const std::string& tag,
110  Teuchos::FancyOStream& out) {
111 
112  typedef int Ordinal;
113 
114  // Serialize
115  Ordinal count = x.size();
116  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
117  char *charBuffer = new char[bytes];
118  serializer.serialize(count, &x[0], bytes, charBuffer);
119 
120  // Reset x to given size
121  for (Ordinal i=0; i<count; i++)
122  x[i].reset(serializer.getSerializerSize());
123 
124  // Deserialize
125  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
126  Teuchos::Array<VecType> x2(count2);
127  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
128 
129  delete [] charBuffer;
130 
131  // Check counts match
132  bool success = (count == count2);
133  out << tag << " serialize/deserialize count test";
134  if (success)
135  out << " passed";
136  else
137  out << " failed";
138  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
139  << std::endl;
140 
141  // Check coefficients match
142  for (Ordinal i=0; i<count; i++) {
143  bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
144  out << tag << " serialize/deserialize vec test " << i;
145  if (success2)
146  out << " passed";
147  else
148  out << " failed";
149  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
150  << "." << std::endl;
151  success = success && success2;
152  }
153 
154  return success;
155 }
156 
157 template <typename VecType, typename Serializer>
159  const Serializer& serializer,
160  const std::string& tag,
161  Teuchos::FancyOStream& out) {
162 
163  typedef int Ordinal;
164 
165  // Serialize
166  Ordinal count = x.size();
167  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
168  char *charBuffer = new char[bytes];
169  serializer.serialize(count, &x[0], bytes, charBuffer);
170 
171  // Reset x to given expansion
172  Ordinal sz = serializer.getSerializerSize();
173  typedef typename Serializer::value_serializer_type VST;
174  RCP<const VST> vs = serializer.getValueSerializer();
175  for (Ordinal i=0; i<count; i++) {
176  x[i].expand(sz);
177  for (Ordinal j=0; j<sz; j++)
178  x[i].fastAccessDx(j).reset(vs->getSerializerSize());
179  x[i].val().reset(vs->getSerializerSize());
180  }
181 
182  // Deserialize
183  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
184  Teuchos::Array<VecType> x2(count2);
185  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
186 
187  delete [] charBuffer;
188 
189  // Check counts match
190  bool success = (count == count2);
191  out << tag << " serialize/deserialize count test";
192  if (success)
193  out << " passed";
194  else
195  out << " failed";
196  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
197  << std::endl;
198 
199  // Check coefficients match
200  for (Ordinal i=0; i<count; i++) {
201  bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
202  out << tag << " serialize/deserialize vec test " << i;
203  if (success2)
204  out << " passed";
205  else
206  out << " failed";
207  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
208  << "." << std::endl;
209  success = success && success2;
210  }
211 
212  return success;
213 }
214 
215 #define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec) \
216  TEUCHOS_UNIT_TEST( Vec##_Serialization, Uniform ) { \
217  int n = 7; \
218  Teuchos::Array<VecType> x(n); \
219  for (int i=0; i<n; i++) { \
220  x[i] = VecType(setup.sz, 0.0); \
221  for (int j=0; j<setup.sz; j++) \
222  x[i].fastAccessCoeff(j) = rnd.number(); \
223  } \
224  bool success1 = testSerialization( \
225  x, std::string(#Vec) + " Uniform", out); \
226  bool success2 = testSerialization( \
227  x, *setup.vec_serializer, std::string(#Vec) + " Uniform PTS", out); \
228  success = success1 && success2; \
229  } \
230  \
231  TEUCHOS_UNIT_TEST( Vec##_Serialization, Empty ) { \
232  int n = 7; \
233  Teuchos::Array<VecType> x(n); \
234  for (int i=0; i<n; i++) { \
235  x[i] = VecType(1, 0.0); \
236  x[i].val() = rnd.number(); \
237  } \
238  bool success1 = testSerialization( \
239  x, std::string(#Vec) + " Empty", out); \
240  bool success2 = testSerialization( \
241  x, *setup.vec_serializer, std::string(#Vec) + " Empty PTS", out); \
242  success = success1 && success2; \
243  } \
244  \
245  TEUCHOS_UNIT_TEST( Vec##_Serialization, Mixed ) { \
246  int n = 6; \
247  int p[] = { 5, 0, 8, 8, 3, 0 }; \
248  Teuchos::Array<VecType> x(n); \
249  for (int i=0; i<n; i++) { \
250  x[i] = VecType(p[i], 0.0); \
251  for (int j=0; j<p[i]; j++) \
252  x[i].fastAccessCoeff(j) = rnd.number(); \
253  } \
254  bool success1 = testSerialization( \
255  x, std::string(#Vec) + " Mixed", out); \
256  bool success2 = testSerialization( \
257  x, *setup.vec_serializer, std::string(#Vec) + " Mixed PTS", out); \
258  success = success1 && success2; \
259  } \
260  \
261  TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecUniform ) { \
262  typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
263  int n = 7; \
264  int p = 3; \
265  Teuchos::Array<FadVecType> x(n); \
266  for (int i=0; i<n; i++) { \
267  VecType f(setup.sz, 0.0); \
268  for (int k=0; k<setup.sz; k++) \
269  f.fastAccessCoeff(k) = rnd.number(); \
270  x[i] = FadVecType(p, f); \
271  for (int j=0; j<p; j++) { \
272  VecType g(setup.sz, 0.0); \
273  for (int k=0; k<setup.sz; k++) \
274  g.fastAccessCoeff(k) = rnd.number(); \
275  x[i].fastAccessDx(j) = g; \
276  } \
277  } \
278  success = \
279  testNestedSerialization(x, *setup.fad_vec_serializer, \
280  std::string(#Vec) + " Nested Uniform", out); \
281  } \
282  TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyInner ) { \
283  typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
284  int n = 7; \
285  int p = 3; \
286  Teuchos::Array<FadVecType> x(n); \
287  for (int i=0; i<n; i++) { \
288  VecType f(setup.sz, 0.0); \
289  for (int k=0; k<setup.sz; k++) \
290  f.fastAccessCoeff(k) = rnd.number(); \
291  x[i] = FadVecType(p, f); \
292  for (int j=0; j<p; j++) \
293  x[i].fastAccessDx(j) = rnd.number(); \
294  } \
295  success = \
296  testNestedSerialization( \
297  x, *setup.fad_vec_serializer, \
298  std::string(#Vec) + " Nested Empty Inner", out); \
299  } \
300  TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyOuter ) { \
301  typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
302  int n = 7; \
303  Teuchos::Array<FadVecType> x(n); \
304  for (int i=0; i<n; i++) { \
305  VecType f(setup.sz, 0.0); \
306  for (int k=0; k<setup.sz; k++) \
307  f.fastAccessCoeff(k) = rnd.number(); \
308  x[i] = FadVecType(f); \
309  } \
310  success = \
311  testNestedSerialization( \
312  x, *setup.fad_vec_serializer, \
313  std::string(#Vec) + " Nested Empty Outer", out); \
314  } \
315  TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyAll ) { \
316  typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
317  int n = 7; \
318  Teuchos::Array<FadVecType> x(n); \
319  for (int i=0; i<n; i++) { \
320  x[i] = rnd.number(); \
321  } \
322  success = \
323  testNestedSerialization( \
324  x, *setup.fad_vec_serializer, \
325  std::string(#Vec) + " Nested Empty All", out); \
326  }
327 
328 namespace DynamicVecTest {
329  Sacado::Random<double> rnd;
330  typedef Kokkos::DefaultExecutionSpace execution_space;
332  typedef Sacado::Fad::DFad<double> fad_type;
335  VEC_SERIALIZATION_TESTS(vec_type, fad_type, DynamicVector)
336 }
337 
338 namespace DynamicStridedVecTest {
339  Sacado::Random<double> rnd;
340  typedef Kokkos::DefaultExecutionSpace execution_space;
342  typedef Sacado::Fad::DFad<double> fad_type;
345  VEC_SERIALIZATION_TESTS(vec_type, fad_type, DynamicStridedVector)
346 }
347 
348 namespace StaticVecTest {
349  Sacado::Random<double> rnd;
350  typedef Kokkos::DefaultExecutionSpace execution_space;
352  typedef Sacado::Fad::DFad<double> fad_type;
356 }
357 
358 namespace StaticFixedVecTest {
359  Sacado::Random<double> rnd;
360  typedef Kokkos::DefaultExecutionSpace execution_space;
362  typedef Sacado::Fad::DFad<double> fad_type;
365  VEC_SERIALIZATION_TESTS(vec_type, fad_type, StaticFixedVector)
366 }
367 
368 int main( int argc, char* argv[] ) {
369  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
371 }
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
#define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec)
Statically allocated storage class.
Sacado::Fad::DFad< double > fad_type
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[])
Sacado::Fad::DFad< double > fad_type
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
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
UnitTestSetup< vec_type, fad_type > setup
bool testSerialization(const Teuchos::Array< VecType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Kokkos::DefaultExecutionSpace execution_space
Sacado::Random< double > rnd
bool testNestedSerialization(Teuchos::Array< VecType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
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
Sacado::MP::Vector< storage_type > vec_type
Statically allocated storage class.
Sacado::Fad::DFad< double > fad_type
RCP< FadVecSerializerT > fad_vec_serializer