42 #ifndef TPETRA_EXPERIMENTAL_BLOCKMULTIVECTOR_DEF_HPP 43 #define TPETRA_EXPERIMENTAL_BLOCKMULTIVECTOR_DEF_HPP 45 #include "Tpetra_Experimental_BlockMultiVector_decl.hpp" 60 template<
class MultiVectorType>
61 struct RawPtrFromMultiVector {
62 typedef typename MultiVectorType::impl_scalar_type impl_scalar_type;
64 static impl_scalar_type* getRawPtr (MultiVectorType& X) {
65 typedef typename MultiVectorType::dual_view_type dual_view_type;
66 typedef typename dual_view_type::t_host::memory_space host_memory_space;
71 X.template modify<host_memory_space> ();
73 dual_view_type X_view = X.getDualView ();
74 impl_scalar_type* X_raw = X_view.h_view.ptr_on_device ();
91 template<
class S,
class LO,
class GO,
class N>
95 return RawPtrFromMultiVector<MV>::getRawPtr (X);
101 namespace Experimental {
103 template<
class Scalar,
class LO,
class GO,
class Node>
104 typename BlockMultiVector<Scalar, LO, GO, Node>::mv_type
105 BlockMultiVector<Scalar, LO, GO, Node>::
106 getMultiVectorView ()
109 mv_.setCopyOrView (Teuchos::View);
115 template<
class Scalar,
class LO,
class GO,
class Node>
116 Teuchos::RCP<const BlockMultiVector<Scalar, LO, GO, Node> >
121 const BMV* src_bmv =
dynamic_cast<const BMV*
> (&src);
122 TEUCHOS_TEST_FOR_EXCEPTION(
123 src_bmv == NULL, std::invalid_argument,
"Tpetra::Experimental::" 124 "BlockMultiVector: The source object of an Import or Export to a " 125 "BlockMultiVector, must also be a BlockMultiVector.");
126 return Teuchos::rcp (src_bmv,
false);
129 template<
class Scalar,
class LO,
class GO,
class Node>
136 pointMap_ (makePointMap (meshMap, blockSize)),
137 mv_ (
Teuchos::rcpFromRef (pointMap_), numVecs),
138 mvData_ (getRawPtrFromMultiVector (mv_)),
139 blockSize_ (blockSize)
145 template<
class Scalar,
class LO,
class GO,
class Node>
153 pointMap_ (pointMap),
154 mv_ (
Teuchos::rcpFromRef (pointMap_), numVecs),
155 mvData_ (getRawPtrFromMultiVector (
mv_)),
156 blockSize_ (blockSize)
162 template<
class Scalar,
class LO,
class GO,
class Node>
166 const LO blockSize) :
170 blockSize_ (blockSize)
186 RCP<const mv_type> X_view_const;
189 Teuchos::Array<size_t> cols (0);
190 X_view_const = X_mv.
subView (cols ());
192 X_view_const = X_mv.
subView (Teuchos::Range1D (0, numCols-1));
194 TEUCHOS_TEST_FOR_EXCEPTION(
195 X_view_const.is_null (), std::logic_error,
"Tpetra::Experimental::" 196 "BlockMultiVector constructor: X_mv.subView(...) returned null. This " 197 "should never happen. Please report this bug to the Tpetra developers.");
202 RCP<mv_type> X_view = Teuchos::rcp_const_cast<
mv_type> (X_view_const);
204 TEUCHOS_TEST_FOR_EXCEPTION(
205 X_view->getCopyOrView () != Teuchos::View, std::logic_error,
"Tpetra::" 206 "Experimental::BlockMultiVector constructor: We just set a MultiVector " 207 "to have view semantics, but it claims that it doesn't have view " 208 "semantics. This should never happen. " 209 "Please report this bug to the Tpetra developers.");
214 Teuchos::RCP<const map_type> pointMap =
mv_.
getMap ();
215 if (! pointMap.is_null ()) {
216 pointMap_ = *pointMap;
218 mvData_ = getRawPtrFromMultiVector (
mv_);
221 template<
class Scalar,
class LO,
class GO,
class Node>
226 const size_t offset) :
228 meshMap_ (newMeshMap),
229 pointMap_ (newPointMap),
231 mvData_ (getRawPtrFromMultiVector (
mv_)),
238 template<
class Scalar,
class LO,
class GO,
class Node>
242 const size_t offset) :
244 meshMap_ (newMeshMap),
247 mvData_ (getRawPtrFromMultiVector (
mv_)),
254 template<
class Scalar,
class LO,
class GO,
class Node>
265 template<
class Scalar,
class LO,
class GO,
class Node>
271 typedef typename Teuchos::ArrayView<const GO>::size_type size_type;
273 const GST gblNumMeshMapInds =
275 const size_t lclNumMeshMapIndices =
277 const GST gblNumPointMapInds =
278 gblNumMeshMapInds *
static_cast<GST
> (blockSize);
279 const size_t lclNumPointMapInds =
280 lclNumMeshMapIndices *
static_cast<size_t> (blockSize);
284 return map_type (gblNumPointMapInds, lclNumPointMapInds, indexBase,
292 const size_type lclNumMeshGblInds = lclMeshGblInds.size ();
293 Teuchos::Array<GO> lclPointGblInds (lclNumPointMapInds);
294 for (size_type g = 0; g < lclNumMeshGblInds; ++g) {
295 const GO meshGid = lclMeshGblInds[g];
296 const GO pointGidStart = indexBase +
297 (meshGid - indexBase) * static_cast<GO> (blockSize);
298 const size_type offset = g *
static_cast<size_type
> (blockSize);
299 for (LO k = 0; k < blockSize; ++k) {
300 const GO pointGid = pointGidStart +
static_cast<GO
> (k);
301 lclPointGblInds[offset +
static_cast<size_type
> (k)] = pointGid;
304 return map_type (gblNumPointMapInds, lclPointGblInds (), indexBase,
310 template<
class Scalar,
class LO,
class GO,
class Node>
315 const Scalar vals[])
const 318 const LO strideX = 1;
325 template<
class Scalar,
class LO,
class GO,
class Node>
330 const Scalar vals[])
const 335 replaceLocalValuesImpl (localRowIndex, colIndex, vals);
340 template<
class Scalar,
class LO,
class GO,
class Node>
345 const Scalar vals[])
const 348 if (localRowIndex == Teuchos::OrdinalTraits<LO>::invalid ()) {
351 replaceLocalValuesImpl (localRowIndex, colIndex, vals);
356 template<
class Scalar,
class LO,
class GO,
class Node>
361 const Scalar vals[])
const 364 const LO strideX = 1;
367 AXPY (STS::one (), X_src, X_dst);
370 template<
class Scalar,
class LO,
class GO,
class Node>
375 const Scalar vals[])
const 380 sumIntoLocalValuesImpl (localRowIndex, colIndex, vals);
385 template<
class Scalar,
class LO,
class GO,
class Node>
390 const Scalar vals[])
const 393 if (localRowIndex == Teuchos::OrdinalTraits<LO>::invalid ()) {
396 sumIntoLocalValuesImpl (localRowIndex, colIndex, vals);
401 template<
class Scalar,
class LO,
class GO,
class Node>
415 template<
class Scalar,
class LO,
class GO,
class Node>
421 if (localRowIndex == Teuchos::OrdinalTraits<LO>::invalid ()) {
430 template<
class Scalar,
class LO,
class GO,
class Node>
434 const LO colIndex)
const 441 const size_t offset = colIndex * this->
getStrideY () +
442 localRowIndex * blockSize * strideX;
443 impl_scalar_type* blockRaw = this->
getRawPtr () + offset;
448 template<
class Scalar,
class LO,
class GO,
class Node>
449 Teuchos::RCP<const typename BlockMultiVector<Scalar, LO, GO, Node>::mv_type>
454 using Teuchos::rcpFromRef;
461 const this_type* srcBlkVec =
dynamic_cast<const this_type*
> (&src);
462 if (srcBlkVec == NULL) {
464 if (srcMultiVec == NULL) {
470 return rcp (srcMultiVec,
false);
473 return rcpFromRef (srcBlkVec->mv_);
477 template<
class Scalar,
class LO,
class GO,
class Node>
481 return ! getMultiVectorFromSrcDistObject (src).is_null ();
484 template<
class Scalar,
class LO,
class GO,
class Node>
488 const Teuchos::ArrayView<const LO>& permuteToLIDs,
489 const Teuchos::ArrayView<const LO>& permuteFromLIDs)
491 const char tfecfFuncName[] =
"copyAndPermute: ";
492 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
493 permuteToLIDs.size() != permuteFromLIDs.size(), std::runtime_error,
494 "permuteToLIDs and permuteFromLIDs must have the same size." 495 << std::endl <<
"permuteToLIDs.size() = " << permuteToLIDs.size ()
496 <<
" != permuteFromLIDs.size() = " << permuteFromLIDs.size () <<
".");
499 Teuchos::RCP<const BMV> srcAsBmvPtr = getBlockMultiVectorFromSrcDistObject (src);
500 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
501 srcAsBmvPtr.is_null (), std::invalid_argument,
502 "The source of an Import or Export to a BlockMultiVector " 503 "must also be a BlockMultiVector.");
504 const BMV& srcAsBmv = *srcAsBmvPtr;
510 const LO numSame =
static_cast<LO
> (numSameIDs);
511 for (LO j = 0; j < numVecs; ++j) {
512 for (LO lclRow = 0; lclRow < numSame; ++lclRow) {
520 const LO numPermuteLIDs =
static_cast<LO
> (permuteToLIDs.size ());
521 for (LO j = 0; j < numVecs; ++j) {
522 for (LO k = numSame; k < numPermuteLIDs; ++k) {
528 template<
class Scalar,
class LO,
class GO,
class Node>
531 const Teuchos::ArrayView<const LO>& exportLIDs,
532 Teuchos::Array<impl_scalar_type>& exports,
533 const Teuchos::ArrayView<size_t>& ,
534 size_t& constantNumPackets,
538 typedef typename Teuchos::ArrayView<const LO>::size_type size_type;
539 const char tfecfFuncName[] =
"packAndPrepare: ";
541 Teuchos::RCP<const BMV> srcAsBmvPtr = getBlockMultiVectorFromSrcDistObject (src);
542 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
543 srcAsBmvPtr.is_null (), std::invalid_argument,
544 "The source of an Import or Export to a BlockMultiVector " 545 "must also be a BlockMultiVector.");
546 const BMV& srcAsBmv = *srcAsBmvPtr;
555 static_cast<size_t> (blockSize) * static_cast<size_t> (numVecs);
556 const size_type numMeshLIDs = exportLIDs.size ();
558 const size_type requiredExportsSize = numMeshLIDs *
559 static_cast<size_type
> (blockSize) * static_cast<size_type> (numVecs);
560 exports.resize (requiredExportsSize);
563 size_type curExportPos = 0;
564 for (size_type meshLidIndex = 0; meshLidIndex < numMeshLIDs; ++meshLidIndex) {
565 for (LO j = 0; j < numVecs; ++j, curExportPos += blockSize) {
566 const LO meshLid = exportLIDs[meshLidIndex];
567 impl_scalar_type*
const curExportPtr = &exports[curExportPos];
574 }
catch (std::exception& e) {
575 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
576 true, std::logic_error,
"Oh no! packAndPrepare on Process " 577 << meshMap_.
getComm ()->getRank () <<
" raised the following exception: " 582 template<
class Scalar,
class LO,
class GO,
class Node>
585 const Teuchos::ArrayView<const impl_scalar_type>& imports,
586 const Teuchos::ArrayView<size_t>& numPacketsPerLID,
587 size_t constantNumPackets,
591 typedef typename Teuchos::ArrayView<const LO>::size_type size_type;
592 const char tfecfFuncName[] =
"unpackAndCombine: ";
594 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
596 std::invalid_argument,
"Invalid CombineMode: " << CM <<
". Valid " 597 "CombineMode values are ADD, REPLACE, INSERT, ABSMAX, and ZERO.");
605 const size_type numMeshLIDs = importLIDs.size ();
609 const size_type requiredImportsSize = numMeshLIDs *
610 static_cast<size_type
> (blockSize) * static_cast<size_type> (numVecs);
611 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
612 imports.size () < requiredImportsSize, std::logic_error,
613 ": imports.size () = " << imports.size ()
614 <<
" < requiredImportsSize = " << requiredImportsSize <<
".");
616 size_type curImportPos = 0;
617 for (size_type meshLidIndex = 0; meshLidIndex < numMeshLIDs; ++meshLidIndex) {
618 for (LO j = 0; j < numVecs; ++j, curImportPos += blockSize) {
619 const LO meshLid = importLIDs[meshLidIndex];
620 const impl_scalar_type*
const curImportPtr = &imports[curImportPos];
627 }
else if (CM ==
ADD) {
628 AXPY (STS::one (), X_src, X_dst);
629 }
else if (CM ==
ABSMAX) {
630 Impl::absMax (X_dst, X_src);
636 template<
class Scalar,
class LO,
class GO,
class Node>
643 template<
class Scalar,
class LO,
class GO,
class Node>
658 #define TPETRA_EXPERIMENTAL_BLOCKMULTIVECTOR_INSTANT(S,LO,GO,NODE) \ 659 template class Experimental::BlockMultiVector< S, LO, GO, NODE >; 661 #endif // TPETRA_EXPERIMENTAL_BLOCKMULTIVECTOR_DEF_HPP void scale(const Scalar &alpha)
Scale in place: this = alpha*this.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
mv_type mv_
The Tpetra::MultiVector used to represent the data.
bool isContiguous() const
True if this Map is distributed contiguously, else false.
void putScalar(const Scalar &val)
Fill all entries with the given value val.
mv_type getMultiVectorView()
Get a Tpetra::MultiVector that views this BlockMultiVector's data.
Tpetra::Map< LO, GO, Node > map_type
The specialization of Tpetra::Map that this class uses.
bool replaceGlobalValues(const GO globalRowIndex, const LO colIndex, const Scalar vals[]) const
Replace all values at the given mesh point, using a global index.
void setCopyOrView(const Teuchos::DataAccess copyOrView)
Set whether this has copy (copyOrView = Teuchos::Copy) or view (copyOrView = Teuchos::View) semantics...
void AXPY(const CoefficientType &alpha, const ViewType1 &x, const ViewType2 &y)
y := y + alpha * x (dense vector or matrix update)
One or more distributed dense vectors.
bool sumIntoGlobalValues(const GO globalRowIndex, const LO colIndex, const Scalar vals[]) const
Sum into all values at the given mesh point, using a global index.
MultiVector for multiple degrees of freedom per mesh point.
GlobalOrdinal getIndexBase() const
The index base for this Map.
static map_type makePointMap(const map_type &meshMap, const LO blockSize)
Create and return the point Map corresponding to the given mesh Map and block size.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
virtual void unpackAndCombine(const Teuchos::ArrayView< const LO > &importLIDs, const Teuchos::ArrayView< const impl_scalar_type > &imports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t constantNumPackets, Tpetra::Distributor &distor, Tpetra::CombineMode CM)
Perform any unpacking and combining after communication.
void deep_copy(const LittleBlock< ST2, LO > &dst, const LittleBlock< ST1, LO > &src, typename std::enable_if< std::is_convertible< ST1, ST2 >::value &&!std::is_const< ST2 >::value, int >::type *=NULL)
Copy the LittleBlock src into the LittleBlock dst.
void scale(const Scalar &val)
Multiply all entries in place by the given value val.
little_vec_type getLocalBlock(const LO localRowIndex, const LO colIndex) const
Get a view of the degrees of freedom at the given mesh point.
bool isNodeLocalElement(LocalOrdinal localIndex) const
Whether the given local index is valid for this Map on the calling process.
LO getBlockSize() const
Get the number of degrees of freedom per mesh point.
size_t global_size_t
Global size_t object.
Insert new values that don't currently exist.
impl_scalar_type * getRawPtr() const
Raw pointer to the MultiVector's data.
Nonowning view of a set of degrees of freedom corresponding to a mesh point in a block vector or mult...
LO getNumVectors() const
Get the number of columns (vectors) in the BlockMultiVector.
Sets up and executes a communication plan for a Tpetra DistObject.
CombineMode
Rule for combining data in an Import or Export.
Sum new values into existing values.
Teuchos::RCP< Node > getNode() const
Get this Map's Node object.
bool getLocalRowView(const LO localRowIndex, const LO colIndex, Scalar *&vals) const
Get a writeable view of the entries at the given mesh point, using a local index. ...
Replace old value with maximum of magnitudes of old and new values.
Abstract base class for objects that can be the source of an Import or Export operation.
bool isValidLocalMeshIndex(const LO meshLocalIndex) const
True if and only if meshLocalIndex is a valid local index in the mesh Map.
Scalar * ptr_on_device() const
Pointer to the vector's entries.
Replace existing values with new values.
Replace old values with zero.
Teuchos::DataAccess getCopyOrView() const
Get whether this has copy (copyOrView = Teuchos::Copy) or view (copyOrView = Teuchos::View) semantics...
void putScalar(const Scalar &value)
Set all values in the multivector with the given value.
Kokkos::Details::ArithTraits< Scalar >::val_type impl_scalar_type
The type used internally in place of Scalar.
LocalOrdinal getLocalElement(GlobalOrdinal globalIndex) const
The local index corresponding to the given global index.
Teuchos::ArrayView< const GlobalOrdinal > getNodeElementList() const
Return a view of the global indices owned by this process.
virtual bool checkSizes(const Tpetra::SrcDistObject &source)
Compare the source and target (this) objects for compatibility.
size_t getNodeNumElements() const
The number of elements belonging to the calling process.
LittleVector< impl_scalar_type, LO > little_vec_type
"Block view" of all degrees of freedom at a mesh point, for a single column of the MultiVector...
size_t getStrideY() const
Stride between consecutive local entries in the same row.
BlockMultiVector()
Default constructor.
virtual void copyAndPermute(const Tpetra::SrcDistObject &source, size_t numSameIDs, const Teuchos::ArrayView< const LO > &permuteToLIDs, const Teuchos::ArrayView< const LO > &permuteFromLIDs)
Perform copies and permutations that are local to this process.
size_t getNumVectors() const
Number of columns in the multivector.
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
bool replaceLocalValues(const LO localRowIndex, const LO colIndex, const Scalar vals[]) const
Replace all values at the given mesh point, using local row and column indices.
Teuchos::RCP< const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > > subView(const Teuchos::Range1D &colRng) const
Return a const MultiVector with const views of selected columns.
size_t getStrideX() const
Stride between consecutive local entries in the same column.
virtual void packAndPrepare(const Tpetra::SrcDistObject &source, const Teuchos::ArrayView< const LO > &exportLIDs, Teuchos::Array< impl_scalar_type > &exports, const Teuchos::ArrayView< size_t > &numPacketsPerLID, size_t &constantNumPackets, Tpetra::Distributor &distor)
Perform any packing or preparation required for communication.
bool sumIntoLocalValues(const LO localRowIndex, const LO colIndex, const Scalar vals[]) const
Sum into all values at the given mesh point, using a local index.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
bool getGlobalRowView(const GO globalRowIndex, const LO colIndex, Scalar *&vals) const
Get a writeable view of the entries at the given mesh point, using a global index.