Sacado Package Browser (Single Doxygen Collection)
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
test
UnitTests
Tay_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_Tay_CacheTaylor.hpp
"
37
#include "
Sacado_mpl_apply.hpp
"
38
#include "
Sacado_Random.hpp
"
39
40
using
Teuchos::RCP;
41
using
Teuchos::rcp;
42
using
Teuchos::ValueTypeSerializer;
43
44
template
<
typename
TayType>
45
bool
testSerialization
(
const
Teuchos::Array<TayType>& x,
46
const
std::string& tag,
47
Teuchos::FancyOStream& out) {
48
49
typedef
int
Ordinal
;
50
typedef
Teuchos::SerializationTraits<Ordinal,TayType> SerT;
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<TayType> 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 coefficients match
76
for
(
Ordinal
i=0; i<count; i++) {
77
bool
success2 =
Sacado::IsEqual<TayType>::eval
(x[i], x2[i]);
78
out << tag <<
" serialize/deserialize taylor 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
TayType,
typename
Serializer>
92
bool
testSerializationObject
(
const
Serializer& serializer,
93
Teuchos::Array<TayType>& x,
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].resize(sz-1,
true
);
109
110
// Deserialize
111
Ordinal
count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
112
Teuchos::Array<TayType> 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 coefficients match
128
for
(
Ordinal
i=0; i<count; i++) {
129
bool
success2 =
Sacado::IsEqual<TayType>::eval
(x[i], x2[i]);
130
out << tag <<
" serialize/deserialize taylor 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
TayType,
typename
Serializer>
144
bool
testNestedSerializationObject
(
const
Serializer& serializer,
145
Teuchos::Array<TayType>& x,
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].resize(sz-1,
true
);
164
for
(
Ordinal
j=0; j<sz; j++)
165
x[i].fastAccessCoeff(j).resize(sz_inner-1,
true
);
166
x[i].val().resize(sz_inner-1,
true
);
167
}
168
169
// Deserialize
170
Ordinal
count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
171
Teuchos::Array<TayType> 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 coefficients match
187
for
(
Ordinal
i=0; i<count; i++) {
188
bool
success2 =
Sacado::IsEqual<TayType>::eval
(x[i], x2[i]);
189
out << tag <<
" serialize/deserialize taylor 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 TAY_SERIALIZATION_TESTS(TayType, TAY) \
203
TEUCHOS_UNIT_TEST( TAY##_Serialization, Uniform ) { \
204
int n = 7; \
205
int p = 5; \
206
ValueTypeSerializer<int,TayType> tts( \
207
rcp(new ValueTypeSerializer<int,double>), p+1); \
208
Teuchos::Array<TayType> x(n); \
209
for (int i=0; i<n; i++) { \
210
x[i] = TayType(p, rnd.number()); \
211
for (int j=0; j<=p; j++) \
212
x[i].fastAccessCoeff(j) = rnd.number(); \
213
} \
214
bool success1 = testSerialization( \
215
x, std::string(#TAY) + " Uniform", out); \
216
bool success2 = testSerializationObject( \
217
tts, x, std::string(#TAY) + " Uniform TTS", out); \
218
success = success1 && success2; \
219
} \
220
\
221
TEUCHOS_UNIT_TEST( TAY##_Serialization, Empty ) { \
222
int n = 7; \
223
ValueTypeSerializer<int,TayType> tts( \
224
rcp(new ValueTypeSerializer<int,double>), 7); \
225
Teuchos::Array<TayType> x(n); \
226
for (int i=0; i<n; i++) { \
227
x[i] = TayType(rnd.number()); \
228
} \
229
bool success1 = testSerialization( \
230
x, std::string(#TAY) + " Empty", out); \
231
bool success2 = testSerializationObject( \
232
tts, x, std::string(#TAY) + " Empty TTS", out); \
233
success = success1 && success2; \
234
} \
235
\
236
TEUCHOS_UNIT_TEST( TAY##_Serialization, Mixed ) { \
237
int n = 6; \
238
int p[] = { 5, 0, 8, 8, 3, 0 }; \
239
ValueTypeSerializer<int,TayType> tts( \
240
rcp(new ValueTypeSerializer<int,double>), 9); \
241
Teuchos::Array<TayType> x(n); \
242
for (int i=0; i<n; i++) { \
243
x[i] = TayType(p[i], rnd.number()); \
244
for (int j=0; j<=p[i]; j++) \
245
x[i].fastAccessCoeff(j) = rnd.number(); \
246
} \
247
bool success1 = testSerialization( \
248
x, std::string(#TAY) + " Mixed", out); \
249
bool success2 = testSerializationObject( \
250
tts, x, std::string(#TAY) + " Mixed TTS", out); \
251
success = success1 && success2; \
252
} \
253
\
254
TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedUniform ) { \
255
typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
256
int n = 7; \
257
int p1 = 5; \
258
int p2 = 3; \
259
RCP< ValueTypeSerializer<int,TayType> > tts = \
260
rcp(new ValueTypeSerializer<int,TayType>( \
261
rcp(new ValueTypeSerializer<int,double>), p1+1)); \
262
ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
263
Teuchos::Array<TayTayType> x(n); \
264
for (int i=0; i<n; i++) { \
265
TayType f(p1, rnd.number()); \
266
for (int k=0; k<=p1; k++) \
267
f.fastAccessCoeff(k) = rnd.number(); \
268
x[i] = TayTayType(p2, f); \
269
for (int j=0; j<=p2; j++) { \
270
TayType g(p1, rnd.number()); \
271
for (int k=0; k<=p1; k++) \
272
g.fastAccessCoeff(k) = rnd.number(); \
273
x[i].fastAccessCoeff(j) = g; \
274
} \
275
} \
276
bool success1 = testSerialization( \
277
x, std::string(#TAY) + " Nested Uniform", out); \
278
bool success2 = testNestedSerializationObject( \
279
ttts, x, std::string(#TAY) + " Nested Uniform TTS", out); \
280
success = success1 && success2; \
281
} \
282
\
283
TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyInner ) { \
284
typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
285
int n = 7; \
286
int p1 = 5; \
287
int p2 = 3; \
288
RCP< ValueTypeSerializer<int,TayType> > tts = \
289
rcp(new ValueTypeSerializer<int,TayType>( \
290
rcp(new ValueTypeSerializer<int,double>), p1+1)); \
291
ValueTypeSerializer<int,TayTayType> ttts(tts, p2+1); \
292
Teuchos::Array<TayTayType> x(n); \
293
for (int i=0; i<n; i++) { \
294
TayType f(p1, rnd.number()); \
295
for (int k=0; k<=p1; k++) \
296
f.fastAccessCoeff(k) = rnd.number(); \
297
x[i] = TayTayType(p2, f); \
298
for (int j=0; j<=p2; j++) \
299
x[i].fastAccessCoeff(j) = rnd.number(); \
300
} \
301
bool success1 = testSerialization( \
302
x, std::string(#TAY) + " Nested Empty Inner", out); \
303
bool success2 = testNestedSerializationObject( \
304
ttts, x, std::string(#TAY) + " Nested Empty Inner TTS", out); \
305
success = success1 && success2; \
306
} \
307
\
308
TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyOuter ) { \
309
typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
310
int n = 7; \
311
int p1 = 5; \
312
RCP< ValueTypeSerializer<int,TayType> > tts = \
313
rcp(new ValueTypeSerializer<int,TayType>( \
314
rcp(new ValueTypeSerializer<int,double>), p1+1)); \
315
ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \
316
Teuchos::Array<TayTayType> x(n); \
317
for (int i=0; i<n; i++) { \
318
TayType f(p1, rnd.number()); \
319
for (int k=0; k<=p1; k++) \
320
f.fastAccessCoeff(k) = rnd.number(); \
321
x[i] = TayTayType(f); \
322
} \
323
bool success1 = testSerialization( \
324
x, std::string(#TAY) + " Nested Empty Outer", out); \
325
bool success2 = testNestedSerializationObject( \
326
ttts, x, std::string(#TAY) + " Nested Empty Outer TTS", out); \
327
success = success1 && success2; \
328
} \
329
\
330
TEUCHOS_UNIT_TEST( TAY##_Serialization, NestedEmptyAll ) { \
331
typedef Sacado::mpl::apply<TayType,TayType>::type TayTayType; \
332
int n = 7; \
333
RCP< ValueTypeSerializer<int,TayType> > tts = \
334
rcp(new ValueTypeSerializer<int,TayType>( \
335
rcp(new ValueTypeSerializer<int,double>), 5)); \
336
ValueTypeSerializer<int,TayTayType> ttts(tts, 5); \
337
Teuchos::Array<TayTayType> x(n); \
338
for (int i=0; i<n; i++) { \
339
x[i] = rnd.number(); \
340
} \
341
bool success1 = testSerialization( \
342
x, std::string(#TAY) + " Nested Empty All", out); \
343
bool success2 = testNestedSerializationObject( \
344
ttts, x, std::string(#TAY) + " Nested Empty All TTS", out); \
345
success = success1 && success2; \
346
} \
347
348
Sacado::Random<double>
rnd
;
349
TAY_SERIALIZATION_TESTS
(
Sacado::Tay::Taylor<double>
, Taylor)
350
TAY_SERIALIZATION_TESTS
(
Sacado::Tay::CacheTaylor<double>
, CacheTaylor)
351
352
int
main
(
int
argc,
char
* argv[] ) {
353
Teuchos::GlobalMPISession mpiSession(&argc, &argv);
354
return
Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
355
}
rnd
Sacado::Random< double > rnd
Definition
CacheFad_CommTests.cpp:40
Ordinal
int Ordinal
Definition
CacheFad_CommTests.cpp:36
Sacado_No_Kokkos.hpp
Sacado_Random.hpp
Sacado_Tay_CacheTaylor.hpp
Sacado_mpl_apply.hpp
testSerialization
bool testSerialization(const Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Definition
Tay_SerializationTests.cpp:45
testSerializationObject
bool testSerializationObject(const Serializer &serializer, Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Definition
Tay_SerializationTests.cpp:92
TAY_SERIALIZATION_TESTS
#define TAY_SERIALIZATION_TESTS(TayType, TAY)
Definition
Tay_SerializationTests.cpp:202
testNestedSerializationObject
bool testNestedSerializationObject(const Serializer &serializer, Teuchos::Array< TayType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Definition
Tay_SerializationTests.cpp:144
Sacado::Random
A random number generator that generates random numbers uniformly distributed in the interval (a,...
Definition
Sacado_Random.hpp:46
Sacado::Tay::CacheTaylor
Forward-mode AD class using dynamic memory allocation.
Definition
Sacado_Tay_CacheTaylor.hpp:228
Sacado::Tay::Taylor
Taylor polynomial class.
Definition
Sacado_Tay_Taylor.hpp:52
main
int main(int argc, char *argv[])
Sacado::IsEqual::eval
static SACADO_INLINE_FUNCTION bool eval(const T &x, const T &y)
Definition
Sacado_Traits.hpp:417
Generated by
1.17.0