Sacado Package Browser (Single Doxygen Collection) Version of the Day
Fad_SerializationTests.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                           Sacado Package
00005 //                 Copyright (2006) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00025 // (etphipp@sandia.gov).
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 #include "Teuchos_UnitTestHarness.hpp"
00030 #include "Teuchos_TestingHelpers.hpp"
00031 #include "Teuchos_UnitTestRepository.hpp"
00032 #include "Teuchos_GlobalMPISession.hpp"
00033 
00034 #include "Teuchos_Array.hpp"
00035 #include "Sacado.hpp"
00036 #include "Sacado_Fad_SimpleFad.hpp"
00037 #include "Sacado_CacheFad_DFad.hpp"
00038 #include "Sacado_CacheFad_SFad.hpp"
00039 #include "Sacado_CacheFad_SLFad.hpp"
00040 #include "Sacado_mpl_apply.hpp"
00041 #include "Sacado_Random.hpp"
00042 
00043 using Teuchos::RCP;
00044 using Teuchos::rcp;
00045 using Teuchos::ValueTypeSerializer;
00046 
00047 template <typename FadType>
00048 bool testSerialization(const Teuchos::Array<FadType>& x, 
00049            const std::string& tag,
00050            Teuchos::FancyOStream& out) {
00051   
00052   typedef int Ordinal;
00053   typedef Teuchos::SerializationTraits<Ordinal,FadType> SerT;
00054   
00055   // Serialize
00056   Ordinal count = x.size();
00057   Ordinal bytes = SerT::fromCountToIndirectBytes(count, &x[0]);
00058   char *charBuffer = new char[bytes];
00059   SerT::serialize(count, &x[0], bytes, charBuffer);
00060   
00061   // Deserialize
00062   Ordinal count2 = SerT::fromIndirectBytesToCount(bytes, charBuffer);
00063   Teuchos::Array<FadType> x2(count2);
00064   SerT::deserialize(bytes, charBuffer, count2, &x2[0]);
00065 
00066   delete [] charBuffer;
00067   
00068   // Check counts match
00069   bool success = (count == count2);
00070   out << tag << " serialize/deserialize count test";
00071   if (success)
00072     out << " passed";
00073   else
00074     out << " failed";
00075   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00076       << std::endl;
00077   
00078   // Check Fads match
00079   for (Ordinal i=0; i<count; i++) {
00080     bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
00081     out << tag << " serialize/deserialize fad test " << i;
00082     if (success2)
00083       out << " passed";
00084     else
00085   out << " failed";
00086     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00087   << "." << std::endl;
00088     success = success && success2;
00089   }
00090 
00091   return success;
00092 }
00093 
00094 template <typename FadType, typename Serializer>
00095 bool testSerializationObject(const Serializer& serializer,
00096            Teuchos::Array<FadType>& x, 
00097            const std::string& tag,
00098            Teuchos::FancyOStream& out) {
00099   
00100   typedef int Ordinal;
00101   
00102   // Serialize
00103   Ordinal count = x.size();
00104   Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
00105   char *charBuffer = new char[bytes];
00106   serializer.serialize(count, &x[0], bytes, charBuffer);
00107 
00108   // Expand x to serializer size
00109   Ordinal sz = serializer.getSerializerSize();
00110   for (Ordinal i=0; i<count; i++)
00111     x[i].expand(sz);
00112   
00113   // Deserialize
00114   Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
00115   Teuchos::Array<FadType> x2(count2);
00116   serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
00117 
00118   delete [] charBuffer;
00119   
00120   // Check counts match
00121   bool success = (count == count2);
00122   out << tag << " serialize/deserialize count test";
00123   if (success)
00124     out << " passed";
00125   else
00126     out << " failed";
00127   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00128       << std::endl;
00129   
00130   // Check Fads match
00131   for (Ordinal i=0; i<count; i++) {
00132     bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
00133     out << tag << " serialize/deserialize fad test " << i;
00134     if (success2)
00135       out << " passed";
00136     else
00137   out << " failed";
00138     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00139   << "." << std::endl;
00140     success = success && success2;
00141   }
00142 
00143   return success;
00144 }
00145 
00146 template <typename FadType, typename Serializer>
00147 bool testNestedSerializationObject(const Serializer& serializer,
00148            Teuchos::Array<FadType>& x, 
00149            const std::string& tag,
00150            Teuchos::FancyOStream& out) {
00151   
00152   typedef int Ordinal;
00153   
00154   // Serialize
00155   Ordinal count = x.size();
00156   Ordinal bytes = serializer.fromCountToIndirectBytes(count, &x[0]);
00157   char *charBuffer = new char[bytes];
00158   serializer.serialize(count, &x[0], bytes, charBuffer);
00159 
00160   // Expand x to serializer size
00161   Ordinal sz = serializer.getSerializerSize();
00162   typedef typename Serializer::value_serializer_type VST;
00163   RCP<const VST> vs = serializer.getValueSerializer();
00164   Ordinal sz_inner = vs->getSerializerSize();
00165   for (Ordinal i=0; i<count; i++) {
00166     x[i].expand(sz);
00167     for (Ordinal j=0; j<sz; j++)
00168       x[i].fastAccessDx(j).expand(sz_inner);
00169     x[i].val().expand(sz_inner);
00170   }
00171   
00172   // Deserialize
00173   Ordinal count2 = serializer.fromIndirectBytesToCount(bytes, charBuffer);
00174   Teuchos::Array<FadType> x2(count2);
00175   serializer.deserialize(bytes, charBuffer, count2, &x2[0]);
00176 
00177   delete [] charBuffer;
00178   
00179   // Check counts match
00180   bool success = (count == count2);
00181   out << tag << " serialize/deserialize count test";
00182   if (success)
00183     out << " passed";
00184   else
00185     out << " failed";
00186   out << ":  \n\tExpected:  " << count << ", \n\tGot:       " << count2 << "." 
00187       << std::endl;
00188   
00189   // Check Fads match
00190   for (Ordinal i=0; i<count; i++) {
00191     bool success2 = Sacado::IsEqual<FadType>::eval(x[i], x2[i]);
00192     out << tag << " serialize/deserialize fad test " << i;
00193     if (success2)
00194       out << " passed";
00195     else
00196   out << " failed";
00197     out << ":  \n\tExpected:  " << x[i] << ", \n\tGot:       " << x2[i] 
00198   << "." << std::endl;
00199     success = success && success2;
00200   }
00201 
00202   return success;
00203 }
00204 
00205 #define FAD_SERIALIZATION_TESTS(FadType, FAD)       \
00206   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) {    \
00207     int n = 7;                \
00208     int p = 5;                \
00209     ValueTypeSerializer<int,FadType> fts(       \
00210       rcp(new ValueTypeSerializer<int,double>), p);     \
00211     Teuchos::Array<FadType> x(n);         \
00212     for (int i=0; i<n; i++) {           \
00213       x[i] = FadType(p, rnd.number());          \
00214       for (int j=0; j<p; j++)           \
00215   x[i].fastAccessDx(j) = rnd.number();        \
00216     }                 \
00217     bool success1 = testSerialization(          \
00218       x, std::string(#FAD) + " Uniform", out);        \
00219     bool success2 = testSerializationObject(        \
00220       fts, x, std::string(#FAD) + " Uniform FTS", out);     \
00221     success = success1 && success2;         \
00222   }                 \
00223                   \
00224   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadEmpty ) {      \
00225     int n = 7;                \
00226     ValueTypeSerializer<int,FadType> fts(       \
00227       rcp(new ValueTypeSerializer<int,double>), 5);     \
00228     Teuchos::Array<FadType> x(n);         \
00229     for (int i=0; i<n; i++) {           \
00230       x[i] = FadType(rnd.number());         \
00231     }                 \
00232     bool success1 = testSerialization(x, std::string(     \
00233           #FAD) + " Empty", out);   \
00234     bool success2 = testSerializationObject(        \
00235       fts, x, std::string(#FAD) + " Empty FTS", out);     \
00236     success = success1 && success2;         \
00237   }                 \
00238                   \
00239   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadMixed ) {      \
00240     int n = 6;                \
00241     int p[] = { 5, 0, 8, 8, 3, 0 };         \
00242     ValueTypeSerializer<int,FadType> fts(       \
00243       rcp(new ValueTypeSerializer<int,double>), 8);     \
00244     Teuchos::Array<FadType> x(n);         \
00245     for (int i=0; i<n; i++) {           \
00246       x[i] = FadType(p[i], rnd.number());       \
00247       for (int j=0; j<p[i]; j++)          \
00248   x[i].fastAccessDx(j) = rnd.number();        \
00249     }                 \
00250     bool success1 = testSerialization(          \
00251       x, std::string(#FAD) + " Mixed", out);        \
00252     bool success2 = testSerializationObject(        \
00253       fts, x, std::string(#FAD) + " Mixed FTS", out);     \
00254     success = success1 && success2;         \
00255   }                 \
00256                   \
00257   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) {   \
00258     typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
00259     int n = 7;                \
00260     int p1 = 5;               \
00261     int p2 = 3;               \
00262     RCP< ValueTypeSerializer<int,FadType> > fts =     \
00263       rcp(new ValueTypeSerializer<int,FadType>(       \
00264       rcp(new ValueTypeSerializer<int,double>), p1));   \
00265     ValueTypeSerializer<int,FadFadType> ffts(fts, p2);      \
00266     Teuchos::Array<FadFadType> x(n);          \
00267     for (int i=0; i<n; i++) {           \
00268       FadType f(p1, rnd.number());          \
00269       for (int k=0; k<p1; k++)            \
00270   f.fastAccessDx(k) = rnd.number();       \
00271       x[i] = FadFadType(p2, f);           \
00272       for (int j=0; j<p2; j++) {          \
00273   FadType g(p1, rnd.number());          \
00274   for (int k=0; k<p1; k++)          \
00275     g.fastAccessDx(k) = rnd.number();       \
00276   x[i].fastAccessDx(j) = g;         \
00277       }                 \
00278     }                 \
00279     bool success1 = testSerialization(          \
00280       x, std::string(#FAD) + " Nested Uniform", out);     \
00281     bool success2 =             \
00282       testNestedSerializationObject(          \
00283   ffts, x, std::string(#FAD) + " Nested Uniform", out);   \
00284     success = success1 && success2;         \
00285   }                 \
00286                   \
00287   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyInner ) {    \
00288     typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
00289     int n = 7;                \
00290     int p1 = 5;               \
00291     int p2 = 3;               \
00292     RCP< ValueTypeSerializer<int,FadType> > fts =     \
00293       rcp(new ValueTypeSerializer<int,FadType>(       \
00294       rcp(new ValueTypeSerializer<int,double>), p1));   \
00295     ValueTypeSerializer<int,FadFadType> ffts(fts, p2);      \
00296     Teuchos::Array<FadFadType> x(n);          \
00297     for (int i=0; i<n; i++) {           \
00298       FadType f(p1, rnd.number());          \
00299       for (int k=0; k<p1; k++)            \
00300   f.fastAccessDx(k) = rnd.number();       \
00301       x[i] = FadFadType(p2, f);           \
00302       for (int j=0; j<p2; j++)            \
00303   x[i].fastAccessDx(j) =  rnd.number();       \
00304     }                 \
00305     bool success1 = testSerialization(          \
00306       x, std::string(#FAD) + " Nested Empty Inner", out);   \
00307     bool success2 = testNestedSerializationObject(      \
00308       ffts, x, std::string(#FAD) + " Nested Empty Inner FTS", out); \
00309     success = success1 && success2;         \
00310   }                 \
00311                   \
00312   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyOuter ) {    \
00313     typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
00314     int n = 7;                \
00315     int p1 = 5;               \
00316     RCP< ValueTypeSerializer<int,FadType> > fts =     \
00317       rcp(new ValueTypeSerializer<int,FadType>(       \
00318       rcp(new ValueTypeSerializer<int,double>), p1));   \
00319     ValueTypeSerializer<int,FadFadType> ffts(fts, 5);     \
00320     Teuchos::Array<FadFadType> x(n);          \
00321     for (int i=0; i<n; i++) {           \
00322       FadType f(p1, rnd.number());          \
00323       for (int k=0; k<p1; k++)            \
00324   f.fastAccessDx(k) = rnd.number();       \
00325       x[i] = FadFadType(f);           \
00326     }                 \
00327     bool success1 =testSerialization(         \
00328   x, std::string(#FAD) + " Nested Empty Outer", out);   \
00329     bool success2 =testNestedSerializationObject(     \
00330       ffts, x, std::string(#FAD) + " Nested Empty Outer FTS", out); \
00331     success = success1 && success2;         \
00332   }                 \
00333                   \
00334   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadEmptyAll ) {    \
00335     typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
00336     int n = 7;                \
00337     RCP< ValueTypeSerializer<int,FadType> > fts =     \
00338       rcp(new ValueTypeSerializer<int,FadType>(       \
00339       rcp(new ValueTypeSerializer<int,double>), 5));    \
00340     ValueTypeSerializer<int,FadFadType> ffts(fts, 5);     \
00341     Teuchos::Array<FadFadType> x(n);          \
00342     for (int i=0; i<n; i++) {           \
00343       x[i] = rnd.number();            \
00344     }                 \
00345     bool success1 = testSerialization(          \
00346   x, std::string(#FAD) + " Nested Empty All", out);   \
00347     bool success2 = testNestedSerializationObject(      \
00348       ffts, x, std::string(#FAD) + " Nested Empty All FTS", out); \
00349     success = success1 && success2;         \
00350   }                 \
00351 
00352 #define SFAD_SERIALIZATION_TESTS(FadType, FAD)        \
00353   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadUniform ) {    \
00354     int n = 7;                \
00355     int p = 5;                \
00356     ValueTypeSerializer<int,FadType> fts(       \
00357       rcp(new ValueTypeSerializer<int,double>), p);     \
00358     Teuchos::Array<FadType> x(n);         \
00359     for (int i=0; i<n; i++) {           \
00360       x[i] = FadType(p, rnd.number());          \
00361       for (int j=0; j<p; j++)           \
00362   x[i].fastAccessDx(j) = rnd.number();        \
00363     }                 \
00364     bool success1 = testSerialization(          \
00365       x, std::string(#FAD) + " Uniform", out);        \
00366     bool success2 = testSerializationObject(        \
00367       fts, x, std::string(#FAD) + " Uniform FTS", out);     \
00368     success = success1 && success2;         \
00369   }                 \
00370                   \
00371   TEUCHOS_UNIT_TEST( FAD##_Serialization, FadFadUniform ) {   \
00372     typedef Sacado::mpl::apply<FadType,FadType>::type FadFadType; \
00373     int n = 7;                \
00374     int p1 = 5;               \
00375     int p2 = 5;               \
00376     RCP< ValueTypeSerializer<int,FadType> > fts =     \
00377       rcp(new ValueTypeSerializer<int,FadType>(       \
00378       rcp(new ValueTypeSerializer<int,double>), p1));   \
00379     ValueTypeSerializer<int,FadFadType> ffts(fts, p2);      \
00380     Teuchos::Array<FadFadType> x(n);          \
00381     for (int i=0; i<n; i++) {           \
00382       FadType f(p1, rnd.number());          \
00383       for (int k=0; k<p1; k++)            \
00384   f.fastAccessDx(k) = rnd.number();       \
00385       x[i] = FadFadType(p2, f);           \
00386       for (int j=0; j<p2; j++) {          \
00387   FadType g(p1, rnd.number());          \
00388   for (int k=0; k<p1; k++)          \
00389     g.fastAccessDx(k) = rnd.number();       \
00390   x[i].fastAccessDx(j) = g;         \
00391       }                 \
00392     }                 \
00393     bool success1 = testSerialization(          \
00394       x, std::string(#FAD) + " Nested Uniform", out);     \
00395     bool success2 = testNestedSerializationObject(      \
00396       ffts, x, std::string(#FAD) + " Nested Uniform FTS", out);   \
00397     success = success1 && success2;         \
00398   }                 \
00399 
00400 
00401 Sacado::Random<double> rnd;
00402 FAD_SERIALIZATION_TESTS(Sacado::Fad::DFad<double>, Fad_DFad)
00403 FAD_SERIALIZATION_TESTS(Sacado::ELRFad::DFad<double>, ELRFad_DFad)
00404 FAD_SERIALIZATION_TESTS(Sacado::ELRCacheFad::DFad<double>, ELRCacheFad_DFad)
00405 FAD_SERIALIZATION_TESTS(Sacado::CacheFad::DFad<double>, CacheFad_DFad)
00406 
00407 typedef Sacado::Fad::SLFad<double,10> Fad_SLFadType;
00408 typedef Sacado::ELRFad::SLFad<double,10> ELRFad_SLFadType;
00409 typedef Sacado::ELRCacheFad::SLFad<double,10> ELRCacheFad_SLFadType;
00410 typedef Sacado::CacheFad::SLFad<double,10> CacheFad_SLFadType;
00411 FAD_SERIALIZATION_TESTS(Fad_SLFadType, Fad_SLFad)
00412 FAD_SERIALIZATION_TESTS(ELRFad_SLFadType, ELRFad_SLFad)
00413 FAD_SERIALIZATION_TESTS(ELRCacheFad_SLFadType, ELRCacheFad_SLFad)
00414 FAD_SERIALIZATION_TESTS(CacheFad_SLFadType, CacheFad_SLFad)
00415 
00416 typedef Sacado::Fad::SFad<double,5> Fad_SFadType;
00417 typedef Sacado::ELRFad::SFad<double,5> ELRFad_SFadType;
00418 typedef Sacado::ELRCacheFad::SFad<double,5> ELRCacheFad_SFadType;
00419 typedef Sacado::CacheFad::SFad<double,5> CacheFad_SFadType;
00420 SFAD_SERIALIZATION_TESTS(Fad_SFadType, Fad_SFad)
00421 SFAD_SERIALIZATION_TESTS(ELRFad_SFadType, ELRFad_SFad)
00422 SFAD_SERIALIZATION_TESTS(ELRCacheFad_SFadType, ELRCacheFad_SFad)
00423 SFAD_SERIALIZATION_TESTS(CacheFad_SFadType, CacheFad_SFad)
00424 
00425 FAD_SERIALIZATION_TESTS(Sacado::Fad::DMFad<double>, Fad_DMFad)
00426 //typedef Sacado::LFad::LogicalSparse<double,int> Fad_LSType;
00427 //FAD_SERIALIZATION_TESTS(Fad_LSType, LFad_LS)
00428 
00429 // DVFad, LFad, Flop
00430 
00431 template <>
00432 Sacado::Fad::MemPool* Sacado::Fad::MemPoolStorage<double>::defaultPool_ = NULL;
00433 template <>
00434 Sacado::Fad::MemPool* Sacado::Fad::MemPoolStorage< Sacado::Fad::DMFad<double> >::defaultPool_ = NULL;
00435 
00436 int main( int argc, char* argv[] ) {
00437   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00438 
00439   Sacado::Fad::MemPoolManager<double> poolManager(100);
00440   Sacado::Fad::MemPool *pool = poolManager.getMemoryPool(10);
00441   Sacado::Fad::DMFad<double>::setDefaultPool(pool);
00442 
00443   Sacado::Fad::MemPoolManager< Sacado::Fad::DMFad<double> > poolManager2(100);
00444   Sacado::Fad::MemPool *pool2 = poolManager2.getMemoryPool(5);
00445   Sacado::Fad::DMFad< Sacado::Fad::DMFad<double> >::setDefaultPool(pool2);
00446 
00447   return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv);
00448 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines