EpetraExt Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HDF5_MATLAB.cpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // EpetraExt: Epetra Extended - Linear Algebra Services Package
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 #include "EpetraExt_ConfigDefs.h"
45 #ifdef HAVE_MPI
46 #include "mpi.h"
47 #include "Epetra_MpiComm.h"
48 #else
49 #include "Epetra_SerialComm.h"
50 #endif
51 #include <vector>
52 #include "Epetra_Map.h"
53 #include "Epetra_MultiVector.h"
54 #include "Epetra_CrsMatrix.h"
55 #include "EpetraExt_HDF5.h"
56 #include "EpetraExt_Utils.h"
57 #include "EpetraExt_Exception.h"
58 
59 // Read a matrix from file "matlab.h5". The matrix has been generated
60 // as sparse matrix in MATLAB, then save to file using MATLAB's commands;
61 // see the Doxygen documentation of class EpetraExt::HDF5 for the MATLAB
62 // commands used.
63 //
64 // This example can be run with any number of processors.
65 //
66 // \author Marzio Sala, D-INFK/ETHZ.
67 //
68 // \date Last modified on 09-Mar-06.
69 
70 int main (int argc, char **argv)
71 {
72 #ifdef HAVE_MPI
73  MPI_Init(&argc, &argv);
74  Epetra_MpiComm Comm(MPI_COMM_WORLD);
75 #else
76  Epetra_SerialComm Comm;
77 #endif
78 
79  try
80  {
81  // This is the HDF5 file manager
82  EpetraExt::HDF5 HDF5(Comm);
83 
84  // creates a new file. To open an existing file, use Open("myfile.h5")
85  // This file contains:
86  // - a sparse (diagonal) matrix, whose group name is "speye"
87  // - a multivector, whose group name is "x"
88  // - a map for 2-processor run, whose group name is "map-2"
89 
90  HDF5.Open("matlab.h5");
91 
92  if (Comm.MyPID() == 0)
93  cout << endl;
94  cout << "*) Reading Epetra_CrsMatrix from HDF5 file matlab.h5..." << endl;
95  cout << endl;
96 
97  // first query for matrix properties:
98  int NumGlobalRows, NumGlobalCols, NumGlobalNonzeros;
99  int NumGlobalDiagonals, MaxNumEntries;
100  double NormOne, NormInf;
101 
102  HDF5.ReadCrsMatrixProperties("speye", NumGlobalRows, NumGlobalCols,
103  NumGlobalNonzeros, NumGlobalDiagonals,
104  MaxNumEntries, NormOne, NormInf);
105 
106  if (Comm.MyPID() == 0)
107  {
108  cout << "Matrix information as given by ReadCrsMatrixProperties()";
109  cout << endl << endl;
110  cout << "NumGlobalRows = " << NumGlobalRows << endl;
111  cout << "NumGlobalCols = " << NumGlobalCols << endl;
112  cout << "NumGlobalNonzeros = " << NumGlobalNonzeros << endl;
113  cout << "NumGlobalDiagonals = " << NumGlobalDiagonals << endl;
114  cout << "MaxNumEntries = " << MaxNumEntries << endl;
115  cout << "NormOne = " << NormOne << endl;
116  cout << "NormInf = " << NormInf << endl;
117  }
118 
119  // the reading the actual matrix, with a linear map, since no map
120  // has been specified.
121  Epetra_CrsMatrix* Matrix = 0;
122  HDF5.Read("speye", Matrix);
123 
124  cout << *Matrix;
125 
126  if (Comm.MyPID() == 0)
127  {
128  cout << endl;
129  cout << "*) Reading Epetra_MultiVector from HDF5 file matlab.h5..." << endl;
130  cout << endl;
131  }
132 
134  HDF5.Read("x", x);
135  cout << *x;
136 
137  if (Comm.NumProc() == 2)
138  {
139  if (Comm.MyPID() == 0)
140  {
141  cout << endl;
142  cout << "*) Reading Epetra_Map from HDF5 file matlab.h5..." << endl;
143  cout << endl;
144  }
145 
146  Epetra_Map* Map;
147  HDF5.Read("map-2", Map);
148  cout << *Map;
149  }
150 
151  // We finally close the file. Better to close it before calling
152  // MPI_Finalize() to avoid MPI-related errors, since Close() might call MPI
153  // functions.
154  HDF5.Close();
155 
156  // delete memory
157  if (Matrix) delete Matrix;
158  }
159  catch(EpetraExt::Exception& rhs)
160  {
161  rhs.Print();
162  }
163  catch (...)
164  {
165  cerr << "Caught generic exception" << endl;
166  }
167 
168 #ifdef HAVE_MPI
169  MPI_Finalize();
170 #endif
171 
172  return(EXIT_SUCCESS);
173 }
void Read(const std::string &GroupName, const std::string &DataSetName, int &data)
Read an integer from group /GroupName/DataSetName.
int MyPID() const
int main(int argc, char **argv)
Definition: HDF5_IO.cpp:67
int NumProc() const
void Open(const std::string FileName, int AccessType=H5F_ACC_RDWR)
Open specified file with given access type.
class HDF5: A class for storing Epetra objects in parallel binary files
void Close()
Close the file.
void ReadCrsMatrixProperties(const std::string &GroupName, int &NumGlobalRows, int &NumGlobalCols, int &NumNonzeros, int &NumGlobalDiagonals, int &MaxNumEntries, double &NormOne, double &NormInf)
Read basic properties of specified Epetra_CrsMatrix.