EpetraExt Package Browser (Single Doxygen Collection) Development
HDF5_MATLAB.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 "EpetraExt_ConfigDefs.h"
00045 #ifdef HAVE_MPI
00046 #include "mpi.h"
00047 #include "Epetra_MpiComm.h"
00048 #else
00049 #include "Epetra_SerialComm.h"
00050 #endif
00051 #include <vector>
00052 #include "Epetra_Map.h"
00053 #include "Epetra_MultiVector.h"
00054 #include "Epetra_CrsMatrix.h"
00055 #include "EpetraExt_HDF5.h"
00056 #include "EpetraExt_Utils.h"
00057 #include "EpetraExt_Exception.h"
00058 
00059 // Read a matrix from file "matlab.h5". The matrix has been generated
00060 // as sparse matrix in MATLAB, then save to file using MATLAB's commands;
00061 // see the Doxygen documentation of class EpetraExt::HDF5 for the MATLAB
00062 // commands used.
00063 //
00064 // This example can be run with any number of processors.
00065 //
00066 // \author Marzio Sala, D-INFK/ETHZ.
00067 //
00068 // \date Last modified on 09-Mar-06.
00069 
00070 int main (int argc, char **argv)
00071 {
00072 #ifdef HAVE_MPI
00073   MPI_Init(&argc, &argv);
00074   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00075 #else
00076   Epetra_SerialComm Comm;
00077 #endif
00078 
00079   try 
00080   {
00081     // This is the HDF5 file manager
00082     EpetraExt::HDF5 HDF5(Comm);
00083 
00084     // creates a new file. To open an existing file, use Open("myfile.h5")
00085     // This file contains:
00086     // - a sparse (diagonal) matrix, whose group name is "speye"
00087     // - a multivector, whose group name is "x"
00088     // - a map for 2-processor run, whose group name is "map-2"
00089 
00090     HDF5.Open("matlab.h5");
00091 
00092     if (Comm.MyPID() == 0)
00093       cout << endl;
00094       cout << "*) Reading Epetra_CrsMatrix from HDF5 file matlab.h5..." << endl;
00095       cout << endl;
00096 
00097     // first query for matrix properties:
00098     int NumGlobalRows, NumGlobalCols, NumGlobalNonzeros;
00099     int NumGlobalDiagonals, MaxNumEntries;
00100     double NormOne, NormInf;
00101 
00102     HDF5.ReadCrsMatrixProperties("speye", NumGlobalRows, NumGlobalCols,
00103                                  NumGlobalNonzeros, NumGlobalDiagonals, 
00104                                  MaxNumEntries, NormOne, NormInf);
00105 
00106     if (Comm.MyPID() == 0)
00107     {
00108       cout << "Matrix information as given by ReadCrsMatrixProperties()";
00109       cout << endl << endl;
00110       cout << "NumGlobalRows = " << NumGlobalRows << endl;
00111       cout << "NumGlobalCols = " << NumGlobalCols << endl;
00112       cout << "NumGlobalNonzeros = " << NumGlobalNonzeros << endl;
00113       cout << "NumGlobalDiagonals = " << NumGlobalDiagonals << endl;
00114       cout << "MaxNumEntries = " << MaxNumEntries << endl;
00115       cout << "NormOne = " << NormOne << endl;
00116       cout << "NormInf = " << NormInf << endl;
00117     }
00118 
00119     // the reading the actual matrix, with a linear map, since no map 
00120     // has been specified.
00121     Epetra_CrsMatrix* Matrix = 0;
00122     HDF5.Read("speye", Matrix);
00123 
00124     cout << *Matrix;
00125 
00126     if (Comm.MyPID() == 0)
00127     {
00128       cout << endl;
00129       cout << "*) Reading Epetra_MultiVector from HDF5 file matlab.h5..." << endl;
00130       cout << endl;
00131     }
00132 
00133     Epetra_MultiVector* x;
00134     HDF5.Read("x", x);
00135     cout << *x;
00136 
00137     if (Comm.NumProc() == 2)
00138     {
00139       if (Comm.MyPID() == 0)
00140       {
00141         cout << endl;
00142         cout << "*) Reading Epetra_Map from HDF5 file matlab.h5..." << endl;
00143         cout << endl;
00144       }
00145 
00146       Epetra_Map* Map;
00147       HDF5.Read("map-2", Map);
00148       cout << *Map;
00149     }
00150 
00151     // We finally close the file. Better to close it before calling
00152     // MPI_Finalize() to avoid MPI-related errors, since Close() might call MPI
00153     // functions.
00154     HDF5.Close();
00155 
00156     // delete memory
00157     if (Matrix) delete Matrix;
00158   }
00159   catch(EpetraExt::Exception& rhs) 
00160   {
00161     rhs.Print();
00162   }
00163   catch (...) 
00164   {
00165     cerr << "Caught generic exception" << endl;
00166   }
00167 
00168 #ifdef HAVE_MPI
00169   MPI_Finalize();
00170 #endif
00171 
00172   return(EXIT_SUCCESS);
00173 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines