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

Generated on Thu Sep 18 12:37:56 2008 for Epetra Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1