Stratimikos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosThyraAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stratimikos: Thyra-based strategies for linear solvers
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
52 #ifndef BELOS_THYRA_ADAPTER_HPP
53 #define BELOS_THYRA_ADAPTER_HPP
54 
55 #include "BelosConfigDefs.hpp"
56 #include "BelosMultiVecTraits.hpp"
57 #include "BelosOperatorTraits.hpp"
58 
59 #include <Thyra_DetachedMultiVectorView.hpp>
60 #include <Thyra_MultiVectorBase.hpp>
61 #include <Thyra_MultiVectorStdOps.hpp>
62 #ifdef HAVE_BELOS_TSQR
63 # include <Thyra_TsqrAdaptor.hpp>
64 #endif // HAVE_BELOS_TSQR
65 
66 namespace Belos {
67 
69  //
70  // Implementation of the Belos::MultiVecTraits for Thyra::MultiVectorBase
71  //
73 
80  template<class ScalarType>
81  class MultiVecTraits< ScalarType, Thyra::MultiVectorBase<ScalarType> >
82  {
83  private:
84  typedef Thyra::MultiVectorBase<ScalarType> TMVB;
86  typedef typename ST::magnitudeType magType;
87 
88  public:
89 
92 
97  static Teuchos::RCP<TMVB> Clone( const TMVB& mv, const int numvecs )
98  {
99  Teuchos::RCP<TMVB> c = Thyra::createMembers( mv.range(), numvecs );
100  return c;
101  }
102 
107  static Teuchos::RCP<TMVB> CloneCopy( const TMVB& mv )
108  {
109  int numvecs = mv.domain()->dim();
110  // create the new multivector
111  Teuchos::RCP< TMVB > cc = Thyra::createMembers( mv.range(), numvecs );
112  // copy the data from the source multivector to the new multivector
113  Thyra::assign(cc.ptr(), mv);
114  return cc;
115  }
116 
122  static Teuchos::RCP<TMVB> CloneCopy( const TMVB& mv, const std::vector<int>& index )
123  {
124  int numvecs = index.size();
125  // create the new multivector
126  Teuchos::RCP<TMVB> cc = Thyra::createMembers( mv.range(), numvecs );
127  // create a view to the relevant part of the source multivector
128  Teuchos::RCP<const TMVB> view = mv.subView(index);
129  // copy the data from the relevant view to the new multivector
130  Thyra::assign(cc.ptr(), *view);
131  return cc;
132  }
133 
134  static Teuchos::RCP<TMVB>
135  CloneCopy (const TMVB& mv, const Teuchos::Range1D& index)
136  {
137  const int numVecs = index.size();
138  // Create the new multivector
139  Teuchos::RCP<TMVB> cc = Thyra::createMembers (mv.range(), numVecs);
140  // Create a view to the relevant part of the source multivector
141  Teuchos::RCP<const TMVB> view = mv.subView (index);
142  // Copy the data from the view to the new multivector.
143  Thyra::assign (cc.ptr(), *view);
144  return cc;
145  }
146 
152  static Teuchos::RCP<TMVB> CloneViewNonConst( TMVB& mv, const std::vector<int>& index )
153  {
154  int numvecs = index.size();
155 
156  // We do not assume that the indices are sorted, nor do we check that
157  // index.size() > 0. This code is fail-safe, in the sense that a zero
158  // length index std::vector will pass the error on the Thyra.
159 
160  // Thyra has two ways to create an indexed View:
161  // * contiguous (via a range of columns)
162  // * indexed (via a std::vector of column indices)
163  // The former is significantly more efficient than the latter, in terms of
164  // computations performed with/against the created view.
165  // We will therefore check to see if the given indices are contiguous, and
166  // if so, we will use the contiguous view creation method.
167 
168  int lb = index[0];
169  bool contig = true;
170  for (int i=0; i<numvecs; i++) {
171  if (lb+i != index[i]) contig = false;
172  }
173 
175  if (contig) {
176  const Thyra::Range1D rng(lb,lb+numvecs-1);
177  // create a contiguous view to the relevant part of the source multivector
178  cc = mv.subView(rng);
179  }
180  else {
181  // create an indexed view to the relevant part of the source multivector
182  cc = mv.subView(index);
183  }
184  return cc;
185  }
186 
187  static Teuchos::RCP<TMVB>
189  {
190  // We let Thyra be responsible for checking that the index range
191  // is nonempty.
192  //
193  // Create and return a contiguous view to the relevant part of
194  // the source multivector.
195  return mv.subView (index);
196  }
197 
198 
204  static Teuchos::RCP<const TMVB> CloneView( const TMVB& mv, const std::vector<int>& index )
205  {
206  int numvecs = index.size();
207 
208  // We do not assume that the indices are sorted, nor do we check that
209  // index.size() > 0. This code is fail-safe, in the sense that a zero
210  // length index std::vector will pass the error on the Thyra.
211 
212  // Thyra has two ways to create an indexed View:
213  // * contiguous (via a range of columns)
214  // * indexed (via a std::vector of column indices)
215  // The former is significantly more efficient than the latter, in terms of
216  // computations performed with/against the created view.
217  // We will therefore check to see if the given indices are contiguous, and
218  // if so, we will use the contiguous view creation method.
219 
220  int lb = index[0];
221  bool contig = true;
222  for (int i=0; i<numvecs; i++) {
223  if (lb+i != index[i]) contig = false;
224  }
225 
227  if (contig) {
228  const Thyra::Range1D rng(lb,lb+numvecs-1);
229  // create a contiguous view to the relevant part of the source multivector
230  cc = mv.subView(rng);
231  }
232  else {
233  // create an indexed view to the relevant part of the source multivector
234  cc = mv.subView(index);
235  }
236  return cc;
237  }
238 
240  CloneView (const TMVB& mv, const Teuchos::Range1D& index)
241  {
242  // We let Thyra be responsible for checking that the index range
243  // is nonempty.
244  //
245  // Create and return a contiguous view to the relevant part of
246  // the source multivector.
247  return mv.subView (index);
248  }
249 
251 
254 
256  static int GetVecLength( const TMVB& mv )
257  { return mv.range()->dim(); }
258 
260  static int GetNumberVecs( const TMVB& mv )
261  { return mv.domain()->dim(); }
262 
264 
267 
270  static void MvTimesMatAddMv( const ScalarType alpha, const TMVB& A,
272  const ScalarType beta, TMVB& mv )
273  {
274  using Teuchos::arrayView; using Teuchos::arcpFromArrayView;
275  const int m = B.numRows();
276  const int n = B.numCols();
277  // Create a view of the B object!
279  B_thyra = Thyra::createMembersView(
280  A.domain(),
282  0, m, 0, n,
283  arcpFromArrayView(arrayView(&B(0,0), B.stride()*B.numCols())), B.stride()
284  )
285  );
286  // perform the operation via A: mv <- alpha*A*B_thyra + beta*mv
287  Thyra::apply<ScalarType>(A, Thyra::NOTRANS, *B_thyra, Teuchos::outArg(mv), alpha, beta);
288  }
289 
292  static void MvAddMv( const ScalarType alpha, const TMVB& A,
293  const ScalarType beta, const TMVB& B, TMVB& mv )
294  {
296  using Teuchos::tuple; using Teuchos::ptrInArg; using Teuchos::inoutArg;
297 
298  Thyra::linear_combination<ScalarType>(
299  tuple(alpha, beta)(), tuple(ptrInArg(A), ptrInArg(B))(), ST::zero(), inoutArg(mv));
300  }
301 
304  static void MvScale ( TMVB& mv, const ScalarType alpha )
305  { Thyra::scale(alpha, Teuchos::inoutArg(mv)); }
306 
309  static void MvScale (TMVB& mv, const std::vector<ScalarType>& alpha)
310  {
311  for (unsigned int i=0; i<alpha.size(); i++) {
312  Thyra::scale<ScalarType> (alpha[i], mv.col(i).ptr());
313  }
314  }
315 
318  static void MvTransMv( const ScalarType alpha, const TMVB& A, const TMVB& mv,
320  {
321  using Teuchos::arrayView; using Teuchos::arcpFromArrayView;
322  // Create a multivector to hold the result (m by n)
323  int m = A.domain()->dim();
324  int n = mv.domain()->dim();
325  // Create a view of the B object!
327  B_thyra = Thyra::createMembersView(
328  A.domain(),
330  0, m, 0, n,
331  arcpFromArrayView(arrayView(&B(0,0), B.stride()*B.numCols())), B.stride()
332  )
333  );
334  Thyra::apply<ScalarType>(A, Thyra::CONJTRANS, mv, B_thyra.ptr(), alpha);
335  }
336 
340  static void MvDot( const TMVB& mv, const TMVB& A, std::vector<ScalarType>& b )
341  { Thyra::dots(mv, A, Teuchos::arrayViewFromVector(b)); }
342 
344 
347 
351  static void MvNorm( const TMVB& mv, std::vector<magType>& normvec,
352  NormType type = TwoNorm )
353  { Thyra::norms_2(mv, Teuchos::arrayViewFromVector(normvec)); }
354 
356 
359 
362  static void SetBlock( const TMVB& A, const std::vector<int>& index, TMVB& mv )
363  {
364  // Extract the "numvecs" columns of mv indicated by the index std::vector.
365  int numvecs = index.size();
366  std::vector<int> indexA(numvecs);
367  int numAcols = A.domain()->dim();
368  for (int i=0; i<numvecs; i++) {
369  indexA[i] = i;
370  }
371  // Thyra::assign requires that both arguments have the same number of
372  // vectors. Enforce this, by shrinking one to match the other.
373  if ( numAcols < numvecs ) {
374  // A does not have enough columns to satisfy index_plus. Shrink
375  // index_plus.
376  numvecs = numAcols;
377  }
378  else if ( numAcols > numvecs ) {
379  numAcols = numvecs;
380  indexA.resize( numAcols );
381  }
382  // create a view to the relevant part of the source multivector
383  Teuchos::RCP< const TMVB > relsource = A.subView(indexA);
384  // create a view to the relevant part of the destination multivector
385  Teuchos::RCP< TMVB > reldest = mv.subView(index);
386  // copy the data to the destination multivector subview
387  Thyra::assign(reldest.ptr(), *relsource);
388  }
389 
390  static void
391  SetBlock (const TMVB& A, const Teuchos::Range1D& index, TMVB& mv)
392  {
393  const int numColsA = A.domain()->dim();
394  const int numColsMv = mv.domain()->dim();
395  // 'index' indexes into mv; it's the index set of the target.
396  const bool validIndex = index.lbound() >= 0 && index.ubound() < numColsMv;
397  // We can't take more columns out of A than A has.
398  const bool validSource = index.size() <= numColsA;
399 
400  if (! validIndex || ! validSource)
401  {
402  std::ostringstream os;
403  os << "Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar> "
404  ">::SetBlock(A, [" << index.lbound() << ", " << index.ubound()
405  << "], mv): ";
406  TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
407  os.str() << "Range lower bound must be nonnegative.");
408  TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numColsMv, std::invalid_argument,
409  os.str() << "Range upper bound must be less than "
410  "the number of columns " << numColsA << " in the "
411  "'mv' output argument.");
412  TEUCHOS_TEST_FOR_EXCEPTION(index.size() > numColsA, std::invalid_argument,
413  os.str() << "Range must have no more elements than"
414  " the number of columns " << numColsA << " in the "
415  "'A' input argument.");
416  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!");
417  }
418 
419  // View of the relevant column(s) of the target multivector mv.
420  // We avoid view creation overhead by only creating a view if
421  // the index range is different than [0, (# columns in mv) - 1].
422  Teuchos::RCP<TMVB> mv_view;
423  if (index.lbound() == 0 && index.ubound()+1 == numColsMv)
424  mv_view = Teuchos::rcpFromRef (mv); // Non-const, non-owning RCP
425  else
426  mv_view = mv.subView (index);
427 
428  // View of the relevant column(s) of the source multivector A.
429  // If A has fewer columns than mv_view, then create a view of
430  // the first index.size() columns of A.
432  if (index.size() == numColsA)
433  A_view = Teuchos::rcpFromRef (A); // Const, non-owning RCP
434  else
435  A_view = A.subView (Teuchos::Range1D(0, index.size()-1));
436 
437  // Copy the data to the destination multivector.
438  Thyra::assign(mv_view.ptr(), *A_view);
439  }
440 
441  static void
442  Assign (const TMVB& A, TMVB& mv)
443  {
444  const int numColsA = A.domain()->dim();
445  const int numColsMv = mv.domain()->dim();
446  if (numColsA > numColsMv)
447  {
448  std::ostringstream os;
449  os << "Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar>"
450  " >::Assign(A, mv): ";
451  TEUCHOS_TEST_FOR_EXCEPTION(numColsA > numColsMv, std::invalid_argument,
452  os.str() << "Input multivector 'A' has "
453  << numColsA << " columns, but output multivector "
454  "'mv' has only " << numColsMv << " columns.");
455  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!");
456  }
457  // Copy the data to the destination multivector.
458  if (numColsA == numColsMv) {
459  Thyra::assign (Teuchos::outArg (mv), A);
460  } else {
461  Teuchos::RCP<TMVB> mv_view =
462  CloneViewNonConst (mv, Teuchos::Range1D(0, numColsA-1));
463  Thyra::assign (mv_view.ptr(), A);
464  }
465  }
466 
469  static void MvRandom( TMVB& mv )
470  {
471  // Thyra::randomize generates via a uniform distribution on [l,u]
472  // We will use this to generate on [-1,1]
473  Thyra::randomize<ScalarType>(
476  Teuchos::outArg(mv));
477  }
478 
480  static void
481  MvInit (TMVB& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero())
482  {
483  Thyra::assign (Teuchos::outArg (mv), alpha);
484  }
485 
487 
490 
493  static void MvPrint( const TMVB& mv, std::ostream& os )
494  { os << describe(mv,Teuchos::VERB_EXTREME); }
495 
497 
498 #ifdef HAVE_BELOS_TSQR
499  typedef Thyra::TsqrAdaptor< ScalarType > tsqr_adaptor_type;
505 #endif // HAVE_BELOS_TSQR
506  };
507 
509  //
510  // Implementation of the Belos::OperatorTraits for Thyra::LinearOpBase
511  //
513 
521  template<class ScalarType>
522  class OperatorTraits <ScalarType,
523  Thyra::MultiVectorBase<ScalarType>,
524  Thyra::LinearOpBase<ScalarType> >
525  {
526  private:
527  typedef Thyra::MultiVectorBase<ScalarType> TMVB;
528  typedef Thyra::LinearOpBase<ScalarType> TLOB;
529 
530  public:
546  static void
547  Apply (const TLOB& Op,
548  const TMVB& x,
549  TMVB& y,
550  ETrans trans = NOTRANS)
551  {
552  Thyra::EOpTransp whichOp;
553 
554  // We don't check here whether the operator implements the
555  // requested operation. Call HasApplyTranspose() to check.
556  // Thyra::LinearOpBase implementations are not required to
557  // implement NOTRANS. However, Belos needs NOTRANS
558  // (obviously!), so we assume that Op implements NOTRANS.
559  if (trans == NOTRANS)
560  whichOp = Thyra::NOTRANS;
561  else if (trans == TRANS)
562  whichOp = Thyra::TRANS;
563  else if (trans == CONJTRANS)
564  whichOp = Thyra::CONJTRANS;
565  else
566  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
567  "Belos::OperatorTraits::Apply (Thyra specialization): "
568  "'trans' argument must be neither NOTRANS=" << NOTRANS
569  << ", TRANS=" << TRANS << ", or CONJTRANS=" << CONJTRANS
570  << ", but instead has an invalid value of " << trans << ".");
571  Thyra::apply<ScalarType>(Op, whichOp, x, Teuchos::outArg(y));
572  }
573 
575  static bool HasApplyTranspose (const TLOB& Op)
576  {
578 
579  // Thyra::LinearOpBase's interface lets you check whether the
580  // operator implements any of all four possible combinations of
581  // conjugation and transpose. Belos only needs transpose
582  // (TRANS) if the operator is real; in that case, Apply() does
583  // the same thing with trans = CONJTRANS or TRANS. If the
584  // operator is complex, Belos needs both transpose and conjugate
585  // transpose (CONJTRANS) if the operator is complex.
586  return Op.opSupported (Thyra::TRANS) &&
587  (! STS::isComplex || Op.opSupported (Thyra::CONJTRANS));
588  }
589  };
590 
591 } // end of Belos namespace
592 
593 #endif
594 // end of file BELOS_THYRA_ADAPTER_HPP
static int GetNumberVecs(const TMVB &mv)
Obtain the number of vectors in mv.
static void MvPrint(const TMVB &mv, std::ostream &os)
Print the mv multi-std::vector to the os output stream.
Stub adaptor from Thyra::MultiVectorBase to TSQR.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv, const std::vector< int > &index)
Creates a new MultiVectorBase and copies the selected contents of mv into the new std::vector (deep c...
static void MvTransMv(const ScalarType alpha, const TMVB &A, const TMVB &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static void MvScale(TMVB &mv, const std::vector< ScalarType > &alpha)
Scale each element of the i-th vector in *this with alpha[i].
static void MvDot(const TMVB &mv, const TMVB &A, std::vector< ScalarType > &b)
Compute a std::vector b where the components are the individual dot-products of the i-th columns of A...
Ordinal ubound() const
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv)
Creates a new MultiVectorBase and copies contents of mv into the new std::vector (deep copy)...
static void MvScale(TMVB &mv, const ScalarType alpha)
Scale each element of the vectors in *this with alpha.
static void MvTimesMatAddMv(const ScalarType alpha, const TMVB &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, TMVB &mv)
Update mv with .
Ptr< T > ptr() const
static Teuchos::RCP< const TMVB > CloneView(const TMVB &mv, const std::vector< int > &index)
Creates a new const MultiVectorBase that shares the selected contents of mv (shallow copy)...
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv, const Teuchos::Range1D &index)
static void Apply(const TLOB &Op, const TMVB &x, TMVB &y, ETrans trans=NOTRANS)
Apply Op to x, storing the result in y.
static void MvRandom(TMVB &mv)
Replace the vectors in mv with random vectors.
static int GetVecLength(const TMVB &mv)
Obtain the std::vector length of mv.
static Teuchos::RCP< TMVB > CloneViewNonConst(TMVB &mv, const Teuchos::Range1D &index)
Ordinal lbound() const
OrdinalType numCols() const
static void SetBlock(const TMVB &A, const std::vector< int > &index, TMVB &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
static void SetBlock(const TMVB &A, const Teuchos::Range1D &index, TMVB &mv)
Ordinal size() const
static void MvNorm(const TMVB &mv, std::vector< magType > &normvec, NormType type=TwoNorm)
Compute the 2-norm of each individual std::vector of mv. Upon return, normvec[i] holds the value of ...
static bool HasApplyTranspose(const TLOB &Op)
Whether the operator implements applying the transpose.
static Teuchos::RCP< TMVB > CloneViewNonConst(TMVB &mv, const std::vector< int > &index)
Creates a new MultiVectorBase that shares the selected contents of mv (shallow copy).
static Teuchos::RCP< const TMVB > CloneView(const TMVB &mv, const Teuchos::Range1D &index)
int n
OrdinalType stride() const
static Teuchos::RCP< TMVB > Clone(const TMVB &mv, const int numvecs)
Creates a new empty MultiVectorBase containing numvecs columns.
static void MvAddMv(const ScalarType alpha, const TMVB &A, const ScalarType beta, const TMVB &B, TMVB &mv)
Replace mv with .
OrdinalType numRows() const