Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tay_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"
18 #include "Sacado_mpl_apply.hpp"
19 #include "Sacado_Random.hpp"
20 
21 using Teuchos::RCP;
22 using Teuchos::rcp;
24 
25 template <typename TayType>
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<TayType> 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 coefficients match
57  for (Ordinal i=0; i<count; i++) {
58  bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
59  out << tag << " serialize/deserialize taylor 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 TayType, 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].resize(sz-1, true);
90 
91  // Deserialize
92  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
93  Teuchos::Array<TayType> 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 coefficients match
109  for (Ordinal i=0; i<count; i++) {
110  bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
111  out << tag << " serialize/deserialize taylor 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 TayType, 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].resize(sz-1, true);
145  for (Ordinal j=0; j<sz; j++)
146  x[i].fastAccessCoeff(j).resize(sz_inner-1, true);
147  x[i].val().resize(sz_inner-1, true);
148  }
149 
150  // Deserialize
151  Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
152  Teuchos::Array<TayType> 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 coefficients match
168  for (Ordinal i=0; i<count; i++) {
169  bool success2 = Sacado::IsEqual<TayType>::eval(x[i], x2[i]);
170  out << tag << " serialize/deserialize taylor 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 TAY_SERIALIZATION_TESTS(TayType, TAY) \
184  TEUCHOS_UNIT_TEST( TAY##_Serialization, Uniform ) { \
185  int n = 7; \
186  int p = 5; \
187  ValueTypeSerializer<int,TayType> tts( \
188  rcp(new ValueTypeSerializer<int,double>), p+1); \
189  Teuchos::Array<TayType> x(n); \
190  for (int i=0; i<n; i++) { \
191  x[i] = TayType(p, rnd.number()); \
192  for (int j=0; j<=p; j++) \
193  x[i].fastAccessCoeff(j) = rnd.number(); \
194  } \
195  bool success1 = testSerialization( \
196  x, std::string(#TAY) + " Uniform", out); \
197  bool success2 = testSerializationObject( \
198  tts, x, std::string(#TAY) + " Uniform TTS", out); \
199  success = success1 && success2; \
200  } \
201  \
202  TEUCHOS_UNIT_TEST( TAY##_Serialization, Empty ) { \
203  int n = 7; \
204  ValueTypeSerializer<int,TayType> tts( \
205  rcp(new ValueTypeSerializer<int,double>), 7); \
206  Teuchos::Array<TayType> x(n); \
207  for (int i=0; i<n; i++) { \
208  x[i] = TayType(rnd.number()); \
209  } \
210  bool success1 = testSerialization( \
211  x, std::string(#TAY) + " Empty", out); \
212  bool success2 = testSerializationObject( \
213  tts, x, std::string(#TAY) + " Empty TTS", out); \
214  success = success1 && success2; \
215  } \
216  \
217  TEUCHOS_UNIT_TEST( TAY##_Serialization, Mixed ) { \
218  int n = 6; \
219  int p[] = { 5, 0, 8, 8, 3, 0 }; \
220  ValueTypeSerializer<int,TayType> tts( \
221  rcp(new ValueTypeSerializer<int,double>), 9); \
222  Teuchos::Array<TayType> x(n); \
223  for (int i=0; i<n; i++) { \
224  x[i] = TayType(p[i], rnd.number()); \
225  for (int j=0; j<=p[i]; j++) \
226  x[i].fastAccessCoeff(j) = rnd.number(); \
227  } \
228  bool success1 = testSerialization( \
229  x, std::string(#TAY) + " Mixed", out); \
230  bool success2 = testSerializationObject( \
231  tts, x, std::string(#TAY) + " Mixed TTS", out); \
232  success = success1 && success2; \
233  } \
234  \
235  TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedUniform ) { \
236  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
237  int n = 7; \
238  int p1 = 5; \
239  int p2 = 3; \
240  RCP< ValueTypeSerializer<int,TayType> > tts = \
241  rcp(new ValueTypeSerializer<int,TayType>( \
242  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
243  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
244  Teuchos::Array<TayTayType> x(n); \
245  for (int i=0; i<n; i++) { \
246  TayType f(p1, rnd.number()); \
247  for (int k=0; k<=p1; k++) \
248  f.fastAccessCoeff(k) = rnd.number(); \
249  x[i] = TayTayType(p2, f); \
250  for (int j=0; j<=p2; j++) { \
251  TayType g(p1, rnd.number()); \
252  for (int k=0; k<=p1; k++) \
253  g.fastAccessCoeff(k) = rnd.number(); \
254  x[i].fastAccessCoeff(j) = g; \
255  } \
256  } \
257  bool success1 = testSerialization( \
258  x, std::string(#TAY) + " Nested Uniform", out); \
259  bool success2 = testNestedSerializationObject( \
260  ttts, x, std::string(#TAY) + " Nested Uniform TTS", out); \
261  success = success1 && success2; \
262  } \
263  \
264  TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyInner ) { \
265  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
266  int n = 7; \
267  int p1 = 5; \
268  int p2 = 3; \
269  RCP< ValueTypeSerializer<int,TayType> > tts = \
270  rcp(new ValueTypeSerializer<int,TayType>( \
271  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
272  ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
273  Teuchos::Array<TayTayType> x(n); \
274  for (int i=0; i<n; i++) { \
275  TayType f(p1, rnd.number()); \
276  for (int k=0; k<=p1; k++) \
277  f.fastAccessCoeff(k) = rnd.number(); \
278  x[i] = TayTayType(p2, f); \
279  for (int j=0; j<=p2; j++) \
280  x[i].fastAccessCoeff(j) = rnd.number(); \
281  } \
282  bool success1 = testSerialization( \
283  x, std::string(#TAY) + " Nested Empty Inner", out); \
284  bool success2 = testNestedSerializationObject( \
285  ttts, x, std::string(#TAY) + " Nested Empty Inner TTS", out); \
286  success = success1 && success2; \
287  } \
288  \
289  TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyOuter ) { \
290  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
291  int n = 7; \
292  int p1 = 5; \
293  RCP< ValueTypeSerializer<int,TayType> > tts = \
294  rcp(new ValueTypeSerializer<int,TayType>( \
295  rcp(new ValueTypeSerializer<int,double>), p1+1)); \
296  ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \
297  Teuchos::Array<TayTayType> x(n); \
298  for (int i=0; i<n; i++) { \
299  TayType f(p1, rnd.number()); \
300  for (int k=0; k<=p1; k++) \
301  f.fastAccessCoeff(k) = rnd.number(); \
302  x[i] = TayTayType(f); \
303  } \
304  bool success1 = testSerialization( \
305  x, std::string(#TAY) + " Nested Empty Outer", out); \
306  bool success2 = testNestedSerializationObject( \
307  ttts, x, std::string(#TAY) + " Nested Empty Outer TTS", out); \
308  success = success1 && success2; \
309  } \
310  \
311  TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyAll ) { \
312  typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
313  int n = 7; \
314  RCP< ValueTypeSerializer<int,TayType> > tts = \
315  rcp(new ValueTypeSerializer<int,TayType>( \
316  rcp(new ValueTypeSerializer<int,double>), 5)); \
317  ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \
318  Teuchos::Array<TayTayType> x(n); \
319  for (int i=0; i<n; i++) { \
320  x[i] = rnd.number(); \
321  } \
322  bool success1 = testSerialization( \
323  x, std::string(#TAY) + " Nested Empty All", out); \
324  bool success2 = testNestedSerializationObject( \
325  ttts, x, std::string(#TAY) + " Nested Empty All TTS", out); \
326  success = success1 && success2; \
327  } \
328 
331 TAY_SERIALIZATION_TESTS(Sacado::Tay::CacheTaylor<double>, CacheTaylor)
332 
333 int main( int argc, char* argv[] ) {
334  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
336 }
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
int * count
#define TAY_SERIALIZATION_TESTS(TayType, TAY)
bool testSerialization(const Teuchos::Array< FadType > &x, const std::string &tag, Teuchos::FancyOStream &out)
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)
int main()
Definition: ad_example.cpp:171
void resize(size_type new_size, const value_type &x=value_type())
static SACADO_INLINE_FUNCTION bool eval(const T &x, const T &y)
int Ordinal
Sacado::Random< double > rnd
size_type size() const