47 template <
typename FadType>
49 const std::string& tag,
57 Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
58 char *charBuffer =
new char[bytes];
59 SerT::serialize(count, &x[0], bytes, charBuffer);
62 Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
64 SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
69 bool success = (count == count2);
70 out << tag <<
" serialize/deserialize count test";
75 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 79 for (
Ordinal i=0; i<count; i++) {
81 out << tag <<
" serialize/deserialize fad test " << i;
86 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
88 success = success && success2;
94 template <
typename FadType,
typename Serializer>
97 const std::string& tag,
104 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
105 char *charBuffer =
new char[bytes];
106 serializer.serialize(count, &x[0], bytes, charBuffer);
109 Ordinal sz = serializer.getSerializerSize();
110 for (
Ordinal i=0; i<count; i++)
114 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
116 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
118 delete [] charBuffer;
121 bool success = (count == count2);
122 out << tag <<
" serialize/deserialize count test";
127 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 131 for (
Ordinal i=0; i<count; i++) {
133 out << tag <<
" serialize/deserialize fad test " << i;
138 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
140 success = success && success2;
146 template <
typename FadType,
typename Serializer>
149 const std::string& tag,
156 Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
157 char *charBuffer =
new char[bytes];
158 serializer.serialize(count, &x[0], bytes, charBuffer);
161 Ordinal sz = serializer.getSerializerSize();
162 typedef typename Serializer::value_serializer_type VST;
164 Ordinal sz_inner = vs->getSerializerSize();
165 for (
Ordinal i=0; i<count; i++) {
169 x[i].val().expand(sz_inner);
173 Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
175 serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
177 delete [] charBuffer;
180 bool success = (count == count2);
181 out << tag <<
" serialize/deserialize count test";
186 out <<
": \n\tExpected: " << count <<
", \n\tGot: " << count2 <<
"." 190 for (
Ordinal i=0; i<count; i++) {
192 out << tag <<
" serialize/deserialize fad test " << i;
197 out <<
": \n\tExpected: " << x[i] <<
", \n\tGot: " << x2[i]
199 success = success && success2;
205 #define FAD_SERIALIZATION_TESTS(FadType, FAD) \ 206 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \ 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(); \ 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; \ 224 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadEmpty ) { \ 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()); \ 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; \ 239 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadMixed ) { \ 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(); \ 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; \ 257 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \ 258 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \ 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; \ 279 bool success1 = testSerialization( \ 280 x, std::string(#FAD) + " Nested Uniform", out); \ 282 testNestedSerializationObject( \ 283 ffts, x, std::string(#FAD) + " Nested Uniform", out); \ 284 success = success1 && success2; \ 287 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyInner ) { \ 288 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \ 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(); \ 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; \ 312 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyOuter ) { \ 313 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \ 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); \ 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; \ 334 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyAll ) { \ 335 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \ 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(); \ 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; \ 352 #define SFAD_SERIALIZATION_TESTS(FadType, FAD) \ 353 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) { \ 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(); \ 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; \ 371 TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) { \ 372 typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \ 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; \ 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; \ 436 int main(
int argc,
char* argv[] ) {
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
static int runUnitTestsFromMain(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int main(int argc, char *argv[])
Derivative array storage class using dynamic memory allocation.
Sacado::Random< double > rnd
Forward-mode AD class using dynamic memory allocation and expression templates.