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