10 #ifndef TPETRA_FECRSGRAPH_DEF_HPP
11 #define TPETRA_FECRSGRAPH_DEF_HPP
13 #include "Tpetra_CrsGraph.hpp"
17 #include <type_traits>
22 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
24 FECrsGraph(
const Teuchos::RCP<const map_type> & ownedRowMap,
25 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
26 const size_t maxNumEntriesPerRow,
27 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
28 const Teuchos::RCP<const map_type> & domainMap,
29 const Teuchos::RCP<const map_type> & ownedRangeMap,
30 const Teuchos::RCP<Teuchos::ParameterList>& params):
31 FECrsGraph(ownedRowMap,ownedPlusSharedRowMap,maxNumEntriesPerRow,
32 domainMap.is_null() ? ownedRowMap : domainMap,
33 ownedPlusSharedToOwnedimporter,domainMap,ownedRangeMap,params)
39 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
41 FECrsGraph(
const Teuchos::RCP<const map_type> & ownedRowMap,
42 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
43 const size_t maxNumEntriesPerRow,
44 const Teuchos::RCP<const map_type> & ownedPlusSharedDomainMap,
45 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
46 const Teuchos::RCP<const map_type> & ownedDomainMap,
47 const Teuchos::RCP<const map_type> & ownedRangeMap,
48 const Teuchos::RCP<Teuchos::ParameterList>& params):
49 crs_graph_type(ownedPlusSharedRowMap, maxNumEntriesPerRow, params),
50 ownedRowsImporter_(ownedPlusSharedToOwnedimporter),
51 ownedDomainMap_(ownedDomainMap.is_null() ? ownedRowMap : ownedDomainMap),
52 ownedRangeMap_(ownedRangeMap.is_null() ? ownedRowMap : ownedRangeMap)
54 this->
domainMap_ = ownedPlusSharedDomainMap.is_null() ? ownedPlusSharedRowMap : ownedPlusSharedDomainMap;
55 Teuchos::RCP<const map_type> dummy;
56 setup(ownedRowMap,ownedPlusSharedRowMap,dummy,params);
59 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
61 FECrsGraph (
const Teuchos::RCP<const map_type> & ownedRowMap,
62 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
63 const Kokkos::DualView<const size_t*, device_type>& numEntPerRow,
64 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
65 const Teuchos::RCP<const map_type> & domainMap,
66 const Teuchos::RCP<const map_type> & ownedRangeMap,
67 const Teuchos::RCP<Teuchos::ParameterList>& params):
68 FECrsGraph(ownedRowMap,ownedPlusSharedRowMap,numEntPerRow,
69 domainMap.is_null() ? ownedRowMap : domainMap,
70 ownedPlusSharedToOwnedimporter,domainMap,ownedRangeMap,params)
75 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
77 FECrsGraph (
const Teuchos::RCP<const map_type> & ownedRowMap,
78 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
79 const Kokkos::DualView<const size_t*, device_type>& numEntPerRow,
80 const Teuchos::RCP<const map_type> & ownedPlusSharedDomainMap,
81 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
82 const Teuchos::RCP<const map_type> & ownedDomainMap,
83 const Teuchos::RCP<const map_type> & ownedRangeMap,
84 const Teuchos::RCP<Teuchos::ParameterList>& params):
86 ownedRowsImporter_(ownedPlusSharedToOwnedimporter),
87 ownedDomainMap_(ownedDomainMap.is_null() ? ownedRowMap : ownedDomainMap),
88 ownedRangeMap_(ownedRangeMap.is_null() ? ownedRowMap : ownedRangeMap)
90 this->
domainMap_ = ownedPlusSharedDomainMap.is_null() ? ownedPlusSharedRowMap : ownedPlusSharedDomainMap;
91 Teuchos::RCP<const map_type> dummy;
92 setup(ownedRowMap,ownedPlusSharedRowMap,dummy,params);
95 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
97 FECrsGraph(
const Teuchos::RCP<const map_type> & ownedRowMap,
98 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
99 const Teuchos::RCP<const map_type> & ownedPlusSharedColMap,
100 const size_t maxNumEntriesPerRow,
101 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
102 const Teuchos::RCP<const map_type> & domainMap,
103 const Teuchos::RCP<const map_type> & ownedRangeMap,
104 const Teuchos::RCP<Teuchos::ParameterList>& params):
105 FECrsGraph(ownedRowMap,ownedPlusSharedRowMap,ownedPlusSharedColMap,maxNumEntriesPerRow,
106 domainMap.is_null() ? ownedRowMap : domainMap,
107 ownedPlusSharedToOwnedimporter, domainMap, ownedRangeMap, params)
112 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
115 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
116 const Teuchos::RCP<const map_type> & ownedPlusSharedColMap,
117 const size_t maxNumEntriesPerRow,
118 const Teuchos::RCP<const map_type> & ownedPlusSharedDomainMap,
119 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
120 const Teuchos::RCP<const map_type> & ownedDomainMap,
121 const Teuchos::RCP<const map_type> & ownedRangeMap,
122 const Teuchos::RCP<Teuchos::ParameterList>& params):
123 crs_graph_type(ownedPlusSharedRowMap, ownedPlusSharedColMap,maxNumEntriesPerRow, params),
124 ownedRowsImporter_(ownedPlusSharedToOwnedimporter),
125 ownedDomainMap_(ownedDomainMap.is_null() ? ownedRowMap : ownedDomainMap),
126 ownedRangeMap_(ownedRangeMap.is_null() ? ownedRowMap : ownedRangeMap)
128 this->
domainMap_ = ownedPlusSharedDomainMap.is_null() ? ownedPlusSharedRowMap : ownedPlusSharedDomainMap;
129 setup(ownedRowMap,ownedPlusSharedRowMap, ownedPlusSharedColMap,params);
132 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
134 FECrsGraph (
const Teuchos::RCP<const map_type> & ownedRowMap,
135 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
136 const Teuchos::RCP<const map_type> & ownedPlusSharedColMap,
137 const Kokkos::DualView<const size_t*, device_type>& numEntPerRow,
138 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
139 const Teuchos::RCP<const map_type> & domainMap,
140 const Teuchos::RCP<const map_type> & ownedRangeMap,
141 const Teuchos::RCP<Teuchos::ParameterList>& params):
142 FECrsGraph(ownedRowMap,ownedPlusSharedRowMap,ownedPlusSharedColMap,numEntPerRow,
143 domainMap.is_null() ? ownedRowMap : domainMap,
144 ownedPlusSharedToOwnedimporter, domainMap, ownedRangeMap, params)
149 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
151 FECrsGraph (
const Teuchos::RCP<const map_type> & ownedRowMap,
152 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
153 const Teuchos::RCP<const map_type> & ownedPlusSharedColMap,
154 const Kokkos::DualView<const size_t*, device_type>& numEntPerRow,
155 const Teuchos::RCP<const map_type> & ownedPlusSharedDomainMap,
156 const Teuchos::RCP<const import_type> & ownedPlusSharedToOwnedimporter,
157 const Teuchos::RCP<const map_type> & ownedDomainMap,
158 const Teuchos::RCP<const map_type> & ownedRangeMap,
159 const Teuchos::RCP<Teuchos::ParameterList>& params):
160 crs_graph_type(ownedPlusSharedRowMap, ownedPlusSharedColMap, numEntPerRow, params),
161 ownedRowsImporter_(ownedPlusSharedToOwnedimporter),
162 ownedDomainMap_(ownedDomainMap.is_null() ? ownedRowMap : ownedDomainMap),
163 ownedRangeMap_(ownedRangeMap.is_null() ? ownedRowMap : ownedRangeMap)
165 this->
domainMap_ = ownedPlusSharedDomainMap.is_null() ? ownedPlusSharedRowMap : ownedPlusSharedDomainMap;
166 setup(ownedRowMap,ownedPlusSharedRowMap, ownedPlusSharedColMap,params);
169 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
171 setup(
const Teuchos::RCP<const map_type> & ownedRowMap,
172 const Teuchos::RCP<const map_type> & ownedPlusSharedRowMap,
173 const Teuchos::RCP<const map_type> & ownedPlusSharedColMap,
174 const Teuchos::RCP<Teuchos::ParameterList>& params)
176 const char tfecfFuncName[] =
"FECrsGraph::setup(): ";
178 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(ownedRowMap.is_null (), std::runtime_error,
"ownedRowMap is null.");
179 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(ownedPlusSharedRowMap.is_null (), std::runtime_error,
"ownedPlusSharedRowMap is null.");
182 if(ownedPlusSharedColMap.is_null()) this->allocateIndices(GlobalIndices);
183 else this->allocateIndices(LocalIndices);
185 activeCrsGraph_ = Teuchos::rcp(
new FE::WhichActive(FE::ACTIVE_OWNED_PLUS_SHARED));
186 fillState_ = Teuchos::rcp(
new FE::FillState(FE::FillState::closed));
189 bool maps_are_the_same = ownedRowMap->isSameAs(*ownedPlusSharedRowMap);
190 if(!maps_are_the_same) {
192 if(ownedRowsImporter_.is_null()) {
193 ownedRowsImporter_ = Teuchos::rcp(
new import_type(ownedRowMap,ownedPlusSharedRowMap));
196 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(!ownedRowMap->isSameAs(*ownedRowsImporter_->getSourceMap()), std::runtime_error,
"ownedRowMap does not match importer source map.");
197 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(!ownedPlusSharedRowMap->isSameAs(*ownedRowsImporter_->getTargetMap()), std::runtime_error,
"ownedPlusSharedRowMap does not match importer target map.");
201 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( ownedRowsImporter_->getNumSameIDs() != ownedRowsImporter_->getSourceMap()->getLocalNumElements(),
202 std::runtime_error,
"ownedRowMap contains entries which are not in the ownedPlusSharedRowMap.");
204 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( ownedRowMap->getLocalNumElements() > ownedPlusSharedRowMap->getLocalNumElements(),
205 std::runtime_error,
"ownedRowMap more entries than the ownedPlusSharedRowMap.");
210 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC( !ownedPlusSharedRowMap->isLocallyFitted(*ownedRowMap),
211 std::runtime_error,
"ownedPlusSharedRowMap must be locally fitted to the ownedRowMap");
217 ownedRowsImporter_ = Teuchos::null;
221 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
222 void FECrsGraph<LocalOrdinal, GlobalOrdinal, Node>::doOwnedPlusSharedToOwned(
const CombineMode CM) {
223 const char tfecfFuncName[] =
"FECrsGraph::doOwnedPlusSharedToOwned(CombineMode): ";
224 if(!ownedRowsImporter_.is_null() && *activeCrsGraph_ == FE::ACTIVE_OWNED_PLUS_SHARED) {
225 Teuchos::RCP<const map_type> ownedRowMap = ownedRowsImporter_->getSourceMap();
228 this->doExport(*
this,*ownedRowsImporter_,CM,
true);
232 if(!this->hasColMap()) {
233 Teuchos::Array<int> remotePIDs (0);
238 crs_graph_type::fillComplete(this->domainMap_,this->getRowMap());
245 const bool checkColGIDsInAtLeastOneOwnedRow =
246 this->getMyNonconstParamList().is_null() ?
true :
247 this->getMyNonconstParamList()->get(
"Check Col GIDs In At Least One Owned Row",
true);
248 if (debug && checkColGIDsInAtLeastOneOwnedRow) {
249 Teuchos::RCP<const map_type> colmap = this->getColMap();
250 Teuchos::Array<bool> flag(colmap->getLocalNumElements(),
false);
251 typename crs_graph_type::nonconst_local_inds_host_view_type indices(
"indices",this->getLocalMaxNumRowEntries());
253 for(
size_t i=0; i<ownedRowMap->getLocalNumElements(); i++) {
255 this->getLocalRowCopy(i,indices,NumEntries);
256 for(
size_t j=0; j<NumEntries; j++)
257 flag[indices[j]] =
true;
261 for(
size_t i=0; i<(size_t)flag.size(); i++)
267 int gblCount = lclCount;
268 auto comm = this->getComm();
269 if (!comm.is_null()) {
270 Teuchos::reduceAll<int,int>(*comm, Teuchos::REDUCE_SUM, 1, &lclCount, &gblCount);
272 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
274 std::invalid_argument,
"if you own an element (in the finite element sense) you "
275 "must also own one of the attached nodes. This assumption has been violated in "
276 "your matrix fill on at least one MPI rank:\n"
277 " locally, there are " + std::to_string(lclCount) +
" col gids not connected to any owned gid.\n"
278 " globally, there are " + std::to_string(gblCount) +
" col gids not connected to any owned gid.\n"
279 "NOTE: you can disable this check by setting a parameter list with the option\n"
280 " 'Check Col GIDs In At Least One Owned Row' set to false.\n"
281 "NOTE: the parameter list must be set AFTER construction, since it would not be recognized as valid"
282 "by the base class CrsGraph.\n");
286 inactiveCrsGraph_ = Teuchos::rcp(
new crs_graph_type(*
this, ownedRowMap));
287 inactiveCrsGraph_->fillComplete(ownedDomainMap_,ownedRangeMap_);
292 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
293 void FECrsGraph<LocalOrdinal, GlobalOrdinal, Node>::doOwnedToOwnedPlusShared(
const CombineMode ) {
297 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
299 if(*activeCrsGraph_ == FE::ACTIVE_OWNED_PLUS_SHARED)
300 *activeCrsGraph_ = FE::ACTIVE_OWNED;
302 *activeCrsGraph_ = FE::ACTIVE_OWNED_PLUS_SHARED;
304 if(inactiveCrsGraph_.is_null())
return;
306 this->swap(*inactiveCrsGraph_);
311 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
313 const Teuchos::RCP<const map_type>& domainMap,
314 const Teuchos::RCP<const map_type>& rangeMap)
335 const char tfecfFuncName[] =
"FECrsGraph::endFill(domainMap, rangeMap): ";
336 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(*activeCrsGraph_ != FE::ACTIVE_OWNED_PLUS_SHARED,std::runtime_error,
"must be in owned+shared mode.");
337 if(ownedRowsImporter_.is_null()) {
339 switchActiveCrsGraph();
340 crs_graph_type::fillComplete(domainMap, rangeMap);
350 switchActiveCrsGraph();
356 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
357 void FECrsGraph<LocalOrdinal, GlobalOrdinal, Node>::endFill() {
358 this->endFill(ownedDomainMap_, ownedRangeMap_);
362 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
363 void FECrsGraph<LocalOrdinal, GlobalOrdinal, Node>::beginFill() {
364 const char tfecfFuncName[] =
"FECrsGraph::beginFill(): ";
368 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(*activeCrsGraph_ == FE::ACTIVE_OWNED,std::runtime_error,
"can only be called once.");
372 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
374 const char tfecfFuncName[] =
"FECrsGraph::beginAssembly: ";
375 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
376 *fillState_ != FE::FillState::closed,
378 "Cannot beginAssembly, matrix is not in a closed state"
380 *fillState_ = FE::FillState::open;
384 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
386 const char tfecfFuncName[] =
"FECrsGraph::endAssembly: ";
387 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
388 *fillState_ != FE::FillState::open,
390 "Cannot endAssembly, matrix is not open to fill but is closed."
392 *fillState_ = FE::FillState::closed;
396 template<
class LocalOrdinal,
class GlobalOrdinal,
class Node>
398 const Teuchos::RCP<const map_type>& domainMap,
399 const Teuchos::RCP<const map_type>& rangeMap)
401 const char tfecfFuncName[] =
"FECrsGraph::endAssembly: ";
402 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
403 *fillState_ != FE::FillState::open,
405 "Cannot endAssembly, matrix is not open to fill but is closed."
407 *fillState_ = FE::FillState::closed;
408 this->endFill(domainMap, rangeMap);
411 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
412 Teuchos::RCP<const Teuchos::ParameterList>
415 auto valid_pl = Teuchos::rcp(
new Teuchos::ParameterList(
"Tpetra::FECrsGraph"));
416 valid_pl->validateParametersAndSetDefaults(*crs_graph_type::getValidParameters());
417 valid_pl->set(
"Check Col GIDs In At Least One Owned Row",
true);
422 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
427 const size_t numInputInds
429 const char tfecfFuncName[] =
"FECrsGraph::insertGlobalIndices: ";
430 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
431 *fillState_ != FE::FillState::open,
433 "Cannot replace global values, matrix is not open to fill but is closed."
435 return crs_graph_type::insertGlobalIndicesImpl(lclRow, inputGblColInds, numInputInds);
438 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
443 const size_t numInputInds,
444 std::function<
void(
const size_t,
const size_t,
const size_t)> fun
446 const char tfecfFuncName[] =
"FECrsGraph::insertGlobalIndices: ";
447 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
448 *fillState_ != FE::FillState::open,
450 "Cannot replace global values, matrix is not open to fill but is closed."
452 return crs_graph_type::insertGlobalIndicesImpl(rowInfo, inputGblColInds, numInputInds, fun);
455 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
458 const local_ordinal_type lclRow,
459 const Teuchos::ArrayView<const local_ordinal_type>& gblColInds,
460 std::function<
void(
const size_t,
const size_t,
const size_t)> fun
462 const char tfecfFuncName[] =
"FECrsGraph::insertLocalIndices: ";
463 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
464 *fillState_ != FE::FillState::open,
466 "Cannot replace global values, matrix is not open to fill but is closed."
468 return crs_graph_type::insertLocalIndicesImpl(lclRow, gblColInds, fun);
479 #define TPETRA_FECRSGRAPH_INSTANT(LO,GO,NODE) \
480 template class FECrsGraph<LO, GO, NODE>;
484 #endif // TPETRA_FECRSGRAPH_DEF
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
GlobalOrdinal global_ordinal_type
The type of the graph's global indices.
void endAssembly()
Migrates data to the owned mode.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Default parameter list suitable for validation.
FECrsGraph(const Teuchos::RCP< const map_type > &ownedRowMap, const Teuchos::RCP< const map_type > &ownedPlusSharedRowMap, const size_t maxNumEntriesPerRow, const Teuchos::RCP< const import_type > &ownedPlusSharedToOwnedimporter=Teuchos::null, const Teuchos::RCP< const map_type > &domainMap=Teuchos::null, const Teuchos::RCP< const map_type > &ownedRangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Constructorfor globally-indexed assembly specifying a single upper bound for the number of entries in...
static bool debug()
Whether Tpetra is in debug mode.
Allocation information for a locally owned row in a CrsGraph or CrsMatrix.
Teuchos::RCP< const map_type > domainMap_
The Map describing the domain of the (matrix corresponding to the) graph.
CombineMode
Rule for combining data in an Import or Export.
LocalOrdinal local_ordinal_type
The type of the graph's local indices.
size_t insertGlobalIndicesImpl(const local_ordinal_type lclRow, const global_ordinal_type inputGblColInds[], const size_t numInputInds)
Insert global indices, using an input local row index.
void switchActiveCrsGraph()
Switches which CrsGraph is active (without migrating data)
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
int makeColMap(Teuchos::RCP< const Tpetra::Map< LO, GO, NT > > &colMap, Teuchos::Array< int > &remotePIDs, const Teuchos::RCP< const Tpetra::Map< LO, GO, NT > > &domMap, const RowGraph< LO, GO, NT > &graph, const bool sortEachProcsGids=true, std::ostream *errStrm=NULL)
Make the graph's column Map.
void beginAssembly()
Activates the owned+shared mode for assembly.
Declaration and definition of Tpetra::Details::getEntryOnHost.
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.