Thyra Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Thyra_TpetraVector_def.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Thyra: Interfaces and Support for Abstract Numerical Algorithms
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// 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 (bartlettra@ornl.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#ifndef THYRA_TPETRA_VECTOR_HPP
43#define THYRA_TPETRA_VECTOR_HPP
44
45
48
49namespace Thyra {
50
51
52// Constructors/initializers/accessors
53
54
55template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
58
59
60template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63 const RCP<Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &tpetraVector
64 )
65{
66 initializeImpl(tpetraVectorSpace, tpetraVector);
67}
68
69
70template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
73 const RCP<const Tpetra::Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &tpetraVector
74 )
75{
76 initializeImpl(tpetraVectorSpace, tpetraVector);
77}
78
79
80template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83{
84 return tpetraVector_.getNonconstObj();
85}
86
87
88template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
94
95
96// Overridden from VectorDefaultBase
97template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
100{
101 if (domainSpace_.is_null()) {
102 domainSpace_ = tpetraVectorSpace<Scalar>(
103 Tpetra::createLocalMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(
104 1,
105 tpetraVector_.getConstObj()->getMap()->getComm()
106 )
107 );
108 }
109 return domainSpace_;
110}
111
112
113// Overridden from SpmdMultiVectorBase
114
115
116template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
119{
120 return tpetraVectorSpace_;
121}
122
123
124// Overridden from SpmdVectorBase
125
126
127template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
129 const Ptr<ArrayRCP<Scalar> > &localValues )
130{
131 *localValues = tpetraVector_.getNonconstObj()->get1dViewNonConst();
132}
133
134
135template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
141
142
143// Overridden from VectorBase
144
145
146template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
148 Scalar l,
149 Scalar u
150 )
151{
152 // Tpetra randomizes with different seed for each proc, so need a global
153 // reduction to get locally-replicated random vector same on each proc.
154 if (!tpetraVector_.getNonconstObj()->isDistributed()) {
155 auto comm = tpetraVector_.getNonconstObj()->getMap()->getComm();
156 if (tpetraVector_.getConstObj()->getMap()->getComm()->getRank() == 0)
157 tpetraVector_.getNonconstObj()->randomize(l, u);
158 else
159 tpetraVector_.getNonconstObj()->putScalar(Teuchos::ScalarTraits<Scalar>::zero());
160 tpetraVector_.getNonconstObj()->reduce();
161 } else {
162 tpetraVector_.getNonconstObj()->randomize(l, u);
163 }
164}
165
166
167template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
169 const VectorBase<Scalar>& x
170 )
171{
172 auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x));
173
174 // If the cast succeeded, call Tpetra directly.
175 // Otherwise, fall back to the RTOp implementation.
176 if (nonnull(tx)) {
177 tpetraVector_.getNonconstObj()->abs(*tx);
178 } else {
180 }
181}
182
183
184template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
186 const VectorBase<Scalar>& x
187 )
188{
189 auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x));
190
191 // If the cast succeeded, call Tpetra directly.
192 // Otherwise, fall back to the RTOp implementation.
193 if (nonnull(tx)) {
194 tpetraVector_.getNonconstObj()->reciprocal(*tx);
195 } else {
197 }
198}
199
200
201template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
203 const VectorBase<Scalar>& x
204 )
205{
206 auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x));
207
208 // If the cast succeeded, call Tpetra directly.
209 // Otherwise, fall back to the RTOp implementation.
210 if (nonnull(tx)) {
212 tpetraVector_.getNonconstObj()->elementWiseMultiply(
213 ST::one(), *tx, *tpetraVector_.getConstObj(), ST::zero());
214 } else {
216 }
217}
218
219
220template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
223 const VectorBase<Scalar>& x
224 ) const
225{
226 auto tx = this->getConstTpetraVector(Teuchos::rcpFromRef(x));
227
228 // If the cast succeeded, call Tpetra directly.
229 // Otherwise, fall back to the RTOp implementation.
230 if (nonnull(tx)) {
231 // Weighted 2-norm function for Tpetra vector seems to be deprecated...
234 = Tpetra::createVector<Scalar>(tx->getMap());
235 temp->elementWiseMultiply(
236 ST::one(), *tx, *tpetraVector_.getConstObj(), ST::zero());
237 return ST::magnitude(ST::squareroot(tpetraVector_.getConstObj()->dot(*temp)));
238 } else {
240 }
241}
242
243
244template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
247 const ArrayView<const Ptr<const VectorBase<Scalar> > > &vecs,
248 const ArrayView<const Ptr<VectorBase<Scalar> > > &targ_vecs,
250 const Ordinal global_offset
251 ) const
252{
253 SpmdVectorDefaultBase<Scalar>::applyOpImpl(op, vecs, targ_vecs, reduct_obj, global_offset);
254}
255
256
257template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
260 const Range1D& rng,
262 ) const
263{
264 SpmdVectorDefaultBase<Scalar>::acquireDetachedVectorViewImpl(rng, sub_vec);
265}
266
267
268template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
271 const Range1D& rng,
273 )
274{
275
276 SpmdVectorDefaultBase<Scalar>::acquireNonconstDetachedVectorViewImpl(rng, sub_vec);
277}
278
279
280template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
284 )
285{
286 SpmdVectorDefaultBase<Scalar>::commitNonconstDetachedVectorViewImpl(sub_vec);
287}
288
289
290// Overridden protected functions from MultiVectorBase
291
292
293template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
295{
296 tpetraVector_.getNonconstObj()->putScalar(alpha);
297}
298
299
300template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
303{
304 auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv));
305
306 // If cast succeeded, call Tpetra directly.
307 // Otherwise, fall back to the RTOp implementation.
308 if (nonnull(tmv)) {
309 tpetraVector_.getNonconstObj()->assign(*tmv);
310 } else {
312 }
313}
314
315
316template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
318{
319 tpetraVector_.getNonconstObj()->scale(alpha);
320}
321
322
323template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
325 Scalar alpha,
327 )
328{
329 auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv));
330
331 // If cast succeeded, call Tpetra directly.
332 // Otherwise, fall back to the RTOp implementation.
334 if (nonnull(tmv)) {
335 tpetraVector_.getNonconstObj()->update(alpha, *tmv, ST::one());
336 } else {
338 }
339}
340
341
342template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
344 const ArrayView<const Scalar>& alpha,
345 const ArrayView<const Ptr<const MultiVectorBase<Scalar> > >& mv,
346 const Scalar& beta
347 )
348{
349#ifdef TEUCHOS_DEBUG
350 TEUCHOS_ASSERT_EQUALITY(alpha.size(), mv.size());
351#endif
352
353 // Try to cast mv to Tpetra objects
356 bool allCastsSuccessful = true;
357 {
358 auto mvIter = mv.begin();
359 auto tmvIter = tmvs.begin();
360 for (; mvIter != mv.end(); ++mvIter, ++tmvIter) {
361 tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromPtr(*mvIter));
362 if (nonnull(tmv)) {
363 *tmvIter = tmv;
364 } else {
365 allCastsSuccessful = false;
366 break;
367 }
368 }
369 }
370
371 // If casts succeeded, or input arrays are size 0, call Tpetra directly.
372 // Otherwise, fall back to the RTOp implementation.
373 auto len = mv.size();
374 if (len == 0) {
375 tpetraVector_.getNonconstObj()->scale(beta);
376 } else if (len == 1 && allCastsSuccessful) {
377 tpetraVector_.getNonconstObj()->update(alpha[0], *tmvs[0], beta);
378 } else if (len == 2 && allCastsSuccessful) {
379 tpetraVector_.getNonconstObj()->update(alpha[0], *tmvs[0], alpha[1], *tmvs[1], beta);
380 } else if (allCastsSuccessful) {
382 auto tmvIter = tmvs.begin();
383 auto alphaIter = alpha.begin();
384
385 // Check if any entry of tmvs aliases this object's wrapped vector.
386 // If so, replace that entry in the array with a copy.
387 tmv = Teuchos::null;
388 for (; tmvIter != tmvs.end(); ++tmvIter) {
389 if (tmvIter->getRawPtr() == tpetraVector_.getConstObj().getRawPtr()) {
390 if (tmv.is_null()) {
392 *tpetraVector_.getConstObj(), Teuchos::Copy));
393 }
394 *tmvIter = tmv;
395 }
396 }
397 tmvIter = tmvs.begin();
398
399 // We add two MVs at a time, so only scale if even num MVs,
400 // and additionally do the first addition if odd num MVs.
401 if ((tmvs.size() % 2) == 0) {
402 tpetraVector_.getNonconstObj()->scale(beta);
403 } else {
404 tpetraVector_.getNonconstObj()->update(*alphaIter, *(*tmvIter), beta);
405 ++tmvIter;
406 ++alphaIter;
407 }
408 for (; tmvIter != tmvs.end(); tmvIter+=2, alphaIter+=2) {
409 tpetraVector_.getNonconstObj()->update(
410 *alphaIter, *(*tmvIter), *(alphaIter+1), *(*(tmvIter+1)), ST::one());
411 }
412 } else {
414 }
415}
416
417
418template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
422 ) const
423{
424 auto tmv = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(mv));
425
426 // If the cast succeeded, call Tpetra directly.
427 // Otherwise, fall back to the RTOp implementation.
428 if (nonnull(tmv)) {
429 tpetraVector_.getConstObj()->dot(*tmv, prods);
430 } else {
432 }
433}
434
435
436template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
439 ) const
440{
441 tpetraVector_.getConstObj()->norm1(norms);
442}
443
444
445template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
448 ) const
449{
450 tpetraVector_.getConstObj()->norm2(norms);
451}
452
453
454template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
457 ) const
458{
459 tpetraVector_.getConstObj()->normInf(norms);
460}
461
462
463template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
465 const EOpTransp M_trans,
467 const Ptr<MultiVectorBase<Scalar> > &Y,
468 const Scalar alpha,
469 const Scalar beta
470 ) const
471{
472 // Try to extract Tpetra objects from X and Y
473 typedef Tpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> TMV;
474 Teuchos::RCP<const TMV> X_tpetra = this->getConstTpetraMultiVector(Teuchos::rcpFromRef(X));
475 Teuchos::RCP<TMV> Y_tpetra = this->getTpetraMultiVector(Teuchos::rcpFromPtr(Y));
476
477 // If the cast succeeded, call Tpetra directly.
478 // Otherwise, fall back to the default implementation.
479 if (nonnull(X_tpetra) && nonnull(Y_tpetra)) {
481 TEUCHOS_TEST_FOR_EXCEPTION(ST::isComplex && (M_trans == CONJ),
482 std::logic_error,
483 "Error, conjugation without transposition is not allowed for complex scalar types!");
484
486 switch (M_trans) {
487 case NOTRANS:
489 break;
490 case CONJ:
492 break;
493 case TRANS:
495 break;
496 case CONJTRANS:
498 break;
499 }
500
501 Y_tpetra->multiply(trans, Teuchos::NO_TRANS, alpha, *tpetraVector_.getConstObj(), *X_tpetra, beta);
502 Kokkos::fence();
503 } else {
505 }
506
507}
508
509
510// private
511
512
513template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
514template<class TpetraVector_t>
516 const RCP<const TpetraVectorSpace<Scalar,LocalOrdinal,GlobalOrdinal,Node> > &tpetraVectorSpace,
517 const RCP<TpetraVector_t> &tpetraVector
518 )
519{
520#ifdef TEUCHOS_DEBUG
521 TEUCHOS_ASSERT(nonnull(tpetraVectorSpace));
522 TEUCHOS_ASSERT(nonnull(tpetraVector));
523#endif
524 tpetraVectorSpace_ = tpetraVectorSpace;
525 tpetraVector_.initialize(tpetraVector);
526 this->updateSpmdSpace();
527}
528
529
530template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
534{
535 using Teuchos::rcp_dynamic_cast;
538
540 if (nonnull(tmv)) {
541 return tmv->getTpetraMultiVector();
542 }
543
545 if (nonnull(tv)) {
546 return tv->getTpetraVector();
547 }
548
549 return Teuchos::null;
550}
551
552
553template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
557{
558 using Teuchos::rcp_dynamic_cast;
561
563 if (nonnull(tmv)) {
564 return tmv->getConstTpetraMultiVector();
565 }
566
568 if (nonnull(tv)) {
569 return tv->getConstTpetraVector();
570 }
571
572 return Teuchos::null;
573}
574
575
576template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
580{
583 if (nonnull(tv)) {
584 return tv->getTpetraVector();
585 } else {
586 return Teuchos::null;
587 }
588}
589
590
591template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
594getConstTpetraVector(const RCP<const VectorBase<Scalar> >& v) const
595{
598 if (nonnull(tv)) {
599 return tv->getConstTpetraVector();
600 } else {
601 return Teuchos::null;
602 }
603}
604
605
606} // end namespace Thyra
607
608
609#endif // THYRA_TPETRA_VECTOR_HPP
bool is_null(const RCP< T > &p)
T * getRawPtr() const
RCP(T *p, const RCPNodeHandle &node)
bool nonnull(const RCP< T > &p)
Concrete Thyra::SpmdVectorBase using Tpetra::Vector.
Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > TpetraMultiVector_t
TpetraVector()
Construct to uninitialized.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)