Tpetra Matrix/Vector Services  Version of the Day
Tpetra_MultiVector_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_MULTIVECTOR_DEF_HPP
43 #define TPETRA_MULTIVECTOR_DEF_HPP
44 
45 #include <Kokkos_DefaultArithmetic.hpp>
46 #include <Kokkos_NodeTrace.hpp>
47 #include <Teuchos_Assert.hpp>
48 #include <Teuchos_as.hpp>
49 #include <Tpetra_Util.hpp>
50 #include <Tpetra_Vector.hpp>
51 
52 #ifdef DOXYGEN_USE_ONLY
53  #include "Tpetra_MultiVector_decl.hpp"
54 #endif
55 
56 namespace Tpetra {
57 
58  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
59  bool
60  MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::
61  vectorIndexOutOfRange(size_t VectorIndex) const {
62  return (VectorIndex < 1 && VectorIndex != 0) || VectorIndex >= getNumVectors();
63  }
64 
65  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
68  size_t NumVectors,
69  bool zeroOut) : /* default is true */
70  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
71  lclMV_ (map->getNode ()),
72  releaseViewsRaisedEfficiencyWarning_ (false),
73  createViewsRaisedEfficiencyWarning_ (false),
74  createViewsNonConstRaisedEfficiencyWarning_ (false)
75  {
76  using Teuchos::ArrayRCP;
77  using Teuchos::RCP;
78 
79  TEUCHOS_TEST_FOR_EXCEPTION(NumVectors < 1, std::invalid_argument,
80  "Tpetra::MultiVector::MultiVector(): NumVectors must be strictly positive.");
81  const size_t myLen = getLocalLength();
82  if (myLen > 0) {
83  RCP<Node> node = map->getNode();
84  // On host-type Kokkos Nodes, allocBuffer() just calls the
85  // one-argument version of arcp to allocate memory. This should
86  // not fill the memory by default, otherwise we would lose the
87  // first-touch allocation optimization.
88  ArrayRCP<Scalar> data = node->template allocBuffer<Scalar>(myLen*NumVectors);
89  MVT::initializeValues(lclMV_,myLen,NumVectors,data,myLen);
90  if (zeroOut) {
91  // MVT uses the Kokkos Node's parallel_for in this case, for
92  // first-touch allocation (across rows).
94  }
95  }
96  else {
97  MVT::initializeValues(lclMV_,0,NumVectors,Teuchos::null,0);
98  }
99  }
100 
101  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
104  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (source),
105  lclMV_ (MVT::getNode (source.lclMV_)),
109  {
110  using Teuchos::ArrayRCP;
111  using Teuchos::RCP;
112 
113  // copy data from the source MultiVector into this multivector
114  RCP<Node> node = MVT::getNode(source.lclMV_);
115  const LocalOrdinal myLen = source.getLocalLength();
116  const size_t numVecs = source.getNumVectors();
117 
118  // On host-type Kokkos Nodes, allocBuffer() just calls the
119  // one-argument version of arcp to allocate memory. This should
120  // not fill the memory by default, otherwise we would lose the
121  // first-touch allocation optimization.
122  ArrayRCP<Scalar> data = (myLen > 0) ?
123  node->template allocBuffer<Scalar> (myLen * numVecs) :
124  Teuchos::null;
125  const size_t stride = (myLen > 0) ? myLen : size_t (0);
126 
127  // This just sets the dimensions, pointer, and stride of lclMV_.
128  MVT::initializeValues (lclMV_, myLen, numVecs, data, stride);
129  // This actually copies the data. It uses the Node's
130  // parallel_for to copy, which should ensure first-touch
131  // allocation on systems that support it.
132  if (source.isConstantStride ()) {
133  MVT::Assign (lclMV_, source.lclMV_);
134  }
135  else {
136  MVT::Assign (lclMV_, source.lclMV_, source.whichVectors_);
137  }
138  }
139 
140  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
143  const Kokkos::MultiVector<Scalar,Node>& localMultiVector,
144  EPrivateComputeViewConstructor /* dummy */) :
145  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
146  lclMV_ (localMultiVector),
150  {
151  const size_t localNumElts = map->getNodeNumElements ();
153  localMultiVector.getNumRows () < localNumElts,
154  std::invalid_argument,
155  "Tpetra::MultiVector constructor: The Map contains " << localNumElts
156  << " on process " << map->getComm ()->getRank () << ", but the given "
157  "Kokkos::MultiVector only has " << localMultiVector.getNumRows ()
158  << " rows.");
159  }
160 
161  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
164  const Teuchos::ArrayRCP<Scalar>& view,
165  size_t LDA,
166  size_t NumVectors,
167  EPrivateHostViewConstructor /* dummy */) :
168  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
169  lclMV_ (map->getNode ()),
173  {
174  using Teuchos::as;
175  using Teuchos::ArrayRCP;
176  using Teuchos::RCP;
177 
178  const char tfecfFuncName[] = "MultiVector(view,LDA,NumVector)";
179  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(NumVectors < 1, std::invalid_argument,
180  ": NumVectors must be strictly positive, but you specified NumVectors = "
181  << NumVectors << ".");
182  const size_t myLen = getLocalLength();
183  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(LDA < myLen, std::invalid_argument,
184  ": LDA must be large enough to accomodate the local entries.");
186  as<size_t> (view.size ()) < LDA*(NumVectors - 1) + myLen,
187  std::invalid_argument,
188  ": view does not contain enough data to specify the entries in this.");
189  MVT::initializeValues (lclMV_, myLen, NumVectors, view, myLen);
190  }
191 
192  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
196  size_t LDA,
197  size_t NumVectors,
198  EPrivateComputeViewConstructor /* dummy */) :
199  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
200  lclMV_ (map->getNode ()),
204  {
205  using Teuchos::as;
206  using Teuchos::ArrayRCP;
207  using Teuchos::RCP;
208 
209  const char tfecfFuncName[] = "MultiVector(data,LDA,NumVector)";
210  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(NumVectors < 1, std::invalid_argument,
211  ": NumVectors must be strictly positive.");
212  const size_t myLen = getLocalLength();
213 #ifdef HAVE_TPETRA_DEBUG
215  as<size_t> (data.size ()) < LDA*(NumVectors - 1) + myLen,
216  std::runtime_error,
217  ": data does not contain enough data to specify the entries in this.");
218 #endif
219  MVT::initializeValues(lclMV_,myLen,NumVectors,data,LDA);
220  }
221 
222  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
226  size_t LDA,
228  EPrivateComputeViewConstructor /* dummy */) :
229  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
230  lclMV_ (map->getNode ()),
231  whichVectors_ (WhichVectors),
235  {
236  using Teuchos::as;
237  using Teuchos::ArrayRCP;
238  using Teuchos::RCP;
239 
240  const char tfecfFuncName[] = "MultiVector(data,LDA,WhichVectors)";
241  const size_t myLen = getLocalLength();
242  size_t maxVector = *std::max_element(WhichVectors.begin(), WhichVectors.end());
243  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(LDA < myLen, std::runtime_error,
244  ": LDA must be large enough to accomodate the local entries.");
245 #ifdef HAVE_TPETRA_DEBUG
246  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
247  as<size_t> (data.size ()) < LDA * maxVector + myLen, std::runtime_error,
248  ": data does not contain enough data to specify the entries in this.");
249 #endif
250  if (WhichVectors.size () == 1) {
251  // shift data so that desired vector is vector 0
252  maxVector = 0;
253  data += LDA * WhichVectors[0];
254  // kill whichVectors_; we are constant stride
255  whichVectors_.clear ();
256  }
257  if (myLen > 0) {
258  MVT::initializeValues(lclMV_,myLen,maxVector+1,data,LDA);
259  }
260  else {
261  MVT::initializeValues(lclMV_,0,WhichVectors.size(),Teuchos::null,0);
262  }
263  }
264 
265 
266  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
269  const Kokkos::MultiVector<Scalar,Node>& localMultiVector,
271  EPrivateComputeViewConstructor /* dummy */) :
272  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
273  lclMV_ (localMultiVector),
274  whichVectors_ (WhichVectors),
278  {
279  const size_t localNumElts = map->getNodeNumElements ();
281  localMultiVector.getNumRows () < localNumElts,
282  std::invalid_argument,
283  "Tpetra::MultiVector constructor: The Map contains " << localNumElts
284  << " on process " << map->getComm ()->getRank () << ", but the given "
285  "Kokkos::MultiVector only has " << localMultiVector.getNumRows ()
286  << " rows.");
288  WhichVectors.size () == 0,
289  std::invalid_argument,
290  "Tpetra::MultiVector constructor: WhichVectors.size() == 0. "
291  "The noncontiguous column view constructor only makes sense for a "
292  "nonzero number of columns to view.");
293 
294  // Optimization for the case of a single column: just make a
295  // contiguous ("constant stride") view of the one column.
296  if (WhichVectors.size () == 1) {
297  const size_t offsetRow = 0;
298  const size_t offsetCol = WhichVectors[0];
299  lclMV_ = lclMV_.offsetViewNonConst (localNumElts, 1, offsetRow, offsetCol);
300  whichVectors_.clear (); // This being empty defines "constant stride."
301  }
302  }
303 
304 
305  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
309  size_t LDA,
310  size_t NumVectors) :
311  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
312  lclMV_ (map->getNode ()),
316  {
317  using Teuchos::as;
318  using Teuchos::ArrayRCP;
319  using Teuchos::ArrayView;
320  using Teuchos::RCP;
321 
322  const char tfecfFuncName[] = "MultiVector(A,LDA)";
323  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(NumVectors < 1, std::invalid_argument,
324  ": NumVectors must be strictly positive.");
325  const size_t myLen = getLocalLength();
326  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(LDA < myLen, std::runtime_error,
327  ": LDA must be large enough to accomodate the local entries.");
328 #ifdef HAVE_TPETRA_DEBUG
330  as<size_t> (A.size ()) < LDA*(NumVectors - 1) + myLen,
331  std::runtime_error,
332  ": A does not contain enough data to specify the entries in this.");
333 #endif
334  if (myLen > 0) {
335  RCP<Node> node = MVT::getNode(lclMV_);
336  ArrayRCP<Scalar> mydata = node->template allocBuffer<Scalar>(myLen*NumVectors);
337  MVT::initializeValues(lclMV_,myLen,NumVectors,mydata,myLen);
338  // FIXME (mfh 13 Sep 2012) It would be better to use the Kokkos
339  // Node's copyToBuffer method to push data directly to the
340  // device pointer, rather than using an intermediate host
341  // buffer. Also, we should have an optimization for the
342  // contiguous storage case (constant stride, and the stride
343  // equals the local number of rows).
344  KOKKOS_NODE_TRACE("MultiVector::MultiVector(1D)")
345  ArrayRCP<Scalar> myview = node->template viewBufferNonConst<Scalar>(Kokkos::WriteOnly,myLen*NumVectors,mydata);
346  typename ArrayView<const Scalar>::iterator srcit = A.begin();
347  for (size_t j = 0; j < NumVectors; ++j) {
348  std::copy(srcit,srcit+myLen,myview);
349  srcit += LDA;
350  myview += myLen;
351  }
352  mydata = Teuchos::null;
353  myview = Teuchos::null;
354  }
355  else {
356  MVT::initializeValues(lclMV_,0,NumVectors,Teuchos::null,0);
357  }
358  }
359 
360  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
363  const Teuchos::ArrayView<const ArrayView<const Scalar> >& ArrayOfPtrs,
364  size_t NumVectors) :
365  DistObject<Scalar,LocalOrdinal,GlobalOrdinal,Node> (map),
366  lclMV_ (map->getNode ()),
370  {
371  using Teuchos::as;
372  using Teuchos::ArrayRCP;
373  using Teuchos::ArrayView;
374  using Teuchos::RCP;
375 
376  const char tfecfFuncName[] = "MultiVector(ArrayOfPtrs)";
378  NumVectors < 1 || NumVectors != as<size_t>(ArrayOfPtrs.size()),
379  std::runtime_error,
380  ": ArrayOfPtrs.size() must be strictly positive and as large as ArrayOfPtrs.");
381  const size_t myLen = getLocalLength();
382  if (myLen > 0) {
383  RCP<Node> node = MVT::getNode(lclMV_);
384  ArrayRCP<Scalar> mydata = node->template allocBuffer<Scalar>(myLen*NumVectors);
385  MVT::initializeValues(lclMV_,myLen,NumVectors,mydata,myLen);
386  KOKKOS_NODE_TRACE("MultiVector::MultiVector(2D)")
387  ArrayRCP<Scalar> myview = node->template viewBufferNonConst<Scalar>(Kokkos::WriteOnly,myLen*NumVectors,mydata);
388  for (size_t j = 0; j < NumVectors; ++j) {
389 #ifdef HAVE_TPETRA_DEBUG
391  as<size_t>(ArrayOfPtrs[j].size()) != getLocalLength(),
392  std::runtime_error,
393  ": ArrayOfPtrs[" << j << "].size() (== " << ArrayOfPtrs[j].size()
394  << ") is not equal to getLocalLength() (== " << getLocalLength());
395 #endif
396  typename ArrayView<const Scalar>::iterator src = ArrayOfPtrs[j].begin();
397  std::copy(src,src+myLen,myview);
398  myview += myLen;
399  }
400  myview = Teuchos::null;
401  mydata = Teuchos::null;
402  }
403  else {
404  MVT::initializeValues(lclMV_,0,NumVectors,Teuchos::null,0);
405  }
406  }
407 
408 
409  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
411  }
412 
413 
414  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
416  return whichVectors_.empty();
417  }
418 
419 
420  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
422  return this->getMap()->getNodeNumElements();
423  }
424 
425 
426  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
428  return this->getMap()->getGlobalNumElements();
429  }
430 
431 
432  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
434  if (isConstantStride()) {
435  return MVT::getStride(lclMV_);
436  }
437  return 0;
438  }
439 
440 
441  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
442  bool
444  checkSizes (const SrcDistObject& sourceObj)
445  {
446  // Check whether the source object is a MultiVector. If not, then
447  // we can't even compare sizes, so it's definitely not OK to
448  // Import or Export from it.
450  const this_type* src = dynamic_cast<const this_type*> (&sourceObj);
451  if (src == NULL) {
452  return false;
453  } else {
454  // The target of the Import or Export calls checkSizes() in
455  // DistObject::doTransfer(). By that point, we've already
456  // constructed an Import or Export object using the two
457  // multivectors' Maps, which means that (hopefully) we've
458  // already checked other attributes of the multivectors. Thus,
459  // all we need to do here is check the number of columns.
460  return src->getNumVectors () == this->getNumVectors ();
461  }
462  }
463 
464 
465  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
466  void
468  copyAndPermute (const SrcDistObject& sourceObj,
469  size_t numSameIDs,
470  const Teuchos::ArrayView<const LocalOrdinal> &permuteToLIDs,
471  const Teuchos::ArrayView<const LocalOrdinal> &permuteFromLIDs)
472  {
473  using Teuchos::ArrayRCP;
474  using Teuchos::ArrayView;
475  using Teuchos::RCP;
477  typedef typename ArrayView<const LocalOrdinal>::size_type size_type;
478  const char tfecfFuncName[] = "copyAndPermute";
479 
481  permuteToLIDs.size() != permuteFromLIDs.size(), std::runtime_error,
482  ": permuteToLIDs and permuteFromLIDs must have the same size."
483  << std::endl << "permuteToLIDs.size() = " << permuteToLIDs.size ()
484  << " != permuteFromLIDs.size() = " << permuteFromLIDs.size () << ".");
485 
486  // We've already called checkSizes(), so this cast must succeed.
487  const MV& sourceMV = dynamic_cast<const MV&> (sourceObj);
488 
489  const size_t numCols = this->getNumVectors ();
490 
491  // Copy rows [0, numSameIDs-1] of the local multivectors.
492  //
493  // For GPU Nodes: All of this happens using device pointers; this
494  // does not require host views of either source or destination.
495  //
496  // mfh 10 Jan 2013: In the current implementation, device data has
497  // already been copied to the host views. If we do a
498  // device-device copy here, it won't synch the host views
499  // correctly. Thus, we replace this step with a host copy, if
500  // Node is a GPU Node. That ensures correctness for now, though
501  // it's really something we need to fix if we plan to run
502  // efficiently on GPUs.
503  if (Node::isHostNode) {
504  if (isConstantStride ()) {
505  const size_t numSrcCols = MVT::getNumCols (sourceMV.lclMV_);
506  const size_t numDestCols = MVT::getNumCols (lclMV_);
507 
508  const KMV src = sourceMV.lclMV_.offsetView (numSameIDs, numSrcCols, 0, 0);
509  KMV dest = lclMV_.offsetViewNonConst (numSameIDs, numDestCols, 0, 0);
510  if (sourceMV.isConstantStride ()) {
511  MVT::Assign (dest, src);
512  }
513  else {
514  MVT::Assign (dest, src, sourceMV.whichVectors_ ());
515  }
516  }
517  else {
518  // Copy the columns one at a time, since MVT doesn't have an
519  // Assign method for noncontiguous access to the destination
520  // multivector.
521  for (size_t j = 0; j < numCols; ++j) {
522  const size_t destCol = isConstantStride () ? j : whichVectors_[j];
523  const size_t srcCol = sourceMV.isConstantStride () ?
524  j : sourceMV.whichVectors_[j];
525  KMV dest_j = lclMV_.offsetViewNonConst (numSameIDs, 1, 0, destCol);
526  const KMV src_j = sourceMV.lclMV_.offsetView (numSameIDs, 1, 0, srcCol);
527  MVT::Assign (dest_j, src_j); // Copy src_j into dest_j
528  }
529  }
530  }
531 
532  // Copy the vectors one at a time. This works whether or not the
533  // multivectors have constant stride.
534  for (size_t j = 0; j < numCols; ++j) {
535  // Get host views of the current source and destination column.
536  //
537  // FIXME (mfh 10 Mar 2012) Copying should really be done on the
538  // device. There's no need to bring everything back to the
539  // host, copy there, and then copy back.
540  ArrayView<const Scalar> srcView =
541  sourceMV.getSubArrayRCP (sourceMV.cview_, j) ();
542  ArrayView<Scalar> dstView = getSubArrayRCP (ncview_, j) ();
543 
544  if (! Node::isHostNode) {
545  // The first numSameIDs IDs are the same between source and
546  // target, so we can just copy the data. (This favors faster
547  // contiguous access whenever we can do it.)
548  std::copy (srcView.begin (), srcView.begin () + numSameIDs,
549  dstView.begin ());
550  }
551 
552  // For the remaining GIDs, execute the permutations. This may
553  // involve noncontiguous access of both source and destination
554  // vectors, depending on the LID lists.
555  //
556  // FIXME (mfh 09 Apr 2012) Permutation should be done on the
557  // device, not on the host.
558  //
559  // FIXME (mfh 20 June 2012) For an Export with duplicate GIDs on
560  // the same process, this merges their values by replacement of
561  // the last encountered GID, not by the specified merge rule
562  // (such as ADD).
563  const size_type numPermuteLIDs =
564  std::min (permuteToLIDs.size (), permuteFromLIDs.size ());
565  for (size_type k = 0; k < numPermuteLIDs; ++k) {
566  dstView[permuteToLIDs[k]] = srcView[permuteFromLIDs[k]];
567  }
568  }
569  }
570 
571 
572  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
573  size_t
576  return this->getNumVectors ();
577  }
578 
579 
580  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
581  void
583  packAndPrepare (const SrcDistObject& sourceObj,
584  const ArrayView<const LocalOrdinal> &exportLIDs,
585  Array<Scalar> &exports,
586  const ArrayView<size_t> &numExportPacketsPerLID,
587  size_t& constantNumPackets,
588  Distributor & /* distor */ )
589  {
590  using Teuchos::Array;
591  using Teuchos::ArrayView;
592  using Teuchos::as;
594  typedef Array<size_t>::size_type size_type;
595 
596  // We've already called checkSizes(), so this cast must succeed.
597  const MV& sourceMV = dynamic_cast<const MV&> (sourceObj);
598 
599  // We don't need numExportPacketsPerLID; forestall "unused
600  // variable" compile warnings.
601  (void) numExportPacketsPerLID;
602 
603  /* The layout in the export for MultiVectors is as follows:
604  exports = { all of the data from row exportLIDs.front() ;
605  ....
606  all of the data from row exportLIDs.back() }
607  This doesn't have the best locality, but is necessary because
608  the data for a Packet (all data associated with an LID) is
609  required to be contiguous. */
610 
611  const size_t numCols = sourceMV.getNumVectors ();
612  const size_t stride = MVT::getStride (sourceMV.lclMV_);
613  // This spares us from needing to fill numExportPacketsPerLID.
614  // Setting constantNumPackets to a nonzero value signals that
615  // all packets have the same number of entries.
616  constantNumPackets = numCols;
617 
618  const size_type numExportLIDs = exportLIDs.size ();
619  const size_type newExportsSize = numCols * numExportLIDs;
620  if (exports.size () != newExportsSize) {
621  exports.resize (newExportsSize);
622  }
623 
624  ArrayView<const Scalar> srcView = sourceMV.cview_ ();
625  if (numCols == 1) { // special case for one column only
626  // MultiVector always represents a single column with constant
627  // stride, but it doesn't hurt to implement both cases anyway.
628  if (sourceMV.isConstantStride ()) {
629  for (size_type k = 0; k < numExportLIDs; ++k) {
630  exports[k] = srcView[exportLIDs[k]];
631  }
632  }
633  else {
634  const size_t offset = sourceMV.whichVectors_[0] * stride;
635  for (size_type k = 0; k < numExportLIDs; ++k) {
636  exports[k] = srcView[exportLIDs[k] + offset];
637  }
638  }
639  }
640  else { // the source MultiVector has multiple columns
641  typename Array<Scalar>::iterator expptr = exports.begin ();
642  if (sourceMV.isConstantStride ()) {
643  for (size_type k = 0; k < numExportLIDs; ++k) {
644  const size_t localRow = as<size_t> (exportLIDs[k]);
645  for (size_t j = 0; j < numCols; ++j) {
646  *expptr++ = srcView[localRow + j*stride];
647  }
648  }
649  }
650  else {
651  ArrayView<const size_t> srcWhichVectors = sourceMV.whichVectors_ ();
652  for (size_type k = 0; k < numExportLIDs; ++k) {
653  const size_t localRow = as<size_t> (exportLIDs[k]);
654  for (size_t j = 0; j < numCols; ++j) {
655  *expptr++ = srcView[localRow + srcWhichVectors[j]*stride];
656  }
657  }
658  }
659  }
660  }
661 
662 
663  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
664  void
667  const ArrayView<const Scalar> &imports,
668  const ArrayView<size_t> &numPacketsPerLID,
669  size_t constantNumPackets,
670  Distributor & /* distor */,
671  CombineMode CM)
672  {
673  using Teuchos::ArrayView;
674  using Teuchos::as;
675  typedef Teuchos::ScalarTraits<Scalar> SCT;
676  typedef typename ArrayView<const LocalOrdinal>::size_type size_type;
677  const char tfecfFuncName[] = "unpackAndCombine";
678 
679  // We don't need numPacketsPerLID; forestall "unused variable"
680  // compile warnings.
681  (void) numPacketsPerLID;
682 
683  /* The layout in the export for MultiVectors is as follows:
684  imports = { all of the data from row exportLIDs.front() ;
685  ....
686  all of the data from row exportLIDs.back() }
687  This doesn't have the best locality, but is necessary because
688  the data for a Packet (all data associated with an LID) is
689  required to be contiguous. */
690 
691  const size_t numVecs = getNumVectors ();
692 
693 #ifdef HAVE_TPETRA_DEBUG
695  as<size_t> (imports.size()) != getNumVectors()*importLIDs.size(),
696  std::runtime_error,
697  ": 'imports' buffer size must be consistent with the amount of data to "
698  "be sent. " << std::endl << "imports.size() = " << imports.size()
699  << " != getNumVectors()*importLIDs.size() = " << getNumVectors() << "*"
700  << importLIDs.size() << " = " << getNumVectors() * importLIDs.size()
701  << ".");
702 
704  as<size_t> (constantNumPackets) == as<size_t> (0), std::runtime_error,
705  ": constantNumPackets input argument must be nonzero.");
706 
707  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
708  as<size_t> (numVecs) != as<size_t> (constantNumPackets),
709  std::runtime_error, ": constantNumPackets must equal numVecs.");
710 #endif // HAVE_TPETRA_DEBUG
711 
712  if (numVecs > 0 && importLIDs.size () > 0) {
713  typename ArrayView<const Scalar>::iterator impptr = imports.begin ();
714  ArrayView<Scalar> destView = ncview_ ();
715  const size_type numImportLIDs = importLIDs.size ();
716  const size_t myStride = MVT::getStride (lclMV_);
717 
718  // NOTE (mfh 10 Mar 2012) If you want to implement custom
719  // combine modes, start editing here. Also, if you trust
720  // inlining, it would be nice to condense this code by using a
721  // binary function object f:
722  //
723  // ncview_[...] = f (ncview_[...], *impptr++);
724  if (CM == INSERT || CM == REPLACE) {
725  if (isConstantStride()) {
726  for (size_type k = 0; k < numImportLIDs; ++k) {
727  const size_t localRow = as<size_t> (importLIDs[k]);
728  for (size_t j = 0; j < numVecs; ++j) {
729  destView[localRow + myStride*j] = *impptr++;
730  }
731  }
732  }
733  else {
734  for (size_type k = 0; k < numImportLIDs; ++k) {
735  const size_t localRow = as<size_t> (importLIDs[k]);
736  for (size_t j = 0; j < numVecs; ++j) {
737  destView[localRow + myStride*whichVectors_[j]] = *impptr++;
738  }
739  }
740  }
741  }
742  else if (CM == ADD) {
743  if (isConstantStride()) {
744  for (size_type k = 0; k < numImportLIDs; ++k) {
745  const size_t localRow = as<size_t> (importLIDs[k]);
746  for (size_t j = 0; j < numVecs; ++j) {
747  destView[localRow + myStride*j] += *impptr++;
748  }
749  }
750  }
751  else {
752  for (size_type k = 0; k < numImportLIDs; ++k) {
753  const size_t localRow = as<size_t> (importLIDs[k]);
754  for (size_t j = 0; j < numVecs; ++j) {
755  destView[localRow + myStride*whichVectors_[j]] += *impptr++;
756  }
757  }
758  }
759  }
760  else if (CM == ABSMAX) {
761  if (isConstantStride()) {
762  for (size_type k = 0; k < numImportLIDs; ++k) {
763  const size_t localRow = as<size_t> (importLIDs[k]);
764  for (size_t j = 0; j < numVecs; ++j) {
765  Scalar &curval = destView[localRow + myStride*j];
766  const Scalar &newval = *impptr++;
767  curval = std::max( SCT::magnitude(curval), SCT::magnitude(newval) );
768  }
769  }
770  }
771  else {
772  for (size_type k = 0; k < numImportLIDs; ++k) {
773  const size_t localRow = as<size_t> (importLIDs[k]);
774  for (size_t j = 0; j < numVecs; ++j) {
775  Scalar &curval = destView[localRow + myStride*whichVectors_[j]];
776  const Scalar &newval = *impptr++;
777  curval = std::max( SCT::magnitude(curval), SCT::magnitude(newval) );
778  }
779  }
780  }
781  }
782  else {
783  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
784  CM != ADD && CM != REPLACE && CM != INSERT && CM != ABSMAX,
785  std::invalid_argument, ": Invalid CombineMode: " << CM << ". Valid "
786  "CombineMode values are ADD, REPLACE, INSERT, and ABSMAX.");
787  }
788  }
789  }
790 
791 
792  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
794  if (isConstantStride()) {
795  return MVT::getNumCols(lclMV_);
796  }
797  else {
798  return whichVectors_.size();
799  }
800  }
801 
802 
803  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
804  void
807  const Teuchos::ArrayView<Scalar> &dots) const
808  {
809  using Teuchos::Array;
810  using Teuchos::ArrayRCP;
811  using Teuchos::as;
812  using Teuchos::arcp_const_cast;
813 
814  const char tfecfFuncName[] = "dot()";
815  const size_t myLen = getLocalLength();
816  const size_t numVecs = getNumVectors();
817 #ifdef HAVE_TPETRA_DEBUG
818  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*A.getMap()), std::runtime_error,
819  ": MultiVectors do not have compatible Maps:" << std::endl
820  << "this->getMap(): " << std::endl << *this->getMap()
821  << "A.getMap(): " << std::endl << *A.getMap() << std::endl);
822 #else
824  ": MultiVectors do not have the same local length.");
825 #endif
826  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != numVecs, std::runtime_error,
827  ": MultiVectors must have the same number of vectors.");
828  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Teuchos::as<size_t>(dots.size()) != numVecs, std::runtime_error,
829  ": dots.size() must be as large as the number of vectors in *this and A.");
830  if (isConstantStride() && A.isConstantStride()) {
831  MVT::Dot(lclMV_,A.lclMV_,dots);
832  }
833  else {
834  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_));
835  ArrayRCP<Scalar> vptr = arcp_const_cast<Scalar>(MVT::getValues(lclMV_));
836  ArrayRCP<Scalar> avptr = arcp_const_cast<Scalar>(MVT::getValues(A.lclMV_));
837  for (size_t j=0; j < numVecs; ++j) {
838  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j);
839  ArrayRCP<Scalar> avj = A.getSubArrayRCP(avptr,j);
840  MVT::initializeValues(a,myLen, 1, avj, myLen);
841  MVT::initializeValues(v,myLen, 1, vj, myLen);
842  dots[j] = MVT::Dot((const KMV&)v,(const KMV &)a);
843  }
844  }
845  if (this->isDistributed()) {
846  Array<Scalar> ldots(dots);
847  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,as<int>(numVecs),ldots.getRawPtr(),dots.getRawPtr());
848  }
849  }
850 
851 
852  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
853  void
856  {
857  using Teuchos::arcp_const_cast;
858  using Teuchos::Array;
859  using Teuchos::ArrayRCP;
860  using Teuchos::ArrayView;
861  using Teuchos::as;
862  using Teuchos::reduceAll;
863  using Teuchos::REDUCE_SUM;
864  using Teuchos::ScalarTraits;
865  typedef typename ScalarTraits<Scalar>::magnitudeType MT;
866  typedef ScalarTraits<MT> STM;
867 
868  const size_t numVecs = this->getNumVectors();
869  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t>(norms.size()) != numVecs,
870  std::runtime_error,
871  "Tpetra::MultiVector::norm2(norms): norms.size() must be as large as the number of vectors in *this.");
872  if (isConstantStride ()) {
873  MVT::Norm2Squared (lclMV_,norms);
874  }
875  else {
876  KMV v (MVT::getNode (lclMV_));
877  ArrayRCP<Scalar> vi;
878  for (size_t i=0; i < numVecs; ++i) {
879  vi = arcp_const_cast<Scalar> (MVT::getValues (lclMV_, whichVectors_[i]));
880  MVT::initializeValues (v, MVT::getNumRows (lclMV_), 1, vi, MVT::getStride (lclMV_));
881  norms[i] = MVT::Norm2Squared (v);
882  }
883  }
884  if (this->isDistributed ()) {
885  Array<MT> lnorms (norms);
886  // FIXME (mfh 25 Apr 2012) Somebody explain to me why we're
887  // calling Teuchos::reduceAll when MultiVector has a perfectly
888  // good reduce() function.
889  reduceAll (*this->getMap ()->getComm (), REDUCE_SUM, as<int> (numVecs),
890  lnorms.getRawPtr (), norms.getRawPtr ());
891  }
892  for (typename ArrayView<MT>::iterator n = norms.begin(); n != norms.begin()+numVecs; ++n) {
893  (*n) = STM::squareroot (*n);
894  }
895  }
896 
897 
898  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
899  void
903  {
904  using Teuchos::arcp_const_cast;
905  using Teuchos::Array;
906  using Teuchos::ArrayRCP;
907  using Teuchos::ArrayView;
908  using Teuchos::as;
909  using Teuchos::reduceAll;
910  using Teuchos::REDUCE_SUM;
911  using Teuchos::ScalarTraits;
912  typedef ScalarTraits<Scalar> SCT;
913  typedef typename SCT::magnitudeType Mag;
914 
915  const char tfecfFuncName[] = "normWeighted()";
916 
917  const Mag OneOverN = ScalarTraits<Mag>::one() / getGlobalLength();
918  bool OneW = false;
919  const size_t numVecs = this->getNumVectors();
920  if (weights.getNumVectors() == 1) {
921  OneW = true;
922  }
923  else {
924  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(weights.getNumVectors() != numVecs, std::runtime_error,
925  ": MultiVector of weights must contain either one vector or the same number of vectors as this.");
926  }
927 #ifdef HAVE_TPETRA_DEBUG
928  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*weights.getMap()), std::runtime_error,
929  ": MultiVectors do not have compatible Maps:" << std::endl
930  << "this->getMap(): " << std::endl << *this->getMap()
931  << "weights.getMap(): " << std::endl << *weights.getMap() << std::endl);
932 #else
933  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( getLocalLength() != weights.getLocalLength(), std::runtime_error,
934  ": MultiVectors do not have the same local length.");
935 #endif
936  const size_t myLen = getLocalLength();
937 
938  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(as<size_t>(norms.size()) != numVecs, std::runtime_error,
939  ": norms.size() must be as large as the number of vectors in *this.");
940  if (isConstantStride() && weights.isConstantStride()) {
941  MVT::WeightedNorm(lclMV_,weights.lclMV_,norms);
942  }
943  else {
944  KMV v(MVT::getNode(lclMV_)), w(MVT::getNode(lclMV_));
945  ArrayRCP<Scalar> vptr = arcp_const_cast<Scalar> (MVT::getValues (lclMV_));
946  ArrayRCP<Scalar> wptr = arcp_const_cast<Scalar> (MVT::getValues (weights.lclMV_));
947  ArrayRCP<Scalar> wj = wptr.persistingView (0,myLen);
948  MVT::initializeValues (w,myLen, 1, wj, myLen);
949  for (size_t j=0; j < numVecs; ++j) {
950  ArrayRCP<Scalar> vj = getSubArrayRCP (vptr, j);
951  MVT::initializeValues(v,myLen, 1, vj, myLen);
952  if (! OneW) {
953  wj = weights.getSubArrayRCP(wptr,j);
954  MVT::initializeValues (w, myLen, 1, wj, myLen);
955  }
956  norms[j] = MVT::WeightedNorm ((const KMV&)v, (const KMV &)w);
957  }
958  }
959  if (this->isDistributed ()) {
960  Array<Mag> lnorms(norms);
961  reduceAll (*this->getMap ()->getComm (), REDUCE_SUM, as<int>(numVecs),
962  lnorms.getRawPtr (), norms.getRawPtr ());
963  }
964  for (typename ArrayView<Mag>::iterator n = norms.begin(); n != norms.begin()+numVecs; ++n) {
965  *n = ScalarTraits<Mag>::squareroot(*n * OneOverN);
966  }
967  }
968 
969 
970  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
971  void
974  {
975  using Teuchos::Array;
976  using Teuchos::ArrayRCP;
977  using Teuchos::arcp_const_cast;
978  using Teuchos::as;
979  using Teuchos::reduceAll;
980  using Teuchos::REDUCE_SUM;
982 
983  const size_t numVecs = this->getNumVectors();
984  TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(norms.size()) != numVecs, std::runtime_error,
985  "Tpetra::MultiVector::norm1(norms): norms.size() must be as large as the number of vectors in *this.");
986  if (isConstantStride()) {
987  MVT::Norm1(lclMV_,norms);
988  }
989  else {
990  KMV v(MVT::getNode(lclMV_));
991  ArrayRCP<Scalar> vj;
992  for (size_t j=0; j < numVecs; ++j) {
993  vj = arcp_const_cast<Scalar> (MVT::getValues(lclMV_,whichVectors_[j]) );
994  MVT::initializeValues (v, MVT::getNumRows(lclMV_), 1, vj, MVT::getStride (lclMV_));
995  norms[j] = MVT::Norm1 ((const KMV&)v);
996  }
997  }
998  if (this->isDistributed ()) {
999  Array<Mag> lnorms (norms);
1000  reduceAll (*this->getMap ()->getComm (), REDUCE_SUM, as<int> (numVecs), lnorms.getRawPtr (), norms.getRawPtr ());
1001  }
1002  }
1003 
1004 
1005  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1006  void
1009  {
1010  using Teuchos::Array;
1011  using Teuchos::ArrayRCP;
1012  using Teuchos::arcp_const_cast;
1013  using Teuchos::as;
1014  using Teuchos::reduceAll;
1015  using Teuchos::REDUCE_MAX;
1016  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Mag;
1017 
1018  const size_t numVecs = this->getNumVectors();
1019  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t>(norms.size()) != numVecs, std::runtime_error,
1020  "Tpetra::MultiVector::normInf(norms): norms.size() must be as large as the number of vectors in *this.");
1021  if (isConstantStride ()) {
1022  MVT::NormInf(lclMV_,norms);
1023  }
1024  else {
1025  KMV v(MVT::getNode(lclMV_));
1026  ArrayRCP<Scalar> vj;
1027  for (size_t j=0; j < numVecs; ++j) {
1028  vj = Teuchos::arcp_const_cast<Scalar>( MVT::getValues(lclMV_,whichVectors_[j]) );
1029  MVT::initializeValues(v,MVT::getNumRows(lclMV_), 1, vj, MVT::getStride(lclMV_));
1030  norms[j] = MVT::NormInf((const KMV&)v);
1031  }
1032  }
1033  if (this->isDistributed()) {
1034  Array<Mag> lnorms(norms);
1035  reduceAll (*this->getMap ()->getComm (), REDUCE_MAX, as<int> (numVecs), lnorms.getRawPtr (), norms.getRawPtr ());
1036  }
1037  }
1038 
1039 
1040  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1041  void
1044  {
1045  using Teuchos::Array;
1046  using Teuchos::ArrayRCP;
1047  using Teuchos::arcp_const_cast;
1048  using Teuchos::as;
1049  using Teuchos::arcp_const_cast;
1050  using Teuchos::reduceAll;
1051  using Teuchos::REDUCE_SUM;
1052  typedef Teuchos::ScalarTraits<Scalar> SCT;
1053 
1054  const size_t numVecs = getNumVectors();
1055  const size_t myLen = getLocalLength();
1056  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t>(means.size()) != numVecs, std::runtime_error,
1057  "Tpetra::MultiVector::meanValue(): means.size() must be as large as the number of vectors in *this.");
1058  // compute local components of the means
1059  // sum these across all nodes
1060  if (isConstantStride()) {
1061  MVT::Sum(lclMV_,means);
1062  }
1063  else {
1064  KMV v(MVT::getNode(lclMV_));
1065  ArrayRCP<Scalar> vptr = arcp_const_cast<Scalar>(MVT::getValues(lclMV_));
1066  for (size_t j=0; j < numVecs; ++j) {
1067  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j);
1068  MVT::initializeValues(v,myLen, 1, vj, myLen);
1069  means[j] = MVT::Sum((const KMV &)v);
1070  }
1071  }
1072  if (this->isDistributed()) {
1073  Array<Scalar> lmeans(means);
1074  // only combine if we are a distributed MV
1075  reduceAll (*this->getMap ()->getComm (), REDUCE_SUM, as<int> (numVecs),
1076  lmeans.getRawPtr (), means.getRawPtr ());
1077  }
1078  // mfh 12 Apr 2012: Don't take out the cast from the ordinal type
1079  // to the magnitude type, since operator/ (std::complex<T>, int)
1080  // isn't necessarily defined.
1081  const Scalar OneOverN =
1082  SCT::one() / as<typename SCT::magnitudeType> (getGlobalLength ());
1083  for (typename ArrayView<Scalar>::iterator i = means.begin();
1084  i != means.begin() + numVecs;
1085  ++i)
1086  {
1087  (*i) = (*i) * OneOverN;
1088  }
1089  }
1090 
1091 
1092  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1093  void
1096  {
1097  if (isConstantStride ()) {
1098  MVT::Random (lclMV_);
1099  }
1100  else {
1101  const size_t numVecs = this->getNumVectors ();
1102  KMV v (MVT::getNode (lclMV_));
1104  for (size_t j = 0; j < numVecs; ++j) {
1105  vj = MVT::getValuesNonConst (lclMV_, whichVectors_[j]);
1106  MVT::initializeValues (v, MVT::getNumRows (lclMV_), 1, vj, MVT::getStride (lclMV_));
1107  MVT::Random (v);
1108  }
1109  }
1110  }
1111 
1112 
1113  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1114  void
1116  putScalar (const Scalar &alpha)
1117  {
1118  const size_t numVecs = getNumVectors();
1119  if (isConstantStride()) {
1120  MVT::Init(lclMV_,alpha);
1121  }
1122  else {
1123  KMV v(MVT::getNode(lclMV_));
1125  for (size_t j=0; j < numVecs; ++j) {
1126  vj = MVT::getValuesNonConst(lclMV_,whichVectors_[j]);
1127  MVT::initializeValues(v,MVT::getNumRows(lclMV_), 1, vj, MVT::getStride(lclMV_));
1128  MVT::Init(v,alpha);
1129  }
1130  }
1131  }
1132 
1133 
1134  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1135  void
1138  {
1139  using Teuchos::ArrayRCP;
1140  using Teuchos::Comm;
1141  using Teuchos::RCP;
1142 
1143  // mfh 28 Mar 2013: This method doesn't forget whichVectors_, so
1144  // it might work if the MV is a column view of another MV.
1145  // However, things might go wrong when restoring the original
1146  // Map, so we don't allow this case for now.
1148  ! this->isConstantStride (), std::logic_error,
1149  "Tpetra::MultiVector::replaceMap: This method does not currently work "
1150  "if the MultiVector is a column view of another MultiVector (that is, if "
1151  "isConstantStride() == false).");
1152 
1153  // Case 1: current Map and new Map are both nonnull on this process.
1154  // Case 2: current Map is nonnull, new Map is null.
1155  // Case 3: current Map is null, new Map is nonnull.
1156  // Case 4: both Maps are null: forbidden.
1157  //
1158  // Case 1 means that we don't have to do anything on this process,
1159  // other than assign the new Map. (We always have to do that.)
1160  // It's an error for the user to supply a Map that requires
1161  // resizing in this case.
1162  //
1163  // Case 2 means that the calling process is in the current Map's
1164  // communicator, but will be excluded from the new Map's
1165  // communicator. We don't have to do anything on the calling
1166  // process; just leave whatever data it may have alone.
1167  //
1168  // Case 3 means that the calling process is excluded from the
1169  // current Map's communicator, but will be included in the new
1170  // Map's communicator. This means we need to (re)allocate the
1171  // local (Kokkos::)MultiVector if it does not have the right
1172  // number of rows. If the new number of rows is nonzero, we'll
1173  // fill the newly allocated local data with zeros, as befits a
1174  // projection operation.
1175  //
1176  // The typical use case for Case 3 is that the MultiVector was
1177  // first created with the Map with more processes, then that Map
1178  // was replaced with a Map with fewer processes, and finally the
1179  // original Map was restored on this call to replaceMap.
1180 
1181 #ifdef HAVE_TEUCHOS_DEBUG
1182  // mfh 28 Mar 2013: We can't check for compatibility across the
1183  // whole communicator, unless we know that the current and new
1184  // Maps are nonnull on _all_ participating processes.
1185  // TEUCHOS_TEST_FOR_EXCEPTION(
1186  // origNumProcs == newNumProcs && ! this->getMap ()->isCompatible (*map),
1187  // std::invalid_argument, "Tpetra::MultiVector::project: "
1188  // "If the input Map's communicator is compatible (has the same number of "
1189  // "processes as) the current Map's communicator, then the two Maps must be "
1190  // "compatible. The replaceMap() method is not for data redistribution; "
1191  // "use Import or Export for that purpose.");
1192 
1193  // TODO (mfh 28 Mar 2013) Add compatibility checks for projections
1194  // of the Map, in case the process counts don't match.
1195 #endif // HAVE_TEUCHOS_DEBUG
1196 
1197  if (this->getMap ().is_null ()) { // current Map is null
1199  newMap.is_null (), std::invalid_argument,
1200  "Tpetra::MultiVector::replaceMap: both current and new Maps are null. "
1201  "This probably means that the input Map is incorrect.");
1202  // Case 3: current Map is null, new Map is nonnull.
1203  const size_t newNumRows = newMap->getNodeNumElements ();
1204  const size_t origNumRows = lclMV_.getNumRows ();
1205  const size_t numCols = getNumVectors ();
1206 
1207  if (origNumRows != newNumRows) {
1208  RCP<Node> node = newMap->getNode ();
1209  ArrayRCP<Scalar> data = newNumRows == 0 ? Teuchos::null :
1210  node->template allocBuffer<Scalar> (newNumRows * numCols);
1211  const size_t stride = newNumRows;
1212  MVT::initializeValues (lclMV_, newNumRows, numCols, data, stride);
1213  if (newNumRows > 0) {
1215  }
1216  }
1217  }
1218  else if (newMap.is_null ()) { // Case 2: current Map is nonnull, new Map is null
1219  // I am an excluded process. Reinitialize my data so that I
1220  // have 0 rows. Keep the number of columns as before.
1221  const size_t numVecs = getNumVectors ();
1222  MVT::initializeValues (lclMV_, 0, numVecs, Teuchos::null, 0);
1223  }
1224  this->map_ = newMap;
1225  }
1226 
1227 
1228  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1229  void
1231  scale (const Scalar &alpha)
1232  {
1233  using Teuchos::arcp_const_cast;
1234  using Teuchos::ArrayRCP;
1235 
1236  // NOTE: can't substitute putScalar(0.0) for scale(0.0), because
1237  // the former will overwrite NaNs present in the MultiVector, while the
1238  // semantics of this call require multiplying them by 0, which IEEE requires to be NaN
1239  const size_t numVecs = getNumVectors();
1240  if (alpha == Teuchos::ScalarTraits<Scalar>::one()) {
1241  // do nothing
1242  }
1243  else if (isConstantStride()) {
1244  MVT::Scale(lclMV_,alpha);
1245  }
1246  else {
1247  KMV v(MVT::getNode(lclMV_));
1248  ArrayRCP<Scalar> vj;
1249  for (size_t j=0; j < numVecs; ++j) {
1250  vj = arcp_const_cast<Scalar> (MVT::getValues(lclMV_, whichVectors_[j]));
1251  MVT::initializeValues (v, MVT::getNumRows (lclMV_), 1, vj, MVT::getStride (lclMV_));
1252  MVT::Scale (v, alpha);
1253  }
1254  }
1255  }
1256 
1257 
1258  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1259  void
1262  {
1263  using Teuchos::arcp_const_cast;
1264  using Teuchos::ArrayRCP;
1265  using Teuchos::as;
1266 
1267  const size_t numVecs = this->getNumVectors();
1268  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t>(alphas.size()) != numVecs, std::runtime_error,
1269  "Tpetra::MultiVector::scale(alphas): alphas.size() must be as large as the number of vectors in *this.");
1270  KMV vec(MVT::getNode(lclMV_));
1271  const size_t myLen = MVT::getNumRows(lclMV_);
1272  if (myLen == 0) return;
1273  ArrayRCP<Scalar> mybuf = MVT::getValuesNonConst(lclMV_);
1274  for (size_t j = 0; j < numVecs; ++j) {
1275  if (alphas[j] == Teuchos::ScalarTraits<Scalar>::one()) {
1276  // do nothing: NaN * 1.0 == NaN, Number*1.0 == Number
1277  }
1278  else {
1279  ArrayRCP<Scalar> mybufj = getSubArrayRCP(mybuf,j);
1280  MVT::initializeValues(vec,myLen,1,mybufj,myLen);
1281  MVT::Scale(vec,alphas[j]);
1282  }
1283  }
1284  }
1285 
1286 
1287  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1288  void
1291  {
1292  using Teuchos::arcp_const_cast;
1293  using Teuchos::ArrayRCP;
1294  using Teuchos::as;
1295 
1296  const char tfecfFuncName[] = "scale(alpha,A)";
1297 
1298  const size_t numVecs = getNumVectors(),
1299  myLen = getLocalLength();
1300 #ifdef HAVE_TPETRA_DEBUG
1301  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(! this->getMap ()->isCompatible (*A.getMap ()),
1302  std::runtime_error, ": MultiVectors do not have compatible Maps:" << std::endl
1303  << "this->getMap(): " << std::endl << *(this->getMap ())
1304  << "A.getMap(): " << std::endl << *(A.getMap ()) << std::endl);
1305 #else
1307  ": MultiVectors do not have the same local length.");
1308 #endif
1309  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != numVecs, std::runtime_error,
1310  ": MultiVectors must have the same number of vectors.");
1311  if (isConstantStride() && A.isConstantStride()) {
1312  // set me == alpha*A
1313  MVT::Scale(lclMV_,alpha,(const KMV&)A.lclMV_);
1314  }
1315  else {
1316  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_));
1317  ArrayRCP<Scalar> vptr = MVT::getValuesNonConst (lclMV_);
1318  ArrayRCP<Scalar> avptr = arcp_const_cast<Scalar> (MVT::getValues (A.lclMV_));
1319  for (size_t j=0; j < numVecs; ++j) {
1320  ArrayRCP<Scalar> vj = getSubArrayRCP (vptr, j);
1321  ArrayRCP<Scalar> avj = A.getSubArrayRCP (avptr, j);
1322  MVT::initializeValues (a,myLen, 1, avj, myLen);
1323  MVT::initializeValues (v,myLen, 1, vj, myLen);
1324  MVT::Scale (v, alpha, (const KMV &)a);
1325  }
1326  }
1327  }
1328 
1329 
1330  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1331  void
1334  {
1335  using Teuchos::arcp_const_cast;
1336  using Teuchos::ArrayRCP;
1337  using Teuchos::as;
1338 
1339  const char tfecfFuncName[] = "reciprocal()";
1340 
1341 #ifdef HAVE_TPETRA_DEBUG
1342  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*A.getMap()), std::runtime_error,
1343  ": MultiVectors do not have compatible Maps:" << std::endl
1344  << "this->getMap(): " << std::endl << *this->getMap()
1345  << "A.getMap(): " << std::endl << *A.getMap() << std::endl);
1346 #else
1348  ": MultiVectors do not have the same local length.");
1349 #endif
1350  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != this->getNumVectors(), std::runtime_error,
1351  ": MultiVectors must have the same number of vectors.");
1352  const size_t numVecs = getNumVectors();
1353  const size_t myLen = getLocalLength();
1354  try {
1355  if (isConstantStride() && A.isConstantStride()) {
1356  MVT::Recip(lclMV_,(const KMV&)A.lclMV_);
1357  }
1358  else {
1359  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_));
1360  ArrayRCP<Scalar> vptr = MVT::getValuesNonConst(lclMV_),
1361  avptr = arcp_const_cast<Scalar>(MVT::getValues(A.lclMV_));
1362  for (size_t j=0; j < numVecs; ++j) {
1363  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j),
1364  avj = A.getSubArrayRCP(avptr,j);
1365  MVT::initializeValues(a,myLen, 1, avj, myLen);
1366  MVT::initializeValues(v,myLen, 1, vj, myLen);
1367  MVT::Recip(v,(const KMV &)a);
1368  }
1369  }
1370  }
1371  catch (std::runtime_error &e) {
1372  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true,std::runtime_error,
1373  ": caught exception from Kokkos:" << std::endl
1374  << e.what() << std::endl);
1375  }
1376  }
1377 
1378 
1379  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1381  using Teuchos::arcp_const_cast;
1382  using Teuchos::ArrayRCP;
1383  using Teuchos::as;
1384 
1385  const char tfecfFuncName[] = "abs()";
1386 #ifdef HAVE_TPETRA_DEBUG
1387  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*A.getMap()), std::runtime_error,
1388  ": MultiVectors do not have compatible Maps:" << std::endl
1389  << "this->getMap(): " << std::endl << *this->getMap()
1390  << "A.getMap(): " << std::endl << *A.getMap() << std::endl);
1391 #else
1393  ": MultiVectors do not have the same local length.");
1394 #endif
1395  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != this->getNumVectors(), std::runtime_error,
1396  ": MultiVectors must have the same number of vectors.");
1397  const size_t myLen = getLocalLength();
1398  const size_t numVecs = getNumVectors();
1399  if (isConstantStride() && A.isConstantStride()) {
1400  MVT::Abs(lclMV_,(const KMV&)A.lclMV_);
1401  }
1402  else {
1403  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_));
1404  ArrayRCP<Scalar> vptr = MVT::getValuesNonConst(lclMV_),
1405  avptr = arcp_const_cast<Scalar>(MVT::getValues(A.lclMV_));
1406  for (size_t j=0; j < numVecs; ++j) {
1407  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j),
1408  avj = A.getSubArrayRCP(avptr,j);
1409  MVT::initializeValues(a,myLen, 1, avj, myLen);
1410  MVT::initializeValues(v,myLen, 1, vj, myLen);
1411  MVT::Abs(v,(const KMV &)a);
1412  }
1413  }
1414  }
1415 
1416 
1417  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1419  const Scalar &alpha, const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &A,
1420  const Scalar &beta)
1421  {
1422  using Teuchos::arcp_const_cast;
1423  using Teuchos::ArrayRCP;
1424  using Teuchos::as;
1425 
1426  const char tfecfFuncName[] = "update()";
1427  // this = beta*this + alpha*A
1428  // must support case where &this == &A
1429  // can't short circuit on alpha==0.0 or beta==0.0, because 0.0*NaN != 0.0
1430 #ifdef HAVE_TPETRA_DEBUG
1431  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*A.getMap()), std::runtime_error,
1432  ": MultiVectors do not have compatible Maps:" << std::endl
1433  << "this->getMap(): " << std::endl << this->getMap()
1434  << "A.getMap(): " << std::endl << *A.getMap() << std::endl);
1435 #else
1437  ": MultiVectors do not have the same local length.");
1438 #endif
1439  const size_t myLen = getLocalLength();
1440  const size_t numVecs = getNumVectors();
1441  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != this->getNumVectors(), std::runtime_error,
1442  ": MultiVectors must have the same number of vectors.");
1443  if (isConstantStride() && A.isConstantStride()) {
1444  MVT::GESUM(lclMV_,alpha,(const KMV&)A.lclMV_,beta);
1445  }
1446  else {
1447  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_));
1448  ArrayRCP<Scalar> vptr = MVT::getValuesNonConst(lclMV_),
1449  avptr = arcp_const_cast<Scalar>(MVT::getValues(A.lclMV_));
1450  for (size_t j=0; j < numVecs; ++j) {
1451  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j),
1452  avj = A.getSubArrayRCP(avptr,j);
1453  MVT::initializeValues(a,myLen, 1, avj, myLen);
1454  MVT::initializeValues(v,myLen, 1, vj, myLen);
1455  MVT::GESUM(v,alpha,(const KMV &)a,beta);
1456  }
1457  }
1458  }
1459 
1460 
1461  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1463  const Scalar &alpha, const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &A,
1464  const Scalar &beta, const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &B,
1465  const Scalar &gamma)
1466  {
1467  using Teuchos::arcp_const_cast;
1468  using Teuchos::ArrayRCP;
1469  using Teuchos::as;
1470 
1471  const char tfecfFuncName[] = "update()";
1472  // this = alpha*A + beta*B + gamma*this
1473  // must support case where &this == &A or &this == &B
1474  // can't short circuit on alpha==0.0 or beta==0.0 or gamma==0.0, because 0.0*NaN != 0.0
1475 #ifdef HAVE_TPETRA_DEBUG
1476  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*A.getMap()) || !this->getMap()->isCompatible(*B.getMap()),
1477  std::runtime_error,
1478  ": MultiVectors do not have compatible Maps:" << std::endl
1479  << "this->getMap(): " << std::endl << *this->getMap()
1480  << "A.getMap(): " << std::endl << *A.getMap() << std::endl
1481  << "B.getMap(): " << std::endl << *B.getMap() << std::endl);
1482 #else
1484  ": MultiVectors do not have the same local length.");
1485 #endif
1486  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(A.getNumVectors() != this->getNumVectors() || B.getNumVectors() != this->getNumVectors(), std::runtime_error,
1487  ": MultiVectors must have the same number of vectors.");
1488  const size_t myLen = getLocalLength();
1489  const size_t numVecs = getNumVectors();
1490  if (isConstantStride() && A.isConstantStride() && B.isConstantStride()) {
1491  MVT::GESUM(lclMV_,alpha,(const KMV&)A.lclMV_,beta,(const KMV&)B.lclMV_,gamma);
1492  }
1493  else {
1494  KMV v(MVT::getNode(lclMV_)), a(MVT::getNode(lclMV_)), b(MVT::getNode(lclMV_));
1495  ArrayRCP<Scalar> vptr = MVT::getValuesNonConst(lclMV_),
1496  avptr = arcp_const_cast<Scalar>(MVT::getValues(A.lclMV_)),
1497  bvptr = arcp_const_cast<Scalar>(MVT::getValues(B.lclMV_));
1498  for (size_t j=0; j < numVecs; ++j) {
1499  ArrayRCP<Scalar> vj = getSubArrayRCP( vptr,j),
1500  avj = A.getSubArrayRCP(avptr,j),
1501  bvj = B.getSubArrayRCP(bvptr,j);
1502  MVT::initializeValues(b,myLen, 1, bvj, myLen);
1503  MVT::initializeValues(a,myLen, 1, avj, myLen);
1504  MVT::initializeValues(v,myLen, 1, vj, myLen);
1505  MVT::GESUM(v,alpha,(const KMV&)a,beta,(const KMV&)b,gamma);
1506  }
1507  }
1508  }
1509 
1510 
1511  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1514  getData (size_t j) const
1515  {
1516  Teuchos::RCP<Node> node = MVT::getNode(lclMV_);
1517  KOKKOS_NODE_TRACE("MultiVector::getData()")
1518  return node->template viewBuffer<Scalar> (getLocalLength (), getSubArrayRCP (MVT::getValues(lclMV_), j));
1519  }
1520 
1521 
1522  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1526  {
1527  Teuchos::RCP<Node> node = MVT::getNode(lclMV_);
1528  KOKKOS_NODE_TRACE("MultiVector::getDataNonConst()")
1529  return node->template viewBufferNonConst<Scalar> (Kokkos::ReadWrite, getLocalLength (), getSubArrayRCP (MVT::getValuesNonConst (lclMV_), j));
1530  }
1531 
1532 
1533  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1537  {
1538  const char tfecfFuncName[] = "operator=()";
1539  // Check for special case of this=Source, in which case we do nothing.
1540  if (this != &source) {
1541 #ifdef HAVE_TPETRA_DEBUG
1542  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !this->getMap()->isCompatible(*source.getMap()), std::invalid_argument,
1543  ": MultiVectors do not have compatible Maps:" << std::endl
1544  << "this->getMap(): " << std::endl << *this->getMap()
1545  << "source.getMap(): " << std::endl << *source.getMap() << std::endl);
1546 #else
1547  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( getLocalLength() != source.getLocalLength(), std::invalid_argument,
1548  ": MultiVectors do not have the same local length.");
1549 #endif
1550  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(source.getNumVectors() != getNumVectors(), std::invalid_argument,
1551  ": MultiVectors must have the same number of vectors.");
1552  Teuchos::RCP<Node> node = MVT::getNode (lclMV_);
1553  const size_t numVecs = getNumVectors();
1554  if (isConstantStride() && source.isConstantStride() && getLocalLength()==getStride() && source.getLocalLength()==source.getStride()) {
1555  // Both multivectors' data are stored contiguously, so we can
1556  // copy in one call.
1557  KOKKOS_NODE_TRACE("MultiVector::operator=()")
1558  node->template copyBuffers<Scalar>(getLocalLength()*numVecs, MVT::getValues(source.lclMV_), MVT::getValuesNonConst(lclMV_) );
1559  }
1560  else {
1561  // We have to copy the columns one at a time.
1562  for (size_t j=0; j < numVecs; ++j) {
1563  KOKKOS_NODE_TRACE("MultiVector::operator=()")
1564  node->template copyBuffers<Scalar>(getLocalLength(), source.getSubArrayRCP(MVT::getValues(source.lclMV_),j),
1565  getSubArrayRCP(MVT::getValuesNonConst(lclMV_),j) );
1566  }
1567  }
1568  }
1569  return(*this);
1570  }
1571 
1572 
1573  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1577  {
1578  using Teuchos::RCP;
1579  using Teuchos::rcp;
1580 
1581  TEUCHOS_TEST_FOR_EXCEPTION(cols.size() < 1, std::runtime_error,
1582  "Tpetra::MultiVector::subCopy(cols): cols must contain at least one column.");
1583  size_t numCopyVecs = cols.size();
1584  const bool zeroData = false;
1585  RCP<Node> node = MVT::getNode(lclMV_);
1587  // mv is allocated with constant stride
1588  mv = rcp (new MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> (this->getMap (), numCopyVecs, zeroData));
1589  // copy data from *this into mv
1590  for (size_t j=0; j<numCopyVecs; ++j) {
1591  KOKKOS_NODE_TRACE("MultiVector::subCopy()")
1592  node->template copyBuffers<Scalar> (getLocalLength (),
1593  getSubArrayRCP (MVT::getValues (lclMV_), cols[j]),
1594  MVT::getValuesNonConst (mv->lclMV_, j));
1595  }
1596  return mv;
1597  }
1598 
1599 
1600  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1603  subCopy (const Teuchos::Range1D &colRng) const
1604  {
1605  using Teuchos::RCP;
1606  using Teuchos::rcp;
1607 
1608  TEUCHOS_TEST_FOR_EXCEPTION(colRng.size() == 0, std::runtime_error,
1609  "Tpetra::MultiVector::subCopy(Range1D): range must include at least one vector.");
1610  size_t numCopyVecs = colRng.size();
1611  const bool zeroData = false;
1612  RCP<Node> node = MVT::getNode(lclMV_);
1614  // mv is allocated with constant stride
1615  mv = rcp (new MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> (this->getMap (), numCopyVecs, zeroData));
1616  // copy data from *this into mv
1617  for (size_t js=colRng.lbound(), jd=0; jd<numCopyVecs; ++jd, ++js) {
1618  KOKKOS_NODE_TRACE("MultiVector::subCopy()")
1619  node->template copyBuffers<Scalar> (getLocalLength (),
1620  getSubArrayRCP (MVT::getValues (lclMV_), js),
1621  MVT::getValuesNonConst (mv->lclMV_, jd));
1622  }
1623  return mv;
1624  }
1625 
1626 
1627  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1631  size_t offset) const
1632  {
1633  using Teuchos::RCP;
1634  using Teuchos::rcp;
1636 
1637  const size_t newNumRows = subMap->getNodeNumElements();
1638  const bool tooManyElts = newNumRows + offset > lclMV_.getOrigNumRows ();
1639  if (tooManyElts) {
1640  const int myRank = this->getMap ()->getComm ()->getRank ();
1642  newNumRows + offset > MVT::getNumRows (lclMV_),
1643  std::runtime_error,
1644  "Tpetra::MultiVector::offsetView: Invalid input Map. Input Map owns "
1645  << subMap->getNodeNumElements() << " elements on process " << myRank
1646  << ". offset = " << offset << ". Yet, the MultiVector contains only "
1647  << lclMV_.getOrigNumRows () << " on this process.");
1648  }
1649  RCP<const MV> constViewMV;
1650  if (isConstantStride()) {
1651  Kokkos::MultiVector<Scalar, Node> newLocalMV =
1652  lclMV_.offsetView (newNumRows, lclMV_.getNumCols (), offset, 0);
1653  constViewMV = rcp (new MV (subMap, newLocalMV, COMPUTE_VIEW_CONSTRUCTOR));
1654  }
1655  else {
1656  // Compute the max column being viewed. This tells us where to stop the view.
1657  const size_t maxCol = *std::max_element (whichVectors_.begin(), whichVectors_.end());
1658  Kokkos::MultiVector<Scalar, Node> newLocalMV =
1659  lclMV_.offsetView (newNumRows, maxCol+1, offset, 0);
1660  constViewMV = rcp (new MV (subMap, newLocalMV, whichVectors_, COMPUTE_VIEW_CONSTRUCTOR));
1661  }
1662  return constViewMV;
1663  }
1664 
1665 
1666  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1670  size_t offset)
1671  {
1672  using Teuchos::RCP;
1673  using Teuchos::rcp;
1675 
1676  const size_t newNumRows = subMap->getNodeNumElements();
1677  const bool tooManyElts = newNumRows + offset > lclMV_.getOrigNumRows ();
1678  if (tooManyElts) {
1679  const int myRank = this->getMap ()->getComm ()->getRank ();
1681  newNumRows + offset > MVT::getNumRows (lclMV_),
1682  std::runtime_error,
1683  "Tpetra::MultiVector::offsetViewNonconst: Invalid input Map. Input Map owns "
1684  << subMap->getNodeNumElements() << " elements on process " << myRank
1685  << ". offset = " << offset << ". Yet, the MultiVector contains only "
1686  << lclMV_.getOrigNumRows () << " on this process.");
1687  }
1688  RCP<MV> subViewMV;
1689  if (isConstantStride()) {
1690  Kokkos::MultiVector<Scalar, Node> newLocalMV =
1691  lclMV_.offsetViewNonConst (newNumRows, lclMV_.getNumCols (), offset, 0);
1692  subViewMV = rcp (new MV (subMap, newLocalMV, COMPUTE_VIEW_CONSTRUCTOR));
1693  }
1694  else {
1695  // Compute the max column being viewed. This tells us where to stop the view.
1696  const size_t maxCol = *std::max_element (whichVectors_.begin(), whichVectors_.end());
1697  Kokkos::MultiVector<Scalar, Node> newLocalMV =
1698  lclMV_.offsetViewNonConst (newNumRows, maxCol+1, offset, 0);
1699  subViewMV = rcp (new MV (subMap, newLocalMV, whichVectors_, COMPUTE_VIEW_CONSTRUCTOR));
1700  }
1701  return subViewMV;
1702  }
1703 
1704 
1705  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1709  {
1710  using Teuchos::arcp_const_cast;
1711  using Teuchos::Array;
1712  using Teuchos::ArrayRCP;
1713  using Teuchos::RCP;
1714  using Teuchos::rcp;
1715  using Teuchos::rcp_const_cast;
1717 
1718  TEUCHOS_TEST_FOR_EXCEPTION(cols.size() == 0, std::runtime_error,
1719  "Tpetra::MultiVector::subView(ArrayView): range must include at least one vector.");
1720  // this is const, so the lclMV_ is const, so that we can only get const buffers
1721  // we will cast away the const; this is okay, because
1722  // a) the constructor doesn't modify the data, and
1723  // b) we are encapsulating in a const MV before returning
1724  const size_t myStride = MVT::getStride(lclMV_),
1725  myLen = MVT::getNumRows(lclMV_),
1726  numViewCols = cols.size();
1727  // use the smallest view possible of the buffer: from the first element of the minInd vector to the last element of the maxInd vector
1728  // this minimizes overlap between views, and keeps view of the minimum amount necessary, in order to allow node to achieve maximum efficiency.
1729  // adjust the indices appropriately; shift so that smallest index is 0
1730  ArrayRCP<const Scalar> cbuf = MVT::getValues(lclMV_);
1731  ArrayRCP<Scalar> ncbuf = arcp_const_cast<Scalar> (cbuf);
1732  Array<size_t> newCols (numViewCols);
1733  size_t minInd = Teuchos::OrdinalTraits<size_t>::max();
1734  size_t maxInd = Teuchos::OrdinalTraits<size_t>::zero();
1735  if (isConstantStride()) {
1736  for (size_t j=0; j < numViewCols; ++j) {
1737  newCols[j] = cols[j];
1738  if (newCols[j] < minInd) minInd = newCols[j];
1739  if (maxInd < newCols[j]) maxInd = newCols[j];
1740  }
1741  }
1742  else {
1743  for (size_t j=0; j < numViewCols; ++j) {
1744  newCols[j] = whichVectors_[cols[j]];
1745  if (newCols[j] < minInd) minInd = newCols[j];
1746  if (maxInd < newCols[j]) maxInd = newCols[j];
1747  }
1748  }
1749  ArrayRCP<Scalar> minbuf = ncbuf.persistingView(minInd * myStride, myStride * (maxInd - minInd) + myLen);
1750  for (size_t j=0; j < numViewCols; ++j) {
1751  newCols[j] -= minInd;
1752  }
1753  RCP<const MV> constViewMV;
1754  return rcp_const_cast<const MV> (rcp (new MV (this->getMap (), minbuf, myStride, newCols (), COMPUTE_VIEW_CONSTRUCTOR)));
1755  }
1756 
1757 
1758  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1761  subView (const Teuchos::Range1D &colRng) const
1762  {
1763  using Teuchos::arcp_const_cast;
1764  using Teuchos::Array;
1765  using Teuchos::ArrayRCP;
1766  using Teuchos::RCP;
1767  using Teuchos::rcp;
1769 
1770  TEUCHOS_TEST_FOR_EXCEPTION(colRng.size() == 0, std::runtime_error,
1771  "Tpetra::MultiVector::subView(Range1D): range must include at least one vector.");
1772  size_t numViewVecs = colRng.size();
1773  // this is const, so the lclMV_ is const, so that we can only get const buffers
1774  // we will cast away the const; this is okay, because
1775  // a) the constructor doesn't modify the data, and
1776  // b) we are encapsulating in a const MV before returning
1777  ArrayRCP<const Scalar> cbuf = MVT::getValues(lclMV_);
1778  ArrayRCP<Scalar> ncbuf = arcp_const_cast<Scalar>(cbuf);
1779  // resulting MultiVector is constant stride only if *this is
1780  if (isConstantStride()) {
1781  // view goes from first entry of first vector to last entry of last vector
1782  ArrayRCP<Scalar> subdata = ncbuf.persistingView( MVT::getStride(lclMV_) * colRng.lbound(),
1783  MVT::getStride(lclMV_) * (numViewVecs-1) + getLocalLength() );
1784  return rcp (new MV (this->getMap (), subdata, MVT::getStride (lclMV_), numViewVecs, COMPUTE_VIEW_CONSTRUCTOR));
1785  }
1786  // otherwise, use a subset of this whichVectors_ to construct new multivector
1787  Array<size_t> whchvecs( whichVectors_.begin()+colRng.lbound(), whichVectors_.begin()+colRng.ubound()+1 );
1788  return rcp (new MV (this->getMap (), ncbuf, MVT::getStride (lclMV_), whchvecs, COMPUTE_VIEW_CONSTRUCTOR));
1789  }
1790 
1791 
1792  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1796  {
1797  using Teuchos::as;
1798  using Teuchos::arcp_const_cast;
1799  using Teuchos::Array;
1800  using Teuchos::ArrayRCP;
1801  using Teuchos::RCP;
1802  using Teuchos::rcp;
1804 
1805  TEUCHOS_TEST_FOR_EXCEPTION(cols.size() == 0, std::runtime_error,
1806  "Tpetra::MultiVector::subViewNonConst(ArrayView): range must include at least one vector.");
1807  if (isConstantStride()) {
1808  return rcp (new MV (this->getMap (), MVT::getValuesNonConst (lclMV_), MVT::getStride (lclMV_), cols, COMPUTE_VIEW_CONSTRUCTOR));
1809  }
1810  // else, lookup current whichVectors_ using cols
1811  Array<size_t> newcols(cols.size());
1812  for (size_t j = 0; j < as<size_t> (cols.size ()); ++j) {
1813  newcols[j] = whichVectors_[cols[j]];
1814  }
1815  return rcp (new MV (this->getMap (), MVT::getValuesNonConst (lclMV_), MVT::getStride (lclMV_), newcols (), COMPUTE_VIEW_CONSTRUCTOR));
1816  }
1817 
1818 
1819  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1823  {
1824  using Teuchos::as;
1825  using Teuchos::arcp_const_cast;
1826  using Teuchos::Array;
1827  using Teuchos::ArrayRCP;
1828  using Teuchos::RCP;
1829  using Teuchos::rcp;
1831 
1832  TEUCHOS_TEST_FOR_EXCEPTION(colRng.size() == 0, std::runtime_error,
1833  "Tpetra::MultiVector::subViewNonConst(Range1D): range must include at least one vector.");
1834  size_t numViewVecs = colRng.size();
1835  // resulting MultiVector is constant stride only if *this is
1836  if (isConstantStride ()) {
1837  // view goes from first entry of first vector to last entry of last vector
1838  const size_t stride = MVT::getStride(lclMV_);
1839  ArrayRCP<Scalar> data = MVT::getValuesNonConst(lclMV_);
1840  ArrayRCP<Scalar> subdata = data.persistingView( stride * colRng.lbound(),
1841  stride * (numViewVecs-1) + getLocalLength() );
1842  return rcp (new MV (this->getMap (), subdata, stride, numViewVecs, COMPUTE_VIEW_CONSTRUCTOR));
1843  }
1844  // otherwise, use a subset of this whichVectors_ to construct new multivector
1845  Array<size_t> whchvecs( whichVectors_.begin()+colRng.lbound(), whichVectors_.begin()+colRng.ubound()+1 );
1846  const size_t stride = MVT::getStride(lclMV_);
1847  ArrayRCP<Scalar> data = MVT::getValuesNonConst(lclMV_);
1848  return rcp (new MV (this->getMap (), data, stride, whchvecs, COMPUTE_VIEW_CONSTRUCTOR));
1849  }
1850 
1851 
1852  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1855  getVector (size_t j) const
1856  {
1857  using Teuchos::arcp_const_cast;
1858  using Teuchos::ArrayRCP;
1859  using Teuchos::rcp;
1860  using Teuchos::rcp_const_cast;
1862 
1863 #ifdef HAVE_TPETRA_DEBUG
1864  TEUCHOS_TEST_FOR_EXCEPTION( vectorIndexOutOfRange(j), std::runtime_error,
1865  "Tpetra::MultiVector::getVector(j): index j (== " << j << ") exceeds valid column range for this multivector.");
1866 #endif
1867  // this is const, so lclMV_ is const, so we get const buff
1868  // it is safe to cast away the const because we will wrap it in a const Vector below
1869  ArrayRCP<Scalar> ncbuff;
1870  if (getLocalLength() > 0) {
1871  ArrayRCP<const Scalar> cbuff = getSubArrayRCP(MVT::getValues(lclMV_),j);
1872  ncbuff = arcp_const_cast<Scalar>(cbuff);
1873  }
1874  return rcp_const_cast<const V> (rcp (new V (this->getMap (), ncbuff, COMPUTE_VIEW_CONSTRUCTOR)));
1875  }
1876 
1877 
1878  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1881  {
1882  using Teuchos::ArrayRCP;
1883  using Teuchos::rcp;
1885 
1886 #ifdef HAVE_TPETRA_DEBUG
1887  TEUCHOS_TEST_FOR_EXCEPTION( vectorIndexOutOfRange(j), std::runtime_error,
1888  "Tpetra::MultiVector::getVectorNonConst(j): index j (== " << j << ") exceeds valid column range for this multivector.");
1889 #endif
1890  ArrayRCP<Scalar> ncbuff;
1891  if (getLocalLength() > 0) {
1892  ncbuff = getSubArrayRCP (MVT::getValuesNonConst (lclMV_), j);
1893  }
1894  return rcp (new V (this->getMap (), ncbuff, COMPUTE_VIEW_CONSTRUCTOR));
1895  }
1896 
1897 
1898  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1899  void
1902  {
1903  using Teuchos::ArrayRCP;
1904  using Teuchos::ArrayView;
1905  using Teuchos::RCP;
1906  using Teuchos::rcp;
1907 
1908  const char tfecfFuncName[] = "get1dCopy(A,LDA)";
1909  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(LDA < getLocalLength(), std::runtime_error,
1910  ": specified stride is not large enough for local vector length.");
1911  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Teuchos::as<size_t>(A.size()) < LDA*(getNumVectors()-1)+getLocalLength(), std::runtime_error,
1912  ": specified stride/storage is not large enough for the number of vectors.");
1913  RCP<Node> node = MVT::getNode(lclMV_);
1914  const size_t myStride = MVT::getStride(lclMV_),
1915  numCols = getNumVectors(),
1916  myLen = getLocalLength();
1917  if (myLen > 0) {
1918  ArrayRCP<const Scalar> mydata = MVT::getValues(lclMV_);
1919  KOKKOS_NODE_TRACE("MultiVector::getVectorNonConst()")
1920  ArrayRCP<const Scalar> myview = node->template viewBuffer<Scalar>(myStride*(numCols-1)+myLen,mydata);
1921  typename ArrayView<Scalar>::iterator Aptr = A.begin();
1922  for (size_t j=0; j<numCols; j++) {
1923  ArrayRCP<const Scalar> myviewj = getSubArrayRCP(myview,j);
1924  std::copy(myviewj,myviewj+myLen,Aptr);
1925  Aptr += LDA;
1926  }
1927  myview = Teuchos::null;
1928  mydata = Teuchos::null;
1929  }
1930  }
1931 
1932 
1933  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1934  void
1937  {
1938  using Teuchos::as;
1939  using Teuchos::ArrayRCP;
1940  using Teuchos::ArrayView;
1941  using Teuchos::RCP;
1942  using Teuchos::rcp;
1943 
1944  const char tfecfFuncName[] = "get2dCopy(ArrayOfPtrs)";
1945  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Teuchos::as<size_t>(ArrayOfPtrs.size()) != getNumVectors(), std::runtime_error,
1946  ": Array of pointers must contain as many pointers as the MultiVector has rows.");
1947  RCP<Node> node = MVT::getNode(lclMV_);
1948  const size_t numCols = getNumVectors(),
1949  myLen = getLocalLength();
1950  if (myLen > 0) {
1951  ArrayRCP<const Scalar> mybuff = MVT::getValues(lclMV_);
1952  KOKKOS_NODE_TRACE("MultiVector::get2dCopy()")
1953  ArrayRCP<const Scalar> myview = node->template viewBuffer<Scalar>(mybuff.size(), mybuff);
1954  for (size_t j=0; j<numCols; ++j) {
1955 #ifdef HAVE_TPETRA_DEBUG
1956  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(as<size_t>(ArrayOfPtrs[j].size()) != getLocalLength(), std::runtime_error,
1957  ": The ArrayView provided in ArrayOfPtrs[" << j << "] was not large enough to contain the local entries.");
1958 #endif
1959  ArrayRCP<const Scalar> myviewj = getSubArrayRCP(myview,j);
1960  std::copy(myviewj,myviewj+myLen,ArrayOfPtrs[j].begin());
1961  }
1962  myview = Teuchos::null;
1963  mybuff = Teuchos::null;
1964  }
1965  }
1966 
1967 
1968  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1971  {
1972  TEUCHOS_TEST_FOR_EXCEPTION(!isConstantStride(), std::runtime_error,
1973  "Tpetra::MultiVector::get1dView() requires that this MultiVector have constant stride.");
1974  Teuchos::RCP<Node> node = MVT::getNode(lclMV_);
1975  KOKKOS_NODE_TRACE("MultiVector::get1dView()")
1976  return node->template viewBuffer<Scalar>( getStride()*(getNumVectors()-1)+getLocalLength(), MVT::getValues(lclMV_) );
1977  }
1978 
1979 
1980  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1983  {
1984  TEUCHOS_TEST_FOR_EXCEPTION(!isConstantStride(), std::runtime_error,
1985  "Tpetra::MultiVector::get1dViewNonConst(): requires that this MultiVector have constant stride.");
1986  Teuchos::RCP<Node> node = MVT::getNode(lclMV_);
1987  KOKKOS_NODE_TRACE("MultiVector::get1dViewNonConst()")
1988  return node->template viewBufferNonConst<Scalar>( Kokkos::ReadWrite, getStride()*(getNumVectors()-1)+getLocalLength(), MVT::getValuesNonConst(lclMV_) );
1989  }
1990 
1991 
1992  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
1995  {
1996  using Teuchos::arcp;
1997  using Teuchos::ArrayRCP;
1998  using Teuchos::RCP;
1999 
2000  RCP<Node> node = MVT::getNode(lclMV_);
2001  ArrayRCP<ArrayRCP<Scalar> > views = arcp<ArrayRCP<Scalar> > (getNumVectors ());
2002  if (isConstantStride ()) {
2003  const size_t myStride = MVT::getStride(lclMV_),
2004  numCols = getNumVectors(),
2005  myLen = getLocalLength();
2006  if (myLen > 0) {
2007  KOKKOS_NODE_TRACE("MultiVector::get2dViewNonConst()")
2008  ArrayRCP<Scalar> myview = node->template viewBufferNonConst<Scalar>(Kokkos::ReadWrite,myStride*(numCols-1)+myLen,MVT::getValuesNonConst(lclMV_));
2009  for (size_t j=0; j<numCols; ++j) {
2010  views[j] = myview.persistingView(0,myLen);
2011  myview += myStride;
2012  }
2013  }
2014  }
2015  else {
2016  const size_t myStride = MVT::getStride(lclMV_),
2017  numCols = MVT::getNumCols(lclMV_),
2018  myCols = getNumVectors(),
2019  myLen = MVT::getNumRows(lclMV_);
2020  if (myLen > 0) {
2021  KOKKOS_NODE_TRACE("MultiVector::get2dViewNonConst()")
2022  ArrayRCP<Scalar> myview = node->template viewBufferNonConst<Scalar>(Kokkos::ReadWrite,myStride*(numCols-1)+myLen,MVT::getValuesNonConst(lclMV_));
2023  for (size_t j=0; j<myCols; ++j) {
2024  views[j] = myview.persistingView(whichVectors_[j]*myStride,myLen);
2025  }
2026  }
2027  }
2028  return views;
2029  }
2030 
2031 
2032  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2035  {
2036  using Teuchos::arcp;
2037  using Teuchos::ArrayRCP;
2038  using Teuchos::RCP;
2039 
2040  RCP<Node> node = MVT::getNode(lclMV_);
2041  ArrayRCP< ArrayRCP<const Scalar> > views = arcp<ArrayRCP<const Scalar> >(getNumVectors());
2042  if (isConstantStride()) {
2043  const size_t myStride = MVT::getStride(lclMV_),
2044  numCols = getNumVectors(),
2045  myLen = getLocalLength();
2046  if (myLen > 0) {
2047  KOKKOS_NODE_TRACE("MultiVector::get2dView()")
2048  ArrayRCP<const Scalar> myview = node->template viewBuffer<Scalar>(myStride*(numCols-1)+myLen,MVT::getValues(lclMV_));
2049  for (size_t j=0; j<numCols; ++j) {
2050  views[j] = myview.persistingView(0,myLen);
2051  myview += myStride;
2052  }
2053  }
2054  }
2055  else {
2056  const size_t myStride = MVT::getStride(lclMV_),
2057  numCols = MVT::getNumCols(lclMV_),
2058  myCols = getNumVectors(),
2059  myLen = MVT::getNumRows(lclMV_);
2060  if (myLen > 0) {
2061  KOKKOS_NODE_TRACE("MultiVector::get2dView()")
2062  ArrayRCP<const Scalar> myview = node->template viewBuffer<Scalar>(myStride*(numCols-1)+myLen,MVT::getValues(lclMV_));
2063  for (size_t j=0; j<myCols; ++j) {
2064  views[j] = myview.persistingView(whichVectors_[j]*myStride,myLen);
2065  }
2066  }
2067  }
2068  return views;
2069  }
2070 
2071 
2072  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2073  void
2076  Teuchos::ETransp transB,
2077  const Scalar &alpha,
2080  const Scalar &beta)
2081  {
2082  using Teuchos::NO_TRANS; // enums
2083  using Teuchos::TRANS;
2084  using Teuchos::CONJ_TRANS;
2085  using Teuchos::null;
2086  using Teuchos::ScalarTraits; // traits
2087  using Teuchos::as;
2088  using Teuchos::RCP;
2089  using Teuchos::rcp;
2091 
2092  // This routine performs a variety of matrix-matrix multiply operations, interpreting
2093  // the MultiVector (this-aka C , A and B) as 2D matrices. Variations are due to
2094  // the fact that A, B and C can be local replicated or global distributed
2095  // MultiVectors and that we may or may not operate with the transpose of
2096  // A and B. Possible cases are:
2097  // Num
2098  // OPERATIONS cases Notes
2099  // 1) C(local) = A^X(local) * B^X(local) 4 (X=Trans or Not, No comm needed)
2100  // 2) C(local) = A^T(distr) * B (distr) 1 (2D dot product, replicate C)
2101  // 3) C(distr) = A (distr) * B^X(local) 2 (2D vector update, no comm needed)
2102  //
2103  // The following operations are not meaningful for 1D distributions:
2104  //
2105  // u1) C(local) = A^T(distr) * B^T(distr) 1
2106  // u2) C(local) = A (distr) * B^X(distr) 2
2107  // u3) C(distr) = A^X(local) * B^X(local) 4
2108  // u4) C(distr) = A^X(local) * B^X(distr) 4
2109  // u5) C(distr) = A^T(distr) * B^X(local) 2
2110  // u6) C(local) = A^X(distr) * B^X(local) 4
2111  // u7) C(distr) = A^X(distr) * B^X(local) 4
2112  // u8) C(local) = A^X(local) * B^X(distr) 4
2113  //
2114  // Total of 32 case (2^5).
2115 
2116  const char errPrefix[] = "Tpetra::MultiVector::multiply(transOpA,transOpB,alpha,A,B,beta): ";
2117 
2118  TEUCHOS_TEST_FOR_EXCEPTION( ScalarTraits<Scalar>::isComplex && (transA == TRANS || transB == TRANS), std::invalid_argument,
2119  errPrefix << "non-conjugate transpose not supported for complex types.");
2120  transA = (transA == NO_TRANS ? NO_TRANS : CONJ_TRANS);
2121  transB = (transB == NO_TRANS ? NO_TRANS : CONJ_TRANS);
2122 
2123  // Compute effective dimensions, w.r.t. transpose operations on
2124  size_t A_nrows = (transA==CONJ_TRANS) ? A.getNumVectors() : A.getLocalLength();
2125  size_t A_ncols = (transA==CONJ_TRANS) ? A.getLocalLength() : A.getNumVectors();
2126  size_t B_nrows = (transB==CONJ_TRANS) ? B.getNumVectors() : B.getLocalLength();
2127  size_t B_ncols = (transB==CONJ_TRANS) ? B.getLocalLength() : B.getNumVectors();
2128 
2129  Scalar beta_local = beta; // local copy of beta; might be reassigned below
2130 
2131  TEUCHOS_TEST_FOR_EXCEPTION( getLocalLength() != A_nrows || getNumVectors() != B_ncols || A_ncols != B_nrows, std::runtime_error,
2132  errPrefix << "dimension of *this, op(A) and op(B) must be consistent.");
2133 
2134  bool A_is_local = !A.isDistributed();
2135  bool B_is_local = !B.isDistributed();
2136  bool C_is_local = !this->isDistributed();
2137  bool Case1 = ( C_is_local && A_is_local && B_is_local); // Case 1: C(local) = A^X(local) * B^X(local)
2138  bool Case2 = ( C_is_local && !A_is_local && !B_is_local && transA==CONJ_TRANS && transB==NO_TRANS); // Case 2: C(local) = A^T(distr) * B (distr)
2139  bool Case3 = (!C_is_local && !A_is_local && B_is_local && transA==NO_TRANS ); // Case 3: C(distr) = A (distr) * B^X(local)
2140 
2141  // Test that we are considering a meaningful cases
2142  TEUCHOS_TEST_FOR_EXCEPTION( !Case1 && !Case2 && !Case3, std::runtime_error,
2143  errPrefix << "multiplication of op(A) and op(B) into *this is not a supported use case.");
2144 
2145  if (beta != ScalarTraits<Scalar>::zero() && Case2)
2146  {
2147  // if Case2, then C is local and contributions must be summed across all nodes
2148  // however, if beta != 0, then accumulate beta*C into the sum
2149  // when summing across all nodes, we only want to accumulate this once, so
2150  // set beta == 0 on all nodes except node 0
2151  int MyPID = this->getMap()->getComm()->getRank();
2152  if (MyPID!=0) beta_local = ScalarTraits<Scalar>::zero();
2153  }
2154 
2155  // Check if A, B, C have constant stride, if not then make temp copy (strided)
2156  RCP<const MV> Atmp, Btmp;
2157  RCP<MV> Ctmp;
2158  if (isConstantStride() == false) Ctmp = rcp (new MV (*this));
2159  else Ctmp = rcp(this,false);
2160 
2161  if (A.isConstantStride() == false) Atmp = rcp (new MV (A));
2162  else Atmp = rcp(&A,false);
2163 
2164  if (B.isConstantStride() == false) Btmp = rcp (new MV (B));
2165  else Btmp = rcp(&B,false);
2166 
2167 #ifdef HAVE_TEUCHOS_DEBUG
2168  TEUCHOS_TEST_FOR_EXCEPTION(!Ctmp->isConstantStride() || !Btmp->isConstantStride() || !Atmp->isConstantStride(), std::logic_error,
2169  errPrefix << "failed making temporary strided copies of input multivectors.");
2170 #endif
2171 
2172  KMV &C_mv = Ctmp->lclMV_;
2173  {
2174  // get local multivectors
2175  const KMV &A_mv = Atmp->lclMV_;
2176  const KMV &B_mv = Btmp->lclMV_;
2177  // do the multiply (GEMM)
2178  MVT::GEMM(C_mv,transA,transB,alpha,A_mv,B_mv,beta_local);
2179  }
2180 
2181  // Dispose of (possibly) extra copies of A, B
2182  Atmp = null;
2183  Btmp = null;
2184 
2185  RCP<Node> node = MVT::getNode(lclMV_);
2186  // If *this was not strided, copy the data from the strided version and then delete it
2187  if (! isConstantStride ()) {
2188  // *this is not strided, we must put data from Ctmp into *this
2189  TEUCHOS_TEST_FOR_EXCEPT(&C_mv != &lclMV_);
2190  const size_t numVecs = MVT::getNumCols(lclMV_);
2191  for (size_t j=0; j < numVecs; ++j) {
2192  KOKKOS_NODE_TRACE("MultiVector::multiply()")
2193  node->template copyBuffers<Scalar>(getLocalLength(),MVT::getValues(C_mv,j),MVT::getValuesNonConst(lclMV_,whichVectors_[j]));
2194  }
2195  }
2196 
2197  // If Case 2 then sum up *this and distribute it to all processors.
2198  if (Case2) {
2199  this->reduce();
2200  }
2201  }
2202 
2203  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2204  void
2206  elementWiseMultiply (Scalar scalarAB,
2209  Scalar scalarThis)
2210  {
2211  using Teuchos::arcp_const_cast;
2212  const char tfecfFuncName[] = "elementWiseMultiply()";
2213 
2214 #ifdef HAVE_TPETRA_DEBUG
2216  ! this->getMap()->isCompatible(*A.getMap()) ||
2217  ! this->getMap()->isCompatible(*B.getMap()), std::runtime_error,
2218  ": MultiVectors do not have compatible Maps:" << std::endl
2219  << "this->getMap(): " << std::endl << *this->getMap()
2220  << "A.getMap(): " << std::endl << *A.getMap() << std::endl
2221  << "B.getMap(): " << std::endl << *B.getMap() << std::endl);
2222 #else
2224  getLocalLength() != A.getLocalLength() ||
2225  getLocalLength() != B.getLocalLength(), std::runtime_error,
2226  ": MultiVectors do not have the same local length.");
2227 #endif
2229  B.getNumVectors() != this->getNumVectors(), std::runtime_error,
2230  ": MultiVectors 'this' and B must have the same number of vectors.");
2231  try {
2232  MVT::ElemMult (lclMV_, scalarThis, scalarAB, (const KMV&) A.lclMV_,
2233  (const KMV&) B.lclMV_);
2234  }
2235  catch (std::runtime_error &e) {
2236  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(true,std::runtime_error,
2237  ": caught exception from Kokkos:" << std::endl
2238  << e.what() << std::endl);
2239  }
2240  }
2241 
2242  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2243  void
2245  {
2246  using Teuchos::Array;
2247  using Teuchos::ArrayView;
2248  using Teuchos::ArrayRCP;
2249  using Teuchos::as;
2250  using Teuchos::RCP;
2251  using Teuchos::reduceAll;
2252  using Teuchos::REDUCE_SUM;
2253 
2254  // This method should be called only for locally replicated
2255  // MultiVectors (! isDistributed ()).
2256  TEUCHOS_TEST_FOR_EXCEPTION(this->isDistributed (), std::runtime_error,
2257  "Tpetra::MultiVector::reduce() should only be called with locally "
2258  "replicated or otherwise not distributed MultiVector objects.");
2259  RCP<const Teuchos::Comm<int> > comm = this->getMap()->getComm();
2260  if (comm->getSize() == 1) return;
2261  RCP<Node> node = MVT::getNode(lclMV_);
2262  // sum the data across all multivectors
2263  // need to have separate packed buffers for send and receive
2264  // if we're packed, we'll just set the receive buffer as our data, the send as a copy
2265  // if we're not packed, we'll use allocated buffers for both.
2266  ArrayView<Scalar> target;
2267  const size_t myStride = MVT::getStride(lclMV_),
2268  numCols = MVT::getNumCols(lclMV_),
2269  myLen = MVT::getNumRows(lclMV_);
2270  Array<Scalar> sourceBuffer(numCols*myLen), tmparr(0);
2271  bool packed = isConstantStride() && (myStride == myLen);
2272  KOKKOS_NODE_TRACE("MultiVector::reduce()")
2273  ArrayRCP<Scalar> bufView = node->template viewBufferNonConst<Scalar>(
2274  Kokkos::ReadWrite,myStride*(numCols-1)+myLen,
2275  MVT::getValuesNonConst(lclMV_) );
2276  if (packed) {
2277  // copy data from view to sourceBuffer, reduce into view below
2278  target = bufView(0,myLen*numCols);
2279  std::copy(target.begin(),target.end(),sourceBuffer.begin());
2280  }
2281  else {
2282  // copy data into sourceBuffer, reduce from sourceBuffer into tmparr below, copy back to view after that
2283  tmparr.resize(myLen*numCols);
2284  Scalar *sptr = sourceBuffer.getRawPtr();
2285  ArrayRCP<const Scalar> vptr = bufView;
2286  for (size_t j=0; j<numCols; ++j) {
2287  std::copy(vptr,vptr+myLen,sptr);
2288  sptr += myLen;
2289  vptr += myStride;
2290  }
2291  target = tmparr();
2292  }
2293  // reduce
2294  reduceAll<int,Scalar> (*comm, REDUCE_SUM, as<int> (numCols*myLen),
2295  sourceBuffer.getRawPtr (), target.getRawPtr ());
2296  if (! packed) {
2297  // copy tmparr back into view
2298  const Scalar *sptr = tmparr.getRawPtr();
2299  ArrayRCP<Scalar> vptr = bufView;
2300  for (size_t j=0; j<numCols; ++j) {
2301  std::copy (sptr, sptr+myLen, vptr);
2302  sptr += myLen;
2303  vptr += myStride;
2304  }
2305  }
2306  bufView = Teuchos::null;
2307  }
2308 
2309 
2310  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2311  void
2313  replaceLocalValue (LocalOrdinal MyRow,
2314  size_t VectorIndex,
2315  const Scalar &ScalarValue)
2316  {
2317 #ifdef HAVE_TPETRA_DEBUG
2318  const LocalOrdinal minLocalIndex = this->getMap()->getMinLocalIndex();
2319  const LocalOrdinal maxLocalIndex = this->getMap()->getMaxLocalIndex();
2321  MyRow < minLocalIndex || MyRow > maxLocalIndex,
2322  std::runtime_error,
2323  "Tpetra::MultiVector::replaceLocalValue: row index " << MyRow
2324  << " is invalid. The range of valid row indices on this process "
2325  << this->getMap()->getComm()->getRank() << " is [" << minLocalIndex
2326  << ", " << maxLocalIndex << "].");
2328  vectorIndexOutOfRange(VectorIndex),
2329  std::runtime_error,
2330  "Tpetra::MultiVector::replaceLocalValue: vector index " << VectorIndex
2331  << " of the multivector is invalid.");
2332 #endif
2333  const size_t colInd = isConstantStride () ?
2334  VectorIndex : whichVectors_[VectorIndex];
2335  lclMV_.replaceLocalValue (Teuchos::as<size_t> (MyRow), colInd, ScalarValue);
2336  }
2337 
2338 
2339  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2340  void
2342  sumIntoLocalValue (LocalOrdinal MyRow,
2343  size_t VectorIndex,
2344  const Scalar &ScalarValue)
2345  {
2346 #ifdef HAVE_TPETRA_DEBUG
2347  const LocalOrdinal minLocalIndex = this->getMap()->getMinLocalIndex();
2348  const LocalOrdinal maxLocalIndex = this->getMap()->getMaxLocalIndex();
2350  MyRow < minLocalIndex || MyRow > maxLocalIndex,
2351  std::runtime_error,
2352  "Tpetra::MultiVector::sumIntoLocalValue: row index " << MyRow
2353  << " is invalid. The range of valid row indices on this process "
2354  << this->getMap()->getComm()->getRank() << " is [" << minLocalIndex
2355  << ", " << maxLocalIndex << "].");
2357  vectorIndexOutOfRange(VectorIndex),
2358  std::runtime_error,
2359  "Tpetra::MultiVector::sumIntoLocalValue: vector index " << VectorIndex
2360  << " of the multivector is invalid.");
2361 #endif
2362  const size_t colInd = isConstantStride () ?
2363  VectorIndex : whichVectors_[VectorIndex];
2364  lclMV_.sumIntoLocalValue (Teuchos::as<size_t> (MyRow), colInd, ScalarValue);
2365  }
2366 
2367 
2368  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2369  void
2371  replaceGlobalValue (GlobalOrdinal GlobalRow,
2372  size_t VectorIndex,
2373  const Scalar &ScalarValue)
2374  {
2375  LocalOrdinal MyRow = this->getMap()->getLocalElement(GlobalRow);
2376 #ifdef HAVE_TPETRA_DEBUG
2379  std::runtime_error,
2380  "Tpetra::MultiVector::replaceGlobalValue: global row index " << GlobalRow
2381  << "is not present on this process " << this->getMap()->getComm()->getRank()
2382  << ".");
2384  vectorIndexOutOfRange(VectorIndex),
2385  std::runtime_error,
2386  "Tpetra::MultiVector::replaceGlobalValue: vector index " << VectorIndex
2387  << " of the multivector is invalid.");
2388 #endif
2389  replaceLocalValue (MyRow, VectorIndex, ScalarValue);
2390  }
2391 
2392  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2393  void
2395  sumIntoGlobalValue (GlobalOrdinal GlobalRow,
2396  size_t VectorIndex,
2397  const Scalar &ScalarValue)
2398  {
2399  LocalOrdinal MyRow = this->getMap()->getLocalElement(GlobalRow);
2400 #ifdef HAVE_TEUCHOS_DEBUG
2403  std::runtime_error,
2404  "Tpetra::MultiVector::sumIntoGlobalValue: global row index " << GlobalRow
2405  << "is not present on this process " << this->getMap()->getComm()->getRank()
2406  << ".");
2408  vectorIndexOutOfRange(VectorIndex),
2409  std::runtime_error,
2410  "Tpetra::MultiVector::sumIntoGlobalValue: vector index " << VectorIndex
2411  << " of the multivector is invalid.");
2412 #endif
2413  sumIntoLocalValue (MyRow, VectorIndex, ScalarValue);
2414  }
2415 
2416  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2417  template <class T>
2421  size_t j) const
2422  {
2423  const size_t stride = MVT::getStride (lclMV_);
2424  const size_t myLen = getLocalLength ();
2426  if (isConstantStride()) {
2427  ret = arr.persistingView (j*stride, myLen);
2428  }
2429  else {
2430  ret = arr.persistingView (whichVectors_[j]*stride, myLen);
2431  }
2432  return ret;
2433  }
2434 
2435  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2436  const Kokkos::MultiVector<Scalar,Node>&
2438  return lclMV_;
2439  }
2440 
2441  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2442  Kokkos::MultiVector<Scalar,Node>&
2444  return lclMV_;
2445  }
2446 
2447  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2448  std::string
2450  {
2451  using std::endl;
2452  std::ostringstream oss;
2453  oss << Teuchos::typeName (*this) << " {" << endl
2454  << " label: \"" << this->getObjectLabel () << "\"" << endl
2455  << " numRows: " << getGlobalLength () << endl
2456  << " numCols: " << getNumVectors () << endl
2457  << " isConstantStride: " << isConstantStride () << endl;
2458  if (isConstantStride ()) {
2459  oss << " columnStride: " << getStride () << endl;
2460  }
2461  oss << "}" << endl;
2462  return oss.str();
2463  }
2464 
2465  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2466  void
2469  const Teuchos::EVerbosityLevel verbLevel) const
2470  {
2471  using Teuchos::ArrayRCP;
2472  using Teuchos::RCP;
2473  using Teuchos::VERB_DEFAULT;
2474  using Teuchos::VERB_NONE;
2475  using Teuchos::VERB_LOW;
2476  using Teuchos::VERB_MEDIUM;
2477  using Teuchos::VERB_HIGH;
2478  using Teuchos::VERB_EXTREME;
2479  using std::endl;
2480  using std::setw;
2481 
2482  // Set default verbosity if applicable.
2483  const Teuchos::EVerbosityLevel vl =
2484  (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
2485 
2486  RCP<const Teuchos::Comm<int> > comm = this->getMap()->getComm();
2487  const int myImageID = comm->getRank();
2488  const int numImages = comm->getSize();
2489 
2490  if (vl != VERB_NONE) {
2491  // Don't set the tab level unless we're printing something.
2492  Teuchos::OSTab tab (out);
2493 
2494  if (myImageID == 0) { // >= VERB_LOW prints description()
2495  out << this->description() << endl;
2496  }
2497  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
2498  if (myImageID == imageCtr) {
2499  if (vl != VERB_LOW) {
2500  // At verbosity > VERB_LOW, each process prints something.
2501  out << "Process " << myImageID << ":" << endl;
2502 
2503  Teuchos::OSTab procTab (out);
2504  // >= VERB_MEDIUM: print the local vector length.
2505  out << "local length=" << getLocalLength();
2506  if (vl != VERB_MEDIUM) {
2507  // >= VERB_HIGH: print isConstantStride() and getStride()
2508  if (isConstantStride()) {
2509  out << ", constant stride=" << getStride() << endl;
2510  }
2511  else {
2512  out << ", not constant stride" << endl;
2513  }
2514  if (vl == VERB_EXTREME) {
2515  // VERB_EXTREME: print all the values in the multivector.
2516  out << "Values:" << endl;
2518  for (size_t i = 0; i < getLocalLength(); ++i) {
2519  for (size_t j = 0; j < getNumVectors(); ++j) {
2520  out << X[j][i];
2521  if (j < getNumVectors() - 1) {
2522  out << " ";
2523  }
2524  } // for each column
2525  out << endl;
2526  } // for each row
2527  } // if vl == VERB_EXTREME
2528  } // if (vl != VERB_MEDIUM)
2529  else { // vl == VERB_LOW
2530  out << endl;
2531  }
2532  } // if vl != VERB_LOW
2533  } // if it is my process' turn to print
2534  comm->barrier();
2535  } // for each process in the communicator
2536  } // if vl != VERB_NONE
2537  }
2538 
2539  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2540  void
2543  {
2545  TPETRA_EFFICIENCY_WARNING(! cview_.is_null (), std::runtime_error,
2546  "::createViews(): The const view "
2547  "has already been created and is therefore not null. (For"
2548  "Tpetra developers: cview_.total_count() = " << cview_.total_count ()
2549  << ". This "
2550  "means that MultiVector is either creating a view unnecessarily, or "
2551  "hanging on to a view beyond its needed scope (since releaseViews() "
2552  "should always release both the const and nonconst views). This probably "
2553  "does not affect correctness, it but does affect total memory use. "
2554  "We will only report this warning once per (Multi)Vector instance. "
2555  "Please report this performance bug to the Tpetra developers.");
2557  }
2558 
2559  Teuchos::RCP<Node> node = this->getMap ()->getNode ();
2560  if (cview_.is_null () && getLocalLength () > 0) {
2561  Teuchos::ArrayRCP<const Scalar> buff = MVT::getValues (lclMV_);
2562  KOKKOS_NODE_TRACE("MultiVector::createViews()")
2563  cview_ = node->template viewBuffer<Scalar> (buff.size (), buff);
2564  }
2565  }
2566 
2567  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2568  void
2570  createViewsNonConst (Kokkos::ReadWriteOption rwo)
2571  {
2573  TPETRA_EFFICIENCY_WARNING(! ncview_.is_null (), std::runtime_error,
2574  "::createViewsNonConst(): The nonconst view "
2575  "has already been created and is therefore not null. (For"
2576  "Tpetra developers: ncview_.total_count() = " << ncview_.total_count ()
2577  << ". This means that MultiVector is either creating a view "
2578  "unnecessarily, or hanging on to a view beyond its needed scope (since "
2579  "releaseViews() should always release both the const and nonconst "
2580  "views). This probably does not affect correctness, it but does affect "
2581  "total memory use. "
2582  "Please report this performance bug to the Tpetra developers.");
2584  }
2585 
2586  Teuchos::RCP<Node> node = this->getMap ()->getNode ();
2587  if (ncview_.is_null () && getLocalLength () > 0) {
2588  Teuchos::ArrayRCP<Scalar> buff = MVT::getValuesNonConst (lclMV_);
2589  KOKKOS_NODE_TRACE("MultiVector::createViewsNonConst()")
2590  ncview_ = node->template viewBufferNonConst<Scalar> (rwo, buff.size (), buff);
2591  }
2592  }
2593 
2594  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2595  void
2597  {
2598  const int constViewCount = cview_.total_count ();
2599  const int nonconstViewCount = ncview_.total_count ();
2600 
2601  // This prevents unused variable compiler warnings, in case Tpetra
2602  // efficiency warnings aren't enabled.
2603  (void) constViewCount;
2604  (void) nonconstViewCount;
2605 
2606  const bool viewWontGetReleased = constViewCount > 1 || nonconstViewCount > 1;
2607  if (viewWontGetReleased && ! releaseViewsRaisedEfficiencyWarning_) {
2608  const bool both = constViewCount > 1 && nonconstViewCount > 1;
2609  const char* const text = both ? "Both the const view and the nonconst view have" :
2610  ((constViewCount > 1) ? "The const view has" : "The nonconst view has");
2611  // Prevent (unused variable) compiler warning, since the macro
2612  // below doesn't exist unless efficiency warnings are enabled.
2613  (void) text;
2614 
2615  TPETRA_EFFICIENCY_WARNING(viewWontGetReleased, std::runtime_error,
2616  "::releaseViews(): " << text << " a reference count greater than 1. "
2617  "For Tpetra developers: cview_.total_count() = " << constViewCount
2618  << " and ncview_.total_count() = " << nonconstViewCount << ". This "
2619  "means that releaseViews() won't actually free memory. This probably "
2620  "does not affect correctness, it but does affect total memory use. "
2621  "We will only report this warning once per (Multi)Vector instance. "
2622  "Please report this performance bug to the Tpetra developers.");
2624  }
2625 
2626  // Release the views.
2627  cview_ = Teuchos::null;
2628  ncview_ = Teuchos::null;
2629  }
2630 
2631 
2632  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
2633  void
2636  {
2637  replaceMap (newMap);
2638  }
2639 
2640 } // namespace Tpetra
2641 
2642 //
2643 // Explicit instantiation macro
2644 //
2645 // Must be expanded from within the Tpetra namespace!
2646 //
2647 
2648 #define TPETRA_MULTIVECTOR_INSTANT(SCALAR,LO,GO,NODE) \
2649  \
2650  template class MultiVector< SCALAR , LO , GO , NODE >; \
2651 
2652 
2653 #endif // TPETRA_MULTIVECTOR_DEF_HPP
Teuchos::RCP< const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > subView(const Teuchos::Range1D &colRng) const
Return a const MultiVector with const views of selected columns.
Teuchos::RCP< const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > offsetView(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &subMap, size_t offset) const
Return a const MultiVector view of a subset of rows.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with the given verbosity level to a FancyOStream.
ArrayRCP< Scalar > ncview_
Nonconst host view created in createViewsNonConst().
void releaseViews() const
Hook for releasing views.
virtual std::string getObjectLabel() const
void norm2(const Teuchos::ArrayView< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
bool is_null(const boost::shared_ptr< T > &p)
ArrayRCP< T > persistingView(size_type lowerOffset, size_type size) const
Teuchos::ArrayRCP< Scalar > get1dViewNonConst()
Nonconst persisting (1-D) view of this multivector&#39;s local values.
void sumIntoGlobalValue(GlobalOrdinal globalRow, size_t vectorIndex, const Scalar &value)
Add value to existing value, using global (row) index.
iterator begin() const
void putScalar(const Scalar &value)
Set all values in the multivector with the given value.
size_t getNumVectors() const
Number of columns in the multivector.
size_t getLocalLength() const
Local number of rows on the calling process.
bool empty() const
void replaceLocalValue(LocalOrdinal myRow, size_t vectorIndex, const Scalar &value)
Replace value, using local (row) index.
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
Teuchos::ArrayRCP< Teuchos::ArrayRCP< Scalar > > get2dViewNonConst()
Return non-const persisting pointers to values.
void replaceGlobalValue(GlobalOrdinal globalRow, size_t vectorIndex, const Scalar &value)
Replace value, using global (row) index.
bool isConstantStride() const
Whether this multivector has constant stride between columns.
static T squareroot(T x)
One or more distributed dense vectors.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > subCopy(const Teuchos::Range1D &colRng) const
Return a MultiVector with copies of selected columns.
bool createViewsNonConstRaisedEfficiencyWarning_
Whether createViewsNonConst() has yet raised an efficiency warning.
size_type size() const
bool isDistributed() const
Whether this is a globally distributed object.
void replaceMap(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
Replace the underlying Map in place.
virtual ~MultiVector()
Destructor (virtual for memory safety of derived classes).
void get1dCopy(Teuchos::ArrayView< Scalar > A, size_t LDA) const
Fill the given array with a copy of this multivector&#39;s local values.
size_type size() const
void get2dCopy(Teuchos::ArrayView< const Teuchos::ArrayView< Scalar > > ArrayOfPtrs) const
Fill the given array with a copy of this multivector&#39;s local values.
void sumIntoLocalValue(LocalOrdinal myRow, size_t vectorIndex, const Scalar &value)
Add value to existing value, using local (row) index.
Teuchos::RCP< Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVectorNonConst(size_t j)
Return a Vector which is a nonconst view of column j.
bool createViewsRaisedEfficiencyWarning_
Whether createViews() has yet raised an efficiency warning.
void createViewsNonConst(Kokkos::ReadWriteOption rwo)
Hook for creating a nonconst view.
Ordinal ubound() const
virtual void copyAndPermute(const SrcDistObject &sourceObj, size_t numSameIDs, const ArrayView< const LocalOrdinal > &permuteToLIDs, const ArrayView< const LocalOrdinal > &permuteFromLIDs)
Perform copies and permutations that are local to this process.
Teuchos::ArrayRCP< const Scalar > getData(size_t j) const
Const view of the local values in a particular vector of this multivector.
Teuchos::RCP< const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVector(size_t j) const
Return a Vector which is a const view of column j.
size_t global_size_t
Global size_t object.
void normInf(const Teuchos::ArrayView< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
Compute Inf-norm of each vector in multi-vector.
T * getRawPtr() const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void scale(const Scalar &alpha)
Scale in place: this = alpha*this.
void multiply(Teuchos::ETransp transA, Teuchos::ETransp transB, const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B, const Scalar &beta)
Matrix-matrix multiplication: this = beta*this + alpha*op(A)*op(B).
void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)
Update: this = beta*this + alpha*A.
void elementWiseMultiply(Scalar scalarAB, const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B, Scalar scalarThis)
Multiply a Vector A elementwise by a MultiVector B.
ArrayRCP< T > getSubArrayRCP(ArrayRCP< T > arr, size_t j) const
Persisting view of j-th column in the given ArrayRCP.
void norm1(const Teuchos::ArrayView< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
Compute 1-norm of each vector in multi-vector.
void normWeighted(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &weights, const Teuchos::ArrayView< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
Sets up and executes a communication plan for a Tpetra DistObject.
CombineMode
Tpetra::Combine Mode enumerable type.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getMap() const
The Map describing the parallel distribution of this object.
bool releaseViewsRaisedEfficiencyWarning_
Whether releaseViews() has yet raised an efficiency warning.
Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > subViewNonConst(const Teuchos::Range1D &colRng)
Return a MultiVector with views of selected columns.
int total_count() const
virtual size_t constantNumberOfPackets() const
Number of packets to send per LID.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
ArrayRCP< const Scalar > cview_
Const host view created in createViews().
void dot(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Teuchos::ArrayView< Scalar > &dots) const
Compute dot product of each corresponding pair of vectors, dots[i] = this[i].dot(A[i]) ...
Abstract base class for objects that can be the source of an Import or Export operation.
Teuchos::ArrayRCP< Scalar > getDataNonConst(size_t j)
View of the local values in a particular vector of this multivector.
void resize(size_type new_size, const value_type &x=value_type())
MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & operator=(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &source)
Assign the contents of source to this multivector (deep copy).
T * getRawPtr() const
#define TPETRA_EFFICIENCY_WARNING(throw_exception_test, Exception, msg)
Print or throw an efficency warning.
void randomize()
Set all values in the multivector to pseudorandom numbers.
iterator end()
virtual void unpackAndCombine(const ArrayView< const LocalOrdinal > &importLIDs, const ArrayView< const Scalar > &imports, const ArrayView< size_t > &numPacketsPerLID, size_t constantNumPackets, Distributor &distor, CombineMode CM)
Perform any unpacking and combining after communication.
Ordinal lbound() const
const Kokkos::MultiVector< Scalar, Node > & getLocalMV() const
Return a const reference to the underlying Kokkos::MultiVector object (advanced use only) ...
KMV lclMV_
The Kokkos::MultiVector containing the compute buffer of data.
Describes a parallel distribution of objects over processes.
void meanValue(const Teuchos::ArrayView< Scalar > &means) const
Compute mean (average) value of each vector in multi-vector. The outcome of this routine is undefined...
iterator end() const
Ordinal size() const
size_type size() const
TypeTo as(const TypeFrom &t)
size_t getStride() const
Stride between columns in the multivector.
MultiVector(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t NumVectors, bool zeroOut=true)
Basic constuctor.
A distributed dense vector.
Stand-alone utility functions and macros.
virtual std::string description() const
A simple one-line description of this object.
void reduce()
Sum values of a locally replicated multivector across all processes.
virtual bool checkSizes(const SrcDistObject &sourceObj)
Whether data redistribution between sourceObj and this object is legal.
Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > offsetViewNonConst(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &subMap, size_t offset)
Return a non-const MultiVector view of a subset of rows.
Teuchos::ArrayRCP< Teuchos::ArrayRCP< const Scalar > > get2dView() const
Return const persisting pointers to values.
void reciprocal(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A)
Put element-wise reciprocal values of input Multi-vector in target, this(i,j) = 1/A(i,j).
Base class for distributed Tpetra objects that support data redistribution.
bool is_null() const
Array< size_t > whichVectors_
Indices of columns this multivector is viewing.
iterator begin()
global_size_t getGlobalLength() const
Global number of rows in the multivector.
void abs(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A)
Put element-wise absolute values of input Multi-vector in target: A = abs(this)
Teuchos::ArrayRCP< const Scalar > get1dView() const
Const persisting (1-D) view of this multivector&#39;s local values.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
std::string typeName(const T &t)
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > map_
The Map over which this object is distributed.
Kokkos::MultiVector< Scalar, Node > & getLocalMVNonConst()
Return a non-const reference to the underlying Kokkos::MultiVector object (advanced use only) ...
virtual void packAndPrepare(const SrcDistObject &sourceObj, const ArrayView< const LocalOrdinal > &exportLIDs, Array< Scalar > &exports, const ArrayView< size_t > &numExportPacketsPerLID, size_t &constantNumPackets, Distributor &distor)
Perform any packing or preparation required for communication.
void createViews() const
Hook for creating a const view.
virtual void removeEmptyProcessesInPlace(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &newMap)
Remove processes owning zero rows from the Map and their communicator.