EpetraExt Package Browser (Single Doxygen Collection) Development
HDF5_MATLAB.cpp
Go to the documentation of this file.
00001 #include "EpetraExt_ConfigDefs.h"
00002 #ifdef HAVE_MPI
00003 #include "mpi.h"
00004 #include "Epetra_MpiComm.h"
00005 #else
00006 #include "Epetra_SerialComm.h"
00007 #endif
00008 #include <vector>
00009 #include "Epetra_Map.h"
00010 #include "Epetra_MultiVector.h"
00011 #include "Epetra_CrsMatrix.h"
00012 #include "EpetraExt_HDF5.h"
00013 #include "EpetraExt_Utils.h"
00014 #include "EpetraExt_Exception.h"
00015 
00016 // Read a matrix from file "matlab.h5". The matrix has been generated
00017 // as sparse matrix in MATLAB, then save to file using MATLAB's commands;
00018 // see the Doxygen documentation of class EpetraExt::HDF5 for the MATLAB
00019 // commands used.
00020 //
00021 // This example can be run with any number of processors.
00022 //
00023 // \author Marzio Sala, D-INFK/ETHZ.
00024 //
00025 // \date Last modified on 09-Mar-06.
00026 
00027 int main (int argc, char **argv)
00028 {
00029 #ifdef HAVE_MPI
00030   MPI_Init(&argc, &argv);
00031   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00032 #else
00033   Epetra_SerialComm Comm;
00034 #endif
00035 
00036   try 
00037   {
00038     // This is the HDF5 file manager
00039     EpetraExt::HDF5 HDF5(Comm);
00040 
00041     // creates a new file. To open an existing file, use Open("myfile.h5")
00042     // This file contains:
00043     // - a sparse (diagonal) matrix, whose group name is "speye"
00044     // - a multivector, whose group name is "x"
00045     // - a map for 2-processor run, whose group name is "map-2"
00046 
00047     HDF5.Open("matlab.h5");
00048 
00049     if (Comm.MyPID() == 0)
00050       cout << endl;
00051       cout << "*) Reading Epetra_CrsMatrix from HDF5 file matlab.h5..." << endl;
00052       cout << endl;
00053 
00054     // first query for matrix properties:
00055     int NumGlobalRows, NumGlobalCols, NumGlobalNonzeros;
00056     int NumGlobalDiagonals, MaxNumEntries;
00057     double NormOne, NormInf;
00058 
00059     HDF5.ReadCrsMatrixProperties("speye", NumGlobalRows, NumGlobalCols,
00060                                  NumGlobalNonzeros, NumGlobalDiagonals, 
00061                                  MaxNumEntries, NormOne, NormInf);
00062 
00063     if (Comm.MyPID() == 0)
00064     {
00065       cout << "Matrix information as given by ReadCrsMatrixProperties()";
00066       cout << endl << endl;
00067       cout << "NumGlobalRows = " << NumGlobalRows << endl;
00068       cout << "NumGlobalCols = " << NumGlobalCols << endl;
00069       cout << "NumGlobalNonzeros = " << NumGlobalNonzeros << endl;
00070       cout << "NumGlobalDiagonals = " << NumGlobalDiagonals << endl;
00071       cout << "MaxNumEntries = " << MaxNumEntries << endl;
00072       cout << "NormOne = " << NormOne << endl;
00073       cout << "NormInf = " << NormInf << endl;
00074     }
00075 
00076     // the reading the actual matrix, with a linear map, since no map 
00077     // has been specified.
00078     Epetra_CrsMatrix* Matrix = 0;
00079     HDF5.Read("speye", Matrix);
00080 
00081     cout << *Matrix;
00082 
00083     if (Comm.MyPID() == 0)
00084     {
00085       cout << endl;
00086       cout << "*) Reading Epetra_MultiVector from HDF5 file matlab.h5..." << endl;
00087       cout << endl;
00088     }
00089 
00090     Epetra_MultiVector* x;
00091     HDF5.Read("x", x);
00092     cout << *x;
00093 
00094     if (Comm.NumProc() == 2)
00095     {
00096       if (Comm.MyPID() == 0)
00097       {
00098         cout << endl;
00099         cout << "*) Reading Epetra_Map from HDF5 file matlab.h5..." << endl;
00100         cout << endl;
00101       }
00102 
00103       Epetra_Map* Map;
00104       HDF5.Read("map-2", Map);
00105       cout << *Map;
00106     }
00107 
00108     // We finally close the file. Better to close it before calling
00109     // MPI_Finalize() to avoid MPI-related errors, since Close() might call MPI
00110     // functions.
00111     HDF5.Close();
00112 
00113     // delete memory
00114     if (Matrix) delete Matrix;
00115   }
00116   catch(EpetraExt::Exception& rhs) 
00117   {
00118     rhs.Print();
00119   }
00120   catch (...) 
00121   {
00122     cerr << "Caught generic exception" << endl;
00123   }
00124 
00125 #ifdef HAVE_MPI
00126   MPI_Finalize();
00127 #endif
00128 
00129   return(EXIT_SUCCESS);
00130 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines