10 #include "Tpetra_MatrixIO.hpp"
23 bool Tpetra::Utils::parseIfmt(Teuchos::ArrayRCP<char> fmt,
int &perline,
int &width) {
24 TEUCHOS_TEST_FOR_EXCEPT(fmt.size() != 0 && fmt[fmt.size()-1] !=
'\0');
27 std::transform(fmt.begin(), fmt.end(), fmt, static_cast < int(*)(int) > (std::toupper));
28 if (std::sscanf(fmt.getRawPtr(),
"(%dI%d)",&perline,&width) == 2) {
34 bool Tpetra::Utils::parseRfmt(Teuchos::ArrayRCP<char> fmt,
int &perline,
int &width,
int &prec,
char &valformat) {
35 TEUCHOS_TEST_FOR_EXCEPT(fmt.size() != 0 && fmt[fmt.size()-1] !=
'\0');
36 std::transform(fmt.begin(), fmt.end(), fmt, static_cast < int(*)(int) > (std::toupper));
38 Teuchos::ArrayRCP<char>::iterator firstLeftParen = std::find( fmt.begin(), fmt.end(),
'(');
39 Teuchos::ArrayRCP<char>::iterator lastRightParen = std::find(std::reverse_iterator<Teuchos::ArrayRCP<char>::iterator>(fmt.end()),
40 std::reverse_iterator<Teuchos::ArrayRCP<char>::iterator>(fmt.begin()),
44 if (firstLeftParen == fmt.end() || lastRightParen == fmt.begin()) {
49 fmt += (firstLeftParen - fmt.begin());
50 size_t newLen = lastRightParen - firstLeftParen + 1;
51 fmt.resize(newLen + 1);
54 if (std::find(fmt.begin(),fmt.end(),
'P') != fmt.end()) {
59 if (std::sscanf(fmt.getRawPtr(),
"(%d%c%d.%d)",&perline,&valformat,&width,&prec) == 4) {
60 if (valformat ==
'E' || valformat ==
'D' || valformat ==
'F') {
68 void Tpetra::Utils::readHBHeader(std::ifstream &fin, Teuchos::ArrayRCP<char> &Title, Teuchos::ArrayRCP<char> &Key, Teuchos::ArrayRCP<char> &Type,
69 int &Nrow,
int &Ncol,
int &Nnzero,
int &Nrhs,
70 Teuchos::ArrayRCP<char> &Ptrfmt, Teuchos::ArrayRCP<char> &Indfmt, Teuchos::ArrayRCP<char> &Valfmt, Teuchos::ArrayRCP<char> &Rhsfmt,
71 int &Ptrcrd,
int &Indcrd,
int &Valcrd,
int &Rhscrd, Teuchos::ArrayRCP<char> &Rhstype) {
72 int Totcrd, Neltvl, Nrhsix;
73 const int MAXLINE = 81;
76 Title.resize(72 + 1); std::fill(Title.begin(), Title.end(),
'\0');
77 Key.resize(8 + 1); std::fill(Key.begin(), Key.end(),
'\0');
78 Type.resize(3 + 1); std::fill(Type.begin(), Type.end(),
'\0');
79 Ptrfmt.resize(16 + 1); std::fill(Ptrfmt.begin(), Ptrfmt.end(),
'\0');
80 Indfmt.resize(16 + 1); std::fill(Indfmt.begin(), Indfmt.end(),
'\0');
81 Valfmt.resize(20 + 1); std::fill(Valfmt.begin(), Valfmt.end(),
'\0');
82 Rhsfmt.resize(20 + 1); std::fill(Rhsfmt.begin(), Rhsfmt.end(),
'\0');
84 const std::string errStr(
"Tpetra::Utils::readHBHeader(): Improperly formatted H/B file: ");
86 fin.getline(line,MAXLINE);
87 TEUCHOS_TEST_FOR_EXCEPTION( std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
88 (void)std::sscanf(line,
"%72c%8[^\n]", Title.getRawPtr(), Key.getRawPtr());
90 fin.getline(line,MAXLINE);
91 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
92 if ( std::sscanf(line,
"%14d%14d%14d%14d%14d",&Totcrd,&Ptrcrd,&Indcrd,&Valcrd,&Rhscrd) != 5 ) {
94 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%14d%14d%14d%14d",&Totcrd,&Ptrcrd,&Indcrd,&Valcrd) != 4, std::runtime_error, errStr <<
"error reading pointers (line 2)");
97 fin.getline(line,MAXLINE);
98 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
99 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%3c%14i%14i%14i%14i", Type.getRawPtr(),&Nrow,&Ncol,&Nnzero,&Neltvl) != 5 , std::runtime_error, errStr <<
"error reading matrix meta-data (line 3)");
100 std::transform(Type.begin(), Type.end(), Type.begin(), static_cast < int(*)(int) > (std::toupper));
102 fin.getline(line,MAXLINE);
103 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
105 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%16c%16c%20c%20c",Ptrfmt.getRawPtr(),Indfmt.getRawPtr(),Valfmt.getRawPtr(),Rhsfmt.getRawPtr()) != 4, std::runtime_error, errStr <<
"error reading formats (line 4)");
108 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%16c%16c%20c",Ptrfmt.getRawPtr(),Indfmt.getRawPtr(),Valfmt.getRawPtr()) != 3, std::runtime_error, errStr <<
"error reading formats (line 4)");
112 Rhstype.resize(3 + 1,
'\0');
113 fin.getline(line,MAXLINE);
114 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
115 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%3c%14d%14d", Rhstype.getRawPtr(), &Nrhs, &Nrhsix) != 3, std::runtime_error, errStr <<
"error reading right-hand-side meta-data (line 5)");
120 void Tpetra::Utils::readHBInfo(
const std::string &filename,
int &M,
int &N,
int &nz, Teuchos::ArrayRCP<char> &Type,
int &Nrhs) {
122 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
123 Teuchos::ArrayRCP<char> Title, Key, Rhstype, Ptrfmt, Indfmt, Valfmt, Rhsfmt;
125 fin.open(filename.c_str(),std::ifstream::in);
126 Tpetra::Utils::readHBHeader(fin, Title, Key, Type, M, N, nz, Nrhs,
127 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
128 Ptrcrd, Indcrd, Valcrd, Rhscrd, Rhstype);
131 catch (std::exception &e) {
132 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
133 "Tpetra::Utils::readHBInfo() of filename \"" << filename <<
"\" caught exception: " << std::endl
134 << e.what() << std::endl);
139 void Tpetra::Utils::readHBMatDouble(
const std::string &filename,
int &numRows,
int &numCols,
int &numNZ, std::string &type, Teuchos::ArrayRCP<int> &colPtrs, Teuchos::ArrayRCP<int> &rowInds, Teuchos::ArrayRCP<double> &vals) {
144 int ptrCrd, indCrd, valCrd;
145 Teuchos::ArrayRCP<char> Title, Key, Ptrfmt, Indfmt, Valfmt;
146 const int MAXSIZE = 81;
147 char lineBuf[MAXSIZE];
149 int ptrsPerLine, ptrWidth, indsPerLine, indWidth, valsPerLine, valWidth, valPrec;
152 fin.open(filename.c_str(),std::ifstream::in);
156 Teuchos::ArrayRCP<char> Rhstype, Rhsfmt;
157 Teuchos::ArrayRCP<char> TypeArray;
158 Tpetra::Utils::readHBHeader(fin, Title, Key, TypeArray, numRows, numCols, numNZ, Nrhs,
159 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
160 ptrCrd, indCrd, valCrd, rhsCrd, Rhstype);
161 if (TypeArray.size() > 0) {
162 type.resize(TypeArray.size()-1);
163 std::copy(TypeArray.begin(), TypeArray.end(), type.begin());
166 const std::string errStr(
"Tpetra::Utils::readHBHeader(): Improperly formatted H/B file.");
167 const bool readPatternOnly = (type[0] ==
'P' || type[0] ==
'p');
168 const bool readComplex = (type[0] ==
'C' || type[0] ==
'c');
170 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseIfmt(Ptrfmt,ptrsPerLine,ptrWidth) ==
true, std::runtime_error,
171 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
172 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseIfmt(Indfmt,indsPerLine,indWidth) ==
true, std::runtime_error,
173 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
174 if (readPatternOnly ==
false) {
175 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseRfmt(Valfmt,valsPerLine,valWidth,valPrec,valFlag) ==
true, std::runtime_error,
176 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
180 if (numCols == 0)
return;
183 colPtrs = Teuchos::arcp<int>(numCols+1);
185 rowInds = Teuchos::arcp<int>(numNZ);
186 if (readPatternOnly ==
false) {
188 vals = Teuchos::arcp<double>(2*numNZ);
191 vals = Teuchos::arcp<double>(numNZ);
201 for (
int lno=0; lno < ptrCrd; ++lno) {
202 fin.getline(lineBuf, MAXSIZE);
203 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
204 char *linePtr = lineBuf;
205 for (
int ptr=0; ptr < ptrsPerLine; ++ptr) {
206 if (colPtrsRead == numCols + 1)
break;
209 std::swap(NullSub,linePtr[ptrWidth]);
211 std::sscanf(linePtr,
"%d", &cptr);
213 std::swap(NullSub,linePtr[ptrWidth]);
215 colPtrs[colPtrsRead++] = cptr;
218 TEUCHOS_TEST_FOR_EXCEPT(colPtrsRead != numCols + 1);
226 for (
int lno=0; lno < indCrd; ++lno) {
227 fin.getline(lineBuf, MAXSIZE);
228 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
229 char *linePtr = lineBuf;
230 for (
int indcntr=0; indcntr < indsPerLine; ++indcntr) {
231 if (indicesRead == numNZ)
break;
234 std::swap(NullSub,linePtr[indWidth]);
236 std::sscanf(linePtr,
"%d", &ind);
238 std::swap(NullSub,linePtr[indWidth]);
240 rowInds[indicesRead++] = ind;
243 TEUCHOS_TEST_FOR_EXCEPT(indicesRead != numNZ);
249 if (readPatternOnly ==
false) {
252 totalNumVals = 2*numNZ;
255 totalNumVals = numNZ;
259 for (
int lno=0; lno < valCrd; ++lno) {
260 fin.getline(lineBuf, MAXSIZE);
261 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
263 if (valFlag ==
'D') std::replace_if(lineBuf, lineBuf+MAXSIZE, [] (
const char c) {
return c ==
'D'; },
'E');
264 char *linePtr = lineBuf;
265 for (
int valcntr=0; valcntr < valsPerLine; ++valcntr) {
266 if (valsRead == totalNumVals)
break;
269 std::swap(NullSub,linePtr[valWidth]);
271 std::sscanf(linePtr,
"%le", &val);
273 std::swap(NullSub,linePtr[valWidth]);
275 vals[valsRead++] = val;
278 TEUCHOS_TEST_FOR_EXCEPT(valsRead != totalNumVals);
282 catch (std::exception &e) {
283 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
284 "Tpetra::Utils::readHBInfo() of filename \"" << filename <<
"\" caught exception: " << std::endl
285 << e.what() << std::endl);
289 #ifdef HAVE_TPETRA_EXPLICIT_INSTANTIATION
291 #include "TpetraCore_ETIHelperMacros.h"
292 #include "Tpetra_MatrixIO_def.hpp"
297 TPETRA_ETI_MANGLING_TYPEDEFS()
299 TPETRA_INSTANTIATE_SLGN(TPETRA_MATRIXIO_INSTANT)
304 #endif // HAVE_TPETRA_EXPLICIT_INSTANTIATION