41 #ifndef __Tpetra_MultiVectorFiller_hpp
42 #define __Tpetra_MultiVectorFiller_hpp
44 #include <Tpetra_MultiVector.hpp>
45 #include <Teuchos_CommHelpers.hpp>
72 sortAndMergeIn (Teuchos::Array<T>& allEntries,
73 Teuchos::ArrayView<T> currentEntries,
74 Teuchos::ArrayView<T> newEntries)
76 using Teuchos::ArrayView;
78 typedef typename ArrayView<T>::iterator iter_type;
79 typedef typename ArrayView<T>::size_type size_type;
81 std::unique (newEntries.begin(), newEntries.end());
82 iter_type it = std::unique (newEntries.begin(), newEntries.end());
83 const size_type numNew = as<size_type> (it - newEntries.begin());
85 ArrayView<T> newEntriesView = newEntries.view (0, numNew);
87 const size_type numCur = currentEntries.size();
88 if (allEntries.size() < numCur + numNew) {
89 allEntries.resize (numCur + numNew);
91 ArrayView<T> allView = allEntries.view (0, numCur + numNew);
92 ArrayView<T> newView = allEntries.view (numCur, numNew);
94 std::copy (newEntries.begin(), newEntries.end(), newView.begin());
95 std::inplace_merge (allView.begin(), newView.begin(), allView.end());
96 iter_type it2 = std::unique (allView.begin(), allView.end());
97 const size_type numTotal = as<size_type> (it2 - allView.begin());
99 return allEntries.view (0, numTotal);
110 typedef typename MV::scalar_type scalar_type;
111 typedef typename MV::local_ordinal_type local_ordinal_type;
112 typedef typename MV::global_ordinal_type global_ordinal_type;
113 typedef typename MV::node_type node_type;
150 const size_t numColumns) :
152 numCols_ (numColumns),
153 sourceIndices_ (numColumns),
154 sourceValues_ (numColumns)
161 const size_t oldNumColumns = getNumColumns();
162 if (newNumColumns >= oldNumColumns) {
163 for (
size_t j = oldNumColumns; j < newNumColumns; ++j) {
164 sourceIndices_.push_back (Teuchos::Array<global_ordinal_type> ());
165 sourceValues_.push_back (Teuchos::Array<scalar_type> ());
170 sourceIndices_.resize (newNumColumns);
171 sourceValues_.resize (newNumColumns);
173 numCols_ = oldNumColumns;
177 sumIntoGlobalValues (Teuchos::ArrayView<const global_ordinal_type> rows,
179 Teuchos::ArrayView<const scalar_type> values)
181 if (column >= getNumColumns()) {
182 for (
size_t j = column; j < getNumColumns(); ++j) {
183 sourceIndices_.push_back (Teuchos::Array<global_ordinal_type> ());
184 sourceValues_.push_back (Teuchos::Array<scalar_type> ());
187 std::copy (rows.begin(), rows.end(), std::back_inserter (sourceIndices_[column]));
188 std::copy (values.begin(), values.end(), std::back_inserter (sourceValues_[column]));
200 Teuchos::ArrayView<const scalar_type> values)
202 typedef global_ordinal_type GO;
203 typedef scalar_type ST;
204 typedef typename Teuchos::ArrayView<const GO>::const_iterator GoIter;
205 typedef typename Teuchos::ArrayView<const ST>::const_iterator StIter;
207 const size_t numColumns = getNumColumns();
208 GoIter rowIter = rows.begin();
209 StIter valIter = values.begin();
210 for (
size_t j = 0; j < numColumns; ++j) {
211 GoIter rowIterNext = rowIter + numColumns;
212 StIter valIterNext = valIter + numColumns;
213 std::copy (rowIter, rowIterNext, std::back_inserter (sourceIndices_[j]));
214 std::copy (valIter, valIterNext, std::back_inserter (sourceValues_[j]));
215 rowIter = rowIterNext;
216 valIter = valIterNext;
244 template<
class BinaryFunction>
248 using Teuchos::Array;
249 using Teuchos::ArrayRCP;
250 using Teuchos::ArrayView;
252 typedef local_ordinal_type LO;
253 typedef global_ordinal_type GO;
254 typedef scalar_type ST;
255 typedef node_type NT;
257 RCP<const Tpetra::Map<LO, GO, NT> > map = X.getMap();
258 Array<LO> localIndices;
259 const size_t numColumns = getNumColumns();
260 for (
size_t j = 0; j < numColumns; ++j) {
261 const typename Array<const GO>::size_type numIndices = sourceIndices_[j].size();
266 if (localIndices.size() < numIndices) {
267 localIndices.resize (numIndices);
269 ArrayView<LO> localIndicesView = localIndices.view (0, numIndices);
270 ArrayView<const GO> globalIndicesView = sourceIndices_[j].view (0, numIndices);
271 for (
typename ArrayView<const GO>::size_type i = 0; i < numIndices; ++i) {
272 localIndices[i] = map->getLocalElement (globalIndicesView[i]);
275 ArrayRCP<ST> X_j = X.getDataNonConst (j);
276 ArrayView<const ST> localValues = sourceValues_[j].view (0, numIndices);
277 for (
typename ArrayView<const GO>::size_type i = 0; i < numIndices; ++i) {
278 const LO localInd = localIndices[i];
279 X_j[localInd] = f (X_j[localInd], localValues[i]);
289 locallyAssemble<std::plus<scalar_type> > (X, f);
294 Teuchos::Array<Teuchos::Array<global_ordinal_type> > newSourceIndices;
295 Teuchos::Array<Teuchos::Array<scalar_type> > newSourceValues;
297 std::swap (sourceIndices_, newSourceIndices);
298 std::swap (sourceValues_, newSourceValues);
302 Teuchos::Array<global_ordinal_type>
305 using Teuchos::Array;
306 using Teuchos::ArrayView;
308 typedef global_ordinal_type GO;
309 typedef typename Array<GO>::size_type size_type;
311 Array<GO> allInds (0);
312 const size_t numCols = getNumColumns();
319 const size_type numNew = sourceIndices_[0].size();
320 allInds.resize (allInds.size() + numNew);
321 std::copy (sourceIndices_[0].begin(), sourceIndices_[0].end(),
323 std::sort (allInds.begin(), allInds.end());
324 typename Array<GO>::iterator it =
325 std::unique (allInds.begin(), allInds.end());
326 const size_type numFinal = as<size_type> (it - allInds.begin());
327 allInds.resize (numFinal);
335 ArrayView<GO> curIndsView = allInds.view (0, 0);
337 for (
size_t j = 0; j < numCols; ++j) {
338 const size_type numNew = sourceIndices_[j].size();
339 if (numNew > newInds.size()) {
340 newInds.resize (numNew);
342 ArrayView<GO> newIndsView = newInds.view (0, numNew);
343 std::copy (sourceIndices_[j].begin(), sourceIndices_[j].end(),
344 newIndsView.begin());
345 curIndsView = sortAndMergeIn<GO> (allInds, curIndsView, newIndsView);
352 Teuchos::RCP<const map_type> map_;
354 Teuchos::Array<Teuchos::Array<global_ordinal_type> > sourceIndices_;
355 Teuchos::Array<Teuchos::Array<scalar_type> > sourceValues_;
357 size_t getNumColumns()
const {
return numCols_; }
368 typedef typename MV::scalar_type scalar_type;
369 typedef typename MV::local_ordinal_type local_ordinal_type;
370 typedef typename MV::global_ordinal_type global_ordinal_type;
371 typedef typename MV::node_type node_type;
386 const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_DEFAULT,
387 const Teuchos::RCP<Teuchos::FancyOStream>& out=Teuchos::null) :
390 verbLevel_ (verbLevel),
412 const size_t numColumns,
413 const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_DEFAULT,
414 const Teuchos::RCP<Teuchos::FancyOStream>& out=Teuchos::null) :
416 numCols_ (numColumns),
417 localVec_ (new MV (map, numColumns)),
418 nonlocalIndices_ (numColumns),
419 nonlocalValues_ (numColumns),
420 verbLevel_ (verbLevel),
427 std::ostringstream oss;
428 oss <<
"Tpetra::MultiVectorFillerData2<"
429 << Teuchos::TypeNameTraits<MV>::name () <<
">";
434 describe (Teuchos::FancyOStream& out,
435 const Teuchos::EVerbosityLevel verbLevel =
436 Teuchos::Describable::verbLevel_default)
const
439 using Teuchos::Array;
440 using Teuchos::ArrayRCP;
441 using Teuchos::ArrayView;
443 using Teuchos::VERB_DEFAULT;
444 using Teuchos::VERB_NONE;
445 using Teuchos::VERB_LOW;
446 using Teuchos::VERB_MEDIUM;
447 using Teuchos::VERB_HIGH;
448 using Teuchos::VERB_EXTREME;
451 const Teuchos::EVerbosityLevel vl =
452 (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
454 RCP<const Teuchos::Comm<int> > comm = map_->getComm();
455 const int myImageID = comm->getRank();
456 const int numImages = comm->getSize();
458 if (vl != VERB_NONE) {
460 Teuchos::OSTab tab (out);
462 if (myImageID == 0) {
463 out << this->description() << endl;
465 for (
int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
466 if (myImageID == imageCtr) {
467 if (vl != VERB_LOW) {
469 out <<
"Process " << myImageID <<
":" << endl;
471 Teuchos::OSTab procTab (out);
473 out <<
"local length=" << localVec_->getLocalLength();
474 if (vl != VERB_MEDIUM) {
476 if (localVec_->isConstantStride()) {
477 out <<
", constant stride=" << localVec_->getStride() << endl;
480 out <<
", not constant stride" << endl;
482 if (vl == VERB_EXTREME) {
484 out <<
"Local values:" << endl;
485 ArrayRCP<ArrayRCP<const scalar_type> > X = localVec_->get2dView();
486 for (
size_t i = 0; i < localVec_->getLocalLength(); ++i) {
487 for (
size_t j = 0; j < localVec_->getNumVectors(); ++j) {
489 if (j < localVec_->getNumVectors() - 1) {
496 out <<
"Nonlocal indices and values:" << endl;
497 for (
size_t j = 0; j < (size_t)nonlocalIndices_.size(); ++j) {
498 ArrayView<const global_ordinal_type> inds = nonlocalIndices_[j]();
499 ArrayView<const scalar_type> vals = nonlocalValues_[j]();
501 for (
typename ArrayView<const global_ordinal_type>::size_type k = 0; k < inds.size(); ++k) {
502 out <<
"X(" << inds[k] <<
"," << j <<
") = " << vals[k] << endl;
517 Teuchos::Array<global_ordinal_type>
520 using Teuchos::Array;
521 using Teuchos::ArrayView;
525 typedef global_ordinal_type GO;
529 Array<GO> nonlocalIndices = getSortedUniqueNonlocalIndices();
532 ArrayView<const GO> localIndices = getLocalIndices ();
538 Array<GO> indices (localIndices.size() + nonlocalIndices.size());
539 ArrayView<GO> localIndView = indices.view (0, localIndices.size());
540 std::copy (localIndices.begin(), localIndices.end(), localIndView.begin());
541 std::sort (localIndView.begin(), localIndView.end());
544 if (nonlocalIndices.size() > 0) {
545 typedef typename ArrayView<GO>::iterator iter_type;
547 iter_type middle = localIndView.end();
553 ArrayView<GO> indView = indices.view (0, indices.size());
556 std::copy (nonlocalIndices.begin(), nonlocalIndices.end(), middle);
557 std::inplace_merge (indView.begin(), middle, indView.end());
574 using Teuchos::Array;
575 using Teuchos::Range1D;
578 const size_t oldNumColumns = numCols_;
579 if (newNumColumns == oldNumColumns) {
584 if (newNumColumns > oldNumColumns) {
585 newLocalVec = rcp (
new MV (map_, newNumColumns));
589 RCP<MV> newLocalVecView =
590 newLocalVec->subViewNonConst (Range1D (0, oldNumColumns-1));
591 *newLocalVecView = *localVec_;
594 if (newNumColumns == 0) {
597 newLocalVec = Teuchos::null;
601 localVec_->subViewNonConst (Range1D (0, newNumColumns-1));
606 nonlocalIndices_.resize (newNumColumns);
607 nonlocalValues_.resize (newNumColumns);
608 localVec_ = newLocalVec;
609 numCols_ = newNumColumns;
616 Teuchos::ArrayView<const scalar_type> values)
618 using Teuchos::ArrayRCP;
619 using Teuchos::ArrayView;
620 typedef local_ordinal_type LO;
621 typedef global_ordinal_type GO;
622 typedef scalar_type ST;
624 if (columnIndex >= getNumColumns()) {
630 typename ArrayView<const GO>::const_iterator rowIter = rows.begin();
631 typename ArrayView<const ST>::const_iterator valIter = values.begin();
632 for ( ; rowIter != rows.end() && valIter != values.end(); ++rowIter, ++valIter) {
633 const GO globalRowIndex = *rowIter;
639 const LO localRowIndex = map_->getLocalElement (globalRowIndex);
640 if (localRowIndex == Teuchos::OrdinalTraits<LO>::invalid()) {
641 nonlocalIndices_[columnIndex].push_back (globalRowIndex);
642 nonlocalValues_[columnIndex].push_back (*valIter);
650 ArrayRCP<ST> X_j = localVec_->getDataNonConst (columnIndex);
654 X_j[localRowIndex] += *valIter;
670 Teuchos::ArrayView<const scalar_type> values)
672 using Teuchos::ArrayView;
673 typedef typename ArrayView<const global_ordinal_type>::size_type size_type;
675 const size_t numCols = getNumColumns();
676 for (
size_t j = 0; j < numCols; ++j) {
677 const size_type offset = numCols*j;
678 const size_type len = numCols;
707 template<
class BinaryFunction>
711 using Teuchos::ArrayRCP;
712 using Teuchos::ArrayView;
713 using Teuchos::FancyOStream;
714 using Teuchos::getFancyOStream;
715 using Teuchos::oblackholestream;
720 typedef local_ordinal_type LO;
721 typedef global_ordinal_type GO;
722 typedef scalar_type ST;
725 RCP<FancyOStream> out = out_.is_null() ?
726 getFancyOStream (rcp (
new oblackholestream)) : out_;
728 Teuchos::OSTab tab (out);
729 *out <<
"locallyAssemble:" << endl;
731 RCP<const map_type> srcMap = X.getMap();
732 ArrayView<const GO> localIndices = map_->getNodeElementList ();
734 for (
size_t j = 0; j < X.getNumVectors(); ++j) {
735 ArrayRCP<ST> X_j = X.getDataNonConst (j);
738 ArrayRCP<const ST> local_j = localVec_->getDataNonConst (j);
739 for (
typename ArrayView<const GO>::const_iterator it = localIndices.begin();
740 it != localIndices.end(); ++it) {
741 const LO rowIndLocal = map_->getLocalElement (*it);
742 const LO rowIndX = srcMap->getLocalElement (*it);
744 TEUCHOS_TEST_FOR_EXCEPTION(rowIndX == Teuchos::OrdinalTraits<LO>::invalid(),
745 std::invalid_argument,
"locallyAssemble(): Input multivector X does "
746 "not own the global index " << *it <<
". This probably means that "
747 "X was not constructed with the right Map.");
748 X_j[rowIndX] = f (X_j[rowIndX], local_j[rowIndLocal]);
752 ArrayView<const GO> nonlocalIndices = nonlocalIndices_[j]();
753 typename ArrayView<const GO>::const_iterator indexIter = nonlocalIndices.begin();
754 ArrayView<const ST> nonlocalValues = nonlocalValues_[j]();
755 typename ArrayView<const ST>::const_iterator valueIter = nonlocalValues.begin();
756 for ( ; indexIter != nonlocalIndices.end() && valueIter != nonlocalValues.end();
757 ++indexIter, ++valueIter) {
758 const LO rowIndX = srcMap->getLocalElement (*indexIter);
759 X_j[rowIndX] = f (X_j[rowIndX], *valueIter);
763 *out <<
"Locally assembled vector:" << endl;
764 X.describe (*out, Teuchos::VERB_EXTREME);
772 locallyAssemble<std::plus<scalar_type> > (X, f);
781 Teuchos::Array<Teuchos::Array<global_ordinal_type> > newNonlocalIndices;
782 Teuchos::Array<Teuchos::Array<scalar_type> > newNonlocalValues;
786 std::swap (nonlocalIndices_, newNonlocalIndices);
787 std::swap (nonlocalValues_, newNonlocalValues);
792 if (! localVec_.is_null()) {
793 localVec_->putScalar (Teuchos::ScalarTraits<scalar_type>::zero());
799 Teuchos::RCP<const map_type> map_;
805 Teuchos::RCP<MV> localVec_;
808 Teuchos::Array<Teuchos::Array<global_ordinal_type> > nonlocalIndices_;
811 Teuchos::Array<Teuchos::Array<scalar_type> > nonlocalValues_;
814 Teuchos::EVerbosityLevel verbLevel_;
817 Teuchos::RCP<Teuchos::FancyOStream> out_;
820 size_t getNumColumns()
const {
return numCols_; }
823 Teuchos::ArrayView<const global_ordinal_type>
824 getLocalIndices()
const
826 return map_->getNodeElementList ();
830 Teuchos::Array<global_ordinal_type>
831 getSortedUniqueNonlocalIndices()
const
833 using Teuchos::Array;
834 using Teuchos::ArrayView;
836 typedef global_ordinal_type GO;
837 typedef typename Array<GO>::size_type size_type;
839 Array<GO> allNonlocals (0);
840 const size_t numCols = getNumColumns();
847 const size_type numNew = nonlocalIndices_[0].size();
848 allNonlocals.resize (allNonlocals.size() + numNew);
849 std::copy (nonlocalIndices_[0].begin(), nonlocalIndices_[0].end(),
850 allNonlocals.begin());
851 std::sort (allNonlocals.begin(), allNonlocals.end());
852 typename Array<GO>::iterator it =
853 std::unique (allNonlocals.begin(), allNonlocals.end());
854 const size_type numFinal = as<size_type> (it - allNonlocals.begin());
855 allNonlocals.resize (numFinal);
863 ArrayView<GO> curNonlocalsView = allNonlocals.view (0, 0);
864 Array<GO> newNonlocals;
865 for (
size_t j = 0; j < numCols; ++j) {
866 const size_type numNew = nonlocalIndices_[j].size();
867 if (numNew > newNonlocals.size()) {
868 newNonlocals.resize (numNew);
870 ArrayView<GO> newNonlocalsView = newNonlocals.view (0, numNew);
871 std::copy (nonlocalIndices_[j].begin(), nonlocalIndices_[j].end(),
872 newNonlocalsView.begin());
873 curNonlocalsView = sortAndMergeIn<GO> (allNonlocals, curNonlocalsView,
912 typedef typename MV::scalar_type scalar_type;
913 typedef typename MV::local_ordinal_type local_ordinal_type;
914 typedef typename MV::global_ordinal_type global_ordinal_type;
915 typedef typename MV::node_type node_type;
948 const size_t numCols);
969 void globalAssemble (MV& X_out,
const bool forceReuseMap =
false);
972 describe (Teuchos::FancyOStream& out,
973 const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default)
const
975 data_.describe (out, verbLevel);
991 Teuchos::ArrayView<const scalar_type> values)
993 data_.sumIntoGlobalValues (rows, column, values);
1013 Teuchos::ArrayView<const scalar_type> values)
1015 data_.sumIntoGlobalValues (rows, values);
1020 Teuchos::RCP<const map_type> ctorMap_;
1026 Teuchos::RCP<const map_type> sourceMap_;
1033 Teuchos::RCP<const map_type> targetMap_;
1047 Teuchos::RCP<MV> sourceVec_;
1056 Teuchos::RCP<export_type> exporter_;
1063 void locallyAssemble (MV& X_in) {
1064 data_.locallyAssemble (X_in);
1068 Teuchos::Array<global_ordinal_type> getSourceIndices ()
const {
1069 return data_.getSourceIndices();
1080 Teuchos::RCP<const map_type>
1081 computeSourceMap (
const global_ordinal_type indexBase,
1082 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1083 const Teuchos::RCP<node_type>& node);
1089 sourceMap_ (Teuchos::null),
1090 targetMap_ (Teuchos::null),
1091 data_ (map, numCols),
1092 exporter_ (Teuchos::null)
1096 Teuchos::RCP<const typename MultiVectorFiller<MV>::map_type>
1099 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1100 const Teuchos::RCP<node_type>& node)
1102 using Teuchos::Array;
1103 using Teuchos::ArrayView;
1105 typedef global_ordinal_type GO;
1107 Array<GO> indices = getSourceIndices ();
1113 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1114 return rcp (
new map_type (invalid, indices, indexBase, comm, node));
1121 using Teuchos::ArrayView;
1122 using Teuchos::Array;
1123 using Teuchos::Range1D;
1127 const size_t numVecs = X_out.getNumVectors();
1142 RCP<const map_type> targetMap;
1143 bool assumeSameTargetMap =
false;
1144 if (targetMap_.is_null()) {
1145 targetMap_ = X_out.getMap();
1146 targetMap = targetMap_;
1147 assumeSameTargetMap =
false;
1150 if (forceReuseMap) {
1151 targetMap = targetMap_;
1152 assumeSameTargetMap =
true;
1160 if (targetMap_->isSameAs (*(X_out.getMap()))) {
1161 assumeSameTargetMap =
true;
1162 targetMap = targetMap_;
1171 RCP<const map_type> sourceMap;
1172 bool computedSourceMap =
false;
1174 if (forceReuseMap && ! sourceMap_.is_null()) {
1175 sourceMap = sourceMap_;
1178 sourceMap = computeSourceMap (ctorMap_->getIndexBase(),
1179 ctorMap_->getComm(),
1180 ctorMap_->getNode());
1181 computedSourceMap =
true;
1184 if (computedSourceMap) {
1185 sourceMap_ = sourceMap;
1191 const bool mustComputeExport =
1192 (exporter_.is_null() || (assumeSameTargetMap && ! computedSourceMap));
1193 if (mustComputeExport) {
1194 exporter_ = rcp (
new export_type (sourceMap_, targetMap_));
1199 const bool mustReallocateVec = sourceVec_.is_null() ||
1200 sourceVec_->getNumVectors() < numVecs || ! assumeSameTargetMap;
1202 if (mustReallocateVec) {
1204 sourceVec_ = rcp (
new MV (sourceMap_, numVecs));
1207 if (sourceVec_->getNumVectors() == numVecs) {
1210 X_in = sourceVec_->subViewNonConst (Range1D (0, numVecs-1));
1216 locallyAssemble (*X_in);
1220 X_out.doExport (*X_in, *exporter_, combineMode);
1233 typedef typename MV::scalar_type scalar_type;
1234 typedef typename MV::local_ordinal_type local_ordinal_type;
1235 typedef typename MV::global_ordinal_type global_ordinal_type;
1236 typedef typename MV::node_type node_type;
1249 const global_ordinal_type eltSize,
1250 const size_t numCols,
1251 const Teuchos::RCP<Teuchos::FancyOStream>& outStream=Teuchos::null,
1252 const Teuchos::EVerbosityLevel verbosityLevel=Teuchos::VERB_DEFAULT)
1254 using Teuchos::Array;
1255 using Teuchos::ArrayRCP;
1256 using Teuchos::ArrayView;
1258 using Teuchos::Comm;
1259 using Teuchos::FancyOStream;
1260 using Teuchos::getFancyOStream;
1261 using Teuchos::oblackholestream;
1265 using Teuchos::rcpFromRef;
1266 using Teuchos::REDUCE_SUM;
1267 using Teuchos::reduceAll;
1271 typedef local_ordinal_type LO;
1272 typedef global_ordinal_type GO;
1273 typedef scalar_type ST;
1274 typedef Teuchos::ScalarTraits<ST> STS;
1276 TEUCHOS_TEST_FOR_EXCEPTION(eltSize % 2 == 0, std::invalid_argument,
1277 "Element size (eltSize) argument must be odd.");
1278 TEUCHOS_TEST_FOR_EXCEPTION(numCols == 0, std::invalid_argument,
1279 "Number of columns (numCols) argument must be nonzero.");
1281 RCP<FancyOStream> out = outStream.is_null() ?
1282 getFancyOStream (rcp (
new oblackholestream)) : outStream;
1283 const Teuchos::EVerbosityLevel verbLevel =
1284 (verbosityLevel == Teuchos::VERB_DEFAULT) ?
1285 Teuchos::VERB_NONE : verbosityLevel;
1289 const GO indexBase = targetMap->getIndexBase();
1290 Array<GO> rows (eltSize);
1291 Array<ST> values (eltSize);
1292 std::fill (values.begin(), values.end(), STS::one());
1296 RCP<MultiVectorFiller<MV> > filler =
1299 TEUCHOS_TEST_FOR_EXCEPTION(! targetMap->isContiguous(),
1300 std::invalid_argument,
"MultiVectorFiller test currently only works "
1301 "for contiguous Maps.");
1303 const GO minGlobalIndex = targetMap->getMinGlobalIndex();
1304 const GO maxGlobalIndex = targetMap->getMaxGlobalIndex();
1305 const GO minAllGlobalIndex = targetMap->getMinAllGlobalIndex();
1306 const GO maxAllGlobalIndex = targetMap->getMaxAllGlobalIndex();
1307 for (
size_t j = 0; j < numCols; ++j) {
1308 for (GO i = minGlobalIndex; i <= maxGlobalIndex; ++i) {
1310 const GO start = std::max (i - eltSize/2, minAllGlobalIndex);
1311 const GO end = std::min (i + eltSize/2, maxAllGlobalIndex);
1312 const GO len = end - start + 1;
1314 TEUCHOS_TEST_FOR_EXCEPTION(len > eltSize, std::logic_error,
1315 "At start,end = " << start <<
"," << end <<
", len = " << len
1316 <<
" > eltSize = " << eltSize <<
".");
1318 for (GO k = 0; k < len; ++k) {
1319 rows[k] = start + k;
1321 if (verbLevel == Teuchos::VERB_EXTREME) {
1322 *out <<
"Inserting: "
1323 << Teuchos::toString (rows.view(0,len)) <<
", "
1324 << Teuchos::toString (values.view(0, len)) << std::endl;
1326 filler->sumIntoGlobalValues (rows.view(0, len), j, values.view(0, len));
1330 if (verbLevel == Teuchos::VERB_EXTREME) {
1331 *out <<
"Filler:" << std::endl;
1332 filler->describe (*out, verbLevel);
1336 MV X_out (targetMap, numCols);
1337 filler->globalAssemble (X_out);
1338 filler = Teuchos::null;
1340 if (verbLevel == Teuchos::VERB_EXTREME) {
1341 *out <<
"X_out:" << std::endl;
1342 X_out.describe (*out, verbLevel);
1347 MV X_expected (targetMap, numCols);
1348 const scalar_type two = STS::one() + STS::one();
1349 for (
size_t j = 0; j < numCols; ++j) {
1350 ArrayRCP<ST> X_j = X_expected.getDataNonConst (j);
1355 for (GO i = minGlobalIndex; i <= maxGlobalIndex; ++i) {
1356 const LO localIndex = targetMap->getLocalElement (i);
1357 TEUCHOS_TEST_FOR_EXCEPTION(i == Teuchos::OrdinalTraits<LO>::invalid(),
1358 std::logic_error,
"Global index " << i <<
" is not in the "
1359 "multivector's Map.");
1361 if (i <= minAllGlobalIndex + eltSize/2) {
1362 X_j[localIndex] = two + as<ST>(i) - as<ST>(indexBase);
1364 else if (i >= maxAllGlobalIndex - eltSize/2) {
1365 X_j[localIndex] = two + as<ST>(maxAllGlobalIndex) - as<ST>(i);
1368 X_j[localIndex] = as<ST>(eltSize);
1373 if (verbLevel == Teuchos::VERB_EXTREME) {
1374 *out <<
"X_expected:" << std::endl;
1375 X_expected.describe (*out, verbLevel);
1379 Array<GO> errorLocations;
1380 for (
size_t j = 0; j < numCols; ++j) {
1381 ArrayRCP<const ST> X_out_j = X_out.getData (j);
1382 ArrayRCP<const ST> X_expected_j = X_expected.getData (j);
1387 for (GO i = minGlobalIndex; i <= maxGlobalIndex; ++i) {
1388 const LO localIndex = targetMap->getLocalElement (i);
1389 TEUCHOS_TEST_FOR_EXCEPTION(i == Teuchos::OrdinalTraits<LO>::invalid(),
1390 std::logic_error,
"Global index " << i <<
" is not in the "
1391 "multivector's Map.");
1395 if (X_out_j[localIndex] != X_expected_j[localIndex]) {
1396 errorLocations.push_back (i);
1400 const typename Array<GO>::size_type localNumErrors = errorLocations.size();
1401 typename Array<GO>::size_type globalNumErrors = 0;
1402 RCP<const Comm<int> > comm = targetMap->getComm();
1403 reduceAll (*comm, REDUCE_SUM, localNumErrors, ptr (&globalNumErrors));
1405 if (globalNumErrors != 0) {
1406 std::ostringstream os;
1407 os <<
"Proc " << comm->getRank() <<
": " << localNumErrors
1408 <<
" incorrect value" << (localNumErrors != 1 ?
"s" :
"")
1409 <<
". Error locations: [ ";
1410 std::copy (errorLocations.begin(), errorLocations.end(),
1411 std::ostream_iterator<GO> (os,
" "));
1415 for (
int p = 0; p < comm->getSize(); ++p) {
1416 if (p == comm->getRank()) {
1417 cerr << os.str() << endl;
1424 TEUCHOS_TEST_FOR_EXCEPTION(globalNumErrors != 0, std::logic_error,
1425 "Over all procs: " << globalNumErrors <<
" total error"
1426 << (globalNumErrors != 1 ?
"s" :
"") <<
".");
1433 template<
class ScalarType,
1434 class LocalOrdinalType,
1435 class GlobalOrdinalType,
1438 testMultiVectorFiller (
const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1439 const Teuchos::RCP<NodeType>& node,
1440 const size_t unknownsPerNode,
1441 const GlobalOrdinalType unknownsPerElt,
1442 const size_t numCols,
1443 const Teuchos::RCP<Teuchos::FancyOStream>& outStream,
1444 const Teuchos::EVerbosityLevel verbLevel)
1447 using Teuchos::FancyOStream;
1448 using Teuchos::getFancyOStream;
1449 using Teuchos::oblackholestream;
1450 using Teuchos::ParameterList;
1456 typedef ScalarType ST;
1457 typedef LocalOrdinalType LO;
1458 typedef GlobalOrdinalType GO;
1459 typedef NodeType NT;
1463 RCP<FancyOStream> out = outStream.is_null() ?
1464 getFancyOStream (rcp (
new oblackholestream)) : outStream;
1466 Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid();
1467 RCP<const MT> targetMap =
1468 createContigMapWithNode<LO, GO, NT> (invalid, unknownsPerNode, comm, node);
1470 std::ostringstream os;
1473 }
catch (std::exception& e) {
1477 for (
int p = 0; p < comm->getSize(); ++p) {
1478 if (p == comm->getRank()) {
1479 *out <<
"On process " << comm->getRank() <<
": " << os.str() << endl;
1493 testSortAndMergeIn ();
1499 #endif // __Tpetra_MultiVectorFiller_hpp
Teuchos::Array< global_ordinal_type > getSourceIndices() const
All source indices (local and nonlocal) of the source Map, sorted and unique.
void locallyAssemble(MV &X)
locallyAssemble() for the usual Tpetra::ADD combine mode.
void sumIntoGlobalValues(Teuchos::ArrayView< const global_ordinal_type > rows, Teuchos::ArrayView< const scalar_type > values)
Set entry (rows[i],j) to values[i], for all i and j.
static void testSameMap(const Teuchos::RCP< const map_type > &targetMap, const global_ordinal_type eltSize, const size_t numCols, const Teuchos::RCP< Teuchos::FancyOStream > &outStream=Teuchos::null, const Teuchos::EVerbosityLevel verbosityLevel=Teuchos::VERB_DEFAULT)
Test global assembly when constructor Map = target Map.
Second implementation of fill and local assembly for MultiVectorFiller.
void clear()
Clear the contents of the vector, making it implicitly a vector of zeros.
MultiVectorFiller(const Teuchos::RCP< const map_type > &map, const size_t numCols)
Constructor.
void locallyAssemble(MV &X, BinaryFunction &f)
Locally assemble into X, with user-specified combine mode.
Teuchos::Array< global_ordinal_type > getSourceIndices() const
All source indices (local and nonlocal) of the source Map, sorted and unique.
One or more distributed dense vectors.
void locallyAssemble(MV &X)
locallyAssemble() for the usual ADD combine mode.
Tests for MultiVectorFiller.
void sumIntoGlobalValues(Teuchos::ArrayView< const global_ordinal_type > rows, Teuchos::ArrayView< const scalar_type > values)
Sum data into the multivector.
Adds nonlocal sum-into functionality to Tpetra::MultiVector.
Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createContigMapWithNode(global_size_t numElements, size_t localNumElements, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const Teuchos::RCP< Node > &node)
Non-member constructor for a (potentially) non-uniformly distributed, contiguous Map with a user-spec...
MultiVectorFillerData(const Teuchos::RCP< const map_type > &map, const size_t numColumns)
Constructor.
void locallyAssemble(MV &X, BinaryFunction &f)
Locally assemble into X.
size_t global_size_t
Global size_t object.
Implementation of fill and local assembly for MultiVectorFiller.
MultiVectorFillerData2(const Teuchos::RCP< const map_type > &map, const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_DEFAULT, const Teuchos::RCP< Teuchos::FancyOStream > &out=Teuchos::null)
Default constructor (sets number of columns to zero).
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
void sumIntoGlobalValues(Teuchos::ArrayView< const global_ordinal_type > rows, Teuchos::ArrayView< const scalar_type > values)
void globalAssemble(MV &X_out, const bool forceReuseMap=false)
Assemble all the data (local and nonlocal) into X_out.
CombineMode
Tpetra::Combine Mode enumerable type.
Describes a parallel distribution of objects over processes.
MultiVectorFillerData2(const Teuchos::RCP< const map_type > &map, const size_t numColumns, const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_DEFAULT, const Teuchos::RCP< Teuchos::FancyOStream > &out=Teuchos::null)
Constructor.
void sumIntoGlobalValues(Teuchos::ArrayView< const global_ordinal_type > rows, size_t columnIndex, Teuchos::ArrayView< const scalar_type > values)
Set entry (rows[i],columnIndex) to values[i], for all i.
MultiVectorFillerData(const Teuchos::RCP< const map_type > &map)
Default constructor (sets number of columns to zero).
void sumIntoGlobalValues(Teuchos::ArrayView< const global_ordinal_type > rows, size_t column, Teuchos::ArrayView< const scalar_type > values)
Sum data into the multivector.
void setNumColumns(const size_t newNumColumns)
Set the number of columns in the output multivector.
void setNumColumns(const size_t newNumColumns)
Set the number of columns in the output multivector.
void clear()
Clear the contents of the multivector.