EpetraExt Package Browser (Single Doxygen Collection) Development
HDF5Converter.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 "Teuchos_CommandLineProcessor.hpp"
00053 #include "Epetra_Map.h"
00054 #include "Epetra_BlockMap.h"
00055 #include "Epetra_CrsMatrix.h"
00056 #include "Epetra_Time.h"
00057 #include "Epetra_Vector.h"
00058 #include "Epetra_Import.h"
00059 #include "EpetraExt_Exception.h"
00060 #include "EpetraExt_Utils.h"
00061 #include "EpetraExt_HDF5.h"
00062 #include "EpetraExt_BlockMapIn.h"
00063 #include "EpetraExt_BlockMapOut.h"
00064 #include "EpetraExt_MultiVectorIn.h"
00065 #include "EpetraExt_MultiVectorOut.h"
00066 #include "EpetraExt_RowMatrixOut.h"
00067 #include "EpetraExt_CrsMatrixIn.h"
00068 
00069 // converts file from EpetraExt format into HDF5 format.
00070 //
00071 // \author Marzio Sala, D-INFK/ETHZ
00072 //
00073 // \date Last updated on 09-Mar-06.
00074 
00075 int main (int argc, char **argv)
00076 {
00077 #ifdef HAVE_MPI
00078   MPI_Init(&argc, &argv);
00079   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00080 #else
00081   Epetra_SerialComm Comm;
00082 #endif
00083 
00084   if (Comm.MyPID() == 0)
00085   {
00086     cout << "Converter from MatrixMarket files to HDF5 files" << endl;
00087     cout << "For notes on the usage, execute" << endl;
00088     cout << "  ./HDF5Converter.exe --help" << endl;
00089     cout << endl;
00090   }
00091 
00092   // Creating an empty command line processor looks like:
00093   Teuchos::CommandLineProcessor CLP;
00094 
00095   string MapFileName    = "not-set";
00096   string XFileName      = "not-set";
00097   string BFileName      = "not-set";
00098   string MatrixFileName = "not-set";
00099   string HDF5FileName   = "myfile.f5";
00100   string MapHDF5Name    = "map";
00101   string XHDF5Name      = "X";
00102   string BHDF5Name      = "B";
00103   string MatrixHDF5Name = "matrix";
00104 
00105   CLP.setOption("in-map",    &MapFileName,    "map file name");
00106   CLP.setOption("in-matrix", &MatrixFileName, "matrix file name");
00107   CLP.setOption("in-x",      &XFileName,      "x vector file name");
00108   CLP.setOption("in-b",      &BFileName,      "b vector file name");
00109   CLP.setOption("output",    &HDF5FileName,   "name of HDF5 file");
00110   CLP.setOption("out-map",    &MapHDF5Name,    "map name in HDF5 file");
00111   CLP.setOption("out-matrix", &MatrixHDF5Name, "matrix name in HDF5 file");
00112   CLP.setOption("out-x",      &XHDF5Name,      "x vector name in HDF5 file");
00113   CLP.setOption("out-b",      &BHDF5Name,      "b vector name in HDF5 file");
00114 
00115   CLP.throwExceptions(false);
00116   CLP.parse(argc,argv);
00117 
00118   Epetra_Map* Map = 0;
00119   Epetra_CrsMatrix* Matrix = 0;
00120   Epetra_MultiVector* X = 0;
00121   Epetra_MultiVector* B = 0;
00122 
00123   if (MapFileName != "not-set")
00124   {
00125     if (Comm.MyPID() == 0)
00126       cout << "Reading map from " << MapFileName << endl;
00127 
00128     EpetraExt::MatrixMarketFileToMap(MapFileName.c_str(), Comm, Map);
00129   }
00130   else
00131   {
00132     cerr << "You need to specify a map, sorry" << endl;
00133 #ifdef HAVE_MPI
00134     MPI_Finalize();
00135 #endif
00136     exit(EXIT_SUCCESS);
00137   }
00138 
00139   if (XFileName != "not-set")
00140   {
00141     if (Comm.MyPID() == 0)
00142       cout << "Reading vector from " << XFileName << endl;
00143 
00144     EpetraExt::MatrixMarketFileToMultiVector(XFileName.c_str(), *Map, X);
00145   }
00146 
00147   if (BFileName != "not-set")
00148   {
00149     if (Comm.MyPID() == 0)
00150       cout << "Reading vector from " << BFileName << endl;
00151 
00152     EpetraExt::MatrixMarketFileToMultiVector(BFileName.c_str(), *Map, B);
00153   }
00154 
00155   if (MatrixFileName != "not-set")
00156   {
00157     if (Comm.MyPID() == 0)
00158       cout << "Reading matrix from " << MatrixFileName << endl;
00159 
00160     EpetraExt::MatrixMarketFileToCrsMatrix(MatrixFileName.c_str(), *Map, Matrix);
00161   }
00162 
00163   // ================================= //
00164   // Open HDF5 file and append data in //
00165   // ================================= //
00166   
00167   EpetraExt::HDF5 HDF5(Comm);
00168 
00169   HDF5.Create(HDF5FileName);
00170 
00171   if (Map)
00172     HDF5.Write(MapHDF5Name + EpetraExt::toString(Comm.NumProc()), *Map);
00173   if (Matrix)
00174     HDF5.Write(MatrixHDF5Name, *Matrix);
00175   if (X)
00176     HDF5.Write(XHDF5Name, *X);
00177   if (B)
00178     HDF5.Write(BHDF5Name, *B);
00179   HDF5.Close();
00180 
00181   if (Map) delete Map;
00182   if (Matrix) delete Matrix;
00183   if (X) delete X;
00184   if (B) delete B;
00185 
00186 #ifdef HAVE_MPI
00187   MPI_Finalize();
00188 #endif
00189 
00190   return(EXIT_SUCCESS);
00191 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines