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