/usr/include/trilinos/Tpetra_MatrixIO_def.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | #ifndef TPETRA_MATRIX_IO_DEF
#define TPETRA_MATRIX_IO_DEF
#include "Tpetra_CrsMatrix.hpp"
#include "Tpetra_MatrixIO.hpp"
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
Tpetra::Utils::generateMatrix(const Teuchos::RCP<Teuchos::ParameterList> &plist,
const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
const Teuchos::RCP<Node> &node,
Teuchos::RCP< Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> > &A)
{
typedef Teuchos::ScalarTraits<Scalar> ST;
TEST_FOR_EXCEPTION( plist == Teuchos::null, std::runtime_error,
"Tpetra::Utils::generateMatrix(): ParameterList is null.");
TEST_FOR_EXCEPTION( Teuchos::isParameterType<std::string>(*plist,"mat_type") == false, std::runtime_error,
"Tpetra::Utils::generateMatrix(): ParameterList did not contain string parameter ""mat_type"".");
std::string mat_type = plist->get<std::string>("mat_type");
if (mat_type == "Lap3D") {
// 3D Laplacian, grid is a cube with dimension gridSize x gridSize x gridSize
const int gridSize = plist->get<int>("gridSize",100);
const GlobalOrdinal gS2 = (GlobalOrdinal)gridSize*(GlobalOrdinal)gridSize;
const GlobalOrdinal numRows = gS2*(GlobalOrdinal)gridSize;
Teuchos::RCP<Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > rowMap;
rowMap = Teuchos::rcp(new Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node>((global_size_t)numRows,(GlobalOrdinal)0,comm,GloballyDistributed,node));
A = rcp(new Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>(rowMap,7,Tpetra::StaticProfile));
// fill matrix, one row at a time
Teuchos::Array<GlobalOrdinal> neighbors;
Teuchos::Array<Scalar> values(7, -ST::one());
values[0] = (Scalar)6;
for (GlobalOrdinal r = rowMap->getMinGlobalIndex(); r <= rowMap->getMaxGlobalIndex(); ++r) {
neighbors.clear();
neighbors.push_back(r); // add diagonal
GlobalOrdinal ixy, iz, ix, iy; // (x,y,z) coords and index in xy plane
ixy = r%gS2;
iz = (r - ixy)/gS2;
ix = ixy%gridSize;
iy = (ixy - ix)/gridSize;
//
if ( ix != 0 ) neighbors.push_back( r-1 );
if ( ix != gridSize-1 ) neighbors.push_back( r+1 );
if ( iy != 0 ) neighbors.push_back( r-gridSize );
if ( iy != gridSize-1 ) neighbors.push_back( r+gridSize );
if ( iz != 0 ) neighbors.push_back( r-gS2 );
if ( iz != gridSize-1 ) neighbors.push_back( r+gS2 );
A->insertGlobalValues( r, neighbors(), values(0,neighbors.size()) );
}
A->fillComplete(DoOptimizeStorage);
}
else {
TEST_FOR_EXCEPTION( true, std::runtime_error,
"Tpetra::Utils::generateMatrix(): ParameterList specified unsupported ""mat_type"".");
}
}
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
Tpetra::Utils::readHBMatrix(const std::string &filename,
const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
const Teuchos::RCP<Node> &node,
Teuchos::RCP< Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps> > &A,
Teuchos::RCP< const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > rowMap)
{
const int myRank = comm->getRank();
int numRows,numCols,numNZ;
Teuchos::ArrayRCP<Scalar> svals;
Teuchos::ArrayRCP<GlobalOrdinal> colinds;
Teuchos::ArrayRCP<int> rowptrs;
Teuchos::ArrayRCP<size_t> nnzPerRow;
Teuchos::ArrayRCP<char> type;
int fail = 0;
if (myRank == 0) {
bool isSymmetric=false;
Teuchos::ArrayRCP<double> dvals;
Teuchos::ArrayRCP<int> colptrs, rowinds;
std::string type;
Tpetra::Utils::readHBMatDouble(filename,numRows,numCols,numNZ,type,colptrs,rowinds,dvals);
TEST_FOR_EXCEPT(type.size() != 3);
if (type[0] != 'R' && type[0] != 'r') {
// only real matrices right now
fail = 1;
}
if (fail == 0 && numNZ > 0) {
if (type[1] == 'S' || type[1] == 's') {
isSymmetric = true;
}
else {
isSymmetric = false;
}
}
if (fail == 0 && numNZ > 0) {
// find num non-zero per row
nnzPerRow = Teuchos::arcp<size_t>(numRows);
std::fill(nnzPerRow.begin(), nnzPerRow.end(), 0);
for (Teuchos::ArrayRCP<int>::const_iterator ri=rowinds.begin(); ri != rowinds.end(); ++ri) {
// count each row index towards its row
++nnzPerRow[*ri-1];
}
if (isSymmetric) {
// count each column toward the corresponding row as well
for (int c=0; c < numCols; ++c) {
// the diagonal was already counted; neglect it, if it exists
for (int i=colptrs[c]-1; i != colptrs[c+1]-1; ++i) {
if (rowinds[i] != c+1) {
++nnzPerRow[c];
++numNZ;
}
}
}
}
// allocate/set new matrix data
svals = Teuchos::arcp<Scalar>(numNZ);
colinds = Teuchos::arcp<GlobalOrdinal>(numNZ);
rowptrs = Teuchos::arcp<int>(numRows+1);
rowptrs[0] = 0;
#ifdef HAVE_TPETRA_DEBUG
Teuchos::ArrayRCP<size_t> nnzPerRow_debug(nnzPerRow.size());
std::copy(nnzPerRow.begin(), nnzPerRow.end(), nnzPerRow_debug.begin());
#endif
for (int j=1; j <= numRows; ++j) {
rowptrs[j] = rowptrs[j-1] + nnzPerRow[j-1];
nnzPerRow[j-1] = 0;
}
// translate from column-oriented to row-oriented
for (int col=0; col<numCols; ++col) {
for (int i=colptrs[col]-1; i != colptrs[col+1]-1; ++i) {
const int row = rowinds[i]-1;
// add entry to (row,col), with value dvals[i]
const size_t entry = rowptrs[row] + nnzPerRow[row];
svals[entry] = Teuchos::as<Scalar>(dvals[i]);
colinds[entry] = Teuchos::as<GlobalOrdinal>(col);
++nnzPerRow[row];
if (isSymmetric && row != col) {
// add entry to (col,row), with value dvals[i]
const size_t symentry = rowptrs[col] + nnzPerRow[col];
svals[symentry] = Teuchos::as<Scalar>(dvals[i]);
colinds[symentry] = Teuchos::as<GlobalOrdinal>(row);
++nnzPerRow[col];
}
}
}
#ifdef HAVE_TPETRA_DEBUG
{
bool isequal = true;
Teuchos::ArrayRCP<size_t>::const_iterator it1, it2;
for (it1 = nnzPerRow.begin(), it2 = nnzPerRow_debug.begin(); it1 != nnzPerRow.end(); ++it1, ++it2) {
if (*it1 != *it2) {
isequal = false;
break;
}
}
TEST_FOR_EXCEPTION(!isequal || nnzPerRow.size() != nnzPerRow_debug.size(), std::logic_error,
"Tpetra::Utils::readHBMatrix(): Logic error.");
}
#endif
}
// std::cout << "Matrix " << filename << " of type " << type << ": " << numRows << " by " << numCols << ", " << numNZ << " nonzeros" << std::endl;
}
// check for read errors
broadcast(*comm,0,&fail);
TEST_FOR_EXCEPTION(fail == 1, std::runtime_error, "Tpetra::Utils::readHBMatrix() can only read Real matrices.");
// distribute global matrix info
broadcast(*comm,0,&numRows);
broadcast(*comm,0,&numCols);
// create map with uniform partitioning
if (rowMap == Teuchos::null) {
rowMap = Teuchos::rcp(new Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node>((global_size_t)numRows,(GlobalOrdinal)0,comm,GloballyDistributed,node));
}
else {
TEST_FOR_EXCEPTION( rowMap->getGlobalNumElements() != (global_size_t)numRows, std::runtime_error,
"Tpetra::Utils::readHBMatrix(): specified map has incorrect number of elements.");
TEST_FOR_EXCEPTION( rowMap->isDistributed() == false && comm->getSize() > 1, std::runtime_error,
"Tpetra::Utils::readHBMatrix(): specified map is not distributed.");
}
Teuchos::ArrayRCP<size_t> myNNZ;
if (rowMap->getNodeNumElements()) {
myNNZ = Teuchos::arcp<size_t>(rowMap->getNodeNumElements());
}
if (myRank == 0) {
size_t numRowsAlreadyDistributed = rowMap->getNodeNumElements();
std::copy(nnzPerRow.begin(), nnzPerRow.begin()+numRowsAlreadyDistributed,myNNZ);
for (int p=1; p < Teuchos::size(*comm); ++p) {
size_t numRowsForP;
Teuchos::receive(*comm,p,&numRowsForP);
Teuchos::send<int,size_t>(*comm,numRowsForP,nnzPerRow(numRowsAlreadyDistributed,numRowsForP).getRawPtr(),p);
numRowsAlreadyDistributed += numRowsForP;
}
}
else {
const size_t numMyRows = rowMap->getNodeNumElements();
Teuchos::send(*comm,numMyRows,0);
Teuchos::receive<int,size_t>(*comm,0,numMyRows,myNNZ(0,numMyRows).getRawPtr());
}
nnzPerRow = Teuchos::null;
// create column map
Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > domMap;
if (numRows == numCols) {
domMap = rowMap;
}
else {
domMap = createUniformContigMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numCols,comm,node);
}
A = rcp(new Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>(rowMap,myNNZ,Tpetra::StaticProfile));
// free this locally, A will keep it allocated as long as it is needed by A (up until allocation of nonzeros)
myNNZ = Teuchos::null;
if (myRank == 0 && numNZ > 0) {
for (int r=0; r < numRows; ++r) {
const size_t nnz = rowptrs[r+1] - rowptrs[r];
if (nnz > 0) {
Teuchos::ArrayView<const GlobalOrdinal> inds = colinds(rowptrs[r],nnz);
Teuchos::ArrayView<const Scalar> vals = svals( rowptrs[r],nnz);
A->insertGlobalValues(r, inds, vals);
}
}
}
// don't need these anymore
colinds = Teuchos::null;
svals = Teuchos::null;
rowptrs = Teuchos::null;
A->fillComplete(domMap,rowMap,Tpetra::DoOptimizeStorage);
}
//
// Explicit instantiation macro
//
// Must be expanded from within the Tpetra::Utils namespace!
//
#define TPETRA_MATRIXIO_INSTANT(SCALAR,LO,GO,NODE) \
template \
void \
readHBMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps>( \
const std::string &, const Teuchos::RCP<const Teuchos::Comm<int> > &, const Teuchos::RCP<NODE > &, \
Teuchos::RCP< CrsMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps > > &, \
Teuchos::RCP< const Tpetra::Map<LO,GO,NODE> >); \
\
template \
void \
generateMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps>( \
const Teuchos::RCP<Teuchos::ParameterList> &plist, const Teuchos::RCP<const Teuchos::Comm<int> > &, const Teuchos::RCP<NODE > &, \
Teuchos::RCP< CrsMatrix<SCALAR,LO,GO,NODE,Kokkos::DefaultKernels<SCALAR,LO,NODE>::SparseOps > > &);
#endif
|