44 #ifndef TPETRA_MATRIX_IO_DEF
45 #define TPETRA_MATRIX_IO_DEF
47 #include "Tpetra_CrsMatrix.hpp"
48 #include "Tpetra_MatrixIO.hpp"
54 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
55 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
58 generateMatrix (
const Teuchos::RCP<Teuchos::ParameterList> &plist,
59 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
62 generateMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> (plist, comm, Teuchos::null, A);
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
68 generateMatrix (
const Teuchos::RCP<Teuchos::ParameterList> &plist,
69 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
70 const Teuchos::RCP<Node> & ,
74 typedef Teuchos::ScalarTraits<Scalar> ST;
77 TEUCHOS_TEST_FOR_EXCEPTION( plist == Teuchos::null, std::runtime_error,
78 "Tpetra::Utils::generateMatrix(): ParameterList is null.");
79 TEUCHOS_TEST_FOR_EXCEPTION( Teuchos::isParameterType<std::string>(*plist,
"mat_type") ==
false, std::runtime_error,
80 "Tpetra::Utils::generateMatrix(): ParameterList did not contain string parameter ""mat_type"".");
81 std::string mat_type = plist->get<std::string>(
"mat_type");
82 if (mat_type ==
"Lap3D") {
84 const GlobalOrdinal gridSize = as<GlobalOrdinal>(plist->get<
int>(
"gridSize",100));
85 const GlobalOrdinal gS2 = gridSize*gridSize;
86 const GlobalOrdinal numRows = gS2*gridSize;
87 Teuchos::RCP<map_type> rowMap;
88 rowMap = Teuchos::rcp (
new map_type (static_cast<global_size_t> (numRows),
89 static_cast<GlobalOrdinal> (0),
90 comm, GloballyDistributed));
93 Teuchos::Array<GlobalOrdinal> neighbors;
94 Teuchos::Array<Scalar> values(7, -ST::one());
95 values[0] = (Scalar)6;
96 for (GlobalOrdinal r = rowMap->getMinGlobalIndex(); r <= rowMap->getMaxGlobalIndex(); ++r) {
98 neighbors.push_back(r);
99 GlobalOrdinal ixy, iz, ix, iy;
103 iy = (ixy - ix)/gridSize;
105 if ( ix != 0 ) neighbors.push_back( r-1 );
106 if ( ix != gridSize-1 ) neighbors.push_back( r+1 );
107 if ( iy != 0 ) neighbors.push_back( r-gridSize );
108 if ( iy != gridSize-1 ) neighbors.push_back( r+gridSize );
109 if ( iz != 0 ) neighbors.push_back( r-gS2 );
110 if ( iz != gridSize-1 ) neighbors.push_back( r+gS2 );
111 A->insertGlobalValues( r, neighbors(), values(0,neighbors.size()) );
116 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
117 "Tpetra::Utils::generateMatrix(): ParameterList specified unsupported ""mat_type"".");
120 #endif // TPETRA_ENABLE_DEPRECATED_CODE
122 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
124 readHBMatrix (
const std::string &filename,
125 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
128 const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
132 const int myRank = comm->getRank();
133 int numRows,numCols,numNZ;
134 Teuchos::ArrayRCP<Scalar> svals;
135 Teuchos::ArrayRCP<GlobalOrdinal> colinds;
136 Teuchos::ArrayRCP<int> rowptrs;
137 Teuchos::ArrayRCP<size_t> nnzPerRow;
140 bool isSymmetric=
false;
141 Teuchos::ArrayRCP<double> dvals;
142 Teuchos::ArrayRCP<int> colptrs, rowinds;
144 Tpetra::Utils::readHBMatDouble(filename,numRows,numCols,numNZ,type,colptrs,rowinds,dvals);
145 TEUCHOS_TEST_FOR_EXCEPT(type.size() != 3);
146 if (type[0] !=
'R' && type[0] !=
'r') {
150 if (fail == 0 && numNZ > 0) {
151 if (type[1] ==
'S' || type[1] ==
's') {
158 if (fail == 0 && numNZ > 0) {
160 nnzPerRow = Teuchos::arcp<size_t>(numRows);
161 std::fill(nnzPerRow.begin(), nnzPerRow.end(), 0);
162 for (Teuchos::ArrayRCP<int>::const_iterator ri=rowinds.begin(); ri != rowinds.end(); ++ri) {
168 for (
int c=0; c < numCols; ++c) {
170 for (
int i=colptrs[c]-1; i != colptrs[c+1]-1; ++i) {
171 if (rowinds[i] != c+1) {
179 svals = Teuchos::arcp<Scalar>(numNZ);
180 colinds = Teuchos::arcp<GlobalOrdinal>(numNZ);
181 rowptrs = Teuchos::arcp<int>(numRows+1);
183 #ifdef HAVE_TPETRA_DEBUG
184 Teuchos::ArrayRCP<size_t> nnzPerRow_debug(nnzPerRow.size());
185 std::copy(nnzPerRow.begin(), nnzPerRow.end(), nnzPerRow_debug.begin());
187 for (
int j=1; j <= numRows; ++j) {
188 rowptrs[j] = rowptrs[j-1] + nnzPerRow[j-1];
192 for (
int col=0; col<numCols; ++col) {
193 for (
int i=colptrs[col]-1; i != colptrs[col+1]-1; ++i) {
194 const int row = rowinds[i]-1;
196 const size_t entry = rowptrs[row] + nnzPerRow[row];
197 svals[entry] = Teuchos::as<Scalar>(dvals[i]);
198 colinds[entry] = Teuchos::as<GlobalOrdinal>(col);
200 if (isSymmetric && row != col) {
202 const size_t symentry = rowptrs[col] + nnzPerRow[col];
203 svals[symentry] = Teuchos::as<Scalar>(dvals[i]);
204 colinds[symentry] = Teuchos::as<GlobalOrdinal>(row);
209 #ifdef HAVE_TPETRA_DEBUG
212 typename Teuchos::ArrayRCP<size_t>::const_iterator it1, it2;
213 for (it1 = nnzPerRow.begin(), it2 = nnzPerRow_debug.begin(); it1 != nnzPerRow.end(); ++it1, ++it2) {
219 TEUCHOS_TEST_FOR_EXCEPTION(!isequal || nnzPerRow.size() != nnzPerRow_debug.size(), std::logic_error,
220 "Tpetra::Utils::readHBMatrix(): Logic error.");
227 broadcast(*comm,0,&fail);
228 TEUCHOS_TEST_FOR_EXCEPTION(fail == 1, std::runtime_error,
"Tpetra::Utils::readHBMatrix() can only read Real matrices.");
230 broadcast(*comm,0,&numRows);
231 broadcast(*comm,0,&numCols);
233 if (rowMap == Teuchos::null) {
234 rowMap = Teuchos::rcp (
new map_type (static_cast<global_size_t> (numRows),
235 static_cast<GlobalOrdinal> (0),
236 comm, GloballyDistributed));
239 TEUCHOS_TEST_FOR_EXCEPTION( rowMap->getGlobalNumElements() != (
global_size_t)numRows, std::runtime_error,
240 "Tpetra::Utils::readHBMatrix(): specified map has incorrect number of elements.");
241 TEUCHOS_TEST_FOR_EXCEPTION( rowMap->isDistributed() ==
false && comm->getSize() > 1, std::runtime_error,
242 "Tpetra::Utils::readHBMatrix(): specified map is not distributed.");
244 Teuchos::Array<size_t> myNNZ (rowMap->getNodeNumElements ());
246 LocalOrdinal numRowsAlreadyDistributed = rowMap->getNodeNumElements();
247 std::copy(nnzPerRow.begin(), nnzPerRow.begin()+numRowsAlreadyDistributed, myNNZ.begin());
248 for (
int p=1; p < Teuchos::size(*comm); ++p) {
250 Teuchos::receive(*comm,p,&numRowsForP);
252 Teuchos::send<int,size_t>(*comm,numRowsForP,nnzPerRow(numRowsAlreadyDistributed,numRowsForP).getRawPtr(),p);
253 numRowsAlreadyDistributed += numRowsForP;
258 const size_t numMyRows = rowMap->getNodeNumElements();
259 Teuchos::send(*comm,numMyRows,0);
261 Teuchos::receive<int,size_t>(*comm,0,numMyRows,myNNZ(0,numMyRows).getRawPtr());
264 nnzPerRow = Teuchos::null;
266 Teuchos::RCP<const map_type> domMap;
267 if (numRows == numCols) {
271 domMap = createUniformContigMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numCols,comm);
278 Teuchos::Array<size_t> empty;
279 std::swap (myNNZ, empty);
281 if (myRank == 0 && numNZ > 0) {
282 for (
int r=0; r < numRows; ++r) {
283 const LocalOrdinal nnz = rowptrs[r+1] - rowptrs[r];
285 Teuchos::ArrayView<const GlobalOrdinal> inds = colinds(rowptrs[r],nnz);
286 Teuchos::ArrayView<const Scalar> vals = svals( rowptrs[r],nnz);
287 A->insertGlobalValues(r, inds, vals);
292 colinds = Teuchos::null;
293 svals = Teuchos::null;
294 rowptrs = Teuchos::null;
295 A->fillComplete(domMap,rowMap,params);
298 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
299 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
302 readHBMatrix (
const std::string &filename,
303 const Teuchos::RCP<
const Teuchos::Comm<int> > &comm,
304 const Teuchos::RCP<Node> &,
307 const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
309 readHBMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>(filename, comm, A, rowMap, params);
311 #endif // TPETRA_ENABLE_DEPRECATED_CODE
322 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
324 #define TPETRA_MATRIXIO_INSTANT(SCALAR,LO,GO,NODE) \
326 readHBMatrix< SCALAR, LO, GO, NODE > (const std::string&, \
327 const Teuchos::RCP<const Teuchos::Comm<int> > &, \
328 Teuchos::RCP<CrsMatrix< SCALAR, LO, GO, NODE > >&, \
329 Teuchos::RCP<const Tpetra::Map< LO, GO, NODE> >, \
330 const Teuchos::RCP<Teuchos::ParameterList>& ); \
333 generateMatrix< SCALAR, LO, GO, NODE> (const Teuchos::RCP<Teuchos::ParameterList>&, \
334 const Teuchos::RCP<const Teuchos::Comm<int> > &, \
335 Teuchos::RCP<CrsMatrix< SCALAR, LO, GO, NODE > >& ); \
338 readHBMatrix< SCALAR, LO, GO, NODE > (const std::string&, \
339 const Teuchos::RCP<const Teuchos::Comm<int> > &, \
340 const Teuchos::RCP< NODE > &, \
341 Teuchos::RCP<CrsMatrix< SCALAR, LO, GO, NODE > >&, \
342 Teuchos::RCP<const Tpetra::Map< LO, GO, NODE> >, \
343 const Teuchos::RCP<Teuchos::ParameterList>& ); \
346 generateMatrix< SCALAR, LO, GO, NODE> (const Teuchos::RCP<Teuchos::ParameterList>&, \
347 const Teuchos::RCP<const Teuchos::Comm<int> > &, \
348 const Teuchos::RCP< NODE > &,\
349 Teuchos::RCP<CrsMatrix< SCALAR, LO, GO, NODE > >& );
351 #else // !TPETRA_ENABLE_DEPRECATED_CODE
353 #define TPETRA_MATRIXIO_INSTANT(SCALAR,LO,GO,NODE) \
355 readHBMatrix< SCALAR, LO, GO, NODE > (const std::string&, \
356 const Teuchos::RCP<const Teuchos::Comm<int> > &, \
357 Teuchos::RCP<CrsMatrix< SCALAR, LO, GO, NODE > >&, \
358 Teuchos::RCP<const Tpetra::Map< LO, GO, NODE> >, \
359 const Teuchos::RCP<Teuchos::ParameterList>& );
362 #endif // TPETRA_ENABLE_DEPRECATED_CODE
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
size_t global_size_t
Global size_t object.
A parallel distribution of indices over processes.