example/my_example/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 /*#########################################################################
00030   This file can be gutted and used as a template for new examples.  The 
00031   code below is from an existing test and does not need to be preserved.
00032   Additional examples should not be checked into the repository in this 
00033   directory, rather new directories should be created if the example is to
00034   be kept in the repository.
00035   It is not necessary to edit the Makefile.am in this directory unless
00036   additional files are added, or if libraries other than epetra, blas
00037   and lapack need to be linked to.  If any of the above changes are
00038   made (causing changes to the Makefile.am), it will be  necessary to bootstrap 
00039   and reconfigure.
00040   #########################################################################*/
00041 
00042 #include "Epetra_CrsGraph.h"
00043 #include "Epetra_Map.h"
00044 #ifdef EPETRA_MPI
00045 #include "Epetra_MpiComm.h"
00046 #include <mpi.h>
00047 #else
00048 #include "Epetra_SerialComm.h"
00049 #endif
00050 #include "../../test/epetra_test_err.h"
00051 #include "Epetra_Version.h"
00052 
00053 // Prototype
00054 int check(Epetra_CrsGraph& A, int NumMyRows1, int NumGlobalRows1, int NumMyNonzeros1,
00055           int NumGlobalNonzeros1, int* MyGlobalElements, bool verbose);
00056 
00057 int checkSharedOwnership(Epetra_Comm& Comm, bool verbose);
00058 int checkCopyAndAssignment(Epetra_Comm& Comm, bool verbose);
00059 
00060 int main(int argc, char *argv[]) {
00061   int ierr = 0;
00062   int i;
00063   int forierr = 0;
00064   int* Indices;
00065   bool debug = true;
00066 
00067 #ifdef EPETRA_MPI
00068 
00069   // Initialize MPI
00070 
00071   MPI_Init(&argc,&argv);
00072   int rank; // My process ID
00073 
00074   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00075   Epetra_MpiComm Comm( MPI_COMM_WORLD );
00076 
00077 #else
00078 
00079   int rank = 0;
00080   Epetra_SerialComm Comm;
00081 
00082 #endif
00083 
00084   bool verbose = false;
00085 
00086   // Check if we should print results to standard out
00087   if(argc > 1) {
00088     if(argv[1][0]=='-' && argv[1][1]=='v') {
00089       verbose = true;
00090     }
00091   }
00092 
00093   if(verbose && rank == 0)
00094   cout << Epetra_Version() << endl << endl;
00095 
00096   //char tmp;
00097   //if (rank==0) cout << "Press any key to continue..."<< endl;
00098   //if (rank==0) cin >> tmp;
00099   //Comm.Barrier();
00100 
00101   Comm.SetTracebackMode(0); // This should shut down any error traceback reporting
00102   int MyPID = Comm.MyPID();
00103   int NumProc = Comm.NumProc();
00104   if(verbose) cout << "Processor "<<MyPID<<" of "<< NumProc << " is alive." << endl;
00105 
00106   bool verbose1 = verbose;
00107 
00108   // Redefine verbose to only print on PE 0
00109   if(verbose && rank != 0) 
00110     verbose = false;
00111 
00112   int NumMyEquations = 5;
00113   int NumGlobalEquations = NumMyEquations*NumProc+EPETRA_MIN(NumProc,3);
00114   if(MyPID < 3) 
00115     NumMyEquations++;
00116 
00117   // Construct a Map that puts approximately the same Number of equations on each processor
00118 
00119   Epetra_Map& Map = *new Epetra_Map(NumGlobalEquations, NumMyEquations, 0, Comm);
00120   
00121   // Get update list and number of local equations from newly created Map
00122   int* MyGlobalElements = new int[Map.NumMyElements()];
00123   Map.MyGlobalElements(MyGlobalElements);
00124 
00125   // Create an integer vector NumNz that is used to build the Petra Matrix.
00126   // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation on this processor
00127 
00128   int* NumNz = new int[NumMyEquations];
00129 
00130   // We are building a tridiagonal matrix where each row has (-1 2 -1)
00131   // So we need 2 off-diagonal terms (except for the first and last equation)
00132 
00133   for(i=0; i<NumMyEquations; i++)
00134     if(MyGlobalElements[i]==0 || MyGlobalElements[i] == NumGlobalEquations-1)
00135       NumNz[i] = 1;
00136     else
00137       NumNz[i] = 2;
00138 
00139   // Create a Epetra_CrsGraph
00140 
00141   Epetra_CrsGraph& A = *new Epetra_CrsGraph(Copy, Map, NumNz);
00142   EPETRA_TEST_ERR(A.IndicesAreGlobal(),ierr);
00143   EPETRA_TEST_ERR(A.IndicesAreLocal(),ierr);
00144   
00145   // Add  rows one-at-a-time
00146   // Need some vectors to help
00147   // Off diagonal Values will always be -1
00148 
00149   Indices = new int[2];
00150   int NumEntries;
00151 
00152   forierr = 0;
00153   for(i = 0; i < NumMyEquations; i++) {
00154     if(MyGlobalElements[i] == 0) {
00155       Indices[0] = 1;
00156       NumEntries = 1;
00157     }
00158     else if(MyGlobalElements[i] == NumGlobalEquations-1) {
00159       Indices[0] = NumGlobalEquations-2;
00160       NumEntries = 1;
00161     }
00162     else {
00163       Indices[0] = MyGlobalElements[i]-1;
00164       Indices[1] = MyGlobalElements[i]+1;
00165       NumEntries = 2;
00166     }
00167     forierr += !(A.InsertGlobalIndices(MyGlobalElements[i], NumEntries, Indices)==0);
00168     forierr += !(A.InsertGlobalIndices(MyGlobalElements[i], 1, MyGlobalElements+i)>0); // Put in the diagonal entry (should cause realloc)
00169   }
00170   EPETRA_TEST_ERR(forierr,ierr);
00171   //A.PrintGraphData(cout);
00172   delete[] Indices;
00173   
00174   // Finish up
00175   EPETRA_TEST_ERR(!(A.IndicesAreGlobal()),ierr);
00176   EPETRA_TEST_ERR(!(A.FillComplete()==0),ierr);
00177   EPETRA_TEST_ERR(!(A.IndicesAreLocal()),ierr);
00178   EPETRA_TEST_ERR(A.StorageOptimized(),ierr);
00179 
00180   A.OptimizeStorage();
00181 
00182   EPETRA_TEST_ERR(!(A.StorageOptimized()),ierr);
00183   EPETRA_TEST_ERR(A.UpperTriangular(),ierr);
00184   EPETRA_TEST_ERR(A.LowerTriangular(),ierr);
00185 
00186   if(verbose) cout << "\n*****Testing variable entry constructor\n" << endl;
00187 
00188   int NumMyNonzeros = 3 * NumMyEquations;
00189   if(A.LRID(0) >= 0) 
00190     NumMyNonzeros--; // If I own first global row, then there is one less nonzero
00191   if(A.LRID(NumGlobalEquations-1) >= 0) 
00192     NumMyNonzeros--; // If I own last global row, then there is one less nonzero
00193 
00194   EPETRA_TEST_ERR(check(A, NumMyEquations, NumGlobalEquations, NumMyNonzeros, 3*NumGlobalEquations-2, 
00195                         MyGlobalElements, verbose),ierr);
00196   forierr = 0;
00197   for(i = 0; i < NumMyEquations; i++) 
00198     forierr += !(A.NumGlobalIndices(MyGlobalElements[i])==NumNz[i]+1);
00199   EPETRA_TEST_ERR(forierr,ierr);
00200   for(i = 0; i < NumMyEquations; i++) 
00201     forierr += !(A.NumMyIndices(i)==NumNz[i]+1);
00202   EPETRA_TEST_ERR(forierr,ierr);
00203 
00204   if(verbose) cout << "NumIndices function check OK" << endl;
00205 
00206   delete &A;
00207 
00208   if(debug) Comm.Barrier();
00209 
00210   if(verbose) cout << "\n*****Testing constant entry constructor\n" << endl;
00211 
00212   Epetra_CrsGraph& AA = *new Epetra_CrsGraph(Copy, Map, 5);
00213   
00214   if(debug) Comm.Barrier();
00215 
00216   for(i = 0; i < NumMyEquations; i++) 
00217     AA.InsertGlobalIndices(MyGlobalElements[i], 1, MyGlobalElements+i);
00218 
00219   // Note:  All processors will call the following Insert routines, but only the processor
00220   //        that owns it will actually do anything
00221 
00222   int One = 1;
00223   if(AA.MyGlobalRow(0)) {
00224     EPETRA_TEST_ERR(!(AA.InsertGlobalIndices(0, 0, &One)==0),ierr);
00225   }
00226   else 
00227     EPETRA_TEST_ERR(!(AA.InsertGlobalIndices(0, 1, &One)==-2),ierr);
00228   EPETRA_TEST_ERR(!(AA.FillComplete()==0),ierr);
00229   EPETRA_TEST_ERR(AA.StorageOptimized(),ierr);
00230   EPETRA_TEST_ERR(!(AA.UpperTriangular()),ierr);
00231   EPETRA_TEST_ERR(!(AA.LowerTriangular()),ierr);
00232 
00233   if(debug) Comm.Barrier();
00234   EPETRA_TEST_ERR(check(AA, NumMyEquations, NumGlobalEquations, NumMyEquations, NumGlobalEquations, 
00235                         MyGlobalElements, verbose),ierr);
00236 
00237   if(debug) Comm.Barrier();
00238 
00239   forierr = 0;
00240   for(i = 0; i < NumMyEquations; i++) 
00241     forierr += !(AA.NumGlobalIndices(MyGlobalElements[i])==1);
00242   EPETRA_TEST_ERR(forierr,ierr);
00243 
00244   if(verbose) cout << "NumIndices function check OK" << endl;
00245 
00246   if(debug) Comm.Barrier();
00247 
00248   if(verbose) cout << "\n*****Testing copy constructor\n" << endl;
00249 
00250   Epetra_CrsGraph& B = *new Epetra_CrsGraph(AA);
00251   delete &AA;
00252 
00253   EPETRA_TEST_ERR(check(B, NumMyEquations, NumGlobalEquations, NumMyEquations, NumGlobalEquations, 
00254                         MyGlobalElements, verbose),ierr);
00255 
00256   forierr = 0;
00257   for(i = 0; i < NumMyEquations; i++) 
00258     forierr += !(B.NumGlobalIndices(MyGlobalElements[i])==1);
00259   EPETRA_TEST_ERR(forierr,ierr);
00260 
00261   if(verbose) cout << "NumIndices function check OK" << endl;
00262 
00263   if(debug) Comm.Barrier();
00264 
00265   if(verbose) cout << "\n*****Testing post construction modifications\n" << endl;
00266 
00267   EPETRA_TEST_ERR(!(B.InsertGlobalIndices(0, 1, &One)==-2),ierr);
00268   delete &B;
00269 
00270   // Release all objects
00271   delete[] MyGlobalElements;
00272   delete[] NumNz;
00273   delete &Map;
00274       
00275 
00276   if (verbose1) {
00277     // Test ostream << operator (if verbose1)
00278     // Construct a Map that puts 2 equations on each PE
00279     
00280     int NumMyElements1 = 4;
00281     int NumMyEquations1 = NumMyElements1;
00282     int NumGlobalEquations1 = NumMyEquations1*NumProc;
00283 
00284     Epetra_Map& Map1 = *new Epetra_Map(NumGlobalEquations1, NumMyElements1, 1, Comm);
00285     
00286     // Get update list and number of local equations from newly created Map
00287     int* MyGlobalElements1 = new int[Map1.NumMyElements()];
00288     Map1.MyGlobalElements(MyGlobalElements1);
00289     
00290     // Create an integer vector NumNz that is used to build the Petra Matrix.
00291     // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation on this processor
00292     
00293     int* NumNz1 = new int[NumMyEquations1];
00294     
00295     // We are building a tridiagonal matrix where each row has (-1 2 -1)
00296     // So we need 2 off-diagonal terms (except for the first and last equation)
00297     
00298     for(i = 0; i < NumMyEquations1; i++)
00299       if(MyGlobalElements1[i]==1 || MyGlobalElements1[i] == NumGlobalEquations1)
00300         NumNz1[i] = 1;
00301       else
00302         NumNz1[i] = 2;
00303     
00304     // Create a Epetra_Graph using 1-based arithmetic
00305     
00306     Epetra_CrsGraph& A1 = *new Epetra_CrsGraph(Copy, Map1, NumNz1);
00307     
00308     // Add  rows one-at-a-time
00309     // Need some vectors to help
00310     // Off diagonal Values will always be -1
00311     
00312     
00313     int* Indices1 = new int[2];
00314     int NumEntries1;
00315 
00316     forierr = 0;
00317     for(i = 0; i < NumMyEquations1; i++) {
00318       if(MyGlobalElements1[i]==1) {
00319         Indices1[0] = 2;
00320         NumEntries1 = 1;
00321       }
00322       else if(MyGlobalElements1[i] == NumGlobalEquations1) {
00323       Indices1[0] = NumGlobalEquations1-1;
00324       NumEntries1 = 1;
00325       }
00326       else {
00327         Indices1[0] = MyGlobalElements1[i]-1;
00328         Indices1[1] = MyGlobalElements1[i]+1;
00329         NumEntries1 = 2;
00330       }
00331       forierr += !(A1.InsertGlobalIndices(MyGlobalElements1[i], NumEntries1, Indices1)==0);
00332       forierr += !(A1.InsertGlobalIndices(MyGlobalElements1[i], 1, MyGlobalElements1+i)>0); // Put in the diagonal entry
00333     }
00334     EPETRA_TEST_ERR(forierr,ierr);
00335     
00336     // Finish up
00337     EPETRA_TEST_ERR(!(A1.FillComplete()==0),ierr);
00338     
00339     if(verbose) cout << "Print out tridiagonal matrix, each part on each processor. Index base is one.\n" << endl;
00340     cout << A1 << endl;
00341     
00342   // Release all objects
00343   delete[] NumNz1;
00344   delete[] Indices1;
00345   delete[] MyGlobalElements1;
00346 
00347   delete &A1;
00348   delete &Map1;
00349   }
00350 
00351   // Test copy constructor, op=, and reference-counting
00352   int tempierr = 0;
00353   if(verbose) cout << "\n*****Checking cpy ctr, op=, and reference counting." << endl;
00354   tempierr = checkCopyAndAssignment(Comm, verbose);
00355   EPETRA_TEST_ERR(tempierr, ierr);
00356   if(verbose && (tempierr == 0)) cout << "Checked OK." << endl;
00357 
00358   // Test shared-ownership code (not implemented yet)
00359   tempierr = 0;
00360   if(verbose) cout << "\n*****Checking shared-ownership tests." << endl;
00361   tempierr = checkSharedOwnership(Comm, verbose);
00362   EPETRA_TEST_ERR(tempierr, ierr);
00363   if(verbose && (tempierr == 0)) cout << "Checked OK." << endl;
00364 
00365       
00366 #ifdef EPETRA_MPI
00367   MPI_Finalize() ;
00368 #endif
00369 /* end main */
00370   return(ierr);
00371 }
00372 
00373 //==============================================================================
00374 int checkSharedOwnership(Epetra_Comm& Comm, bool verbose) {
00375   // check to make sure each function returns 1 when it should
00376   // check to make sure each function doesn't return 1 when it shouldn't
00377   int ierr = 0;
00378 
00379   // initialize Map
00380   const int NumMyElements = 10;
00381   const int IndexBase = 0;
00382   Epetra_Map Map1(-1, NumMyElements, IndexBase, Comm);
00383   // initialize Graphs
00384   const int NumIndicesPerRow = 5;
00385   Epetra_CrsGraph SoleOwner(Copy, Map1, Map1, NumIndicesPerRow);
00386   Epetra_CrsGraph SharedOrig(Copy, Map1, Map1, NumIndicesPerRow);
00387   Epetra_CrsGraph SharedOwner(SharedOrig);
00388   // arrays used by Insert & Remove
00389   Epetra_IntSerialDenseVector array1(2);
00390   array1[0] = NumIndicesPerRow / 2;
00391   array1[1] = array1[0] + 1;
00392   Epetra_IntSerialDenseVector array2(NumIndicesPerRow);
00393   for(int i = 0; i < NumIndicesPerRow; i++)
00394     array2[i] = i;
00395   // output variables (declaring them here lets us comment out indiv. tests)
00396   int soleOutput, sharedOutput;
00397 
00398   // InsertMyIndices
00399   if(verbose) cout << "InsertMyIndices..." << endl;
00400   soleOutput = SoleOwner.InsertMyIndices(0, 2, array1.Values());
00401   sharedOutput = SharedOwner.InsertMyIndices(0, 2, array1.Values());
00402   EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00403   EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00404   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00405 
00406   // SortIndices
00407   //if(verbose) cout << "SortIndices..." << endl;
00408   //soleOutput = SoleOwner.SortIndices();
00409   //sharedOutput = SharedOwner.SortIndices();
00410   //EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00411   //EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00412   //if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00413 
00414   // RemoveRedundantIndices
00415   //if(verbose) cout << "RemoveRedundantIndices..." << endl;
00416   //SoleOwner.InsertGlobalIndices(0, 1, array1.Values());
00417   //SharedOwner.InsertGlobalIndices(0, 1, array1.Values());
00418   //soleOutput = SoleOwner.RemoveRedundantIndices();
00419   //sharedOutput = SharedOwner.RemoveRedundantIndices();
00420   //EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00421   //EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00422   //if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00423 
00424   // FillComplete (#1)
00425   if(verbose) cout << "FillComplete..." << endl;
00426   soleOutput = SoleOwner.FillComplete();
00427   sharedOutput = SharedOwner.FillComplete();
00428   EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00429   EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00430   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00431 
00432   // OptimizeStorage
00433   if(verbose) cout << "OptimizeStorage..." << endl;
00434   soleOutput = SoleOwner.OptimizeStorage();
00435   sharedOutput = SharedOwner.OptimizeStorage();
00436   EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00437   EPETRA_TEST_ERR(!(sharedOutput == 0), ierr);
00438   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00439 
00440   // RemoveMyIndices (#1)
00441   if(verbose) cout << "RemoveMyIndices..." << endl;
00442   soleOutput = SoleOwner.RemoveMyIndices(0, 1, &array1[1]);
00443   sharedOutput = SharedOwner.RemoveMyIndices(0, 1, &array1[1]);
00444   EPETRA_TEST_ERR(!(soleOutput == -1), ierr);
00445   EPETRA_TEST_ERR(!(sharedOutput == -1), ierr);
00446   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00447 
00448   // RemoveMyIndices (#2)
00449   if(verbose) cout << "RemoveMyIndices(#2)..." << endl;
00450   soleOutput = SoleOwner.RemoveMyIndices(0);
00451   sharedOutput = SharedOwner.RemoveMyIndices(0);
00452   EPETRA_TEST_ERR(!(soleOutput == -1), ierr);
00453   EPETRA_TEST_ERR(!(sharedOutput == -1), ierr);
00454   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00455 
00456   // FillComplete (#2)
00457   if(verbose) cout << "FillComplete(#2)..." << endl;
00458   soleOutput = SoleOwner.FillComplete(SoleOwner.DomainMap(), SoleOwner.RangeMap());
00459   sharedOutput = SharedOwner.FillComplete(SharedOwner.DomainMap(), SharedOwner.RangeMap());
00460   EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00461   EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00462   if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00463 
00464   {
00465     // make new Graphs so that we can insert Global instead of Local
00466     // inside of new scope so that we can use same names
00467     Epetra_CrsGraph SoleOwner(Copy, Map1, NumIndicesPerRow);
00468     Epetra_CrsGraph SharedOrig(Copy, Map1, NumIndicesPerRow);
00469     Epetra_CrsGraph SharedOwner(SharedOrig);
00470     
00471     int GlobalRow = SoleOwner.GRID(0);
00472 
00473     // InsertGlobalIndices
00474     if(verbose) cout << "InsertGlobalIndices..." << endl;
00475     soleOutput = SoleOwner.InsertGlobalIndices(GlobalRow, 2, array2.Values());
00476     sharedOutput = SharedOwner.InsertGlobalIndices(GlobalRow, 2, array2.Values());
00477     EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00478     EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00479     if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00480     
00481     // RemoveGlobalIndices (#1)
00482     if(verbose) cout << "RemoveGlobalIndices..." << endl;
00483     soleOutput = SoleOwner.RemoveGlobalIndices(GlobalRow, 1, &array2[1]);
00484     sharedOutput = SharedOwner.RemoveGlobalIndices(GlobalRow, 1, &array2[1]);
00485     EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00486     EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00487     if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00488     
00489     // RemoveGlobalIndices (#2)
00490     if(verbose) cout << "RemoveGlobalIndices(#2)..." << endl;
00491     soleOutput = SoleOwner.RemoveGlobalIndices(GlobalRow);
00492     sharedOutput = SharedOwner.RemoveGlobalIndices(GlobalRow);
00493     EPETRA_TEST_ERR(!(soleOutput == 0), ierr);
00494     EPETRA_TEST_ERR(!(sharedOutput == 1), ierr);
00495     if(verbose && ierr > 0) cout << "soleOutput = " << soleOutput << " sharedOutput = " << sharedOutput << endl;
00496     }
00497 
00498 
00499   // *PROT* InsertIndices
00500   // *PROT* MakeIndicesLocal
00501 
00502   return(ierr);
00503 
00504 }
00505 
00506 //==============================================================================
00507 int checkCopyAndAssignment(Epetra_Comm& Comm, bool verbose) {
00508   int ierr = 0;
00509   // check to make sure that copy ctr and op= are working correctly
00510   // (making level 1 deep copies)
00511 
00512   // create initial Map and Graph
00513   const int NumIndicesPerRow = 10;
00514   const int NumGlobalElements = 50;
00515   const int IndexBase = 0;
00516   Epetra_Map Map1(NumGlobalElements, IndexBase, Comm);
00517   Epetra_CrsGraph Graph1(Copy, Map1, NumIndicesPerRow);
00518   int g1count = Graph1.ReferenceCount();
00519   const Epetra_CrsGraphData* g1addr = Graph1.DataPtr();
00520   EPETRA_TEST_ERR(!(g1count == 1), ierr);
00521   if(verbose) cout << "Graph1 created (def ctr). data addr = " << g1addr << " ref. count = " << g1count << endl;
00522 
00523   //test copy constructor
00524   {
00525     Epetra_CrsGraph Graph2(Graph1);
00526     int g2count = Graph2.ReferenceCount();
00527     const Epetra_CrsGraphData* g2addr = Graph2.DataPtr();
00528     EPETRA_TEST_ERR(!(g2count == g1count+1), ierr);
00529     EPETRA_TEST_ERR(!(g2addr == g1addr), ierr);
00530     if(verbose) cout << "Graph2 created (cpy ctr). data addr = " << g2addr << " ref. count = " << g2count << endl;
00531   }
00532   int g1newcount = Graph1.ReferenceCount();
00533   const Epetra_CrsGraphData* g1newaddr = Graph1.DataPtr();
00534   EPETRA_TEST_ERR(!(g1newcount == g1count), ierr);
00535   EPETRA_TEST_ERR(!(g1newaddr = g1addr), ierr);
00536   if(verbose) cout << "Graph2 destroyed. Graph1 data addr = " << g1newaddr << " ref. count = " << g1newcount << endl;
00537 
00538   //test op=
00539   Epetra_CrsGraph Graph3(Copy, Map1, NumIndicesPerRow+1);
00540   int g3count = Graph3.ReferenceCount();
00541   const Epetra_CrsGraphData* g3addr = Graph3.DataPtr();
00542   EPETRA_TEST_ERR(!(g3addr != g1addr), ierr);
00543   if(verbose) cout << "Graph3 created (op= before). data addr = " << g3addr << " ref. count = " << g3count << endl;
00544   Graph3 = Graph1;
00545   g3count = Graph3.ReferenceCount();
00546   g3addr = Graph3.DataPtr();
00547   EPETRA_TEST_ERR(!(g3count == g1count+1), ierr);
00548   EPETRA_TEST_ERR(!(g3addr == g1addr), ierr);
00549   if(verbose) cout << "Graph3 set equal to Graph1 (op= after). data addr = " << g3addr << " ref. count = " << g3count << endl;
00550 
00551   return(ierr);
00552 }
00553 
00554 //==============================================================================
00555 int check(Epetra_CrsGraph& A, int NumMyRows1, int NumGlobalRows1, int NumMyNonzeros1,
00556     int NumGlobalNonzeros1, int* MyGlobalElements, bool verbose)
00557 {  
00558   (void)MyGlobalElements;
00559   int ierr = 0;
00560   int i;
00561   int j;
00562   int forierr = 0;
00563   int NumGlobalIndices;
00564   int NumMyIndices;
00565   int* MyViewIndices;
00566   int MaxNumIndices = A.MaxNumIndices();
00567   int* MyCopyIndices = new int[MaxNumIndices];
00568   int* GlobalCopyIndices = new int[MaxNumIndices];
00569 
00570   // Test query functions
00571 
00572   int NumMyRows = A.NumMyRows();
00573   if(verbose) cout << "Number of local Rows = " << NumMyRows << endl;
00574 
00575   EPETRA_TEST_ERR(!(NumMyRows==NumMyRows1),ierr);
00576 
00577   int NumMyNonzeros = A.NumMyNonzeros();
00578   if(verbose) cout << "Number of local Nonzero entries = " << NumMyNonzeros << endl;
00579 
00580   EPETRA_TEST_ERR(!(NumMyNonzeros==NumMyNonzeros1),ierr);
00581 
00582   int NumGlobalRows = A.NumGlobalRows();
00583   if(verbose) cout << "Number of global Rows = " << NumGlobalRows << endl;
00584 
00585   EPETRA_TEST_ERR(!(NumGlobalRows==NumGlobalRows1),ierr);
00586 
00587   int NumGlobalNonzeros = A.NumGlobalNonzeros();
00588   if(verbose) cout << "Number of global Nonzero entries = " << NumGlobalNonzeros << endl;
00589 
00590   EPETRA_TEST_ERR(!(NumGlobalNonzeros==NumGlobalNonzeros1),ierr);
00591 
00592   // GlobalRowView should be illegal (since we have local indices)
00593 
00594   EPETRA_TEST_ERR(!(A.ExtractGlobalRowView(A.RowMap().MaxMyGID(), NumGlobalIndices, GlobalCopyIndices)==-2),ierr);
00595 
00596   // Other binary tests
00597 
00598   EPETRA_TEST_ERR(A.NoDiagonal(),ierr);
00599   EPETRA_TEST_ERR(!(A.Filled()),ierr);
00600   EPETRA_TEST_ERR(!(A.MyGRID(A.RowMap().MaxMyGID())),ierr);
00601   EPETRA_TEST_ERR(!(A.MyGRID(A.RowMap().MinMyGID())),ierr);
00602   EPETRA_TEST_ERR(A.MyGRID(1+A.RowMap().MaxMyGID()),ierr);
00603   EPETRA_TEST_ERR(A.MyGRID(-1+A.RowMap().MinMyGID()),ierr);
00604   EPETRA_TEST_ERR(!(A.MyLRID(0)),ierr);
00605   EPETRA_TEST_ERR(!(A.MyLRID(NumMyRows-1)),ierr);
00606   EPETRA_TEST_ERR(A.MyLRID(-1),ierr);
00607   EPETRA_TEST_ERR(A.MyLRID(NumMyRows),ierr);
00608     
00609   forierr = 0;
00610   for(i = 0; i < NumMyRows; i++) {
00611     int Row = A.GRID(i);
00612     A.ExtractGlobalRowCopy(Row, MaxNumIndices, NumGlobalIndices, GlobalCopyIndices);
00613     A.ExtractMyRowView(i, NumMyIndices, MyViewIndices);
00614     forierr += !(NumGlobalIndices==NumMyIndices);
00615     for(j = 1; j < NumMyIndices; j++) EPETRA_TEST_ERR(!(MyViewIndices[j-1]<MyViewIndices[j]),ierr);
00616     for(j = 0; j < NumGlobalIndices; j++) {
00617       forierr += !(GlobalCopyIndices[j]==A.GCID(MyViewIndices[j]));
00618       forierr += !(A.LCID(GlobalCopyIndices[j])==MyViewIndices[j]);
00619     }
00620   }
00621   EPETRA_TEST_ERR(forierr,ierr);
00622   forierr = 0;
00623   for(i = 0; i < NumMyRows; i++) {
00624     int Row = A.GRID(i);
00625     A.ExtractGlobalRowCopy(Row, MaxNumIndices, NumGlobalIndices, GlobalCopyIndices);
00626     A.ExtractMyRowCopy(i, MaxNumIndices, NumMyIndices, MyCopyIndices);
00627     forierr += !(NumGlobalIndices==NumMyIndices);
00628     for(j = 1; j < NumMyIndices; j++) 
00629       EPETRA_TEST_ERR(!(MyCopyIndices[j-1]<MyCopyIndices[j]),ierr);
00630     for(j = 0; j < NumGlobalIndices; j++) {
00631       forierr += !(GlobalCopyIndices[j]==A.GCID(MyCopyIndices[j]));
00632       forierr += !(A.LCID(GlobalCopyIndices[j])==MyCopyIndices[j]);
00633     }
00634 
00635   }
00636   EPETRA_TEST_ERR(forierr,ierr);
00637 
00638   delete[] MyCopyIndices;
00639   delete[] GlobalCopyIndices;
00640 
00641   if(verbose) cout << "Rows sorted check OK" << endl;
00642 
00643   return(ierr);
00644 }

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