41#include "Teuchos_UnitTestHarness.hpp"
42#include "Teuchos_TestingHelpers.hpp"
43#include "Teuchos_UnitTestRepository.hpp"
44#include "Teuchos_GlobalMPISession.hpp"
46#include "Teuchos_Array.hpp"
49#include "Sacado_Fad_DFad.hpp"
50#include "Sacado_mpl_apply.hpp"
51#include "Sacado_Random.hpp"
53#include <Kokkos_Core.hpp>
66template <
typename VecType,
typename FadType>
73 typedef typename Sacado::mpl::apply<FadType,VecType>::type
FadVecType;
83 rcp(
new Teuchos::ValueTypeSerializer<int,double>()),
sz));
88template <
typename VecType>
90 const std::string& tag,
91 Teuchos::FancyOStream& out) {
94 typedef Teuchos::SerializationTraits<Ordinal,VecType> SerT;
98 Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
99 char *charBuffer =
new char[bytes];
100 SerT::serialize(count, &x[0], bytes, charBuffer);
101 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
104 bool success = (count == count2);
105 out << tag <<
" serialize/deserialize count test";
110 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
114 Teuchos::Array<VecType> x2(count2);
115 for (
Ordinal i=0; i<count2; i++)
116 x2[i].reset(x[i].size());
117 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
119 delete [] charBuffer;
122 for (
Ordinal i=0; i<count; i++) {
123 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
124 out << tag <<
" serialize/deserialize vec test " << i;
129 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
131 success = success && success2;
137template <
typename VecType,
typename Serializer>
139 const Serializer& serializer,
140 const std::string& tag,
141 Teuchos::FancyOStream& out) {
147 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
148 char *charBuffer =
new char[bytes];
149 serializer.serialize(count, &x[0], bytes, charBuffer);
152 for (
Ordinal i=0; i<count; i++)
153 x[i].reset(serializer.getSerializerSize());
156 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
157 Teuchos::Array<VecType> x2(count2);
158 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
160 delete [] charBuffer;
163 bool success = (count == count2);
164 out << tag <<
" serialize/deserialize count test";
169 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
173 for (
Ordinal i=0; i<count; i++) {
174 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
175 out << tag <<
" serialize/deserialize vec test " << i;
180 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
182 success = success && success2;
188template <
typename VecType,
typename Serializer>
190 const Serializer& serializer,
191 const std::string& tag,
192 Teuchos::FancyOStream& out) {
198 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
199 char *charBuffer =
new char[bytes];
200 serializer.serialize(count, &x[0], bytes, charBuffer);
203 Ordinal sz = serializer.getSerializerSize();
204 typedef typename Serializer::value_serializer_type VST;
205 RCP<const VST> vs = serializer.getValueSerializer();
206 for (
Ordinal i=0; i<count; i++) {
210 x[i].val().reset(vs->getSerializerSize());
214 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
215 Teuchos::Array<VecType> x2(count2);
216 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
218 delete [] charBuffer;
221 bool success = (count == count2);
222 out << tag <<
" serialize/deserialize count test";
227 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
231 for (
Ordinal i=0; i<count; i++) {
232 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
233 out << tag <<
" serialize/deserialize vec test " << i;
238 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
240 success = success && success2;
246#define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec) \
247 TEUCHOS_UNIT_TEST( Vec##_Serialization, Uniform ) { \
249 Teuchos::Array<VecType> x(n); \
250 for (int i=0; i<n; i++) { \
251 x[i] = VecType(setup.sz, 0.0); \
252 for (int j=0; j<setup.sz; j++) \
253 x[i].fastAccessCoeff(j) = rnd.number(); \
255 bool success1 = testSerialization( \
256 x, std::string(#Vec) + " Uniform", out); \
257 bool success2 = testSerialization( \
258 x, *setup.vec_serializer, std::string(#Vec) + " Uniform PTS", out); \
259 success = success1 && success2; \
262 TEUCHOS_UNIT_TEST( Vec##_Serialization, Empty ) { \
264 Teuchos::Array<VecType> x(n); \
265 for (int i=0; i<n; i++) { \
266 x[i] = VecType(1, 0.0); \
267 x[i].val() = rnd.number(); \
269 bool success1 = testSerialization( \
270 x, std::string(#Vec) + " Empty", out); \
271 bool success2 = testSerialization( \
272 x, *setup.vec_serializer, std::string(#Vec) + " Empty PTS", out); \
273 success = success1 && success2; \
276 TEUCHOS_UNIT_TEST( Vec##_Serialization, Mixed ) { \
278 int p[] = { 5, 0, 8, 8, 3, 0 }; \
279 Teuchos::Array<VecType> x(n); \
280 for (int i=0; i<n; i++) { \
281 x[i] = VecType(p[i], 0.0); \
282 for (int j=0; j<p[i]; j++) \
283 x[i].fastAccessCoeff(j) = rnd.number(); \
285 bool success1 = testSerialization( \
286 x, std::string(#Vec) + " Mixed", out); \
287 bool success2 = testSerialization( \
288 x, *setup.vec_serializer, std::string(#Vec) + " Mixed PTS", out); \
289 success = success1 && success2; \
292 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecUniform ) { \
293 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
296 Teuchos::Array<FadVecType> x(n); \
297 for (int i=0; i<n; i++) { \
298 VecType f(setup.sz, 0.0); \
299 for (int k=0; k<setup.sz; k++) \
300 f.fastAccessCoeff(k) = rnd.number(); \
301 x[i] = FadVecType(p, f); \
302 for (int j=0; j<p; j++) { \
303 VecType g(setup.sz, 0.0); \
304 for (int k=0; k<setup.sz; k++) \
305 g.fastAccessCoeff(k) = rnd.number(); \
306 x[i].fastAccessDx(j) = g; \
310 testNestedSerialization(x, *setup.fad_vec_serializer, \
311 std::string(#Vec) + " Nested Uniform", out); \
313 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyInner ) { \
314 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
317 Teuchos::Array<FadVecType> x(n); \
318 for (int i=0; i<n; i++) { \
319 VecType f(setup.sz, 0.0); \
320 for (int k=0; k<setup.sz; k++) \
321 f.fastAccessCoeff(k) = rnd.number(); \
322 x[i] = FadVecType(p, f); \
323 for (int j=0; j<p; j++) \
324 x[i].fastAccessDx(j) = rnd.number(); \
327 testNestedSerialization( \
328 x, *setup.fad_vec_serializer, \
329 std::string(#Vec) + " Nested Empty Inner", out); \
331 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyOuter ) { \
332 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
334 Teuchos::Array<FadVecType> x(n); \
335 for (int i=0; i<n; i++) { \
336 VecType f(setup.sz, 0.0); \
337 for (int k=0; k<setup.sz; k++) \
338 f.fastAccessCoeff(k) = rnd.number(); \
339 x[i] = FadVecType(f); \
342 testNestedSerialization( \
343 x, *setup.fad_vec_serializer, \
344 std::string(#Vec) + " Nested Empty Outer", out); \
346 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyAll ) { \
347 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
349 Teuchos::Array<FadVecType> x(n); \
350 for (int i=0; i<n; i++) { \
351 x[i] = rnd.number(); \
354 testNestedSerialization( \
355 x, *setup.fad_vec_serializer, \
356 std::string(#Vec) + " Nested Empty All", out); \
360 Sacado::Random<double>
rnd;
363 typedef Sacado::Fad::DFad<double>
fad_type;
370 Sacado::Random<double>
rnd;
373 typedef Sacado::Fad::DFad<double>
fad_type;
380 Sacado::Random<double>
rnd;
383 typedef Sacado::Fad::DFad<double>
fad_type;
390 Sacado::Random<double>
rnd;
393 typedef Sacado::Fad::DFad<double>
fad_type;
400 Teuchos::GlobalMPISession mpiSession(&argc, &
argv);
401 return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc,
argv);
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
int main(int argc, char *argv[])
#define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec)
bool testNestedSerialization(Teuchos::Array< VecType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
bool testSerialization(const Teuchos::Array< VecType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Sacado::Fad::DFad< double > fad_type
Statically allocated storage class.
Statically allocated storage class.
Stokhos::DynamicStridedStorage< int, double, execution_space > storage_type
Sacado::Fad::DFad< double > fad_type
Sacado::MP::Vector< storage_type > vec_type
Kokkos::DefaultExecutionSpace execution_space
Sacado::Random< double > rnd
UnitTestSetup< vec_type, fad_type > setup
Sacado::MP::Vector< storage_type > vec_type
Kokkos::DefaultExecutionSpace execution_space
Sacado::Fad::DFad< double > fad_type
Stokhos::DynamicStorage< int, double, execution_space > storage_type
UnitTestSetup< vec_type, fad_type > setup
Sacado::Random< double > rnd
Sacado::MP::Vector< storage_type > vec_type
Sacado::Random< double > rnd
Sacado::Fad::DFad< double > fad_type
Kokkos::DefaultExecutionSpace execution_space
Stokhos::StaticFixedStorage< int, double, 8, execution_space > storage_type
UnitTestSetup< vec_type, fad_type > setup
Kokkos::DefaultExecutionSpace execution_space
Sacado::MP::Vector< storage_type > vec_type
UnitTestSetup< vec_type, fad_type > setup
Sacado::Fad::DFad< double > fad_type
Sacado::Random< double > rnd
Stokhos::StaticStorage< int, double, 8, execution_space > storage_type
Sacado::mpl::apply< FadType, VecType >::type FadVecType
Teuchos::ValueTypeSerializer< int, VecType > VecSerializerT
RCP< FadVecSerializerT > fad_vec_serializer
RCP< VecSerializerT > vec_serializer
Teuchos::ValueTypeSerializer< int, FadVecType > FadVecSerializerT