44 #ifndef TPETRA_MATRIX_IO_DEF 45 #define TPETRA_MATRIX_IO_DEF 47 #include "Tpetra_CrsMatrix.hpp" 48 #include "Tpetra_MatrixIO.hpp" 51 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
61 "Tpetra::Utils::generateMatrix(): ParameterList is null.");
63 "Tpetra::Utils::generateMatrix(): ParameterList did not contain string parameter ""mat_type"".");
64 std::string mat_type = plist->
get<std::string>(
"mat_type");
65 if (mat_type ==
"Lap3D") {
67 const GlobalOrdinal gridSize = as<GlobalOrdinal>(plist->
get<
int>(
"gridSize",100));
68 const GlobalOrdinal gS2 = gridSize*gridSize;
69 const GlobalOrdinal numRows = gS2*gridSize;
76 values[0] = (Scalar)6;
77 for (GlobalOrdinal r = rowMap->getMinGlobalIndex(); r <= rowMap->getMaxGlobalIndex(); ++r) {
80 GlobalOrdinal ixy, iz, ix, iy;
84 iy = (ixy - ix)/gridSize;
86 if ( ix != 0 ) neighbors.
push_back( r-1 );
87 if ( ix != gridSize-1 ) neighbors.
push_back( r+1 );
88 if ( iy != 0 ) neighbors.
push_back( r-gridSize );
89 if ( iy != gridSize-1 ) neighbors.
push_back( r+gridSize );
90 if ( iz != 0 ) neighbors.
push_back( r-gS2 );
91 if ( iz != gridSize-1 ) neighbors.
push_back( r+gS2 );
92 A->insertGlobalValues( r, neighbors(), values(0,neighbors.
size()) );
98 "Tpetra::Utils::generateMatrix(): ParameterList specified unsupported ""mat_type"".");
103 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node,
class LocalMatOps>
105 Tpetra::Utils::readHBMatrix(
const std::string &filename,
112 const int myRank = comm->getRank();
113 int numRows,numCols,numNZ;
120 bool isSymmetric=
false;
124 Tpetra::Utils::readHBMatDouble(filename,numRows,numCols,numNZ,type,colptrs,rowinds,dvals);
126 if (type[0] !=
'R' && type[0] !=
'r') {
130 if (fail == 0 && numNZ > 0) {
131 if (type[1] ==
'S' || type[1] ==
's') {
138 if (fail == 0 && numNZ > 0) {
140 nnzPerRow = Teuchos::arcp<size_t>(numRows);
141 std::fill(nnzPerRow.
begin(), nnzPerRow.
end(), 0);
148 for (
int c=0; c < numCols; ++c) {
150 for (
int i=colptrs[c]-1; i != colptrs[c+1]-1; ++i) {
151 if (rowinds[i] != c+1) {
159 svals = Teuchos::arcp<Scalar>(numNZ);
160 colinds = Teuchos::arcp<GlobalOrdinal>(numNZ);
161 rowptrs = Teuchos::arcp<int>(numRows+1);
163 #ifdef HAVE_TPETRA_DEBUG 165 std::copy(nnzPerRow.
begin(), nnzPerRow.
end(), nnzPerRow_debug.begin());
167 for (
int j=1; j <= numRows; ++j) {
168 rowptrs[j] = rowptrs[j-1] + nnzPerRow[j-1];
172 for (
int col=0; col<numCols; ++col) {
173 for (
int i=colptrs[col]-1; i != colptrs[col+1]-1; ++i) {
174 const int row = rowinds[i]-1;
176 const size_t entry = rowptrs[row] + nnzPerRow[row];
177 svals[entry] = Teuchos::as<Scalar>(dvals[i]);
178 colinds[entry] = Teuchos::as<GlobalOrdinal>(col);
180 if (isSymmetric && row != col) {
182 const size_t symentry = rowptrs[col] + nnzPerRow[col];
183 svals[symentry] = Teuchos::as<Scalar>(dvals[i]);
184 colinds[symentry] = Teuchos::as<GlobalOrdinal>(row);
189 #ifdef HAVE_TPETRA_DEBUG 193 for (it1 = nnzPerRow.
begin(), it2 = nnzPerRow_debug.
begin(); it1 != nnzPerRow.
end(); ++it1, ++it2) {
200 "Tpetra::Utils::readHBMatrix(): Logic error.");
207 broadcast(*comm,0,&fail);
210 broadcast(*comm,0,&numRows);
211 broadcast(*comm,0,&numCols);
213 if (rowMap == Teuchos::null) {
218 "Tpetra::Utils::readHBMatrix(): specified map has incorrect number of elements.");
220 "Tpetra::Utils::readHBMatrix(): specified map is not distributed.");
223 if (rowMap->getNodeNumElements()) {
224 myNNZ = Teuchos::arcp<size_t>(rowMap->getNodeNumElements());
227 LocalOrdinal numRowsAlreadyDistributed = rowMap->getNodeNumElements();
228 std::copy(nnzPerRow.
begin(), nnzPerRow.
begin()+numRowsAlreadyDistributed,myNNZ);
229 for (
int p=1; p < Teuchos::size(*comm); ++p) {
231 Teuchos::receive(*comm,p,&numRowsForP);
233 Teuchos::send<int,size_t>(*comm,numRowsForP,nnzPerRow(numRowsAlreadyDistributed,numRowsForP).
getRawPtr(),p);
234 numRowsAlreadyDistributed += numRowsForP;
239 const size_t numMyRows = rowMap->getNodeNumElements();
242 Teuchos::receive<int,size_t>(*comm,0,numMyRows,myNNZ(0,numMyRows).getRawPtr());
245 nnzPerRow = Teuchos::null;
248 if (numRows == numCols) {
252 domMap = createUniformContigMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numCols,comm,node);
256 myNNZ = Teuchos::null;
257 if (myRank == 0 && numNZ > 0) {
258 for (
int r=0; r < numRows; ++r) {
259 const LocalOrdinal nnz = rowptrs[r+1] - rowptrs[r];
263 A->insertGlobalValues(r, inds, vals);
268 colinds = Teuchos::null;
269 svals = Teuchos::null;
270 rowptrs = Teuchos::null;
271 A->fillComplete(domMap,rowMap,params);
282 #define TPETRA_MATRIXIO_INSTANT(SCALAR,LO,GO,NODE) \ 285 readHBMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps>( \ 286 const std::string &, const Teuchos::RCP<const Teuchos::Comm<int> > &, const Teuchos::RCP<NODE > &, \ 287 Teuchos::RCP< CrsMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps > > &, \ 288 Teuchos::RCP< const Tpetra::Map<LO,GO,NODE> >, \ 289 const Teuchos::RCP< Teuchos::ParameterList > & ); \ 293 generateMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps>( \ 294 const Teuchos::RCP<Teuchos::ParameterList> &plist, const Teuchos::RCP<const Teuchos::Comm<int> > &, const Teuchos::RCP<NODE > &, \ 295 Teuchos::RCP< CrsMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps > > &);
Sparse matrix that presents a compressed sparse row interface.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_t global_size_t
Global size_t object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
void push_back(const value_type &x)
Describes a parallel distribution of objects over processes.
TypeTo as(const TypeFrom &t)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)