18 #include "Sacado_Fad_DFad.hpp"
19 #include "Sacado_mpl_apply.hpp"
20 #include "Sacado_Random.hpp"
22 #include <Kokkos_Core.hpp>
35 template <
typename VecType,
typename FadType>
42 typedef typename Sacado::mpl::apply<FadType,VecType>::type
FadVecType;
57 template <
typename VecType>
59 const std::string& tag,
66 Ordinal count = x.
size();
67 Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
68 char *charBuffer =
new char[bytes];
69 SerT::serialize(count, &x[0], bytes, charBuffer);
70 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
73 bool success = (count == count2);
74 out << tag <<
" serialize/deserialize count test";
79 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
84 for (Ordinal i=0; i<count2; i++)
85 x2[i].reset(x[i].size());
86 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
91 for (Ordinal i=0; i<count; i++) {
92 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
93 out << tag <<
" serialize/deserialize vec test " << i;
98 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
100 success = success && success2;
106 template <
typename VecType,
typename Serializer>
108 const Serializer& serializer,
109 const std::string& tag,
115 Ordinal count = x.
size();
116 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
117 char *charBuffer =
new char[bytes];
118 serializer.serialize(count, &x[0], bytes, charBuffer);
121 for (Ordinal i=0; i<count; i++)
122 x[i].reset(serializer.getSerializerSize());
125 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
127 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
129 delete [] charBuffer;
132 bool success = (count == count2);
133 out << tag <<
" serialize/deserialize count test";
138 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
142 for (Ordinal i=0; i<count; i++) {
143 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
144 out << tag <<
" serialize/deserialize vec test " << i;
149 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
151 success = success && success2;
157 template <
typename VecType,
typename Serializer>
159 const Serializer& serializer,
160 const std::string& tag,
166 Ordinal count = x.
size();
167 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
168 char *charBuffer =
new char[bytes];
169 serializer.serialize(count, &x[0], bytes, charBuffer);
172 Ordinal sz = serializer.getSerializerSize();
173 typedef typename Serializer::value_serializer_type VST;
175 for (Ordinal i=0; i<count; i++) {
177 for (Ordinal
j=0;
j<sz;
j++)
179 x[i].val().reset(vs->getSerializerSize());
183 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
185 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
187 delete [] charBuffer;
190 bool success = (count == count2);
191 out << tag <<
" serialize/deserialize count test";
196 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"."
200 for (Ordinal i=0; i<count; i++) {
201 bool success2 = Sacado::IsEqual<VecType>::eval(x[i], x2[i]);
202 out << tag <<
" serialize/deserialize vec test " << i;
207 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
209 success = success && success2;
215 #define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec) \
216 TEUCHOS_UNIT_TEST( Vec##_Serialization, Uniform ) { \
218 Teuchos::Array<VecType> x(n); \
219 for (int i=0; i<n; i++) { \
220 x[i] = VecType(setup.sz, 0.0); \
221 for (int j=0; j<setup.sz; j++) \
222 x[i].fastAccessCoeff(j) = rnd.number(); \
224 bool success1 = testSerialization( \
225 x, std::string(#Vec) + " Uniform", out); \
226 bool success2 = testSerialization( \
227 x, *setup.vec_serializer, std::string(#Vec) + " Uniform PTS", out); \
228 success = success1 && success2; \
231 TEUCHOS_UNIT_TEST( Vec##_Serialization, Empty ) { \
233 Teuchos::Array<VecType> x(n); \
234 for (int i=0; i<n; i++) { \
235 x[i] = VecType(1, 0.0); \
236 x[i].val() = rnd.number(); \
238 bool success1 = testSerialization( \
239 x, std::string(#Vec) + " Empty", out); \
240 bool success2 = testSerialization( \
241 x, *setup.vec_serializer, std::string(#Vec) + " Empty PTS", out); \
242 success = success1 && success2; \
245 TEUCHOS_UNIT_TEST( Vec##_Serialization, Mixed ) { \
247 int p[] = { 5, 0, 8, 8, 3, 0 }; \
248 Teuchos::Array<VecType> x(n); \
249 for (int i=0; i<n; i++) { \
250 x[i] = VecType(p[i], 0.0); \
251 for (int j=0; j<p[i]; j++) \
252 x[i].fastAccessCoeff(j) = rnd.number(); \
254 bool success1 = testSerialization( \
255 x, std::string(#Vec) + " Mixed", out); \
256 bool success2 = testSerialization( \
257 x, *setup.vec_serializer, std::string(#Vec) + " Mixed PTS", out); \
258 success = success1 && success2; \
261 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecUniform ) { \
262 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
265 Teuchos::Array<FadVecType> x(n); \
266 for (int i=0; i<n; i++) { \
267 VecType f(setup.sz, 0.0); \
268 for (int k=0; k<setup.sz; k++) \
269 f.fastAccessCoeff(k) = rnd.number(); \
270 x[i] = FadVecType(p, f); \
271 for (int j=0; j<p; j++) { \
272 VecType g(setup.sz, 0.0); \
273 for (int k=0; k<setup.sz; k++) \
274 g.fastAccessCoeff(k) = rnd.number(); \
275 x[i].fastAccessDx(j) = g; \
279 testNestedSerialization(x, *setup.fad_vec_serializer, \
280 std::string(#Vec) + " Nested Uniform", out); \
282 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyInner ) { \
283 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
286 Teuchos::Array<FadVecType> x(n); \
287 for (int i=0; i<n; i++) { \
288 VecType f(setup.sz, 0.0); \
289 for (int k=0; k<setup.sz; k++) \
290 f.fastAccessCoeff(k) = rnd.number(); \
291 x[i] = FadVecType(p, f); \
292 for (int j=0; j<p; j++) \
293 x[i].fastAccessDx(j) = rnd.number(); \
296 testNestedSerialization( \
297 x, *setup.fad_vec_serializer, \
298 std::string(#Vec) + " Nested Empty Inner", out); \
300 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyOuter ) { \
301 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
303 Teuchos::Array<FadVecType> x(n); \
304 for (int i=0; i<n; i++) { \
305 VecType f(setup.sz, 0.0); \
306 for (int k=0; k<setup.sz; k++) \
307 f.fastAccessCoeff(k) = rnd.number(); \
308 x[i] = FadVecType(f); \
311 testNestedSerialization( \
312 x, *setup.fad_vec_serializer, \
313 std::string(#Vec) + " Nested Empty Outer", out); \
315 TEUCHOS_UNIT_TEST( Vec##_Serialization, FadVecEmptyAll ) { \
316 typedef Sacado::mpl::apply<FadType,VecType>::type FadVecType; \
318 Teuchos::Array<FadVecType> x(n); \
319 for (int i=0; i<n; i++) { \
320 x[i] = rnd.number(); \
323 testNestedSerialization( \
324 x, *setup.fad_vec_serializer, \
325 std::string(#Vec) + " Nested Empty All", out); \
328 namespace DynamicVecTest {
329 Sacado::Random<double>
rnd;
332 typedef Sacado::Fad::DFad<double>
fad_type;
338 namespace DynamicStridedVecTest {
339 Sacado::Random<double>
rnd;
342 typedef Sacado::Fad::DFad<double>
fad_type;
348 namespace StaticVecTest {
349 Sacado::Random<double>
rnd;
352 typedef Sacado::Fad::DFad<double>
fad_type;
358 namespace StaticFixedVecTest {
359 Sacado::Random<double>
rnd;
362 typedef Sacado::Fad::DFad<double>
fad_type;
Sacado::Fad::DFad< double > fad_type
UnitTestSetup< vec_type, fad_type > setup
Stokhos::StaticFixedStorage< int, double, 8, execution_space > storage_type
Kokkos::DefaultExecutionSpace execution_space
#define VEC_SERIALIZATION_TESTS(VecType, FadType, Vec)
Sacado::Random< double > rnd
Statically allocated storage class.
Sacado::Fad::DFad< double > fad_type
Kokkos::DefaultExecutionSpace execution_space
UnitTestSetup< vec_type, fad_type > setup
Teuchos::ValueTypeSerializer< int, VecType > VecSerializerT
Sacado::Fad::DFad< double > fad_type
Stokhos::DynamicStorage< int, double, execution_space > storage_type
UnitTestSetup< vec_type, fad_type > setup
Stokhos::DynamicStridedStorage< int, double, execution_space > storage_type
Stokhos::StaticStorage< int, double, 8, execution_space > storage_type
Sacado::MP::Vector< storage_type > vec_type
static int runUnitTestsFromMain(int argc, char *argv[])
Sacado::Fad::DFad< double > fad_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< VecSerializerT > vec_serializer
Sacado::Random< double > rnd
Teuchos::ValueTypeSerializer< int, FadVecType > FadVecSerializerT
expr expr expr expr fastAccessDx(i, j)) FAD_UNARYOP_MACRO(exp
UnitTestSetup< vec_type, fad_type > setup
bool testSerialization(const Teuchos::Array< VecType > &x, const std::string &tag, Teuchos::FancyOStream &out)
Kokkos::DefaultExecutionSpace execution_space
Sacado::Random< double > rnd
bool testNestedSerialization(Teuchos::Array< VecType > &x, const Serializer &serializer, const std::string &tag, Teuchos::FancyOStream &out)
int main(int argc, char **argv)
Kokkos::DefaultExecutionSpace execution_space
Sacado::MP::Vector< storage_type > vec_type
Sacado::MP::Vector< storage_type > vec_type
Sacado::mpl::apply< FadType, VecType >::type FadVecType
Sacado::MP::Vector< storage_type > vec_type
Sacado::Random< double > rnd
Statically allocated storage class.
Sacado::Fad::DFad< double > fad_type
RCP< FadVecSerializerT > fad_vec_serializer