PlayaVectorTester.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 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 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #ifndef PLAYA_VECTORTESTER_HPP
00044 #define PLAYA_VECTORTESTER_HPP
00045 
00046 #include "PlayaVectorDecl.hpp"
00047 #include "PlayaLinearCombinationImpl.hpp"
00048 #include "Thyra_TestSpecifier.hpp"
00049 #include "Teuchos_ScalarTraits.hpp"
00050 
00051 using namespace Playa;
00052 using namespace PlayaExprTemplates;
00053 using namespace Teuchos;
00054 
00055 
00056 
00057 namespace Playa
00058 {
00059 
00060   /** 
00061    * Run comparisons between element-wise calculations and Vector member
00062    * functions.
00063    */
00064   template <class Scalar>
00065   class VectorTester
00066   {
00067   public:
00068     /** \brief Local typedef for promoted scalar magnitude */
00069     typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType ScalarMag;
00070 
00071     /** */
00072     VectorTester(const VectorSpace<Scalar>& space,
00073                  const TestSpecifier<Scalar>& spec,
00074                  const Playa::MPIComm& comm = Playa::MPIComm::world());
00075 
00076     /** */
00077     bool runAllTests() const ;
00078 
00079     /** */
00080     bool sumTest() const ;
00081 
00082     /** */
00083     bool dotStarTest() const ;
00084 
00085     /** */
00086     bool dotSlashTest() const ;
00087 
00088     /** */
00089     bool scalarMultTest() const ;
00090 
00091     /** */
00092     bool overloadedUpdateTest() const ;
00093 
00094 
00095   private:
00096 
00097     /** */
00098     void randomizeVec(Vector<Scalar>& x) const ;
00099 
00100     TestSpecifier<Scalar> spec_;
00101 
00102     VectorSpace<Scalar> space_;
00103 
00104     Playa::MPIComm comm_;
00105 
00106   };
00107 
00108   template <class Scalar> 
00109   inline VectorTester<Scalar>
00110   ::VectorTester(const VectorSpace<Scalar>& space,
00111                  const TestSpecifier<Scalar>& spec,
00112                  const Playa::MPIComm& comm)
00113     : spec_(spec), space_(space), comm_(comm)
00114   {;}
00115 
00116   template <class Scalar> 
00117   inline bool VectorTester<Scalar>
00118   ::runAllTests() const
00119   {
00120     bool pass = true;
00121 
00122     pass = sumTest() && pass;
00123     pass = dotStarTest() && pass;
00124     pass = dotSlashTest() && pass;
00125     pass = scalarMultTest() && pass;
00126     pass = overloadedUpdateTest() && pass;
00127 
00128     return pass;
00129   }
00130 
00131   template <class Scalar> 
00132   inline void VectorTester<Scalar>
00133   ::randomizeVec(Vector<Scalar>& x) const
00134   {
00135     typedef Teuchos::ScalarTraits<Scalar> ST;
00136 
00137     /* do the operation elementwise */
00138     SequentialIterator<Scalar> i;
00139     for (i=space_.begin(); i != space_.end(); i++)
00140       {
00141         x[i] = 2.0*(drand48()-0.5);
00142       }    
00143   }
00144 
00145   template <class Scalar> 
00146   inline bool VectorTester<Scalar>
00147   ::sumTest() const 
00148   {
00149     if (spec_.doTest())
00150       {
00151         std::cerr << "running vector addition test..." << std::endl;
00152 
00153         Vector<Scalar> a = space_.createMember();
00154         Vector<Scalar> b = space_.createMember();
00155         Vector<Scalar> x = space_.createMember();
00156         Vector<Scalar> y = space_.createMember();
00157         x.zero();
00158         y.zero();
00159         cout << "x = " << x << std::endl;
00160         cout << "y = " << y << std::endl;
00161         randomizeVec(a);
00162         randomizeVec(b);
00163         cout << "a = " << a << std::endl;
00164         cout << "b = " << b << std::endl;
00165 
00166         /* do the operation elementwise */
00167         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00168           {
00169             y[i] = a[i] + b[i];
00170           }
00171 
00172         /* do the operation with member functions */
00173         x = a + b ;
00174 
00175         cout << "op   (a+b)=" << std::endl << x << std::endl;
00176         cout << "loop (a+b)=" << std::endl << y << std::endl;
00177   
00178         double err = (x-y).normInf();
00179 
00180         std::cerr << "|sum error|=" << err << std::endl;
00181         if (err > spec_.errorTol())
00182           {
00183             std::cerr << "vector sum test FAILED: tol = " 
00184                  << spec_.errorTol() << std::endl;
00185             return false;
00186           }
00187         else if (err > spec_.warningTol())
00188           {
00189             std::cerr << "WARNING: vector sum test could not beat tol = " 
00190                  << spec_.warningTol() << std::endl;
00191           }
00192   
00193       }
00194     else
00195       {
00196         std::cerr << "skipping vector addition test..." << std::endl;
00197       }
00198     std::cerr << "vector addition test PASSED: tol = " 
00199          << spec_.errorTol() << std::endl;
00200     return true;
00201   }
00202 
00203   
00204 
00205   
00206 
00207   template <class Scalar> 
00208   inline bool VectorTester<Scalar>
00209   ::dotStarTest() const 
00210   {
00211     if (spec_.doTest())
00212       {
00213         std::cerr << "running vector dotStar test..." << std::endl;
00214 
00215         Vector<Scalar> a = space_.createMember();
00216         Vector<Scalar> b = space_.createMember();
00217         Vector<Scalar> x = space_.createMember();
00218         Vector<Scalar> y = space_.createMember();
00219         randomizeVec(a);
00220         randomizeVec(b);
00221 
00222 
00223         /* do the operation with member functions */
00224         x = a.dotStar(b);
00225 
00226         /* do the operation elementwise */
00227         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00228           {
00229             y[i] = a[i] * b[i];
00230           }
00231 
00232         double err = (x-y).normInf();
00233 
00234         std::cerr << "|dotStar error|=" << err << std::endl;
00235         if (err > spec_.errorTol())
00236           {
00237             std::cerr << "vector dotStar test FAILED: tol = " 
00238                  << spec_.errorTol() << std::endl;
00239             return false;
00240           }
00241         else if (err > spec_.warningTol())
00242           {
00243             std::cerr << "WARNING: vector dotStar test could not beat tol = " 
00244                  << spec_.warningTol() << std::endl;
00245           }
00246   
00247       }
00248     else
00249       {
00250         std::cerr << "skipping vector dotStar test..." << std::endl;
00251       }
00252     std::cerr << "vector dotStar test PASSED: tol = " 
00253          << spec_.errorTol() << std::endl;
00254     return true;
00255   }
00256 
00257 
00258   template <class Scalar> 
00259   inline bool VectorTester<Scalar>
00260   ::dotSlashTest() const 
00261   {
00262     if (spec_.doTest())
00263       {
00264         std::cerr << "running vector dotSlash test..." << std::endl;
00265 
00266         Vector<Scalar> a = space_.createMember();
00267         Vector<Scalar> b = space_.createMember();
00268         Vector<Scalar> x = space_.createMember();
00269         Vector<Scalar> y = space_.createMember();
00270         randomizeVec(a);
00271         randomizeVec(b);
00272 
00273 
00274         /* do the operation with member functions */
00275         x = a.dotSlash(b);
00276 
00277 
00278         /* do the operation elementwise */
00279         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00280           {
00281             y[i] = a[i] / b[i];
00282           }
00283   
00284         double err = (x-y).normInf();
00285 
00286         std::cerr << "|dotSlash error|=" << err << std::endl;
00287         if (err > spec_.errorTol())
00288           {
00289             std::cerr << "vector dotSlash test FAILED: tol = " 
00290                  << spec_.errorTol() << std::endl;
00291             return false;
00292           }
00293         else if (err > spec_.warningTol())
00294           {
00295             std::cerr << "WARNING: vector dotSlash test could not beat tol = " 
00296                  << spec_.warningTol() << std::endl;
00297           }
00298   
00299       }
00300     else
00301       {
00302         std::cerr << "skipping vector dotSlash test..." << std::endl;
00303       }
00304     std::cerr << "vector dotSlash test PASSED: tol = " 
00305          << spec_.errorTol() << std::endl;
00306     return true;
00307   }
00308 
00309   
00310   template <class Scalar> 
00311   inline bool VectorTester<Scalar>
00312   ::scalarMultTest() const 
00313   {
00314     if (spec_.doTest())
00315       {
00316         std::cerr << "running vector scalarMult test..." << std::endl;
00317 
00318         Vector<Scalar> a = space_.createMember();
00319         Vector<Scalar> x = space_.createMember();
00320         Vector<Scalar> y = space_.createMember();
00321         randomizeVec(a);
00322 
00323 
00324         /* do the operation with member functions */
00325         x = 3.14*a;
00326 
00327         /* do the operation elementwise */
00328         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00329           {
00330             y[i] = 3.14 * a[i];
00331           }
00332 
00333         double err = (x-y).normInf();
00334 
00335         std::cerr << "|scalarMult error|=" << err << std::endl;
00336         if (err > spec_.errorTol())
00337           {
00338             std::cerr << "vector scalarMult test FAILED: tol = " 
00339                  << spec_.errorTol() << std::endl;
00340             return false;
00341           }
00342         else if (err > spec_.warningTol())
00343           {
00344             std::cerr << "WARNING: vector scalarMult test could not beat tol = " 
00345                  << spec_.warningTol() << std::endl;
00346           }
00347   
00348       }
00349     else
00350       {
00351         std::cerr << "skipping vector scalarMult test..." << std::endl;
00352       }
00353     std::cerr << "vector scalarMult test PASSED: tol = " 
00354          << spec_.errorTol() << std::endl;
00355     return true;
00356   }
00357  
00358   template <class Scalar> 
00359   inline bool VectorTester<Scalar>
00360   ::overloadedUpdateTest() const 
00361   {
00362     if (spec_.doTest())
00363       {
00364         std::cerr << "running vector overloadedUpdate test..." << std::endl;
00365 
00366         Vector<Scalar> a = space_.createMember();
00367         Vector<Scalar> b = space_.createMember();
00368         Vector<Scalar> x = space_.createMember();
00369         Vector<Scalar> y = space_.createMember();
00370         randomizeVec(a);
00371         randomizeVec(b);
00372 
00373 
00374         /* do the operation with member functions */
00375         x = 3.14*a + 1.4*b;
00376 
00377         /* do the operation elementwise */
00378         for (SequentialIterator<Scalar> i=space_.begin(); i!=space_.end(); i++)
00379           {
00380             y[i] = 3.14*a[i] + 1.4*b[i];
00381           }
00382 
00383         double err = (x-y).normInf();
00384 
00385         std::cerr << "|overloadedUpdate error|=" << err << std::endl;
00386         if (err > spec_.errorTol())
00387           {
00388             std::cerr << "vector overloadedUpdate test FAILED: tol = " 
00389                  << spec_.errorTol() << std::endl;
00390             return false;
00391           }
00392         else if (err > spec_.warningTol())
00393           {
00394             std::cerr << "WARNING: vector overloadedUpdate test could not beat tol = " 
00395                  << spec_.warningTol() << std::endl;
00396           }
00397   
00398       }
00399     else
00400       {
00401         std::cerr << "skipping vector overloadedUpdate test..." << std::endl;
00402       }
00403     std::cerr << "vector overloadedUpdate test PASSED: tol = " 
00404          << spec_.errorTol() << std::endl;
00405     return true;
00406   }
00407 
00408 
00409 
00410 }
00411 #endif

Site Contact