44 #include "Tpetra_MatrixIO.hpp"
57 bool Tpetra::Utils::parseIfmt(Teuchos::ArrayRCP<char> fmt,
int &perline,
int &width) {
58 TEUCHOS_TEST_FOR_EXCEPT(fmt.size() != 0 && fmt[fmt.size()-1] !=
'\0');
61 std::transform(fmt.begin(), fmt.end(), fmt, static_cast < int(*)(int) > (std::toupper));
62 if (std::sscanf(fmt.getRawPtr(),
"(%dI%d)",&perline,&width) == 2) {
68 bool Tpetra::Utils::parseRfmt(Teuchos::ArrayRCP<char> fmt,
int &perline,
int &width,
int &prec,
char &valformat) {
69 TEUCHOS_TEST_FOR_EXCEPT(fmt.size() != 0 && fmt[fmt.size()-1] !=
'\0');
70 std::transform(fmt.begin(), fmt.end(), fmt, static_cast < int(*)(int) > (std::toupper));
72 Teuchos::ArrayRCP<char>::iterator firstLeftParen = std::find( fmt.begin(), fmt.end(),
'(');
73 Teuchos::ArrayRCP<char>::iterator lastRightParen = std::find(std::reverse_iterator<Teuchos::ArrayRCP<char>::iterator>(fmt.end()),
74 std::reverse_iterator<Teuchos::ArrayRCP<char>::iterator>(fmt.begin()),
78 if (firstLeftParen == fmt.end() || lastRightParen == fmt.begin()) {
83 fmt += (firstLeftParen - fmt.begin());
84 size_t newLen = lastRightParen - firstLeftParen + 1;
85 fmt.resize(newLen + 1);
88 if (std::find(fmt.begin(),fmt.end(),
'P') != fmt.end()) {
93 if (std::sscanf(fmt.getRawPtr(),
"(%d%c%d.%d)",&perline,&valformat,&width,&prec) == 4) {
94 if (valformat ==
'E' || valformat ==
'D' || valformat ==
'F') {
102 void Tpetra::Utils::readHBHeader(std::ifstream &fin, Teuchos::ArrayRCP<char> &Title, Teuchos::ArrayRCP<char> &Key, Teuchos::ArrayRCP<char> &Type,
103 int &Nrow,
int &Ncol,
int &Nnzero,
int &Nrhs,
104 Teuchos::ArrayRCP<char> &Ptrfmt, Teuchos::ArrayRCP<char> &Indfmt, Teuchos::ArrayRCP<char> &Valfmt, Teuchos::ArrayRCP<char> &Rhsfmt,
105 int &Ptrcrd,
int &Indcrd,
int &Valcrd,
int &Rhscrd, Teuchos::ArrayRCP<char> &Rhstype) {
106 int Totcrd, Neltvl, Nrhsix;
107 const int MAXLINE = 81;
110 Title.resize(72 + 1); std::fill(Title.begin(), Title.end(),
'\0');
111 Key.resize(8 + 1); std::fill(Key.begin(), Key.end(),
'\0');
112 Type.resize(3 + 1); std::fill(Type.begin(), Type.end(),
'\0');
113 Ptrfmt.resize(16 + 1); std::fill(Ptrfmt.begin(), Ptrfmt.end(),
'\0');
114 Indfmt.resize(16 + 1); std::fill(Indfmt.begin(), Indfmt.end(),
'\0');
115 Valfmt.resize(20 + 1); std::fill(Valfmt.begin(), Valfmt.end(),
'\0');
116 Rhsfmt.resize(20 + 1); std::fill(Rhsfmt.begin(), Rhsfmt.end(),
'\0');
118 const std::string errStr(
"Tpetra::Utils::readHBHeader(): Improperly formatted H/B file: ");
120 fin.getline(line,MAXLINE);
121 TEUCHOS_TEST_FOR_EXCEPTION( std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
122 (void)std::sscanf(line,
"%72c%8[^\n]", Title.getRawPtr(), Key.getRawPtr());
124 fin.getline(line,MAXLINE);
125 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
126 if ( std::sscanf(line,
"%14d%14d%14d%14d%14d",&Totcrd,&Ptrcrd,&Indcrd,&Valcrd,&Rhscrd) != 5 ) {
128 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)");
131 fin.getline(line,MAXLINE);
132 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
133 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)");
134 std::transform(Type.begin(), Type.end(), Type.begin(), static_cast < int(*)(int) > (std::toupper));
136 fin.getline(line,MAXLINE);
137 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
139 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)");
142 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)");
146 Rhstype.resize(3 + 1,
'\0');
147 fin.getline(line,MAXLINE);
148 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(line,
"%*s") < 0, std::runtime_error, errStr <<
"error buffering line.");
149 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)");
154 void Tpetra::Utils::readHBInfo(
const std::string &filename,
int &M,
int &N,
int &nz, Teuchos::ArrayRCP<char> &Type,
int &Nrhs) {
156 int Ptrcrd, Indcrd, Valcrd, Rhscrd;
157 Teuchos::ArrayRCP<char> Title, Key, Rhstype, Ptrfmt, Indfmt, Valfmt, Rhsfmt;
159 fin.open(filename.c_str(),std::ifstream::in);
160 Tpetra::Utils::readHBHeader(fin, Title, Key, Type, M, N, nz, Nrhs,
161 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
162 Ptrcrd, Indcrd, Valcrd, Rhscrd, Rhstype);
165 catch (std::exception &e) {
166 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
167 "Tpetra::Utils::readHBInfo() of filename \"" << filename <<
"\" caught exception: " << std::endl
168 << e.what() << std::endl);
173 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) {
178 int ptrCrd, indCrd, valCrd;
179 Teuchos::ArrayRCP<char> Title, Key, Ptrfmt, Indfmt, Valfmt;
180 const int MAXSIZE = 81;
181 char lineBuf[MAXSIZE];
183 int ptrsPerLine, ptrWidth, indsPerLine, indWidth, valsPerLine, valWidth, valPrec;
186 fin.open(filename.c_str(),std::ifstream::in);
190 Teuchos::ArrayRCP<char> Rhstype, Rhsfmt;
191 Teuchos::ArrayRCP<char> TypeArray;
192 Tpetra::Utils::readHBHeader(fin, Title, Key, TypeArray, numRows, numCols, numNZ, Nrhs,
193 Ptrfmt, Indfmt, Valfmt, Rhsfmt,
194 ptrCrd, indCrd, valCrd, rhsCrd, Rhstype);
195 if (TypeArray.size() > 0) {
196 type.resize(TypeArray.size()-1);
197 std::copy(TypeArray.begin(), TypeArray.end(), type.begin());
200 const std::string errStr(
"Tpetra::Utils::readHBHeader(): Improperly formatted H/B file.");
201 const bool readPatternOnly = (type[0] ==
'P' || type[0] ==
'p');
202 const bool readComplex = (type[0] ==
'C' || type[0] ==
'c');
204 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseIfmt(Ptrfmt,ptrsPerLine,ptrWidth) ==
true, std::runtime_error,
205 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
206 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseIfmt(Indfmt,indsPerLine,indWidth) ==
true, std::runtime_error,
207 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
208 if (readPatternOnly ==
false) {
209 TEUCHOS_TEST_FOR_EXCEPTION( Tpetra::Utils::parseRfmt(Valfmt,valsPerLine,valWidth,valPrec,valFlag) ==
true, std::runtime_error,
210 "Tpetra::Utils::readHBMatDouble(): error parsing. Invalid/unsupported file format.");
214 if (numCols == 0)
return;
217 colPtrs = Teuchos::arcp<int>(numCols+1);
219 rowInds = Teuchos::arcp<int>(numNZ);
220 if (readPatternOnly ==
false) {
222 vals = Teuchos::arcp<double>(2*numNZ);
225 vals = Teuchos::arcp<double>(numNZ);
235 for (
int lno=0; lno < ptrCrd; ++lno) {
236 fin.getline(lineBuf, MAXSIZE);
237 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
238 char *linePtr = lineBuf;
239 for (
int ptr=0; ptr < ptrsPerLine; ++ptr) {
240 if (colPtrsRead == numCols + 1)
break;
243 std::swap(NullSub,linePtr[ptrWidth]);
245 std::sscanf(linePtr,
"%d", &cptr);
247 std::swap(NullSub,linePtr[ptrWidth]);
249 colPtrs[colPtrsRead++] = cptr;
252 TEUCHOS_TEST_FOR_EXCEPT(colPtrsRead != numCols + 1);
260 for (
int lno=0; lno < indCrd; ++lno) {
261 fin.getline(lineBuf, MAXSIZE);
262 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
263 char *linePtr = lineBuf;
264 for (
int indcntr=0; indcntr < indsPerLine; ++indcntr) {
265 if (indicesRead == numNZ)
break;
268 std::swap(NullSub,linePtr[indWidth]);
270 std::sscanf(linePtr,
"%d", &ind);
272 std::swap(NullSub,linePtr[indWidth]);
274 rowInds[indicesRead++] = ind;
277 TEUCHOS_TEST_FOR_EXCEPT(indicesRead != numNZ);
283 if (readPatternOnly ==
false) {
286 totalNumVals = 2*numNZ;
289 totalNumVals = numNZ;
293 for (
int lno=0; lno < valCrd; ++lno) {
294 fin.getline(lineBuf, MAXSIZE);
295 TEUCHOS_TEST_FOR_EXCEPTION(std::sscanf(lineBuf,
"%*s") < 0, std::runtime_error, errStr);
297 if (valFlag ==
'D') std::replace_if(lineBuf, lineBuf+MAXSIZE, [] (
const char c) {
return c ==
'D'; },
'E');
298 char *linePtr = lineBuf;
299 for (
int valcntr=0; valcntr < valsPerLine; ++valcntr) {
300 if (valsRead == totalNumVals)
break;
303 std::swap(NullSub,linePtr[valWidth]);
305 std::sscanf(linePtr,
"%le", &val);
307 std::swap(NullSub,linePtr[valWidth]);
309 vals[valsRead++] = val;
312 TEUCHOS_TEST_FOR_EXCEPT(valsRead != totalNumVals);
316 catch (std::exception &e) {
317 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
318 "Tpetra::Utils::readHBInfo() of filename \"" << filename <<
"\" caught exception: " << std::endl
319 << e.what() << std::endl);
323 #ifdef HAVE_TPETRA_EXPLICIT_INSTANTIATION
325 #include "TpetraCore_ETIHelperMacros.h"
326 #include "Tpetra_MatrixIO_def.hpp"
331 TPETRA_ETI_MANGLING_TYPEDEFS()
333 TPETRA_INSTANTIATE_SLGN(TPETRA_MATRIXIO_INSTANT)
338 #endif // HAVE_TPETRA_EXPLICIT_INSTANTIATION
void transform(const char kernelLabel[], ExecutionSpace execSpace, GlobalDataStructure &input, GlobalDataStructure &output, UnaryFunctionType f)
For each local entry input_i of input, assign f(input_i) to the corresponding local entry output_i of...