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

Generated on Wed May 12 21:41:04 2010 for Epetra Package Browser (Single Doxygen Collection) by  doxygen 1.4.7