46 #ifndef MUELU_SEGREGATEDAFACTORY_DEF_HPP
47 #define MUELU_SEGREGATEDAFACTORY_DEF_HPP
60 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
65 validParamList->
set<std::string>(
"droppingScheme",
"vague",
"Strategy to drop entries from matrix A based on the input of some map(s) [blockmap, map-pair]");
70 return validParamList;
73 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
78 "Please specify a generating factory for the matrix \"A\" to be filtered.")
80 "Input map type not selected. Please select one of the available strategies.")
82 (pL.
get<std::string>(
"droppingScheme") !=
"blockmap" && pL.
get<std::string>(
"droppingScheme") !=
"map-pair"),
84 "Unknown User Input: droppingScheme (=" << pL.
get<std::string>(
"droppingScheme") <<
")")
86 Input(currentLevel,
"A");
88 if (pL.
get<std::string>(
"droppingScheme") ==
"blockmap") {
92 Input(currentLevel,
"dropMap1");
94 }
else if (pL.
get<std::string>(
"droppingScheme") ==
"map-pair") {
99 Input(currentLevel,
"dropMap1");
100 Input(currentLevel,
"dropMap2");
107 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
111 const std::string parameterName =
"droppingScheme";
112 if (pL.
get<std::string>(parameterName) ==
"blockmap") {
113 BuildBasedOnBlockmap(currentLevel);
114 }
else if (pL.
get<std::string>(parameterName) ==
"map-pair") {
115 BuildBasedOnMapPair(currentLevel);
118 "MueLu::SegregatedAFactory::Build(): Unknown map type of user input. "
119 "Set a valid value for the parameter \""
120 << parameterName <<
"\".")
124 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
126 FactoryMonitor m(*
this,
"Matrix filtering (segregation, blockmap)", currentLevel);
128 RCP<Matrix> Ain = Get<RCP<Matrix>>(currentLevel,
"A");
130 const std::string dropMap1Name =
"dropMap1";
133 if (currentLevel.GetLevelID() == 0) {
135 GetOStream(
Statistics0) <<
"User provided dropMap1 \"" << dropMap1Name <<
"\": length dimension=" << dropMap1->getGlobalNumElements() << std::endl;
137 dropMap1 = Get<RCP<const Map>>(currentLevel, dropMap1Name);
142 Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries());
144 size_t numLocalRows = Ain->getLocalNumRows();
145 for (
size_t row = 0; row < numLocalRows; row++) {
146 GlobalOrdinal grid = Ain->getRowMap()->getGlobalElement(row);
147 bool isInMap = dropMap1->isNodeGlobalElement(grid);
150 auto lclMat = Ain->getLocalMatrixHost();
151 auto rowView = lclMat.row(row);
157 size_t nNonzeros = 0;
158 for (
LO jj = 0; jj < rowView.length; ++jj) {
159 LO lcid = rowView.colidx(jj);
160 GO gcid = Ain->getColMap()->getGlobalElement(lcid);
161 auto val = rowView.value(jj);
162 bool isInMap2 = dropMap1->isNodeGlobalElement(gcid);
164 if (isInMap == isInMap2) {
165 indout[nNonzeros] = gcid;
166 valout[nNonzeros] = val;
173 Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.
view(0, indout.
size()), valout.
view(0, valout.
size()));
176 Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
179 Aout->SetFixedBlockSize(Ain->GetFixedBlockSize());
181 GetOStream(
Statistics0, 0) <<
"Nonzeros in A (input): " << Ain->getGlobalNumEntries() <<
", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
183 Set(currentLevel,
"A", Aout);
186 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
188 FactoryMonitor m(*
this,
"Matrix filtering (segregation, map-pair)", currentLevel);
190 RCP<Matrix> Ain = Get<RCP<Matrix>>(currentLevel,
"A");
196 const std::string dropMap1Name =
"dropMap1";
197 const std::string dropMap2Name =
"dropMap2";
199 if (currentLevel.GetLevelID() == 0) {
202 GetOStream(
Statistics0) <<
"User provided dropMap1 \"" << dropMap1Name <<
"\": length dimension=" << dropMap1->getGlobalNumElements() << std::endl;
203 GetOStream(
Statistics0) <<
"User provided dropMap2 \"" << dropMap2Name <<
"\": length dimension=" << dropMap2->getGlobalNumElements() << std::endl;
205 dropMap1 = Get<RCP<const Map>>(currentLevel, dropMap1Name);
206 dropMap2 = Get<RCP<const Map>>(currentLevel, dropMap2Name);
213 Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries());
224 size_t numLocalMatrixRows = Ain->getLocalNumRows();
226 for (
size_t row = 0; row < numLocalMatrixRows; row++) {
227 GO grid = Ain->getRowMap()->getGlobalElement(row);
228 bool rowIsInMap1 = finalDropMap1->isNodeGlobalElement(grid);
229 bool rowIsInMap2 = finalDropMap2->isNodeGlobalElement(grid);
232 auto lclMat = Ain->getLocalMatrixHost();
233 auto rowView = lclMat.row(row);
239 size_t nNonzeros = 0;
240 for (
LO jj = 0; jj < rowView.length; ++jj) {
241 LO lcid = rowView.colidx(jj);
242 GO gcid = Ain->getColMap()->getGlobalElement(lcid);
243 auto val = rowView.value(jj);
244 bool colIsInMap1 = finalDropMap1->isNodeGlobalElement(gcid);
245 bool colIsInMap2 = finalDropMap2->isNodeGlobalElement(gcid);
247 if ((rowIsInMap1 && colIsInMap2) || (rowIsInMap2 && colIsInMap1)) {
250 indout[nNonzeros] = gcid;
251 valout[nNonzeros] = val;
257 Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.
view(0, indout.
size()), valout.
view(0, valout.
size()));
260 Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
263 Aout->SetFixedBlockSize(Ain->GetFixedBlockSize());
265 GetOStream(
Statistics0, 0) <<
"Nonzeros in A (input): " << Ain->getGlobalNumEntries() <<
", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
267 currentLevel.Set(
"A", Aout,
this);
272 #endif // MUELU_SEGREGATEDAFACTORY_DEF_HPP
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void Build(Level ¤tLevel) const override
Build method.
static const NoFactory * get()
void BuildBasedOnBlockmap(Level ¤tLevel) const
void resize(const size_type n, const T &val=T())
Print statistics that do not involve significant additional computation.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
void DeclareInput(Level ¤tLevel) const override
Specifies the data that this class needs, and the factories that generate that data.
RCP< const ParameterList > GetValidParameterList() const override
Input.
Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > importOffRankDroppingInfo(Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node >> &localDropMap, Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &Ain)
void BuildBasedOnMapPair(Level ¤tLevel) const
int GetLevelID() const
Return level number.
#define TEUCHOS_ASSERT(assertion_test)
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
Exception throws to report invalid user entry.
ArrayView< T > view(size_type lowerOffset, size_type size) const