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

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