EpetraExt Package Browser (Single Doxygen Collection) Development
HDF5_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 "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_CrsMatrix.h"
00054 #include "Epetra_MultiVector.h"
00055 #include "EpetraExt_HDF5.h"
00056 #include "EpetraExt_Utils.h"
00057 #include "EpetraExt_Exception.h"
00058 #include "Teuchos_ParameterList.hpp"
00059 
00060 // Showing the usage of HDF5 I/O.
00061 // This example can be run with any number of processors.
00062 //
00063 // \author Marzio Sala, D-INFK/ETHZ.
00064 //
00065 // \date Last modified on 09-Mar-06.
00066 
00067 int main (int argc, char **argv)
00068 {
00069 #ifdef HAVE_MPI
00070   MPI_Init(&argc, &argv);
00071   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00072 #else
00073   Epetra_SerialComm Comm;
00074 #endif
00075 
00076   try 
00077   {
00078     int n = Comm.NumProc() * 4;
00079     Epetra_Map Map(n, 0, Comm);
00080     Epetra_MultiVector x(Map, 2); x.Random();
00081     Epetra_MultiVector b(Map, 2); x.Random();
00082     Epetra_CrsMatrix Matrix(Copy, Map, 0);
00083     // diagonal matrix
00084     for (int i = 0; i < Map.NumMyElements(); ++i)
00085     {
00086       int ii = Map.GID(i);
00087       double one = 1.0;
00088       Matrix.InsertGlobalValues(ii, 1, &one, &ii);
00089     }
00090     Matrix.FillComplete();
00091 
00092     // create a Teuchos::ParameterList and populate it
00093     Teuchos::ParameterList List;
00094     List.set("bool type", true);
00095     List.set("int type", 2);
00096     List.set("double type", 3.0);
00097     List.set("std::string type", "a std::string");
00098 
00099     // This is the HDF5 file manager
00100     EpetraExt::HDF5 HDF5(Comm);
00101 
00102     // creates a new file. To open an existing file, use Open("myfile.h5")
00103     HDF5.Create("myfile.h5");
00104 
00105     // =========================== //
00106     // P A R T   I:  W R I T I N G //
00107     // =========================== //
00108 
00109     if (Comm.MyPID() == 0)
00110       std::cout << "Writing objects to HDF5 file myfile.h5..." << std::endl << std::endl;
00111 
00112     // We first write the map, whose name contains the number of processors
00113     HDF5.Write("map-" + EpetraExt::toString(Comm.NumProc()), Map);
00114     // Then we write the matrix...
00115     HDF5.Write("matrix", Matrix);
00116     // ... and the x, b vectors
00117     HDF5.Write("x", x);
00118     HDF5.Write("b", b);
00119     // we can associate basic data types with a given group, for example:
00120     HDF5.Write("matrix", "integration order", 1);
00121     HDF5.Write("matrix", "numerical drop", 0.1);
00122     HDF5.Write("matrix", "package name", "EpetraExt");
00123     HDF5.Write("matrix", "author", "Marzio Sala");
00124     HDF5.Write("matrix", "institution", "ETHZ/D-INFK");
00125     // or put them in a new group
00126     HDF5.Write("my parameters", "latitude", 12);
00127     HDF5.Write("my parameters", "longitude", 67);
00128 
00129     // Now we write the parameter list we have used to create the matrix
00130     HDF5.Write("List", List);
00131     // We can also write integers/doubles/arrays. All these quantities are
00132     // supposed to have the same value on all processors.
00133     std::vector<int> iarray(3); 
00134     iarray[0] = 0, iarray[1] = 1; iarray[2] = 2;
00135     HDF5.Write("my parameters", "int array", H5T_NATIVE_INT, 3, &iarray[0]);
00136 
00137     std::vector<double> darray(3); 
00138     darray[0] = 0.1, darray[1] = 1.1; darray[2] = 2.1;
00139     HDF5.Write("my parameters", "double array", H5T_NATIVE_DOUBLE, 3, &darray[0]);
00140 
00141     // To analyze the content of the file, one can use 
00142     // "h5dump filename.h5" or "h5dump filename.h5 -H" o
00143 
00144     // ============================ //
00145     // P A R T   II:  R E A D I N G //
00146     // ============================ //
00147 
00148     if (Comm.MyPID() == 0)
00149       std::cout << "Reading objects from HDF5 file myfile.h5..." << std::endl << std::endl;
00150 
00151     Epetra_Map* NewMap = 0;
00152     Epetra_CrsMatrix* NewMatrix = 0;
00153     Epetra_MultiVector* NewX,* NewB;
00154 
00155     // Check if the map is there (in this case it is). If it is, read the
00156     // matrix using the map, otherwise read the matrix with a linear map.
00157     if (HDF5.IsContained("map-" + EpetraExt::toString(Comm.NumProc())))
00158     {
00159       HDF5.Read("map-" + EpetraExt::toString(Comm.NumProc()), NewMap);
00160       HDF5.Read("matrix", *NewMap, *NewMap, NewMatrix);
00161     }
00162     else
00163     {
00164       int NumGlobalRows;
00165       HDF5.Read("matrix", "NumGlobalRows", NumGlobalRows);
00166       NewMap = new Epetra_Map(NumGlobalRows, 0, Comm);
00167       HDF5.Read("matrix", *NewMap, *NewMap, NewMatrix);
00168     }
00169 
00170     // read the number of nonzeros from file, compare them with the
00171     // actual number of NewMatrix
00172     int NewNumGlobalNonzeros;
00173     HDF5.Read("matrix", "NumGlobalNonzeros", NewNumGlobalNonzeros);
00174 
00175     assert (Matrix.NumGlobalNonzeros() == NewNumGlobalNonzeros);
00176 
00177     // Now read the MultiVector's
00178     HDF5.Read("x", NewX);
00179     HDF5.Read("b", NewB);
00180 
00181     // the int/double values, and the int/double arrays
00182     int new_latitude, new_longitude;
00183     std::vector<int>    new_iarray(3);
00184     std::vector<double> new_darray(3);
00185     HDF5.Read("my parameters", "latitude", new_latitude);
00186     HDF5.Read("my parameters", "longitude", new_longitude);
00187     HDF5.Read("my parameters", "int array", H5T_NATIVE_INT, 3, &new_iarray[0]);
00188     HDF5.Read("my parameters", "double array", H5T_NATIVE_DOUBLE, 3, &new_darray[0]);
00189     // and the std::string values associated with group "matrix", recordered
00190     // with dataset "package name".
00191     std::string PackageName;
00192     HDF5.Read("matrix", "package name", PackageName);
00193 
00194     Teuchos::ParameterList newList;
00195     HDF5.Read("List", newList);
00196     if (Comm.MyPID() == 0)
00197     {
00198       std::cout << "New list as read from file is:" << std::endl;
00199       std::cout << "bool type = " << newList.get("bool type", false) << std::endl;
00200       std::cout << "int type = " << newList.get("int type", -1) << std::endl;
00201       std::cout << "double type = " << newList.get("double type", -1.0) << std::endl;
00202       std::cout << "std::string type = " << newList.get("std::string type", "not-set") << std::endl;
00203       std::cout << std::endl;
00204 
00205       std::cout << "Checking some read and written data..." << std::endl;
00206       for (int i = 0; i < 3; ++i)
00207         std::cout << "iarray[" << i << "] = " << iarray[i] << " should be " << new_iarray[i] << std::endl;
00208       for (int i = 0; i < 3; ++i)
00209         std::cout << "darray[" << i << "] = " << darray[i] << " should be " << new_darray[i] << std::endl;
00210       std::cout << std::endl;
00211       std::cout << "Try to print out the content of myfile.h5 using the command" << std::endl;
00212       std::cout << "    h5dump myfile.h5" << std::endl;
00213     }
00214 
00215     // We finally close the file. Better to close it before calling
00216     // MPI_Finalize() to avoid MPI-related errors, since Close() might call MPI
00217     // functions.
00218     HDF5.Close();
00219 
00220     // delete memory
00221     if (NewMap   ) delete NewMap;
00222     if (NewMatrix) delete NewMatrix;
00223     if (NewX     ) delete NewX;
00224     if (NewB     ) delete NewB;
00225   }
00226   catch(EpetraExt::Exception& rhs) 
00227   {
00228     rhs.Print();
00229   }
00230   catch (...) 
00231   {
00232     std::cerr << "Caught generic std::exception" << std::endl;
00233   }
00234 
00235 
00236 #ifdef HAVE_MPI
00237   MPI_Finalize();
00238 #endif
00239 
00240   return(EXIT_SUCCESS);
00241 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines