10 #ifndef MUELU_SEGREGATEDAFACTORY_DEF_HPP
11 #define MUELU_SEGREGATEDAFACTORY_DEF_HPP
24 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
29 validParamList->
set<std::string>(
"droppingScheme",
"vague",
"Strategy to drop entries from matrix A based on the input of some map(s) [blockmap, map-pair]");
34 return validParamList;
37 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
42 "Please specify a generating factory for the matrix \"A\" to be filtered.")
44 "Input map type not selected. Please select one of the available strategies.")
46 (pL.
get<std::string>(
"droppingScheme") !=
"blockmap" && pL.
get<std::string>(
"droppingScheme") !=
"map-pair"),
48 "Unknown User Input: droppingScheme (=" << pL.
get<std::string>(
"droppingScheme") <<
")")
50 Input(currentLevel,
"A");
52 if (pL.
get<std::string>(
"droppingScheme") ==
"blockmap") {
56 Input(currentLevel,
"dropMap1");
58 }
else if (pL.
get<std::string>(
"droppingScheme") ==
"map-pair") {
63 Input(currentLevel,
"dropMap1");
64 Input(currentLevel,
"dropMap2");
71 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
75 const std::string parameterName =
"droppingScheme";
76 if (pL.
get<std::string>(parameterName) ==
"blockmap") {
77 BuildBasedOnBlockmap(currentLevel);
78 }
else if (pL.
get<std::string>(parameterName) ==
"map-pair") {
79 BuildBasedOnMapPair(currentLevel);
82 "MueLu::SegregatedAFactory::Build(): Unknown map type of user input. "
83 "Set a valid value for the parameter \""
84 << parameterName <<
"\".")
88 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
90 FactoryMonitor m(*
this,
"Matrix filtering (segregation, blockmap)", currentLevel);
92 RCP<Matrix> Ain = Get<RCP<Matrix>>(currentLevel,
"A");
94 const std::string dropMap1Name =
"dropMap1";
97 if (currentLevel.GetLevelID() == 0) {
99 GetOStream(
Statistics0) <<
"User provided dropMap1 \"" << dropMap1Name <<
"\": length dimension=" << dropMap1->getGlobalNumElements() << std::endl;
101 dropMap1 = Get<RCP<const Map>>(currentLevel, dropMap1Name);
106 Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries());
108 size_t numLocalRows = Ain->getLocalNumRows();
109 for (
size_t row = 0; row < numLocalRows; row++) {
110 GlobalOrdinal grid = Ain->getRowMap()->getGlobalElement(row);
111 bool isInMap = dropMap1->isNodeGlobalElement(grid);
114 auto lclMat = Ain->getLocalMatrixHost();
115 auto rowView = lclMat.row(row);
121 size_t nNonzeros = 0;
122 for (
LO jj = 0; jj < rowView.length; ++jj) {
123 LO lcid = rowView.colidx(jj);
124 GO gcid = Ain->getColMap()->getGlobalElement(lcid);
125 auto val = rowView.value(jj);
126 bool isInMap2 = dropMap1->isNodeGlobalElement(gcid);
128 if (isInMap == isInMap2) {
129 indout[nNonzeros] = gcid;
130 valout[nNonzeros] = val;
137 Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.
view(0, indout.
size()), valout.
view(0, valout.
size()));
140 Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
143 Aout->SetFixedBlockSize(Ain->GetFixedBlockSize());
145 GetOStream(
Statistics0, 0) <<
"Nonzeros in A (input): " << Ain->getGlobalNumEntries() <<
", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
147 Set(currentLevel,
"A", Aout);
150 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
152 FactoryMonitor m(*
this,
"Matrix filtering (segregation, map-pair)", currentLevel);
154 RCP<Matrix> Ain = Get<RCP<Matrix>>(currentLevel,
"A");
160 const std::string dropMap1Name =
"dropMap1";
161 const std::string dropMap2Name =
"dropMap2";
163 if (currentLevel.GetLevelID() == 0) {
166 GetOStream(
Statistics0) <<
"User provided dropMap1 \"" << dropMap1Name <<
"\": length dimension=" << dropMap1->getGlobalNumElements() << std::endl;
167 GetOStream(
Statistics0) <<
"User provided dropMap2 \"" << dropMap2Name <<
"\": length dimension=" << dropMap2->getGlobalNumElements() << std::endl;
169 dropMap1 = Get<RCP<const Map>>(currentLevel, dropMap1Name);
170 dropMap2 = Get<RCP<const Map>>(currentLevel, dropMap2Name);
177 Teuchos::RCP<Matrix> Aout = MatrixFactory::Build(Ain->getRowMap(), Ain->getGlobalMaxNumRowEntries());
188 size_t numLocalMatrixRows = Ain->getLocalNumRows();
190 for (
size_t row = 0; row < numLocalMatrixRows; row++) {
191 GO grid = Ain->getRowMap()->getGlobalElement(row);
192 bool rowIsInMap1 = finalDropMap1->isNodeGlobalElement(grid);
193 bool rowIsInMap2 = finalDropMap2->isNodeGlobalElement(grid);
196 auto lclMat = Ain->getLocalMatrixHost();
197 auto rowView = lclMat.row(row);
203 size_t nNonzeros = 0;
204 for (
LO jj = 0; jj < rowView.length; ++jj) {
205 LO lcid = rowView.colidx(jj);
206 GO gcid = Ain->getColMap()->getGlobalElement(lcid);
207 auto val = rowView.value(jj);
208 bool colIsInMap1 = finalDropMap1->isNodeGlobalElement(gcid);
209 bool colIsInMap2 = finalDropMap2->isNodeGlobalElement(gcid);
211 if ((rowIsInMap1 && colIsInMap2) || (rowIsInMap2 && colIsInMap1)) {
214 indout[nNonzeros] = gcid;
215 valout[nNonzeros] = val;
221 Aout->insertGlobalValues(Ain->getRowMap()->getGlobalElement(row), indout.
view(0, indout.
size()), valout.
view(0, valout.
size()));
224 Aout->fillComplete(Ain->getDomainMap(), Ain->getRangeMap());
227 Aout->SetFixedBlockSize(Ain->GetFixedBlockSize());
229 GetOStream(
Statistics0, 0) <<
"Nonzeros in A (input): " << Ain->getGlobalNumEntries() <<
", Nonzeros after filtering A: " << Aout->getGlobalNumEntries() << std::endl;
231 currentLevel.Set(
"A", Aout,
this);
236 #endif // MUELU_SEGREGATEDAFACTORY_DEF_HPP
T & get(const std::string &name, T def_value)
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.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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