10 #ifndef MUELU_AGGREGATEQUALITYESTIMATEFACTORY_DEF_HPP
11 #define MUELU_AGGREGATEQUALITYESTIMATEFACTORY_DEF_HPP
24 #include "MueLu_Utilities.hpp"
30 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
33 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
36 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
38 Input(fineLevel,
"A");
39 Input(fineLevel,
"Aggregates");
40 Input(fineLevel,
"CoarseMap");
43 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
47 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
57 #undef SET_VALID_ENTRY
63 return validParamList;
66 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
71 RCP<Aggregates> aggregates = Get<RCP<Aggregates>>(fineLevel,
"Aggregates");
75 assert(!aggregates->AggregatesCrossProcessors());
77 std::string mode = pL.get<std::string>(
"aggregate qualities: mode");
78 GetOStream(
Statistics1) <<
"AggregateQuality: mode " << mode << std::endl;
81 if (mode ==
"eigenvalue" || mode ==
"both") {
83 ComputeAggregateQualities(A, aggregates, aggregate_qualities);
84 OutputAggQualities(fineLevel, aggregate_qualities);
86 if (mode ==
"size" || mode ==
"both") {
88 ComputeAggregateSizes(A, aggregates, aggregate_sizes);
89 Set(fineLevel,
"AggregateSizes", aggregate_sizes);
90 OutputAggSizes(fineLevel, aggregate_sizes);
92 Set(coarseLevel,
"AggregateQualities", aggregate_qualities);
95 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
111 aggsToIndices =
ArrayRCP<LO>(numAggs + LO_ONE, LO_ZERO);
113 for (
LO i = 0; i < numAggs; ++i) {
114 aggsToIndices[i + LO_ONE] = aggsToIndices[i] + aggSizes[i];
120 LO numNodes = vertex2AggId->getLocalLength();
122 std::vector<LO> vertexInsertionIndexByAgg(numNodes, LO_ZERO);
124 for (
LO i = 0; i < numNodes; ++i) {
125 LO aggId = vertex2AggIdData[i];
126 if (aggId < 0 || aggId >= numAggs)
continue;
128 aggSortedVertices[aggsToIndices[aggId] + vertexInsertionIndexByAgg[aggId]] = i;
129 vertexInsertionIndexByAgg[aggId]++;
133 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
136 const SC SCALAR_TWO = SCALAR_ONE + SCALAR_ONE;
149 std::string algostr = pL.
get<std::string>(
"aggregate qualities: algorithm");
150 MT zeroThreshold = Teuchos::as<MT>(pL.
get<
double>(
"aggregate qualities: zero threshold"));
151 enum AggAlgo { ALG_FORWARD = 0,
154 if (algostr ==
"forward") {
156 GetOStream(
Statistics1) <<
"AggregateQuality: Using 'forward' algorithm" << std::endl;
157 }
else if (algostr ==
"reverse") {
159 GetOStream(
Statistics1) <<
"AggregateQuality: Using 'reverse' algorithm" << std::endl;
164 bool check_symmetry = pL.
get<
bool>(
"aggregate qualities: check symmetry");
165 if (check_symmetry) {
167 x->Xpetra_randomize();
175 tmp->norm2(tmp_norm());
178 tmp->norm2(x_norm());
180 if (tmp_norm[0] > 1e-10 * x_norm[0]) {
181 std::string transpose_string =
"transpose";
185 assert(A->getMap()->isSameAs(*(AT->getMap())));
197 ArrayRCP<LO> aggSortedVertices, aggsToIndices, aggSizes;
198 ConvertAggregatesData(aggs, aggSortedVertices, aggsToIndices, aggSizes);
213 for (
LO aggId = LO_ZERO; aggId < numAggs; ++aggId) {
214 LO aggSize = aggSizes[aggId];
215 DenseMatrix A_aggPart(aggSize, aggSize,
true);
216 DenseVector offDiagonalAbsoluteSums(aggSize,
true);
219 for (
LO idx = LO_ZERO; idx < aggSize; ++idx) {
220 LO nodeId = aggSortedVertices[idx + aggsToIndices[aggId]];
221 A->getLocalRowView(nodeId, rowIndices, rowValues);
222 AT->getLocalRowView(nodeId, rowIndices, colValues);
225 for (
LO elem = LO_ZERO; elem < rowIndices.
size(); ++elem) {
226 LO nodeId2 = rowIndices[elem];
227 SC val = (rowValues[elem] + colValues[elem]) / SCALAR_TWO;
229 LO idxInAgg = -LO_ONE;
234 for (
LO idx2 = LO_ZERO; idx2 < aggSize; ++idx2) {
235 if (aggSortedVertices[idx2 + aggsToIndices[aggId]] == nodeId2) {
242 if (idxInAgg == -LO_ONE) {
255 DenseMatrix A_aggPartDiagonal(aggSize, aggSize,
true);
256 MT diag_sum = MT_ZERO;
257 for (
int i = 0; i < aggSize; ++i) {
262 DenseMatrix ones(aggSize, aggSize,
false);
263 ones.putScalar(MT_ONE);
267 DenseMatrix tmp(aggSize, aggSize,
false);
268 DenseMatrix topMatrix(A_aggPartDiagonal);
274 DenseMatrix bottomMatrix(A_aggPart);
275 MT matrixNorm = A_aggPart.normInf();
280 for (
int i = 0; i < aggSize; ++i) {
281 bottomMatrix(i, i) -= offDiagonalAbsoluteSums(i) + boost;
286 DenseVector alpha_real(aggSize,
false);
287 DenseVector alpha_imag(aggSize,
false);
288 DenseVector beta(aggSize,
false);
290 DenseVector workArray(14 * (aggSize + 1),
false);
299 const char compute_flag =
'N';
300 if (algo == ALG_FORWARD) {
302 myLapack.
GGES(compute_flag, compute_flag, compute_flag, ptr2func, aggSize,
303 topMatrix.values(), aggSize, bottomMatrix.values(), aggSize, &sdim,
304 alpha_real.values(), alpha_imag.values(), beta.values(), vl, aggSize,
305 vr, aggSize, workArray.values(), workArray.length(), bwork,
309 MT maxEigenVal = MT_ZERO;
310 for (
int i = LO_ZERO; i < aggSize; ++i) {
313 maxEigenVal = std::max(maxEigenVal, alpha_real[i] / beta[i]);
316 (agg_qualities->getDataNonConst(0))[aggId] = (MT_ONE + MT_ONE) * maxEigenVal;
320 myLapack.
GGES(compute_flag, compute_flag, compute_flag, ptr2func, aggSize,
321 bottomMatrix.values(), aggSize, topMatrix.values(), aggSize, &sdim,
322 alpha_real.values(), alpha_imag.values(), beta.values(), vl, aggSize,
323 vr, aggSize, workArray.values(), workArray.length(), bwork,
328 MT minEigenVal = MT_ZERO;
330 for (
int i = LO_ZERO; i < aggSize; ++i) {
331 MT ev = alpha_real[i] / beta[i];
332 if (ev > zeroThreshold) {
333 if (minEigenVal == MT_ZERO)
336 minEigenVal = std::min(minEigenVal, ev);
339 if (minEigenVal == MT_ZERO)
342 (agg_qualities->getDataNonConst(0))[aggId] = (MT_ONE + MT_ONE) / minEigenVal;
347 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
351 magnitudeType good_agg_thresh = Teuchos::as<magnitudeType>(pL.
get<
double>(
"aggregate qualities: good aggregate threshold"));
359 MT mean_bad_agg = 0.0;
360 MT mean_good_agg = 0.0;
362 for (
size_t i = 0; i < agg_qualities->getLocalLength(); ++i) {
363 if (data[i] > good_agg_thresh) {
365 mean_bad_agg += data[i];
367 mean_good_agg += data[i];
369 worst_agg = std::max(worst_agg, data[i]);
372 if (num_bad_aggs > 0) mean_bad_agg /= num_bad_aggs;
373 mean_good_agg /= agg_qualities->getLocalLength() - num_bad_aggs;
375 if (num_bad_aggs == 0) {
376 GetOStream(
Statistics1) <<
"All aggregates passed the quality measure. Worst aggregate had quality " << worst_agg <<
". Mean aggregate quality " << mean_good_agg <<
"." << std::endl;
378 GetOStream(
Statistics1) << num_bad_aggs <<
" out of " << agg_qualities->getLocalLength() <<
" did not pass the quality measure. Worst aggregate had quality " << worst_agg <<
". "
379 <<
"Mean bad aggregate quality " << mean_bad_agg <<
". Mean good aggregate quality " << mean_good_agg <<
"." << std::endl;
382 if (pL.
get<
bool>(
"aggregate qualities: file output")) {
383 std::string filename = pL.
get<std::string>(
"aggregate qualities: file base") +
"." + std::to_string(level.
GetLevelID());
388 const auto n = size_t(agg_qualities->getLocalLength());
393 for (
size_t i = 0; i < n; ++i) {
394 tmp.push_back(data[i]);
397 std::sort(tmp.begin(), tmp.end());
401 GetOStream(
Statistics1) <<
"AGG QUALITY HEADER : | LEVEL | TOTAL |";
402 for (
auto percent : percents) {
403 GetOStream(
Statistics1) << std::fixed << std::setprecision(4) << 100.0 * percent <<
"% |";
408 for (
auto percent : percents) {
409 size_t i = size_t(n * percent);
410 i = i < n ? i : n - 1u;
412 GetOStream(
Statistics1) << std::fixed << std::setprecision(4) << tmp[i] <<
" |";
418 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
420 ArrayRCP<LO> aggSortedVertices, aggsToIndices, aggSizes;
421 ConvertAggregatesData(aggs, aggSortedVertices, aggsToIndices, aggSizes);
424 auto data = agg_sizes->getDataNonConst(0);
425 for (
LO i = 0; i < (
LO)aggSizes.
size(); i++)
426 data[i] = aggSizes[i];
429 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
436 if (pL.
get<
bool>(
"aggregate qualities: file output")) {
437 std::string filename = pL.
get<std::string>(
"aggregate qualities: file base") +
".sizes." + std::to_string(level.
GetLevelID());
442 size_t n = (size_t)agg_sizes->getLocalLength();
447 for (
size_t i = 0; i < n; ++i) {
448 tmp.push_back(Teuchos::as<MT>(data[i]));
451 std::sort(tmp.begin(), tmp.end());
455 GetOStream(
Statistics1) <<
"AGG SIZE HEADER : | LEVEL | TOTAL |";
456 for (
auto percent : percents) {
457 GetOStream(
Statistics1) << std::fixed << std::setprecision(4) << 100.0 * percent <<
"% |";
462 for (
auto percent : percents) {
463 size_t i = size_t(n * percent);
464 i = i < n ? i : n - 1u;
466 GetOStream(
Statistics1) << std::fixed << std::setprecision(4) << tmp[i] <<
" |";
474 #endif // MUELU_AGGREGATEQUALITYESTIMATE_DEF_HPP
void OutputAggQualities(const Level &level, RCP< const Xpetra::MultiVector< magnitudeType, LO, GO, Node >> agg_qualities) const
virtual ~AggregateQualityEstimateFactory()
Destructor.
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
KOKKOS_INLINE_FUNCTION LO GetNumAggregates() const
void ComputeAggregateSizes(RCP< const Matrix > A, RCP< const Aggregates > aggs, RCP< LocalOrdinalVector > agg_sizes) const
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)
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
static magnitudeType real(T a)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define SET_VALID_ENTRY(name)
void Build(Level &fineLevel, Level &coarseLevel) const
Build aggregate quality esimates with this factory.
void GGES(const char &JOBVL, const char &JOBVR, const char &SORT, OrdinalType &(*ptr2func)(ScalarType *, ScalarType *, ScalarType *), const OrdinalType &n, ScalarType *A, const OrdinalType &lda, ScalarType *B, const OrdinalType &ldb, OrdinalType *sdim, MagnitudeType *ALPHAR, MagnitudeType *ALPHAI, MagnitudeType *BETA, ScalarType *VL, const OrdinalType &ldvl, ScalarType *VR, const OrdinalType &ldvr, ScalarType *WORK, const OrdinalType &lwork, OrdinalType *BWORK, OrdinalType *info) const
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Teuchos::ArrayRCP< LocalOrdinal > ComputeAggregateSizesArrayRCP(bool forceRecompute=false) const
Compute sizes of aggregates.
void ComputeAggregateQualities(RCP< const Matrix > A, RCP< const Aggregates > aggs, RCP< Xpetra::MultiVector< magnitudeType, LO, GO, Node >> agg_qualities) const
const RCP< LOMultiVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
void OutputAggSizes(const Level &level, RCP< const LocalOrdinalVector > agg_sizes) const
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
static magnitudeType magnitude(T a)
static void ConvertAggregatesData(RCP< const Aggregates > aggs, ArrayRCP< LO > &aggSortedVertices, ArrayRCP< LO > &aggsToIndices, ArrayRCP< LO > &aggSizes)
Build aggregate quality esimates with this factory.
int GetLevelID() const
Return level number.
Exception throws to report errors in the internal logical of the program.
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitudeType
AggregateQualityEstimateFactory()
Constructor.