Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fad_SerializationTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
33 
34 #include "Teuchos_Array.hpp"
35 #include "Sacado_No_Kokkos.hpp"
36 #include "Sacado_Fad_SimpleFad.hpp"
37 #include "Sacado_mpl_apply.hpp"
38 #include "Sacado_Random.hpp"
39 
40 using Teuchos::RCP;
41 using Teuchos::rcp;
43 
44 template <typename FadType>
46  const std::string& tag,
47  Teuchos::FancyOStream& out) {
48 
49  typedef int Ordinal;
51 
52  // Serialize
53  Ordinal count = x.size();
54  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
55  char *charBuffer = new char[bytes];
56  SerT::serialize(count, &x[0], bytes, charBuffer);
57 
58  // Deserialize
59  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
60  Teuchos::Array<FadType> x2(count2);
61  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
62 
63  delete [] charBuffer;
64 
65  // Check counts match
66  bool success = (count == count2);
67  out << tag << " serialize/deserialize count test";
68  if (success)
69  out << " passed";
70  else
71  out << " failed";
72  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
73  << std::endl;
74 
75  // Check Fads match
76  for (Ordinal i=0; i<count; i++) {
77  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
78  out << tag << " serialize/deserialize fad test " << i;
79  if (success2)
80  out << " passed";
81  else
82  out << " failed";
83  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
84  << "." << std::endl;
85  success = success && success2;
86  }
87 
88  return success;
89 }
90 
91 template <typename FadType, typename Serializer>
92 bool testSerializationObject(const Serializer& serializer,
94  const std::string& tag,
95  Teuchos::FancyOStream& out) {
96 
97  typedef int Ordinal;
98 
99  // Serialize
100  Ordinal count = x.size();
101  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
102  char *charBuffer = new char[bytes];
103  serializer.serialize(count, &x[0], bytes, charBuffer);
104 
105  // Expand x to serializer size
106  Ordinal sz = serializer.getSerializerSize();
107  for (Ordinal i=0; i<count; i++)
108  x[i].expand(sz);
109 
110  // Deserialize
111  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
112  Teuchos::Array<FadType> x2(count2);
113  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
114 
115  delete [] charBuffer;
116 
117  // Check counts match
118  bool success = (count == count2);
119  out << tag << " serialize/deserialize count test";
120  if (success)
121  out << " passed";
122  else
123  out << " failed";
124  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
125  << std::endl;
126 
127  // Check Fads match
128  for (Ordinal i=0; i<count; i++) {
129  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
130  out << tag << " serialize/deserialize fad test " << i;
131  if (success2)
132  out << " passed";
133  else
134  out << " failed";
135  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
136  << "." << std::endl;
137  success = success && success2;
138  }
139 
140  return success;
141 }
142 
143 template <typename FadType, typename Serializer>
144 bool testNestedSerializationObject(const Serializer& serializer,
146  const std::string& tag,
147  Teuchos::FancyOStream& out) {
148 
149  typedef int Ordinal;
150 
151  // Serialize
152  Ordinal count = x.size();
153  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
154  char *charBuffer = new char[bytes];
155  serializer.serialize(count, &x[0], bytes, charBuffer);
156 
157  // Expand x to serializer size
158  Ordinal sz = serializer.getSerializerSize();
159  typedef typename Serializer::value_serializer_type VST;
160  RCP<const VST> vs = serializer.getValueSerializer();
161  Ordinal sz_inner = vs->getSerializerSize();
162  for (Ordinal i=0; i<count; i++) {
163  x[i].expand(sz);
164  for (Ordinal j=0; j<sz; j++)
165  x[i].fastAccessDx(j).expand(sz_inner);
166  x[i].val().expand(sz_inner);
167  }
168 
169  // Deserialize
170  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171  Teuchos::Array<FadType> x2(count2);
172  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
173 
174  delete [] charBuffer;
175 
176  // Check counts match
177  bool success = (count == count2);
178  out << tag << " serialize/deserialize count test";
179  if (success)
180  out << " passed";
181  else
182  out << " failed";
183  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
184  << std::endl;
185 
186  // Check Fads match
187  for (Ordinal i=0; i<count; i++) {
188  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
189  out << tag << " serialize/deserialize fad test " << i;
190  if (success2)
191  out << " passed";
192  else
193  out << " failed";
194  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
195  << "." << std::endl;
196  success = success && success2;
197  }
198 
199  return success;
200 }
201 
202 #define FAD_SERIALIZATION_TESTS(FadType, FAD) \
203  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
204  int n = 7; \
205  int p = 5; \
206  ValueTypeSerializer<int,FadType> fts( \
207  rcp(new ValueTypeSerializer<int,double>), p); \
208  Teuchos::Array<FadType> x(n); \
209  for (int i=0; i<n; i++) { \
210  x[i] = FadType(p, rnd.number()); \
211  for (int j=0; j<p; j++) \
212  x[i].fastAccessDx(j) = rnd.number(); \
213  } \
214  bool success1 = testSerialization( \
215  x, std::string(#FAD) + " Uniform", out); \
216  bool success2 = testSerializationObject( \
217  fts, x, std::string(#FAD) + " Uniform FTS", out); \
218  success = success1 && success2; \
219  } \
220  \
221  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadEmpty ) { \
222  int n = 7; \
223  ValueTypeSerializer<int,FadType> fts( \
224  rcp(new ValueTypeSerializer<int,double>), 5); \
225  Teuchos::Array<FadType> x(n); \
226  for (int i=0; i<n; i++) { \
227  x[i] = FadType(rnd.number()); \
228  } \
229  bool success1 = testSerialization(x, std::string( \
230  #FAD) + " Empty", out); \
231  bool success2 = testSerializationObject( \
232  fts, x, std::string(#FAD) + " Empty FTS", out); \
233  success = success1 && success2; \
234  } \
235  \
236  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadMixed ) { \
237  int n = 6; \
238  int p[] = { 5, 0, 8, 8, 3, 0 }; \
239  ValueTypeSerializer<int,FadType> fts( \
240  rcp(new ValueTypeSerializer<int,double>), 8); \
241  Teuchos::Array<FadType> x(n); \
242  for (int i=0; i<n; i++) { \
243  x[i] = FadType(p[i], rnd.number()); \
244  for (int j=0; j<p[i]; j++) \
245  x[i].fastAccessDx(j) = rnd.number(); \
246  } \
247  bool success1 = testSerialization( \
248  x, std::string(#FAD) + " Mixed", out); \
249  bool success2 = testSerializationObject( \
250  fts, x, std::string(#FAD) + " Mixed FTS", out); \
251  success = success1 && success2; \
252  } \
253  \
254  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
255  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
256  int n = 7; \
257  int p1 = 5; \
258  int p2 = 3; \
259  RCP< ValueTypeSerializer<int,FadType> > fts = \
260  rcp(new ValueTypeSerializer<int,FadType>( \
261  rcp(new ValueTypeSerializer<int,double>), p1)); \
262  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
263  Teuchos::Array<FadFadType> x(n); \
264  for (int i=0; i<n; i++) { \
265  FadType f(p1, rnd.number()); \
266  for (int k=0; k<p1; k++) \
267  f.fastAccessDx(k) = rnd.number(); \
268  x[i] = FadFadType(p2, f); \
269  for (int j=0; j<p2; j++) { \
270  FadType g(p1, rnd.number()); \
271  for (int k=0; k<p1; k++) \
272  g.fastAccessDx(k) = rnd.number(); \
273  x[i].fastAccessDx(j) = g; \
274  } \
275  } \
276  bool success1 = testSerialization( \
277  x, std::string(#FAD) + " Nested Uniform", out); \
278  bool success2 = \
279  testNestedSerializationObject( \
280  ffts, x, std::string(#FAD) + " Nested Uniform", out); \
281  success = success1 && success2; \
282  } \
283  \
284  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyInner ) { \
285  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
286  int n = 7; \
287  int p1 = 5; \
288  int p2 = 3; \
289  RCP< ValueTypeSerializer<int,FadType> > fts = \
290  rcp(new ValueTypeSerializer<int,FadType>( \
291  rcp(new ValueTypeSerializer<int,double>), p1)); \
292  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
293  Teuchos::Array<FadFadType> x(n); \
294  for (int i=0; i<n; i++) { \
295  FadType f(p1, rnd.number()); \
296  for (int k=0; k<p1; k++) \
297  f.fastAccessDx(k) = rnd.number(); \
298  x[i] = FadFadType(p2, f); \
299  for (int j=0; j<p2; j++) \
300  x[i].fastAccessDx(j) = rnd.number(); \
301  } \
302  bool success1 = testSerialization( \
303  x, std::string(#FAD) + " Nested Empty Inner", out); \
304  bool success2 = testNestedSerializationObject( \
305  ffts, x, std::string(#FAD) + " Nested Empty Inner FTS", out); \
306  success = success1 && success2; \
307  } \
308  \
309  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyOuter ) { \
310  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
311  int n = 7; \
312  int p1 = 5; \
313  RCP< ValueTypeSerializer<int,FadType> > fts = \
314  rcp(new ValueTypeSerializer<int,FadType>( \
315  rcp(new ValueTypeSerializer<int,double>), p1)); \
316  ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
317  Teuchos::Array<FadFadType> x(n); \
318  for (int i=0; i<n; i++) { \
319  FadType f(p1, rnd.number()); \
320  for (int k=0; k<p1; k++) \
321  f.fastAccessDx(k) = rnd.number(); \
322  x[i] = FadFadType(f); \
323  } \
324  bool success1 =testSerialization( \
325  x, std::string(#FAD) + " Nested Empty Outer", out); \
326  bool success2 =testNestedSerializationObject( \
327  ffts, x, std::string(#FAD) + " Nested Empty Outer FTS", out); \
328  success = success1 && success2; \
329  } \
330  \
331  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyAll ) { \
332  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
333  int n = 7; \
334  RCP< ValueTypeSerializer<int,FadType> > fts = \
335  rcp(new ValueTypeSerializer<int,FadType>( \
336  rcp(new ValueTypeSerializer<int,double>), 5)); \
337  ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
338  Teuchos::Array<FadFadType> x(n); \
339  for (int i=0; i<n; i++) { \
340  x[i] = rnd.number(); \
341  } \
342  bool success1 = testSerialization( \
343  x, std::string(#FAD) + " Nested Empty All", out); \
344  bool success2 = testNestedSerializationObject( \
345  ffts, x, std::string(#FAD) + " Nested Empty All FTS", out); \
346  success = success1 && success2; \
347  } \
348 
349 #define SFAD_SERIALIZATION_TESTS(FadType, FAD) \
350  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
351  int n = 7; \
352  int p = 5; \
353  ValueTypeSerializer<int,FadType> fts( \
354  rcp(new ValueTypeSerializer<int,double>), p); \
355  Teuchos::Array<FadType> x(n); \
356  for (int i=0; i<n; i++) { \
357  x[i] = FadType(p, rnd.number()); \
358  for (int j=0; j<p; j++) \
359  x[i].fastAccessDx(j) = rnd.number(); \
360  } \
361  bool success1 = testSerialization( \
362  x, std::string(#FAD) + " Uniform", out); \
363  bool success2 = testSerializationObject( \
364  fts, x, std::string(#FAD) + " Uniform FTS", out); \
365  success = success1 && success2; \
366  } \
367  \
368  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
369  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
370  int n = 7; \
371  int p1 = 5; \
372  int p2 = 5; \
373  RCP< ValueTypeSerializer<int,FadType> > fts = \
374  rcp(new ValueTypeSerializer<int,FadType>( \
375  rcp(new ValueTypeSerializer<int,double>), p1)); \
376  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
377  Teuchos::Array<FadFadType> x(n); \
378  for (int i=0; i<n; i++) { \
379  FadType f(p1, rnd.number()); \
380  for (int k=0; k<p1; k++) \
381  f.fastAccessDx(k) = rnd.number(); \
382  x[i] = FadFadType(p2, f); \
383  for (int j=0; j<p2; j++) { \
384  FadType g(p1, rnd.number()); \
385  for (int k=0; k<p1; k++) \
386  g.fastAccessDx(k) = rnd.number(); \
387  x[i].fastAccessDx(j) = g; \
388  } \
389  } \
390  bool success1 = testSerialization( \
391  x, std::string(#FAD) + " Nested Uniform", out); \
392  bool success2 = testNestedSerializationObject( \
393  ffts, x, std::string(#FAD) + " Nested Uniform FTS", out); \
394  success = success1 && success2; \
395  } \
396 
397 
400 FAD_SERIALIZATION_TESTS(Sacado::ELRFad::DFad<double>, ELRFad_DFad)
401 FAD_SERIALIZATION_TESTS(Sacado::ELRCacheFad::DFad<double>, ELRCacheFad_DFad)
402 FAD_SERIALIZATION_TESTS(Sacado::CacheFad::DFad<double>, CacheFad_DFad)
403 
404 typedef Sacado::Fad::SLFad<double,10> Fad_SLFadType;
405 typedef Sacado::ELRFad::SLFad<double,10> ELRFad_SLFadType;
406 typedef Sacado::ELRCacheFad::SLFad<double,10> ELRCacheFad_SLFadType;
407 typedef Sacado::CacheFad::SLFad<double,10> CacheFad_SLFadType;
408 FAD_SERIALIZATION_TESTS(Fad_SLFadType, Fad_SLFad)
409 FAD_SERIALIZATION_TESTS(ELRFad_SLFadType, ELRFad_SLFad)
410 FAD_SERIALIZATION_TESTS(ELRCacheFad_SLFadType, ELRCacheFad_SLFad)
411 FAD_SERIALIZATION_TESTS(CacheFad_SLFadType, CacheFad_SLFad)
412 
413 typedef Sacado::Fad::SFad<double,5> Fad_SFadType;
414 typedef Sacado::ELRFad::SFad<double,5> ELRFad_SFadType;
415 typedef Sacado::ELRCacheFad::SFad<double,5> ELRCacheFad_SFadType;
416 typedef Sacado::CacheFad::SFad<double,5> CacheFad_SFadType;
417 SFAD_SERIALIZATION_TESTS(Fad_SFadType, Fad_SFad)
418 SFAD_SERIALIZATION_TESTS(ELRFad_SFadType, ELRFad_SFad)
419 SFAD_SERIALIZATION_TESTS(ELRCacheFad_SFadType, ELRCacheFad_SFad)
420 SFAD_SERIALIZATION_TESTS(CacheFad_SFadType, CacheFad_SFad)
421 
422 //typedef Sacado::LFad::LogicalSparse<double,int> Fad_LSType;
423 //FAD_SERIALIZATION_TESTS(Fad_LSType, LFad_LS)
424 
425 // DVFad, LFad, Flop
426 
427 int main( int argc, char* argv[] ) {
428  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
429 
431 }
#define FAD_SERIALIZATION_TESTS(FadType, FAD)
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
static KOKKOS_INLINE_FUNCTION bool eval(const T &x, const T &y)
bool testSerialization(const Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
GeneralFad< StaticStorage< T, Num > > SLFad
#define SFAD_SERIALIZATION_TESTS(FadType, FAD)
bool testSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
GeneralFad< DynamicStorage< T > > DFad
int main()
Definition: ad_example.cpp:191
int Ordinal
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
Sacado::Random< double > rnd
size_type size() const
GeneralFad< StaticFixedStorage< T, Num > > SFad