EpetraExt Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
HDF5Converter.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
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 "Teuchos_CommandLineProcessor.hpp"
53#include "Epetra_Map.h"
54#include "Epetra_BlockMap.h"
55#include "Epetra_CrsMatrix.h"
56#include "Epetra_Time.h"
57#include "Epetra_Vector.h"
58#include "Epetra_Import.h"
59#include "EpetraExt_Exception.h"
60#include "EpetraExt_Utils.h"
61#include "EpetraExt_HDF5.h"
68
69// converts file from EpetraExt format into HDF5 format.
70//
71// \author Marzio Sala, D-INFK/ETHZ
72//
73// \date Last updated on 09-Mar-06.
74
75int main (int argc, char **argv)
76{
77#ifdef HAVE_MPI
78 MPI_Init(&argc, &argv);
79 Epetra_MpiComm Comm(MPI_COMM_WORLD);
80#else
82#endif
83
84 if (Comm.MyPID() == 0)
85 {
86 cout << "Converter from MatrixMarket files to HDF5 files" << endl;
87 cout << "For notes on the usage, execute" << endl;
88 cout << " ./HDF5Converter.exe --help" << endl;
89 cout << endl;
90 }
91
92 // Creating an empty command line processor looks like:
93 Teuchos::CommandLineProcessor CLP;
94
95 string MapFileName = "not-set";
96 string XFileName = "not-set";
97 string BFileName = "not-set";
98 string MatrixFileName = "not-set";
99 string HDF5FileName = "myfile.f5";
100 string MapHDF5Name = "map";
101 string XHDF5Name = "X";
102 string BHDF5Name = "B";
103 string MatrixHDF5Name = "matrix";
104
105 CLP.setOption("in-map", &MapFileName, "map file name");
106 CLP.setOption("in-matrix", &MatrixFileName, "matrix file name");
107 CLP.setOption("in-x", &XFileName, "x vector file name");
108 CLP.setOption("in-b", &BFileName, "b vector file name");
109 CLP.setOption("output", &HDF5FileName, "name of HDF5 file");
110 CLP.setOption("out-map", &MapHDF5Name, "map name in HDF5 file");
111 CLP.setOption("out-matrix", &MatrixHDF5Name, "matrix name in HDF5 file");
112 CLP.setOption("out-x", &XHDF5Name, "x vector name in HDF5 file");
113 CLP.setOption("out-b", &BHDF5Name, "b vector name in HDF5 file");
114
115 CLP.throwExceptions(false);
116 CLP.parse(argc,argv);
117
118 Epetra_Map* Map = 0;
119 Epetra_CrsMatrix* Matrix = 0;
120 Epetra_MultiVector* X = 0;
121 Epetra_MultiVector* B = 0;
122
123 if (MapFileName != "not-set")
124 {
125 if (Comm.MyPID() == 0)
126 cout << "Reading map from " << MapFileName << endl;
127
128 EpetraExt::MatrixMarketFileToMap(MapFileName.c_str(), Comm, Map);
129 }
130 else
131 {
132 cerr << "You need to specify a map, sorry" << endl;
133#ifdef HAVE_MPI
134 MPI_Finalize();
135#endif
136 exit(EXIT_SUCCESS);
137 }
138
139 if (XFileName != "not-set")
140 {
141 if (Comm.MyPID() == 0)
142 cout << "Reading vector from " << XFileName << endl;
143
144 EpetraExt::MatrixMarketFileToMultiVector(XFileName.c_str(), *Map, X);
145 }
146
147 if (BFileName != "not-set")
148 {
149 if (Comm.MyPID() == 0)
150 cout << "Reading vector from " << BFileName << endl;
151
152 EpetraExt::MatrixMarketFileToMultiVector(BFileName.c_str(), *Map, B);
153 }
154
155 if (MatrixFileName != "not-set")
156 {
157 if (Comm.MyPID() == 0)
158 cout << "Reading matrix from " << MatrixFileName << endl;
159
160 EpetraExt::MatrixMarketFileToCrsMatrix(MatrixFileName.c_str(), *Map, Matrix);
161 }
162
163 // ================================= //
164 // Open HDF5 file and append data in //
165 // ================================= //
166
167 EpetraExt::HDF5 HDF5(Comm);
168
169 HDF5.Create(HDF5FileName);
170
171 if (Map)
172 HDF5.Write(MapHDF5Name + EpetraExt::toString(Comm.NumProc()), *Map);
173 if (Matrix)
174 HDF5.Write(MatrixHDF5Name, *Matrix);
175 if (X)
176 HDF5.Write(XHDF5Name, *X);
177 if (B)
178 HDF5.Write(BHDF5Name, *B);
179 HDF5.Close();
180
181 if (Map) delete Map;
182 if (Matrix) delete Matrix;
183 if (X) delete X;
184 if (B) delete B;
185
186#ifdef HAVE_MPI
187 MPI_Finalize();
188#endif
189
190 return(EXIT_SUCCESS);
191}
int main(int argc, char **argv)
class HDF5: A class for storing Epetra objects in parallel binary files
void Close()
Close the file.
void Write(const std::string &GroupName, const std::string &DataSetName, int data)
Write an integer in group GroupName using the given DataSetName.
void Create(const std::string FileName)
Create a new file.
int NumProc() const
int MyPID() const
int MatrixMarketFileToMultiVector(const char *filename, const Epetra_BlockMap &map, Epetra_MultiVector *&A)
Constructs an Epetra_MultiVector object from a Matrix Market format file.
int MatrixMarketFileToCrsMatrix(const char *filename, const Epetra_Comm &comm, Epetra_CrsMatrix *&A)
int MatrixMarketFileToMap(const char *filename, const Epetra_Comm &comm, Epetra_Map *&map)
Constructs an Epetra_BlockMap object from a Matrix Market format file.
std::string toString(const int &x)