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