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 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
14 
15 #include "Teuchos_Array.hpp"
16 #include "Sacado_No_Kokkos.hpp"
17 #include "Sacado_Fad_SimpleFad.hpp"
18 #include "Sacado_mpl_apply.hpp"
19 #include "Sacado_Random.hpp"
20 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
24 
25 template <typename FadType>
27  const std::string& tag,
28  Teuchos::FancyOStream& out) {
29 
30  typedef int Ordinal;
32 
33  // Serialize
34  Ordinal count = x.size();
35  Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
36  char *charBuffer = new char[bytes];
37  SerT::serialize(count, &x[0], bytes, charBuffer);
38 
39  // Deserialize
40  Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
41  Teuchos::Array<FadType> x2(count2);
42  SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
43 
44  delete [] charBuffer;
45 
46  // Check counts match
47  bool success = (count == count2);
48  out << tag << " serialize/deserialize count test";
49  if (success)
50  out << " passed";
51  else
52  out << " failed";
53  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
54  << std::endl;
55 
56  // Check Fads match
57  for (Ordinal i=0; i<count; i++) {
58  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
59  out << tag << " serialize/deserialize fad test " << i;
60  if (success2)
61  out << " passed";
62  else
63  out << " failed";
64  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
65  << "." << std::endl;
66  success = success && success2;
67  }
68 
69  return success;
70 }
71 
72 template <typename FadType, typename Serializer>
73 bool testSerializationObject(const Serializer& serializer,
75  const std::string& tag,
76  Teuchos::FancyOStream& out) {
77 
78  typedef int Ordinal;
79 
80  // Serialize
81  Ordinal count = x.size();
82  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
83  char *charBuffer = new char[bytes];
84  serializer.serialize(count, &x[0], bytes, charBuffer);
85 
86  // Expand x to serializer size
87  Ordinal sz = serializer.getSerializerSize();
88  for (Ordinal i=0; i<count; i++)
89  x[i].expand(sz);
90 
91  // Deserialize
92  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
93  Teuchos::Array<FadType> x2(count2);
94  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
95 
96  delete [] charBuffer;
97 
98  // Check counts match
99  bool success = (count == count2);
100  out << tag << " serialize/deserialize count test";
101  if (success)
102  out << " passed";
103  else
104  out << " failed";
105  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
106  << std::endl;
107 
108  // Check Fads match
109  for (Ordinal i=0; i<count; i++) {
110  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
111  out << tag << " serialize/deserialize fad test " << i;
112  if (success2)
113  out << " passed";
114  else
115  out << " failed";
116  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
117  << "." << std::endl;
118  success = success && success2;
119  }
120 
121  return success;
122 }
123 
124 template <typename FadType, typename Serializer>
125 bool testNestedSerializationObject(const Serializer& serializer,
127  const std::string& tag,
128  Teuchos::FancyOStream& out) {
129 
130  typedef int Ordinal;
131 
132  // Serialize
133  Ordinal count = x.size();
134  Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
135  char *charBuffer = new char[bytes];
136  serializer.serialize(count, &x[0], bytes, charBuffer);
137 
138  // Expand x to serializer size
139  Ordinal sz = serializer.getSerializerSize();
140  typedef typename Serializer::value_serializer_type VST;
141  RCP<const VST> vs = serializer.getValueSerializer();
142  Ordinal sz_inner = vs->getSerializerSize();
143  for (Ordinal i=0; i<count; i++) {
144  x[i].expand(sz);
145  for (Ordinal j=0; j<sz; j++)
146  x[i].fastAccessDx(j).expand(sz_inner);
147  x[i].val().expand(sz_inner);
148  }
149 
150  // Deserialize
151  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
152  Teuchos::Array<FadType> x2(count2);
153  serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
154 
155  delete [] charBuffer;
156 
157  // Check counts match
158  bool success = (count == count2);
159  out << tag << " serialize/deserialize count test";
160  if (success)
161  out << " passed";
162  else
163  out << " failed";
164  out << ": \n\tExpected: " << count << ", \n\tGot: " << count2 << "."
165  << std::endl;
166 
167  // Check Fads match
168  for (Ordinal i=0; i<count; i++) {
169  bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
170  out << tag << " serialize/deserialize fad test " << i;
171  if (success2)
172  out << " passed";
173  else
174  out << " failed";
175  out << ": \n\tExpected: " << x[i] << ", \n\tGot: " << x2[i]
176  << "." << std::endl;
177  success = success && success2;
178  }
179 
180  return success;
181 }
182 
183 #define FAD_SERIALIZATION_TESTS(FadType, FAD) \
184  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
185  int n = 7; \
186  int p = 5; \
187  ValueTypeSerializer<int,FadType> fts( \
188  rcp(new ValueTypeSerializer<int,double>), p); \
189  Teuchos::Array<FadType> x(n); \
190  for (int i=0; i<n; i++) { \
191  x[i] = FadType(p, rnd.number()); \
192  for (int j=0; j<p; j++) \
193  x[i].fastAccessDx(j) = rnd.number(); \
194  } \
195  bool success1 = testSerialization( \
196  x, std::string(#FAD) + " Uniform", out); \
197  bool success2 = testSerializationObject( \
198  fts, x, std::string(#FAD) + " Uniform FTS", out); \
199  success = success1 && success2; \
200  } \
201  \
202  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadEmpty ) { \
203  int n = 7; \
204  ValueTypeSerializer<int,FadType> fts( \
205  rcp(new ValueTypeSerializer<int,double>), 5); \
206  Teuchos::Array<FadType> x(n); \
207  for (int i=0; i<n; i++) { \
208  x[i] = FadType(rnd.number()); \
209  } \
210  bool success1 = testSerialization(x, std::string( \
211  #FAD) + " Empty", out); \
212  bool success2 = testSerializationObject( \
213  fts, x, std::string(#FAD) + " Empty FTS", out); \
214  success = success1 && success2; \
215  } \
216  \
217  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadMixed ) { \
218  int n = 6; \
219  int p[] = { 5, 0, 8, 8, 3, 0 }; \
220  ValueTypeSerializer<int,FadType> fts( \
221  rcp(new ValueTypeSerializer<int,double>), 8); \
222  Teuchos::Array<FadType> x(n); \
223  for (int i=0; i<n; i++) { \
224  x[i] = FadType(p[i], rnd.number()); \
225  for (int j=0; j<p[i]; j++) \
226  x[i].fastAccessDx(j) = rnd.number(); \
227  } \
228  bool success1 = testSerialization( \
229  x, std::string(#FAD) + " Mixed", out); \
230  bool success2 = testSerializationObject( \
231  fts, x, std::string(#FAD) + " Mixed FTS", out); \
232  success = success1 && success2; \
233  } \
234  \
235  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
236  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
237  int n = 7; \
238  int p1 = 5; \
239  int p2 = 3; \
240  RCP< ValueTypeSerializer<int,FadType> > fts = \
241  rcp(new ValueTypeSerializer<int,FadType>( \
242  rcp(new ValueTypeSerializer<int,double>), p1)); \
243  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
244  Teuchos::Array<FadFadType> x(n); \
245  for (int i=0; i<n; i++) { \
246  FadType f(p1, rnd.number()); \
247  for (int k=0; k<p1; k++) \
248  f.fastAccessDx(k) = rnd.number(); \
249  x[i] = FadFadType(p2, f); \
250  for (int j=0; j<p2; j++) { \
251  FadType g(p1, rnd.number()); \
252  for (int k=0; k<p1; k++) \
253  g.fastAccessDx(k) = rnd.number(); \
254  x[i].fastAccessDx(j) = g; \
255  } \
256  } \
257  bool success1 = testSerialization( \
258  x, std::string(#FAD) + " Nested Uniform", out); \
259  bool success2 = \
260  testNestedSerializationObject( \
261  ffts, x, std::string(#FAD) + " Nested Uniform", out); \
262  success = success1 && success2; \
263  } \
264  \
265  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyInner ) { \
266  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
267  int n = 7; \
268  int p1 = 5; \
269  int p2 = 3; \
270  RCP< ValueTypeSerializer<int,FadType> > fts = \
271  rcp(new ValueTypeSerializer<int,FadType>( \
272  rcp(new ValueTypeSerializer<int,double>), p1)); \
273  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
274  Teuchos::Array<FadFadType> x(n); \
275  for (int i=0; i<n; i++) { \
276  FadType f(p1, rnd.number()); \
277  for (int k=0; k<p1; k++) \
278  f.fastAccessDx(k) = rnd.number(); \
279  x[i] = FadFadType(p2, f); \
280  for (int j=0; j<p2; j++) \
281  x[i].fastAccessDx(j) = rnd.number(); \
282  } \
283  bool success1 = testSerialization( \
284  x, std::string(#FAD) + " Nested Empty Inner", out); \
285  bool success2 = testNestedSerializationObject( \
286  ffts, x, std::string(#FAD) + " Nested Empty Inner FTS", out); \
287  success = success1 && success2; \
288  } \
289  \
290  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyOuter ) { \
291  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
292  int n = 7; \
293  int p1 = 5; \
294  RCP< ValueTypeSerializer<int,FadType> > fts = \
295  rcp(new ValueTypeSerializer<int,FadType>( \
296  rcp(new ValueTypeSerializer<int,double>), p1)); \
297  ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
298  Teuchos::Array<FadFadType> x(n); \
299  for (int i=0; i<n; i++) { \
300  FadType f(p1, rnd.number()); \
301  for (int k=0; k<p1; k++) \
302  f.fastAccessDx(k) = rnd.number(); \
303  x[i] = FadFadType(f); \
304  } \
305  bool success1 =testSerialization( \
306  x, std::string(#FAD) + " Nested Empty Outer", out); \
307  bool success2 =testNestedSerializationObject( \
308  ffts, x, std::string(#FAD) + " Nested Empty Outer FTS", out); \
309  success = success1 && success2; \
310  } \
311  \
312  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyAll ) { \
313  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
314  int n = 7; \
315  RCP< ValueTypeSerializer<int,FadType> > fts = \
316  rcp(new ValueTypeSerializer<int,FadType>( \
317  rcp(new ValueTypeSerializer<int,double>), 5)); \
318  ValueTypeSerializer<int,FadFadType> ffts(fts, 5); \
319  Teuchos::Array<FadFadType> x(n); \
320  for (int i=0; i<n; i++) { \
321  x[i] = rnd.number(); \
322  } \
323  bool success1 = testSerialization( \
324  x, std::string(#FAD) + " Nested Empty All", out); \
325  bool success2 = testNestedSerializationObject( \
326  ffts, x, std::string(#FAD) + " Nested Empty All FTS", out); \
327  success = success1 && success2; \
328  } \
329 
330 #define SFAD_SERIALIZATION_TESTS(FadType, FAD) \
331  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \
332  int n = 7; \
333  int p = 5; \
334  ValueTypeSerializer<int,FadType> fts( \
335  rcp(new ValueTypeSerializer<int,double>), p); \
336  Teuchos::Array<FadType> x(n); \
337  for (int i=0; i<n; i++) { \
338  x[i] = FadType(p, rnd.number()); \
339  for (int j=0; j<p; j++) \
340  x[i].fastAccessDx(j) = rnd.number(); \
341  } \
342  bool success1 = testSerialization( \
343  x, std::string(#FAD) + " Uniform", out); \
344  bool success2 = testSerializationObject( \
345  fts, x, std::string(#FAD) + " Uniform FTS", out); \
346  success = success1 && success2; \
347  } \
348  \
349  TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \
350  typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
351  int n = 7; \
352  int p1 = 5; \
353  int p2 = 5; \
354  RCP< ValueTypeSerializer<int,FadType> > fts = \
355  rcp(new ValueTypeSerializer<int,FadType>( \
356  rcp(new ValueTypeSerializer<int,double>), p1)); \
357  ValueTypeSerializer<int,FadFadType> ffts(fts, p2); \
358  Teuchos::Array<FadFadType> x(n); \
359  for (int i=0; i<n; i++) { \
360  FadType f(p1, rnd.number()); \
361  for (int k=0; k<p1; k++) \
362  f.fastAccessDx(k) = rnd.number(); \
363  x[i] = FadFadType(p2, f); \
364  for (int j=0; j<p2; j++) { \
365  FadType g(p1, rnd.number()); \
366  for (int k=0; k<p1; k++) \
367  g.fastAccessDx(k) = rnd.number(); \
368  x[i].fastAccessDx(j) = g; \
369  } \
370  } \
371  bool success1 = testSerialization( \
372  x, std::string(#FAD) + " Nested Uniform", out); \
373  bool success2 = testNestedSerializationObject( \
374  ffts, x, std::string(#FAD) + " Nested Uniform FTS", out); \
375  success = success1 && success2; \
376  } \
377 
378 
381 FAD_SERIALIZATION_TESTS(Sacado::ELRFad::DFad<double>, ELRFad_DFad)
382 FAD_SERIALIZATION_TESTS(Sacado::ELRCacheFad::DFad<double>, ELRCacheFad_DFad)
383 FAD_SERIALIZATION_TESTS(Sacado::CacheFad::DFad<double>, CacheFad_DFad)
384 
385 typedef Sacado::Fad::SLFad<double,10> Fad_SLFadType;
386 typedef Sacado::ELRFad::SLFad<double,10> ELRFad_SLFadType;
387 typedef Sacado::ELRCacheFad::SLFad<double,10> ELRCacheFad_SLFadType;
388 typedef Sacado::CacheFad::SLFad<double,10> CacheFad_SLFadType;
389 FAD_SERIALIZATION_TESTS(Fad_SLFadType, Fad_SLFad)
390 FAD_SERIALIZATION_TESTS(ELRFad_SLFadType, ELRFad_SLFad)
391 FAD_SERIALIZATION_TESTS(ELRCacheFad_SLFadType, ELRCacheFad_SLFad)
392 FAD_SERIALIZATION_TESTS(CacheFad_SLFadType, CacheFad_SLFad)
393 
394 typedef Sacado::Fad::SFad<double,5> Fad_SFadType;
395 typedef Sacado::ELRFad::SFad<double,5> ELRFad_SFadType;
396 typedef Sacado::ELRCacheFad::SFad<double,5> ELRCacheFad_SFadType;
397 typedef Sacado::CacheFad::SFad<double,5> CacheFad_SFadType;
398 SFAD_SERIALIZATION_TESTS(Fad_SFadType, Fad_SFad)
399 SFAD_SERIALIZATION_TESTS(ELRFad_SFadType, ELRFad_SFad)
400 SFAD_SERIALIZATION_TESTS(ELRCacheFad_SFadType, ELRCacheFad_SFad)
401 SFAD_SERIALIZATION_TESTS(CacheFad_SFadType, CacheFad_SFad)
402 
403 //typedef Sacado::LFad::LogicalSparse<double,int> Fad_LSType;
404 //FAD_SERIALIZATION_TESTS(Fad_LSType, LFad_LS)
405 
406 // DVFad, LFad, Flop
407 
408 int main( int argc, char* argv[] ) {
409  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
410 
412 }
#define FAD_SERIALIZATION_TESTS(FadType, FAD)
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
int * count
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:171
static SACADO_INLINE_FUNCTION bool eval(const T &x, const T &y)
int Ordinal
expr expr expr fastAccessDx(i)) FAD_UNARYOP_MACRO(exp
Sacado::Random< double > rnd
size_type size() const
GeneralFad< StaticFixedStorage< T, Num > > SFad