EpetraExt Package Browser (Single Doxygen Collection) Development
MatrixMarket_IO.cpp
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ***********************************************************************
00004 //
00005 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00006 //                 Copyright (2011) Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //@HEADER
00042 */
00043 
00044 #include "Epetra_ConfigDefs.h"
00045 #include "EpetraExt_Version.h"
00046 
00047 #ifdef EPETRA_MPI
00048 #include "mpi.h"
00049 #include "Epetra_MpiComm.h"
00050 #else
00051 #include "Epetra_SerialComm.h"
00052 #endif
00053 
00054 #include "Trilinos_Util.h"
00055 #include "Epetra_Comm.h"
00056 #include "Epetra_Map.h"
00057 #include "Epetra_Time.h"
00058 #include "Epetra_BlockMap.h"
00059 #include "Epetra_MultiVector.h"
00060 #include "Epetra_Vector.h"
00061 #include "Epetra_Export.h"
00062 
00063 #include "Epetra_VbrMatrix.h"
00064 #include "Epetra_CrsMatrix.h"
00065 
00066 #include "EpetraExt_RowMatrixOut.h"
00067 
00068 int main(int argc, char *argv[]) {
00069 
00070 #ifdef EPETRA_MPI
00071   MPI_Init(&argc,&argv);
00072   Epetra_MpiComm Comm (MPI_COMM_WORLD);
00073 #else
00074   Epetra_SerialComm Comm;
00075 #endif
00076 
00077   int MyPID = Comm.MyPID();
00078 
00079   bool verbose = true; 
00080   if (MyPID==0) verbose = true;
00081 
00082   if (verbose)
00083     cout << EpetraExt::EpetraExt_Version() << endl << endl;
00084                                                                                 
00085   cout << Comm << endl;
00086 
00087   if(argc < 2 && verbose) {
00088     cerr << "Usage: " << argv[0] 
00089    << " HB_filename" << endl;
00090     return(1);
00091 
00092   }
00093 
00094   // Uncomment the next three lines to debug in mpi mode
00095   //int tmp;
00096   //if (MyPID==0) cin >> tmp;
00097   //Comm.Barrier();
00098 
00099   Epetra_Map * readMap;
00100   Epetra_CrsMatrix * readA; 
00101   Epetra_Vector * readx; 
00102   Epetra_Vector * readb;
00103   Epetra_Vector * readxexact;
00104    
00105   // Call routine to read in HB problem
00106   Trilinos_Util_ReadHb2Epetra(argv[1], Comm, readMap, readA, readx, readb, readxexact);
00107 
00108   // Create uniform distributed map
00109   Epetra_Map map(readMap->NumGlobalElements(), 0, Comm);
00110 
00111   // Create Exporter to distribute read-in matrix and vectors
00112 
00113   Epetra_Export exporter(*readMap, map);
00114   Epetra_CrsMatrix A(Copy, map, 0);
00115   Epetra_Vector x(map);
00116   Epetra_Vector b(map);
00117   Epetra_Vector xexact(map);
00118 
00119   Epetra_Time FillTimer(Comm);
00120   x.Export(*readx, exporter, Add);
00121   b.Export(*readb, exporter, Add);
00122   xexact.Export(*readxexact, exporter, Add);
00123   Comm.Barrier();
00124   double vectorRedistributeTime = FillTimer.ElapsedTime();
00125   A.Export(*readA, exporter, Add);
00126   Comm.Barrier();
00127   double matrixRedistributeTime = FillTimer.ElapsedTime() - vectorRedistributeTime;
00128   assert(A.FillComplete()==0);    
00129   Comm.Barrier();
00130   double fillCompleteTime = FillTimer.ElapsedTime() - matrixRedistributeTime;
00131   if (Comm.MyPID()==0)  {
00132     cout << "\n\n****************************************************" << endl;
00133     cout << "\n Vector redistribute  time (sec) = " << vectorRedistributeTime<< endl;
00134     cout << "    Matrix redistribute time (sec) = " << matrixRedistributeTime << endl;
00135     cout << "    Transform to Local  time (sec) = " << fillCompleteTime << endl<< endl;
00136   }
00137   Epetra_Vector tmp1(*readMap);
00138   Epetra_Vector tmp2(map);
00139   readA->Multiply(false, *readxexact, tmp1);
00140 
00141   A.Multiply(false, xexact, tmp2);
00142   double residual;
00143   tmp1.Norm2(&residual);
00144   if (verbose) cout << "Norm of Ax from file            = " << residual << endl;
00145   tmp2.Norm2(&residual);
00146   if (verbose) cout << "Norm of Ax after redistribution = " << residual << endl << endl << endl;
00147 
00148   //cout << "A from file = " << *readA << endl << endl << endl;
00149 
00150   //cout << "A after dist = " << A << endl << endl << endl;
00151 
00152   delete readA;
00153   delete readx;
00154   delete readb;
00155   delete readxexact;
00156   delete readMap;
00157 
00158   Comm.Barrier();
00159 
00160   EpetraExt::RowMatrixToMatrixMarketFile("test.mm", A, "test matrix", "This is a test matrix");
00161                
00162 #ifdef EPETRA_MPI
00163   MPI_Finalize() ;
00164 #endif
00165 
00166 return 0 ;
00167 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines