Anasazi  Version of the Day
AnasaziTpetraAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) 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 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 #ifndef ANASAZI_TPETRA_ADAPTER_HPP
30 #define ANASAZI_TPETRA_ADAPTER_HPP
31 
32 #include <Kokkos_NodeTrace.hpp>
33 
43 
44 #include <Tpetra_MultiVector.hpp>
45 #include <Tpetra_Operator.hpp>
46 #include <Teuchos_Assert.hpp>
47 #include <Teuchos_ScalarTraits.hpp>
48 #include <Teuchos_Array.hpp>
49 #include <Teuchos_DefaultSerialComm.hpp>
50 
51 #include <AnasaziConfigDefs.hpp>
52 #include <AnasaziTypes.hpp>
55 #include <Kokkos_NodeAPIConfigDefs.hpp>
56 
57 #ifdef HAVE_ANASAZI_TSQR
58 # include <Tpetra_TsqrAdaptor.hpp>
59 #endif // HAVE_ANASAZI_TSQR
60 
61 
62 namespace Anasazi {
63 
65  //
66  // Implementation of the Anasazi::MultiVecTraits for Tpetra::MultiVector.
67  //
69 
74  template<class Scalar, class LO, class GO, class Node>
75  class MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar,LO,GO,Node> >
76  {
77  public:
78 #ifdef HAVE_ANASAZI_TPETRA_TIMERS
79  static Teuchos::RCP<Teuchos::Time> mvTimesMatAddMvTimer_, mvTransMvTimer_;
80 #endif
81 
82  static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > Clone( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const int numvecs )
83  {
84  return Teuchos::rcp( new Tpetra::MultiVector<Scalar,LO,GO,Node>(mv.getMap(),numvecs));
85  }
86 
87  static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneCopy( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
88  {
89  KOKKOS_NODE_TRACE("Anasazi::MVT::CloneCopy(MV)")
90  return Teuchos::rcp( new Tpetra::MultiVector<Scalar,LO,GO,Node>( mv ) );
91  }
92 
93  static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneCopy( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
94  {
95  KOKKOS_NODE_TRACE("Anasazi::MVT::CloneCopy(MV,ind)")
96  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0,std::invalid_argument,
97  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::Clone(mv,index): numvecs must be greater than zero.");
98 #ifdef HAVE_TPETRA_DEBUG
99  TEUCHOS_TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::runtime_error,
100  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::Clone(mv,index): indices must be >= zero.");
101  TEUCHOS_TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::runtime_error,
102  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::Clone(mv,index): indices must be < mv.getNumVectors().");
103 #endif
104  for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
105  if (index[j] != index[j-1]+1) {
106  // not contiguous; short circuit
107  Teuchos::Array<size_t> stinds(index.begin(), index.end());
108  return mv.subCopy(stinds());
109  }
110  }
111  // contiguous
112  return mv.subCopy(Teuchos::Range1D(index.front(),index.back()));
113  }
114 
115 
117  CloneCopy (const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
118  const Teuchos::Range1D& index)
119  {
120  KOKKOS_NODE_TRACE("Anasazi::MVT::CloneCopy(MV,ind)")
121  const bool validRange = index.size() > 0 &&
122  index.lbound() >= 0 &&
123  index.ubound() < mv.getNumVectors();
124  if (! validRange)
125  {
126  std::ostringstream os;
127  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<...> >::"
128  "CloneCopy(mv,index=[" << index.lbound() << ", " << index.ubound()
129  << "]): ";
130  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0, std::invalid_argument,
131  os.str() << "Empty index range is not allowed.");
132  TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
133  os.str() << "Index range includes negative "
134  "index/ices, which is not allowed.");
135  // Range1D bounds are signed; size_t is unsigned.
136  TEUCHOS_TEST_FOR_EXCEPTION((size_t) index.ubound() >= mv.getNumVectors(),
137  std::invalid_argument,
138  os.str() << "Index range exceeds number of vectors "
139  << mv.getNumVectors() << " in the input multivector.");
140  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
141  os.str() << "Should never get here!");
142  }
143  return mv.subCopy (index);
144  }
145 
146 
147  static Teuchos::RCP<Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneViewNonConst( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
148  {
149  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0,std::invalid_argument,
150  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneViewNonConst(mv,index): numvecs must be greater than zero.");
151 #ifdef HAVE_TPETRA_DEBUG
152  TEUCHOS_TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::invalid_argument,
153  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneViewNonConst(mv,index): indices must be >= zero.");
154  TEUCHOS_TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::invalid_argument,
155  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneViewNonConst(mv,index): indices must be < mv.getNumVectors().");
156 #endif
157  for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
158  if (index[j] != index[j-1]+1) {
159  // not contiguous; short circuit
160  Teuchos::Array<size_t> stinds(index.begin(), index.end());
161  return mv.subViewNonConst(stinds);
162  }
163  }
164  // contiguous
165  return mv.subViewNonConst(Teuchos::Range1D(index.front(),index.back()));
166  }
167 
168 
170  CloneViewNonConst (Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
171  const Teuchos::Range1D& index)
172  {
173  // FIXME (mfh 11 Jan 2011) Should check for overflowing int!
174  const int numCols = static_cast<int> (mv.getNumVectors());
175  const bool validRange = index.size() > 0 &&
176  index.lbound() >= 0 && index.ubound() < numCols;
177  if (! validRange)
178  {
179  std::ostringstream os;
180  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<...> >::"
181  "CloneViewNonConst(mv,index=[" << index.lbound() << ", "
182  << index.ubound() << "]): ";
183  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0, std::invalid_argument,
184  os.str() << "Empty index range is not allowed.");
185  TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
186  os.str() << "Index range includes negative "
187  "index/ices, which is not allowed.");
188  TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numCols, std::invalid_argument,
189  os.str() << "Index range exceeds number of "
190  "vectors " << numCols << " in the input "
191  "multivector.");
192  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
193  os.str() << "Should never get here!");
194  }
195  return mv.subViewNonConst (index);
196  }
197 
198 
199  static Teuchos::RCP<const Tpetra::MultiVector<Scalar,LO,GO,Node> > CloneView(const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<int>& index )
200  {
201  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0,std::invalid_argument,
202  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): numvecs must be greater than zero.");
203 #ifdef HAVE_TPETRA_DEBUG
204  TEUCHOS_TEST_FOR_EXCEPTION( *std::min_element(index.begin(),index.end()) < 0, std::invalid_argument,
205  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be >= zero.");
206  TEUCHOS_TEST_FOR_EXCEPTION( (size_t)*std::max_element(index.begin(),index.end()) >= mv.getNumVectors(), std::invalid_argument,
207  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::CloneView(mv,index): indices must be < mv.getNumVectors().");
208 #endif
209  for (typename std::vector<int>::size_type j=1; j<index.size(); ++j) {
210  if (index[j] != index[j-1]+1) {
211  // not contiguous; short circuit
212  Teuchos::Array<size_t> stinds(index.begin(), index.end());
213  return mv.subView(stinds);
214  }
215  }
216  // contiguous
217  return mv.subView(Teuchos::Range1D(index.front(),index.back()));
218  }
219 
221  CloneView (const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
222  const Teuchos::Range1D& index)
223  {
224  // FIXME (mfh 11 Jan 2011) Should check for overflowing int!
225  const int numCols = static_cast<int> (mv.getNumVectors());
226  const bool validRange = index.size() > 0 &&
227  index.lbound() >= 0 && index.ubound() < numCols;
228  if (! validRange)
229  {
230  std::ostringstream os;
231  os << "Belos::MultiVecTraits<Scalar, Tpetra::MultiVector<...> >::"
232  "CloneView(mv, index=[" << index.lbound() << ", "
233  << index.ubound() << "]): ";
234  TEUCHOS_TEST_FOR_EXCEPTION(index.size() == 0, std::invalid_argument,
235  os.str() << "Empty index range is not allowed.");
236  TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
237  os.str() << "Index range includes negative "
238  "index/ices, which is not allowed.");
239  TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numCols, std::invalid_argument,
240  os.str() << "Index range exceeds number of "
241  "vectors " << numCols << " in the input "
242  "multivector.");
243  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
244  os.str() << "Should never get here!");
245  }
246  return mv.subView (index);
247  }
248 
249  static int GetVecLength( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
250  { return mv.getGlobalLength(); }
251 
252  static int GetNumberVecs( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
253  { return mv.getNumVectors(); }
254 
255  static void MvTimesMatAddMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A,
257  Scalar beta, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
258  {
259  KOKKOS_NODE_TRACE("Anasazi::MVT::MvTimesMatAddMv()")
260 #ifdef HAVE_ANASAZI_TPETRA_TIMERS
261  Teuchos::TimeMonitor lcltimer(*mvTimesMatAddMvTimer_);
262 #endif
263  // create local map
265  Tpetra::Map<LO,GO,Node> LocalMap(B.numRows(), 0, Teuchos::rcpFromRef< const Teuchos::Comm<int> >(scomm), Tpetra::LocallyReplicated, A.getMap()->getNode());
266  // encapsulate Teuchos::SerialDenseMatrix data in ArrayView
268  // create locally replicated MultiVector with a copy of this data
269  Tpetra::MultiVector<Scalar,LO,GO,Node> B_mv(Teuchos::rcpFromRef(LocalMap),Bvalues,B.stride(),B.numCols());
270  // multiply
271  mv.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, alpha, A, B_mv, beta);
272  }
273 
274  static void MvAddMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, Scalar beta, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
275  {
276  mv.update(alpha,A,beta,B,Teuchos::ScalarTraits<Scalar>::zero());
277  }
278 
279  static void MvScale ( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, Scalar alpha )
280  { mv.scale(alpha); }
281 
282  static void MvScale ( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, const std::vector<Scalar>& alphas )
283  { mv.scale(alphas); }
284 
285  static void MvTransMv( Scalar alpha, const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, Teuchos::SerialDenseMatrix<int,Scalar>& C)
286  {
287  KOKKOS_NODE_TRACE("Anasazi::MVT::MvTransMv()")
288 #ifdef HAVE_ANASAZI_TPETRA_TIMERS
289  Teuchos::TimeMonitor lcltimer(*mvTransMvTimer_);
290 #endif
291  // form alpha * A^H * B, then copy into SDM
292  // we will create a multivector C_mv from a a local map
293  // this map has a serial comm, the purpose being to short-circuit the MultiVector::reduce() call at the end of MultiVector::multiply()
294  // otherwise, the reduced multivector data would be copied back to the GPU, only to turn around and have to get it back here.
295  // this saves us a round trip for this data.
296  const int numRowsC = C.numRows(),
297  numColsC = C.numCols(),
298  strideC = C.stride();
300  // create local map with serial comm
301  Tpetra::Map<LO,GO,Node> LocalMap(numRowsC, 0, Teuchos::rcpFromRef< const Teuchos::Comm<int> >(scomm), Tpetra::LocallyReplicated, A.getMap()->getNode());
302  // create local multivector to hold the result
303  const bool INIT_TO_ZERO = true;
304  Tpetra::MultiVector<Scalar,LO,GO,Node> C_mv(Teuchos::rcpFromRef(LocalMap),numColsC, INIT_TO_ZERO);
305  // multiply result into local multivector
307  // get comm
308  Teuchos::RCP< const Teuchos::Comm<int> > pcomm = A.getMap()->getComm();
309  // create arrayview encapsulating the Teuchos::SerialDenseMatrix
310  Teuchos::ArrayView<Scalar> C_view(C.values(),strideC*numColsC);
311  if (pcomm->getSize() == 1) {
312  // no accumulation to do; simply extract the multivector data into C
313  // extract a copy of the result into the array view (and therefore, the SerialDenseMatrix)
314  C_mv.get1dCopy(C_view,strideC);
315  }
316  else {
317  // get a const host view of the data in C_mv
318  Teuchos::ArrayRCP<const Scalar> C_mv_view = C_mv.get1dView();
319  if (strideC == numRowsC) {
320  // sumall into C
321  Teuchos::reduceAll<int,Scalar>(*pcomm,Teuchos::REDUCE_SUM,numColsC*numRowsC,C_mv_view.getRawPtr(),C_view.getRawPtr());
322  }
323  else {
324  // sumall into temp, copy into C
325  Teuchos::Array<Scalar> destBuff(numColsC*numRowsC);
326  Teuchos::reduceAll<int,Scalar>(*pcomm,Teuchos::REDUCE_SUM,numColsC*numRowsC,C_mv_view.getRawPtr(),destBuff.getRawPtr());
327  for (int j=0; j < numColsC; ++j) {
328  for (int i=0; i < numRowsC; ++i) {
329  C_view[strideC*j+i] = destBuff[numRowsC*j+i];
330  }
331  }
332  }
333  }
334  }
335 
336  static void MvDot( const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const Tpetra::MultiVector<Scalar,LO,GO,Node>& B, std::vector<Scalar> &dots)
337  {
338  TEUCHOS_TEST_FOR_EXCEPTION(A.getNumVectors() != B.getNumVectors(),std::invalid_argument,
339  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): A and B must have the same number of vectors.");
340 #ifdef HAVE_TPETRA_DEBUG
341  TEUCHOS_TEST_FOR_EXCEPTION(dots.size() < (typename std::vector<int>::size_type)A.getNumVectors(),std::invalid_argument,
342  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): dots must have room for all dot products.");
343 #endif
345  A.dot(B,av(0,A.getNumVectors()));
346  }
347 
348  static void MvNorm(const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, std::vector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &normvec)
349  {
350 #ifdef HAVE_TPETRA_DEBUG
351  TEUCHOS_TEST_FOR_EXCEPTION(normvec.size() < (typename std::vector<int>::size_type)mv.getNumVectors(),std::invalid_argument,
352  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvNorm(mv,normvec): normvec must have room for all norms.");
353 #endif
355  mv.norm2(av(0,mv.getNumVectors()));
356  }
357 
358  static void SetBlock( const Tpetra::MultiVector<Scalar,LO,GO,Node>& A, const std::vector<int>& index, Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
359  {
360  KOKKOS_NODE_TRACE("Anasazi::MVT::SetBlock()")
361 #ifdef HAVE_TPETRA_DEBUG
362  TEUCHOS_TEST_FOR_EXCEPTION((typename std::vector<int>::size_type)A.getNumVectors() < index.size(),std::invalid_argument,
363  "Anasazi::MultiVecTraits<Scalar,Tpetra::MultiVector>::SetBlock(A,index,mv): index must be the same size as A.");
364 #endif
366  if ((typename std::vector<int>::size_type)A.getNumVectors() > index.size()) {
368  (*mvsub) = (*Asub);
369  }
370  else {
371  (*mvsub) = A;
372  }
373  mvsub = Teuchos::null;
374  }
375 
376  static void
377  SetBlock (const Tpetra::MultiVector<Scalar,LO,GO,Node>& A,
378  const Teuchos::Range1D& index,
379  Tpetra::MultiVector<Scalar,LO,GO,Node>& mv)
380  {
381  KOKKOS_NODE_TRACE("Anasazi::MVT::SetBlock()")
382 
383  // Range1D bounds are signed; size_t is unsigned.
384  // Assignment of Tpetra::MultiVector is a deep copy.
385 
386  // Tpetra::MultiVector::getNumVectors() returns size_t. It's
387  // fair to assume that the number of vectors won't overflow int,
388  // since the typical use case of multivectors involves few
389  // columns, but it's friendly to check just in case.
390  const size_t maxInt = static_cast<size_t> (Teuchos::OrdinalTraits<int>::max());
391  const bool overflow = maxInt < A.getNumVectors() && maxInt < mv.getNumVectors();
392  if (overflow)
393  {
394  std::ostringstream os;
395  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar, ..."
396  "> >::SetBlock(A, index=[" << index.lbound() << ", "
397  << index.ubound() << "], mv): ";
398  TEUCHOS_TEST_FOR_EXCEPTION(maxInt < A.getNumVectors(), std::range_error,
399  os.str() << "Number of columns in the input multi"
400  "vector 'A' (a size_t) overflows int.");
401  TEUCHOS_TEST_FOR_EXCEPTION(maxInt < mv.getNumVectors(), std::range_error,
402  os.str() << "Number of columns in the output multi"
403  "vector 'mv' (a size_t) overflows int.");
404  }
405  // We've already validated the static casts above.
406  const int numColsA = static_cast<int> (A.getNumVectors());
407  const int numColsMv = static_cast<int> (mv.getNumVectors());
408  // 'index' indexes into mv; it's the index set of the target.
409  const bool validIndex = index.lbound() >= 0 && index.ubound() < numColsMv;
410  // We can't take more columns out of A than A has.
411  const bool validSource = index.size() <= numColsA;
412 
413  if (! validIndex || ! validSource)
414  {
415  std::ostringstream os;
416  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar, ..."
417  "> >::SetBlock(A, index=[" << index.lbound() << ", "
418  << index.ubound() << "], mv): ";
419  TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
420  os.str() << "Range lower bound must be nonnegative.");
421  TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numColsMv, std::invalid_argument,
422  os.str() << "Range upper bound must be less than "
423  "the number of columns " << numColsA << " in the "
424  "'mv' output argument.");
425  TEUCHOS_TEST_FOR_EXCEPTION(index.size() > numColsA, std::invalid_argument,
426  os.str() << "Range must have no more elements than"
427  " the number of columns " << numColsA << " in the "
428  "'A' input argument.");
429  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!");
430  }
433 
434  // View of the relevant column(s) of the target multivector mv.
435  // We avoid view creation overhead by only creating a view if
436  // the index range is different than [0, (# columns in mv) - 1].
437  MV_ptr mv_view;
438  if (index.lbound() == 0 && index.ubound()+1 == numColsMv)
439  mv_view = Teuchos::rcpFromRef (mv); // Non-const, non-owning RCP
440  else
441  mv_view = CloneViewNonConst (mv, index);
442 
443  // View of the relevant column(s) of the source multivector A.
444  // If A has fewer columns than mv_view, then create a view of
445  // the first index.size() columns of A.
446  const_MV_ptr A_view;
447  if (index.size() == numColsA)
448  A_view = Teuchos::rcpFromRef (A); // Const, non-owning RCP
449  else
450  A_view = CloneView (A, Teuchos::Range1D(0, index.size()-1));
451 
452  // Assignment of Tpetra::MultiVector objects via operator=()
453  // assumes that both arguments have compatible Maps. If
454  // HAVE_TPETRA_DEBUG is defined at compile time, operator=()
455  // will throw an std::runtime_error if the Maps are
456  // incompatible.
457  *mv_view = *A_view;
458  }
459 
460  static void
461  Assign (const Tpetra::MultiVector<Scalar,LO,GO,Node>& A,
462  Tpetra::MultiVector<Scalar,LO,GO,Node>& mv)
463  {
464  KOKKOS_NODE_TRACE("Anasazi::MVT::Assign()")
465 
466  // Range1D bounds are signed; size_t is unsigned.
467  // Assignment of Tpetra::MultiVector is a deep copy.
468 
469  // Tpetra::MultiVector::getNumVectors() returns size_t. It's
470  // fair to assume that the number of vectors won't overflow int,
471  // since the typical use case of multivectors involves few
472  // columns, but it's friendly to check just in case.
473  const size_t maxInt = static_cast<size_t> (Teuchos::OrdinalTraits<int>::max());
474  const bool overflow = maxInt < A.getNumVectors() && maxInt < mv.getNumVectors();
475  if (overflow)
476  {
477  std::ostringstream os;
478  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar, ..."
479  "> >::Assign(A, mv): ";
480  TEUCHOS_TEST_FOR_EXCEPTION(maxInt < A.getNumVectors(), std::range_error,
481  os.str() << "Number of columns in the input multi"
482  "vector 'A' (a size_t) overflows int.");
483  TEUCHOS_TEST_FOR_EXCEPTION(maxInt < mv.getNumVectors(), std::range_error,
484  os.str() << "Number of columns in the output multi"
485  "vector 'mv' (a size_t) overflows int.");
486  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!");
487  }
488  // We've already validated the static casts above.
489  const int numColsA = static_cast<int> (A.getNumVectors());
490  const int numColsMv = static_cast<int> (mv.getNumVectors());
491  if (numColsA > numColsMv)
492  {
493  std::ostringstream os;
494  os << "Anasazi::MultiVecTraits<Scalar, Tpetra::MultiVector<Scalar, ..."
495  "> >::Assign(A, mv): ";
496  TEUCHOS_TEST_FOR_EXCEPTION(numColsA > numColsMv, std::invalid_argument,
497  os.str() << "Input multivector 'A' has "
498  << numColsA << " columns, but output multivector "
499  "'mv' has only " << numColsMv << " columns.");
500  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Should never get here!");
501  }
502  // Assignment of Tpetra::MultiVector objects via operator=()
503  // assumes that both arguments have compatible Maps. If
504  // HAVE_TPETRA_DEBUG is defined at compile time, operator=()
505  // will throw an std::runtime_error if the Maps are
506  // incompatible.
507  if (numColsA == numColsMv)
508  mv = A;
509  else
510  {
512  CloneViewNonConst (mv, Teuchos::Range1D(0, numColsA-1));
513  *mv_view = A;
514  }
515  }
516 
517  static void MvRandom( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv )
518  {
519  KOKKOS_NODE_TRACE("Anasazi::MVT::randomize()")
520  mv.randomize();
521  }
522 
523  static void MvInit( Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, Scalar alpha = Teuchos::ScalarTraits<Scalar>::zero() )
524  { mv.putScalar(alpha); }
525 
526  static void MvPrint( const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv, std::ostream& os )
527  { mv.print(os); }
528 
529 #ifdef HAVE_ANASAZI_TSQR
530  typedef Tpetra::TsqrAdaptor< Tpetra::MultiVector< Scalar, LO, GO, Node > > tsqr_adaptor_type;
534 #endif // HAVE_ANASAZI_TSQR
535  };
536 
538  //
539  // Implementation of the Anasazi::OperatorTraits for Tpetra::Operator.
540  //
542 
543  template <class Scalar, class LO, class GO, class Node>
544  class OperatorTraits < Scalar, Tpetra::MultiVector<Scalar,LO,GO,Node>, Tpetra::Operator<Scalar,LO,GO,Node> >
545  {
546  public:
547  static void Apply ( const Tpetra::Operator<Scalar,LO,GO,Node> & Op,
548  const Tpetra::MultiVector<Scalar,LO,GO,Node> & X,
549  Tpetra::MultiVector<Scalar,LO,GO,Node> & Y)
550  {
551  Op.apply(X,Y,Teuchos::NO_TRANS);
552  }
553  };
554 
555 } // end of Anasazi namespace
556 
557 #endif
558 // end of file ANASAZI_TPETRA_ADAPTER_HPP
ScalarType * values() const
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
Declaration of basic traits for the multivector type.
static int GetVecLength(const MV &mv)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
T * getRawPtr() const
static void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
Compute a vector b where the components are the individual dot-products of the i-th columns of A and ...
static void Assign(const MV &A, MV &mv)
mv := A
Ordinal ubound() const
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
Virtual base class which defines basic traits for the operator type.
static void MvScale(MV &mv, const ScalarType alpha)
Scale each element of the vectors in mv with alpha.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Ordinal lbound() const
OrdinalType numCols() const
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
Ordinal size() const
Types and exceptions used within Anasazi solvers and interfaces.
static void MvPrint(const MV &mv, std::ostream &os)
Print the mv multi-vector to the os output stream.
OrdinalType stride() const
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
OrdinalType numRows() const