43 #ifndef IFPACK2_SPARSECONTAINER_DEF_HPP
44 #define IFPACK2_SPARSECONTAINER_DEF_HPP
46 #include "Ifpack2_SparseContainer_decl.hpp"
49 #include "Teuchos_DefaultMpiComm.hpp"
51 #include "Teuchos_DefaultSerialComm.hpp"
57 template<
class MatrixType,
class InverseType>
58 SparseContainer<MatrixType,InverseType>::SparseContainer(
const size_t NumRows,
const size_t NumVectors) :
60 NumVectors_(NumVectors),
61 IsInitialized_(false),
72 template<
class MatrixType,
class InverseType>
73 SparseContainer<MatrixType,InverseType>::SparseContainer(
const SparseContainer<MatrixType,InverseType>& rhs)
79 template<
class MatrixType,
class InverseType>
80 SparseContainer<MatrixType,InverseType>::~SparseContainer()
86 template<
class MatrixType,
class InverseType>
87 size_t SparseContainer<MatrixType,InverseType>::getNumRows()
const
89 if (isInitialized())
return NumRows_;
95 template<
class MatrixType,
class InverseType>
96 void SparseContainer<MatrixType,InverseType>::setNumVectors(
const size_t NumVectors_in)
98 TEUCHOS_TEST_FOR_EXCEPTION(NumVectors_in<=0, std::runtime_error,
"Ifpack2::SparseContainer::setNumVectors NumVectors cannot be negative.");
101 if(!IsInitialized_ || NumVectors_!=NumVectors_in) {
102 NumVectors_=NumVectors_in;
103 X_ =
Teuchos::rcp(
new Tpetra::MultiVector<InverseScalar,InverseLocalOrdinal,InverseGlobalOrdinal,InverseNode>(Map_,NumVectors_) );
104 Y_ =
Teuchos::rcp(
new Tpetra::MultiVector<InverseScalar,InverseLocalOrdinal,InverseGlobalOrdinal,InverseNode>(Map_,NumVectors_) );
111 template<
class MatrixType,
class InverseType>
112 typename MatrixType::local_ordinal_type & SparseContainer<MatrixType,InverseType>::ID(
const size_t i)
119 template<
class MatrixType,
class InverseType>
120 bool SparseContainer<MatrixType,InverseType>::isInitialized()
const
122 return IsInitialized_;
127 template<
class MatrixType,
class InverseType>
128 bool SparseContainer<MatrixType,InverseType>::isComputed()
const
135 template<
class MatrixType,
class InverseType>
142 template<
class MatrixType,
class InverseType>
143 void SparseContainer<MatrixType,InverseType>::initialize()
145 if(IsInitialized_) destroy();
146 IsInitialized_=
false;
149 Map_ =
Teuchos::rcp(
new Tpetra::Map<InverseLocalOrdinal,InverseGlobalOrdinal,InverseNode>(NumRows_,0,LocalComm_) );
150 Matrix_ =
Teuchos::rcp(
new Tpetra::CrsMatrix<InverseScalar,InverseLocalOrdinal,InverseGlobalOrdinal,InverseNode>(Map_,0) );
151 GID_.resize(NumRows_);
152 setNumVectors(NumVectors_);
156 TEUCHOS_TEST_FOR_EXCEPTION( Inverse_ == Teuchos::null, std::runtime_error,
"Ifpack2::SparseContainer::initialize error in inverse constructor.");
157 Inverse_->setParameters(List_);
162 IsInitialized_ =
true;
167 template<
class MatrixType,
class InverseType>
168 void SparseContainer<MatrixType,InverseType>::compute(
const Teuchos::RCP<
const Tpetra::RowMatrix<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode> >& Matrix)
171 TEUCHOS_TEST_FOR_EXCEPTION( !IsInitialized_, std::runtime_error,
"Ifpack2::SparseContainer::compute please call initialize first.");
177 Inverse_->initialize();
184 template<
class MatrixType,
class InverseType>
185 void SparseContainer<MatrixType,InverseType>::apply(
const Tpetra::MultiVector<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode>& X,
186 Tpetra::MultiVector<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode>& Y,
191 TEUCHOS_TEST_FOR_EXCEPTION( !IsComputed_, std::runtime_error,
"Ifpack2::SparseContainer::apply compute has not been called.");
192 TEUCHOS_TEST_FOR_EXCEPTION( X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
"Ifpack2::SparseContainer::apply X/Y have different numbers of vectors.");
195 setNumVectors(X.getNumVectors());
204 for(
size_t j=0; j < NumRows_; j++){
205 MatrixLocalOrdinal LID = ID(j);
206 for (
size_t k = 0 ; k < NumVectors_ ; k++)
207 local_x_ptr[k][j] = global_x_ptr[k][LID];
211 Inverse_->apply(*X_, *Y_);
214 for (
size_t j = 0 ; j < NumRows_ ; j++) {
215 MatrixLocalOrdinal LID = ID(j);
216 for (
size_t k = 0 ; k < NumVectors_ ; k++)
217 global_y_ptr[k][LID] = alpha * local_y_ptr[k][j] + beta * global_y_ptr[k][LID];
225 template<
class MatrixType,
class InverseType>
226 void SparseContainer<MatrixType,InverseType>::weightedApply(
const Tpetra::MultiVector<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode>& X,
227 Tpetra::MultiVector<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode>& Y,
228 const Tpetra::Vector<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode>& D,
233 TEUCHOS_TEST_FOR_EXCEPTION( !IsComputed_, std::runtime_error,
"Ifpack2::SparseContainer::apply compute has not been called.");
234 TEUCHOS_TEST_FOR_EXCEPTION( X.getNumVectors() != Y.getNumVectors(), std::runtime_error,
"Ifpack2::SparseContainer::apply X/Y have different numbers of vectors.");
237 setNumVectors(X.getNumVectors());
247 for(
size_t j=0; j < NumRows_; j++){
248 MatrixLocalOrdinal LID = ID(j);
249 for (
size_t k = 0 ; k < NumVectors_ ; k++)
250 local_x_ptr[k][j] = global_d_ptr[LID] * global_x_ptr[k][LID];
254 Inverse_->apply(*X_, *Y_);
257 for (
size_t j = 0 ; j < NumRows_ ; j++) {
258 MatrixLocalOrdinal LID = ID(j);
259 for (
size_t k = 0 ; k < NumVectors_ ; k++)
260 global_y_ptr[k][LID] = alpha * global_d_ptr[LID] * local_y_ptr[k][j] + beta * global_y_ptr[k][LID];
266 template<
class MatrixType,
class InverseType>
267 void SparseContainer<MatrixType,InverseType>::destroy()
269 IsInitialized_ =
false;
275 template<
class MatrixType,
class InverseType>
276 std::ostream& SparseContainer<MatrixType,InverseType>::print(std::ostream& os)
const
279 fos.setOutputToRootOnly(0);
286 template<
class MatrixType,
class InverseType>
287 std::string SparseContainer<MatrixType,InverseType>::description()
const
289 std::ostringstream oss;
291 if (isInitialized()) {
293 oss <<
"{status = initialized, computed";
296 oss <<
"{status = initialized, not computed";
300 oss <<
"{status = not initialized, not computed";
308 template<
class MatrixType,
class InverseType>
313 os <<
"================================================================================" << endl;
314 os <<
"Ifpack2_SparseContainer" << endl;
315 os <<
"Number of rows = " << NumRows_ << endl;
316 os <<
"Number of vectors = " << NumVectors_ << endl;
317 os <<
"isInitialized() = " << IsInitialized_ << endl;
318 os <<
"isComputed() = " << IsComputed_ << endl;
319 os <<
"================================================================================" << endl;
325 template<
class MatrixType,
class InverseType>
326 void SparseContainer<MatrixType,InverseType>::extract(
const Teuchos::RCP<
const Tpetra::RowMatrix<MatrixScalar,MatrixLocalOrdinal,MatrixGlobalOrdinal,MatrixNode> >& Matrix_in)
328 size_t MatrixInNumRows= Matrix_in->getNodeNumRows();
331 for (
size_t j = 0 ; j < NumRows_ ; ++j) {
332 TEUCHOS_TEST_FOR_EXCEPTION( GID_[j] < 0 || (
size_t) GID_[j] >= MatrixInNumRows, std::runtime_error,
"Ifpack2::SparseContainer::applyInverse compute has not been called.");
335 int Length = Matrix_in->getNodeMaxNumRowEntries();
343 Values_insert.
resize(Length);
344 Indices_insert.
resize(Length);
346 for (
size_t j = 0 ; j < NumRows_ ; ++j) {
347 MatrixLocalOrdinal LRID = ID(j);
350 Matrix_in->getLocalRowCopy(LRID,Indices(),Values(),NumEntries);
352 size_t num_entries_found=0;
353 for (
size_t k = 0 ; k < NumEntries ; ++k) {
354 MatrixLocalOrdinal LCID = Indices[k];
357 if ((
size_t)LCID >= MatrixInNumRows)
363 InverseLocalOrdinal jj = -1;
364 for (
size_t kk = 0 ; kk < NumRows_ ; ++kk)
369 Indices_insert[num_entries_found] = Map_->getGlobalElement(jj);
370 Values_insert[num_entries_found] = Values[k];
375 Matrix_->insertGlobalValues(j,Indices_insert(0,num_entries_found),Values_insert(0,num_entries_found));
378 Matrix_->fillComplete();
383 #endif // IFPACK2_SPARSECONTAINER_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual std::string description() const
void resize(size_type new_size, const value_type &x=value_type())