41 #ifndef __Tpetra_MultiVectorFiller_hpp 42 #define __Tpetra_MultiVectorFiller_hpp 44 #include <Tpetra_MultiVector.hpp> 45 #include <Teuchos_CommHelpers.hpp> 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) {
170 sourceIndices_.resize (newNumColumns);
171 sourceValues_.resize (newNumColumns);
173 numCols_ = oldNumColumns;
181 if (column >= getNumColumns()) {
182 for (
size_t j = column; j < getNumColumns(); ++j) {
187 std::copy (rows.
begin(), rows.
end(), std::back_inserter (sourceIndices_[column]));
188 std::copy (values.
begin(), values.
end(), std::back_inserter (sourceValues_[column]));
202 typedef global_ordinal_type GO;
203 typedef scalar_type ST;
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>
252 typedef local_ordinal_type LO;
253 typedef global_ordinal_type GO;
254 typedef scalar_type ST;
255 typedef node_type NT;
259 const size_t numColumns = getNumColumns();
260 for (
size_t j = 0; j < numColumns; ++j) {
266 if (localIndices.
size() < numIndices) {
267 localIndices.
resize (numIndices);
272 localIndices[i] = map->getLocalElement (globalIndicesView[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);
297 std::swap (sourceIndices_, newSourceIndices);
298 std::swap (sourceValues_, newSourceValues);
308 typedef global_ordinal_type GO;
312 const size_t numCols = getNumColumns();
319 const size_type numNew = sourceIndices_[0].size();
321 std::copy (sourceIndices_[0].begin(), sourceIndices_[0].end(),
323 std::sort (allInds.
begin(), allInds.
end());
325 std::unique (allInds.
begin(), allInds.
end());
326 const size_type numFinal = as<size_type> (it - allInds.
begin());
327 allInds.
resize (numFinal);
337 for (
size_t j = 0; j < numCols; ++j) {
338 const size_type numNew = sourceIndices_[j].size();
339 if (numNew > newInds.
size()) {
343 std::copy (sourceIndices_[j].begin(), sourceIndices_[j].end(),
344 newIndsView.
begin());
345 curIndsView = sortAndMergeIn<GO> (allInds, curIndsView, newIndsView);
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;
390 verbLevel_ (verbLevel),
412 const size_t numColumns,
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<" 455 const int myImageID = comm->getRank();
456 const int numImages = comm->getSize();
462 if (myImageID == 0) {
463 out << this->description() << endl;
465 for (
int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
466 if (myImageID == imageCtr) {
469 out <<
"Process " << myImageID <<
":" << endl;
473 out <<
"local length=" << localVec_->getLocalLength();
476 if (localVec_->isConstantStride()) {
477 out <<
", constant stride=" << localVec_->getStride() << endl;
480 out <<
", not constant stride" << endl;
484 out <<
"Local values:" << endl;
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) {
502 out <<
"X(" << inds[k] <<
"," << j <<
") = " << vals[k] << endl;
525 typedef global_ordinal_type GO;
529 Array<GO> nonlocalIndices = getSortedUniqueNonlocalIndices();
540 std::copy (localIndices.
begin(), localIndices.
end(), localIndView.
begin());
541 std::sort (localIndView.
begin(), localIndView.
end());
544 if (nonlocalIndices.
size() > 0) {
547 iter_type middle = localIndView.
end();
556 std::copy (nonlocalIndices.
begin(), nonlocalIndices.
end(), middle);
557 std::inplace_merge (indView.
begin(), middle, indView.
end());
578 const size_t oldNumColumns = numCols_;
579 if (newNumColumns == oldNumColumns) {
584 if (newNumColumns > oldNumColumns) {
585 newLocalVec =
rcp (
new MV (map_, newNumColumns));
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;
620 typedef local_ordinal_type LO;
621 typedef global_ordinal_type GO;
622 typedef scalar_type ST;
624 if (columnIndex >= getNumColumns()) {
632 for ( ; rowIter != rows.
end() && valIter != values.
end(); ++rowIter, ++valIter) {
633 const GO globalRowIndex = *rowIter;
639 const LO localRowIndex = map_->getLocalElement (globalRowIndex);
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;
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;
679 sumIntoGlobalValues (rows.
view (offset, len), j, values.
view (offset, len));
707 template<
class BinaryFunction>
714 using Teuchos::getFancyOStream;
720 typedef local_ordinal_type LO;
721 typedef global_ordinal_type GO;
722 typedef scalar_type ST;
729 *out <<
"locallyAssemble:" << endl;
734 for (
size_t j = 0; j < X.getNumVectors(); ++j) {
740 it != localIndices.
end(); ++it) {
741 const LO rowIndLocal = map_->getLocalElement (*it);
742 const LO rowIndX = srcMap->getLocalElement (*it);
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]);
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;
772 locallyAssemble<std::plus<scalar_type> > (X, f);
786 std::swap (nonlocalIndices_, newNonlocalIndices);
787 std::swap (nonlocalValues_, newNonlocalValues);
792 if (! localVec_.is_null()) {
820 size_t getNumColumns()
const {
return numCols_; }
824 getLocalIndices()
const 826 return map_->getNodeElementList ();
831 getSortedUniqueNonlocalIndices()
const 836 typedef global_ordinal_type GO;
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());
853 std::unique (allNonlocals.
begin(), allNonlocals.
end());
854 const size_type numFinal = as<size_type> (it - allNonlocals.
begin());
855 allNonlocals.
resize (numFinal);
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);
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);
975 data_.describe (out, verbLevel);
993 data_.sumIntoGlobalValues (rows, column, values);
1015 data_.sumIntoGlobalValues (rows, values);
1081 computeSourceMap (
const global_ordinal_type indexBase,
1091 data_ (map, numCols),
1105 typedef global_ordinal_type GO;
1107 Array<GO> indices = getSourceIndices ();
1114 return rcp (
new map_type (invalid, indices, indexBase, comm, node));
1127 const size_t numVecs = X_out.getNumVectors();
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_;
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) {
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,
1260 using Teuchos::getFancyOStream;
1265 using Teuchos::rcpFromRef;
1266 using Teuchos::REDUCE_SUM;
1271 typedef local_ordinal_type LO;
1272 typedef global_ordinal_type GO;
1273 typedef scalar_type ST;
1277 "Element size (eltSize) argument must be odd.");
1279 "Number of columns (numCols) argument must be nonzero.");
1289 const GO indexBase = targetMap->getIndexBase();
1292 std::fill (values.
begin(), values.
end(), STS::one());
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;
1315 "At start,end = " << start <<
"," << end <<
", len = " << len
1316 <<
" > eltSize = " << eltSize <<
".");
1318 for (GO k = 0; k < len; ++k) {
1319 rows[k] = start + k;
1322 *out <<
"Inserting: " 1326 filler->sumIntoGlobalValues (rows.
view(0, len), j, values.
view(0, len));
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;
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) {
1355 for (GO i = minGlobalIndex; i <= maxGlobalIndex; ++i) {
1356 const LO localIndex = targetMap->getLocalElement (i);
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);
1374 *out <<
"X_expected:" << std::endl;
1375 X_expected.describe (*out, verbLevel);
1380 for (
size_t j = 0; j < numCols; ++j) {
1387 for (GO i = minGlobalIndex; i <= maxGlobalIndex; ++i) {
1388 const LO localIndex = targetMap->getLocalElement (i);
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]) {
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;
1425 "Over all procs: " << globalNumErrors <<
" total error" 1426 << (globalNumErrors != 1 ?
"s" :
"") <<
".");
1433 template<
class ScalarType,
1434 class LocalOrdinalType,
1435 class GlobalOrdinalType,
1440 const size_t unknownsPerNode,
1441 const GlobalOrdinalType unknownsPerElt,
1442 const size_t numCols,
1448 using Teuchos::getFancyOStream;
1456 typedef ScalarType ST;
1457 typedef LocalOrdinalType LO;
1458 typedef GlobalOrdinalType GO;
1459 typedef NodeType NT;
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.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
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.
ArrayView< T > view(size_type offset, size_type size)
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.
basic_FancyOStream< char > FancyOStream
void locallyAssemble(MV &X)
locallyAssemble() for the usual ADD combine mode.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const_pointer const_iterator
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...
Implementation details of Tpetra.
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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
void resize(size_type new_size, const value_type &x=value_type())
static const EVerbosityLevel verbLevel_default
void push_back(const value_type &x)
Describes a parallel distribution of objects over processes.
TypeTo as(const TypeFrom &t)
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.
ArrayView< T > view(size_type offset, size_type size) const
void setNumColumns(const size_t newNumColumns)
Set the number of columns in the output multivector.
void clear()
Clear the contents of the multivector.
std::vector< T >::iterator iterator
static std::string name()
basic_oblackholestream< char, std::char_traits< char > > oblackholestream
std::string toString(const T &t)