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 2001 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   Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
00110   int MyPID = Comm.MyPID();
00111   int NumProc = Comm.NumProc(); 
00112 
00113   if (verbose && MyPID==0)
00114     out << Epetra_Version() << endl << endl;
00115 
00116   if (verbose) out << Comm <<endl;
00117 
00118   bool verbose1 = verbose;
00119 
00120   // Redefine verbose to only print on PE 0
00121   if (verbose && rank!=0) verbose = false;
00122 
00123   int NumMyElements = 10000;
00124   int NumMyElements1 = NumMyElements; // Needed for localmap
00125   int NumGlobalElements = NumMyElements*NumProc+EPETRA_MIN(NumProc,3);
00126   if (MyPID < 3) NumMyElements++;
00127   int IndexBase = 0;
00128   int ElementSize = 7;
00129   
00130   // Test LocalMap constructor
00131   // and Petra-defined uniform linear distribution constructor
00132 
00133   if (verbose) out << "\n*********************************************************" << endl;
00134   if (verbose) out << "Checking Epetra_LocalMap(NumMyElements1, IndexBase, Comm)" << endl;
00135   if (verbose) out << "     and Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm)" << endl;
00136   if (verbose) out << "*********************************************************" << endl;
00137 
00138   Epetra_LocalMap *LocalMap = new Epetra_LocalMap(NumMyElements1, IndexBase,
00139                               Comm);
00140   Epetra_BlockMap * BlockMap = new Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm);
00141   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00142 
00143   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00144 
00145   delete BlockMap;
00146 
00147   // Test User-defined linear distribution constructor
00148 
00149   if (verbose) out << "\n*********************************************************" << endl;
00150   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm)" << endl;
00151   if (verbose) out << "*********************************************************" << endl;
00152 
00153   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm);
00154 
00155   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00156 
00157   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00158 
00159   delete BlockMap;
00160 
00161   // Test User-defined arbitrary distribution constructor
00162   // Generate Global Element List.  Do in reverse for fun!
00163 
00164   int * MyGlobalElements = new int[NumMyElements];
00165   int MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
00166   if (Comm.MyPID()>2) MaxMyGID+=3;
00167   for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
00168 
00169   if (verbose) out << "\n*********************************************************" << endl;
00170   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements,  ElementSize, IndexBase, Comm)" << endl;
00171   if (verbose) out << "*********************************************************" << endl;
00172 
00173   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize,
00174           IndexBase, Comm);
00175   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00176 
00177   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00178 
00179   delete BlockMap;
00180 
00181   int * ElementSizeList = new int[NumMyElements];
00182   int NumMyEquations = 0;
00183   int NumGlobalEquations = 0;
00184   for (i = 0; i<NumMyElements; i++) 
00185     {
00186       ElementSizeList[i] = i%6+2; // blocksizes go from 2 to 7
00187       NumMyEquations += ElementSizeList[i];
00188     }
00189   ElementSize = 7; // Set to maximum for use in checkmap
00190   NumGlobalEquations = Comm.NumProc()*NumMyEquations;
00191 
00192   // Adjust NumGlobalEquations based on processor ID
00193   if (Comm.NumProc() > 3)
00194     {
00195       if (Comm.MyPID()>2)
00196   NumGlobalEquations += 3*((NumMyElements)%6+2);
00197       else 
00198   NumGlobalEquations -= (Comm.NumProc()-3)*((NumMyElements-1)%6+2);
00199     }
00200 
00201   if (verbose) out << "\n*********************************************************" << endl;
00202   if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements,  ElementSizeList, IndexBase, Comm)" << endl;
00203   if (verbose) out << "*********************************************************" << endl;
00204 
00205   BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList,
00206           IndexBase, Comm);
00207   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00208 
00209   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00210 
00211   // Test Copy constructor
00212 
00213   if (verbose) out << "\n*********************************************************" << endl;
00214   if (verbose) out << "Checking Epetra_BlockMap(*BlockMap)" << endl;
00215   if (verbose) out << "*********************************************************" << endl;
00216 
00217   Epetra_BlockMap * BlockMap1 = new Epetra_BlockMap(*BlockMap);
00218 
00219   EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr);
00220 
00221   EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr);
00222 
00223   delete [] ElementSizeList;
00224   delete [] MyGlobalElements;
00225   delete BlockMap;
00226   delete BlockMap1;
00227 
00228 
00229   // Test Petra-defined uniform linear distribution constructor
00230 
00231   if (verbose) out << "\n*********************************************************" << endl;
00232   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, IndexBase, Comm)" << endl;
00233   if (verbose) out << "*********************************************************" << endl;
00234 
00235   Epetra_Map * Map = new Epetra_Map(NumGlobalElements, IndexBase, Comm);
00236   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00237 
00238   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00239 
00240   delete Map;
00241 
00242   // Test User-defined linear distribution constructor
00243 
00244   if (verbose) out << "\n*********************************************************" << endl;
00245   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm)" << endl;
00246   if (verbose) out << "*********************************************************" << endl;
00247 
00248   Map = new Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm);
00249 
00250   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00251 
00252   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00253 
00254   delete Map;
00255 
00256   // Test User-defined arbitrary distribution constructor
00257   // Generate Global Element List.  Do in reverse for fun!
00258 
00259   MyGlobalElements = new int[NumMyElements];
00260   MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase;
00261   if (Comm.MyPID()>2) MaxMyGID+=3;
00262   for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i;
00263 
00264   if (verbose) out << "\n*********************************************************" << endl;
00265   if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements,  IndexBase, Comm)" << endl;
00266   if (verbose) out << "*********************************************************" << endl;
00267 
00268   Map = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, 
00269           IndexBase, Comm);
00270   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00271 
00272   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00273 
00274   // Test Copy constructor
00275 
00276   if (verbose) out << "\n*********************************************************" << endl;
00277   if (verbose) out << "Checking Epetra_Map(*Map)" << endl;
00278   if (verbose) out << "*********************************************************" << endl;
00279  
00280   Epetra_Map Map1(*Map);
00281 
00282   EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr);
00283 
00284   EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr);
00285 
00286   delete [] MyGlobalElements;
00287   delete Map;
00288 
00289   if (verbose1)
00290     {
00291       // Test Vector MFLOPS for 2D Dot Product
00292       int M = 1;
00293       int K = 1000000;
00294       Epetra_Map Map2(-1, K, IndexBase, Comm);
00295       Epetra_LocalMap Map3(M, IndexBase, Comm);
00296       
00297       Epetra_Vector A(Map2);A.Random();
00298       Epetra_Vector B(Map2);B.Random();
00299       Epetra_Vector C(Map3);C.Random();
00300 
00301       // Test Epetra_Vector label
00302       const char* VecLabel = A.Label();
00303       const char* VecLabel1 = "Epetra::Vector";
00304       if (verbose) out << endl << endl <<"This should say " << VecLabel1 << ": " << VecLabel << endl << endl << endl;
00305       EPETRA_TEST_ERR(strcmp(VecLabel1,VecLabel),ierr);
00306       if (verbose) out << "Testing Assignment operator" << endl;
00307 
00308       double tmp1 = 1.00001* (double) (MyPID+1);
00309       double tmp2 = tmp1;
00310       A[1] = tmp1;
00311       tmp2 = A[1];
00312       out << "On PE "<< MyPID << "  A[1] should equal = " << tmp1;
00313       if (tmp1==tmp2) out << " and it does!" << endl;
00314       else out << " but it equals " << tmp2;
00315  
00316       Comm.Barrier();
00317     
00318       if (verbose) out << endl << endl << "Testing MFLOPs" << endl;
00319       Epetra_Flops counter;
00320       C.SetFlopCounter(counter);
00321       Epetra_Time mytimer(Comm);
00322       C.Multiply('T', 'N', 0.5, A, B, 0.0);
00323       double Multiply_time = mytimer.ElapsedTime();
00324       double Multiply_flops = C.Flops();
00325       if (verbose) out << "\n\nTotal FLOPs = " << Multiply_flops << endl;
00326       if (verbose) out << "Total Time  = " << Multiply_time << endl;
00327       if (verbose) out << "MFLOPs      = " << Multiply_flops/Multiply_time/1000000.0 << endl;
00328 
00329       Comm.Barrier(); 
00330     
00331       // Test Vector ostream operator with Petra-defined uniform linear distribution constructor
00332       // and a small vector
00333       
00334       Epetra_Map Map4(100, IndexBase, Comm);
00335       double * Dp = new double[100]; 
00336       for (i=0; i<100; i++)
00337   Dp[i] = i;
00338       Epetra_Vector D(View, Map4,Dp);
00339     
00340       if (verbose) out << "\n\nTesting ostream operator:  Multivector  should be 100-by-2 and print i,j indices" 
00341      << endl << endl;
00342       out << D << endl;
00343 
00344       if (verbose) out << "Traceback Mode value = " << D.GetTracebackMode() << endl;
00345       delete [] Dp;
00346     }
00347 
00348 #ifdef EPETRA_MPI
00349   MPI_Finalize();
00350 #endif
00351 
00352   return ierr;
00353 
00354 }
00355 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines