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