Epetra Package Browser (Single Doxygen Collection) Development
test/Vector/cxx_main.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //               Epetra: Linear Algebra Services Package 
00005 //                 Copyright 2011 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 Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 
00043 // Epetra_Vector Test routine
00044 
00045 #include "Epetra_Time.h"
00046 #include "Epetra_BlockMap.h"
00047 #include "Epetra_Vector.h"
00048 #include "BuildTestProblems.h"
00049 #include "ExecuteTestProblems.h"
00050 #include "../epetra_test_err.h"
00051 #include "Epetra_Version.h"
00052 
00053 #ifdef EPETRA_MPI
00054 #  include "Epetra_MpiComm.h"
00055 #  include <mpi.h>
00056 #else
00057 #  include "Epetra_SerialComm.h"
00058 #endif
00059 
00060 #ifdef HAVE_EPETRA_TEUCHOS
00061 #  include "Teuchos_VerboseObject.hpp"
00062 #endif
00063 
00064 
00065 int main(int argc, char *argv[]) {
00066 
00067   int ierr = 0, i;
00068 
00069 #ifdef EPETRA_MPI
00070 
00071   // Initialize MPI
00072 
00073   MPI_Init(&argc,&argv);
00074   int rank; // My process ID
00075 
00076   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00077   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00078 
00079 #else
00080 
00081   int rank = 0;
00082   Epetra_SerialComm Comm;
00083 
00084 #endif
00085 
00086 #ifdef HAVE_EPETRA_TEUCHOS
00087   Teuchos::RCP<Teuchos::FancyOStream>
00088     fancyOut = Teuchos::VerboseObjectBase::getDefaultOStream();
00089   if (Comm.NumProc() > 1 ) {
00090     fancyOut->setShowProcRank(true);
00091     fancyOut->setOutputToRootOnly(-1);
00092   }
00093   std::ostream &out = *fancyOut;
00094 #else
00095   std::ostream &out = std::cout;
00096 #endif
00097 
00098   Comm.SetTracebackMode(0); // This should shut down any error tracing
00099   bool verbose = false;
00100 
00101   // Check if we should print results to standard out
00102   if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true;
00103 
00104   //  char tmp;
00105   //  if (rank==0) out << "Press any key to continue..."<< endl;
00106   //  if (rank==0) cin >> tmp;
00107   //  Comm.Barrier();
00108 
00109   int MyPID = Comm.MyPID();
00110   int NumProc = Comm.NumProc(); 
00111 
00112   if (verbose && MyPID==0)
00113     out << Epetra_Version() << endl << endl;
00114 
00115   if (verbose) out << Comm <<endl;
00116 
00117   bool verbose1 = verbose;
00118 
00119   // Redefine verbose to only print on PE 0
00120   if (verbose && rank!=0) verbose = false;
00121 
00122   int NumMyElements = 10000;
00123   int NumMyElements1 = NumMyElements; // Needed for localmap
00124   int NumGlobalElements = NumMyElements*NumProc+EPETRA_MIN(NumProc,3);
00125   if (MyPID < 3) NumMyElements++;
00126   int IndexBase = 0;
00127   int ElementSize = 7;
00128   
00129   // Test LocalMap constructor
00130   // and Petra-defined uniform linear distribution constructor
00131 
00132   if (verbose) out << "\n*********************************************************" << endl;
00133   if (verbose) out << "Checking Epetra_LocalMap(NumMyElements1, IndexBase, Comm)" << endl;
00134   if (verbose) out << "     and Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm)" << endl;
00135   if (verbose) out << "*********************************************************" << endl;
00136 
00137   Epetra_LocalMap *LocalMap = new Epetra_LocalMap(NumMyElements1, IndexBase,
00138                               Comm);
00139   Epetra_BlockMap * BlockMap = new Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm);
00140   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00141 
00142   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00143 
00144   delete BlockMap;
00145 
00146   // Test User-defined linear distribution constructor
00147 
00148   if (verbose) out << "\n*********************************************************" << endl;
00149   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm)" << endl;
00150   if (verbose) out << "*********************************************************" << endl;
00151 
00152   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm);
00153 
00154   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00155 
00156   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00157 
00158   delete BlockMap;
00159 
00160   // Test User-defined arbitrary distribution constructor
00161   // Generate Global Element List.  Do in reverse for fun!
00162 
00163   int * MyGlobalElements = new int[NumMyElements];
00164   int MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
00165   if (Comm.MyPID()>2) MaxMyGID+=3;
00166   for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
00167 
00168   if (verbose) out << "\n*********************************************************" << endl;
00169   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements,  ElementSize, IndexBase, Comm)" << endl;
00170   if (verbose) out << "*********************************************************" << endl;
00171 
00172   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize,
00173           IndexBase, Comm);
00174   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00175 
00176   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00177 
00178   delete BlockMap;
00179 
00180   int * ElementSizeList = new int[NumMyElements];
00181   int NumMyEquations = 0;
00182   int NumGlobalEquations = 0;
00183   for (i = 0; i<NumMyElements; i++) 
00184     {
00185       ElementSizeList[i] = i%6+2; // blocksizes go from 2 to 7
00186       NumMyEquations += ElementSizeList[i];
00187     }
00188   ElementSize = 7; // Set to maximum for use in checkmap
00189   NumGlobalEquations = Comm.NumProc()*NumMyEquations;
00190 
00191   // Adjust NumGlobalEquations based on processor ID
00192   if (Comm.NumProc() > 3)
00193     {
00194       if (Comm.MyPID()>2)
00195   NumGlobalEquations += 3*((NumMyElements)%6+2);
00196       else 
00197   NumGlobalEquations -= (Comm.NumProc()-3)*((NumMyElements-1)%6+2);
00198     }
00199 
00200   if (verbose) out << "\n*********************************************************" << endl;
00201   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements,  ElementSizeList, IndexBase, Comm)" << endl;
00202   if (verbose) out << "*********************************************************" << endl;
00203 
00204   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList,
00205           IndexBase, Comm);
00206   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00207 
00208   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00209 
00210   // Test Copy constructor
00211 
00212   if (verbose) out << "\n*********************************************************" << endl;
00213   if (verbose) out << "Checking Epetra_BlockMap(*BlockMap)" << endl;
00214   if (verbose) out << "*********************************************************" << endl;
00215 
00216   Epetra_BlockMap * BlockMap1 = new Epetra_BlockMap(*BlockMap);
00217 
00218   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00219 
00220   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00221 
00222   delete [] ElementSizeList;
00223   delete [] MyGlobalElements;
00224   delete BlockMap;
00225   delete BlockMap1;
00226 
00227 
00228   // Test Petra-defined uniform linear distribution constructor
00229 
00230   if (verbose) out << "\n*********************************************************" << endl;
00231   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, IndexBase, Comm)" << endl;
00232   if (verbose) out << "*********************************************************" << endl;
00233 
00234   Epetra_Map * Map = new Epetra_Map(NumGlobalElements, IndexBase, Comm);
00235   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00236 
00237   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00238 
00239   delete Map;
00240 
00241   // Test User-defined linear distribution constructor
00242 
00243   if (verbose) out << "\n*********************************************************" << endl;
00244   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm)" << endl;
00245   if (verbose) out << "*********************************************************" << endl;
00246 
00247   Map = new Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm);
00248 
00249   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00250 
00251   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00252 
00253   delete Map;
00254 
00255   // Test User-defined arbitrary distribution constructor
00256   // Generate Global Element List.  Do in reverse for fun!
00257 
00258   MyGlobalElements = new int[NumMyElements];
00259   MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
00260   if (Comm.MyPID()>2) MaxMyGID+=3;
00261   for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
00262 
00263   if (verbose) out << "\n*********************************************************" << endl;
00264   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements,  IndexBase, Comm)" << endl;
00265   if (verbose) out << "*********************************************************" << endl;
00266 
00267   Map = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, 
00268           IndexBase, Comm);
00269   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00270 
00271   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00272 
00273   // Test Copy constructor
00274 
00275   if (verbose) out << "\n*********************************************************" << endl;
00276   if (verbose) out << "Checking Epetra_Map(*Map)" << endl;
00277   if (verbose) out << "*********************************************************" << endl;
00278  
00279   Epetra_Map Map1(*Map);
00280 
00281   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00282 
00283   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00284 
00285   delete [] MyGlobalElements;
00286   delete Map;
00287 
00288   if (verbose1)
00289     {
00290       // Test Vector MFLOPS for 2D Dot Product
00291       int M = 1;
00292       int K = 1000000;
00293       Epetra_Map Map2(-1, K, IndexBase, Comm);
00294       Epetra_LocalMap Map3(M, IndexBase, Comm);
00295       
00296       Epetra_Vector A(Map2);A.Random();
00297       Epetra_Vector B(Map2);B.Random();
00298       Epetra_Vector C(Map3);C.Random();
00299 
00300       // Test Epetra_Vector label
00301       const char* VecLabel = A.Label();
00302       const char* VecLabel1 = "Epetra::Vector";
00303       if (verbose) out << endl << endl <<"This should say " << VecLabel1 << ": " << VecLabel << endl << endl << endl;
00304       EPETRA_TEST_ERR(strcmp(VecLabel1,VecLabel),ierr);
00305       if (verbose) out << "Testing Assignment operator" << endl;
00306 
00307       double tmp1 = 1.00001* (double) (MyPID+1);
00308       double tmp2 = tmp1;
00309       A[1] = tmp1;
00310       tmp2 = A[1];
00311       out << "On PE "<< MyPID << "  A[1] should equal = " << tmp1;
00312       if (tmp1==tmp2) out << " and it does!" << endl;
00313       else out << " but it equals " << tmp2;
00314  
00315       Comm.Barrier();
00316     
00317       if (verbose) out << endl << endl << "Testing MFLOPs" << endl;
00318       Epetra_Flops counter;
00319       C.SetFlopCounter(counter);
00320       Epetra_Time mytimer(Comm);
00321       C.Multiply('T', 'N', 0.5, A, B, 0.0);
00322       double Multiply_time = mytimer.ElapsedTime();
00323       double Multiply_flops = C.Flops();
00324       if (verbose) out << "\n\nTotal FLOPs = " << Multiply_flops << endl;
00325       if (verbose) out << "Total Time  = " << Multiply_time << endl;
00326       if (verbose) out << "MFLOPs      = " << Multiply_flops/Multiply_time/1000000.0 << endl;
00327 
00328       Comm.Barrier(); 
00329     
00330       // Test Vector ostream operator with Petra-defined uniform linear distribution constructor
00331       // and a small vector
00332       
00333       Epetra_Map Map4(100, IndexBase, Comm);
00334       double * Dp = new double[100]; 
00335       for (i=0; i<100; i++)
00336   Dp[i] = i;
00337       Epetra_Vector D(View, Map4,Dp);
00338     
00339       if (verbose) out << "\n\nTesting ostream operator:  Multivector  should be 100-by-2 and print i,j indices" 
00340      << endl << endl;
00341       out << D << endl;
00342 
00343       if (verbose) out << "Traceback Mode value = " << D.GetTracebackMode() << endl;
00344       delete [] Dp;
00345     }
00346 
00347 #ifdef EPETRA_MPI
00348   MPI_Finalize();
00349 #endif
00350 
00351   return ierr;
00352 
00353 }
00354 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines