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