10 #ifndef MUELU_PERFUTILS_DEF_HPP
11 #define MUELU_PERFUTILS_DEF_HPP
17 #include <Teuchos_CommHelpers.hpp>
23 #include <Xpetra_CrsMatrixWrap.hpp>
32 void calculateStats(Type& minVal, Type& maxVal,
double& avgVal,
double& devVal,
int& minProc,
int& maxProc,
const RCP<
const Teuchos::Comm<int> >& comm,
int numActiveProcs,
const Type& v) {
33 Type sumVal, sum2Val, v2 = v * v;
43 w = (minVal == v) ? comm->getRank() : -1;
45 w = (maxVal == v) ? comm->getRank() : -1;
49 MT avgVal_MT = Teuchos::as<MT>(avgVal);
56 double avgVal, devVal;
58 calculateStats<Type>(minVal, maxVal, avgVal, devVal, minProc, maxProc, comm, numActiveProcs, v);
62 std::ostringstream buf;
64 if ((avgVal != zero) && (paramList.is_null() || !paramList->isParameter(
"print abs") || paramList->get<
bool>(
"print abs") ==
false)) {
65 double relDev = (devVal / avgVal) * 100;
68 buf <<
"avg = " << std::scientific << std::setw(10) << std::setprecision(2) << avgVal <<
", "
69 <<
"dev = " << std::fixed << std::setw(6) << std::setprecision(1) << relDev <<
"%, "
70 <<
"min = " << std::fixed << std::setw(7) << std::setprecision(1) << std::setw(7) << relMin <<
"%"
71 <<
" (" << std::scientific << std::setw(10) << std::setprecision(2) << minVal <<
" on " << std::fixed << std::setw(4) << minProc <<
"), "
72 <<
"max = " << std::fixed << std::setw(7) << std::setprecision(1) << relMax <<
"%"
73 <<
" (" << std::scientific << std::setw(10) << std::setprecision(2) << maxVal <<
" on " << std::fixed << std::setw(4) << maxProc <<
")";
75 double relDev = (avgVal != zero ? (devVal / avgVal) * 100 : zero);
76 buf <<
"avg = " << std::scientific << std::setw(10) << std::setprecision(2) << avgVal <<
", "
77 <<
"dev = " << std::fixed << std::setw(6) << std::setprecision(1) << relDev <<
"%, "
78 <<
"min = " << std::scientific << std::setw(10) << std::setprecision(2) << minVal
79 <<
" (on " << std::fixed << std::setw(4) << minProc <<
"), "
80 <<
"max = " << std::scientific << std::setw(10) << std::setprecision(2) << maxVal
81 <<
" (on " << std::fixed << std::setw(4) << maxProc <<
")";
87 bool cmp_less(
typename Map::value_type& v1,
typename Map::value_type& v2) {
88 return v1.second < v2.second;
91 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
98 std::ostringstream ss;
100 ss << msgTag <<
" size = " << A.getGlobalNumRows() <<
" x " << A.getGlobalNumCols();
101 if (A.haveGlobalConstants())
102 ss <<
", nnz = " << A.getGlobalNumEntries();
108 bool printLoadBalanceInfo =
false, printCommInfo =
false, printEntryStats =
false;
109 if (params->
isParameter(
"printLoadBalancingInfo") && params->
get<
bool>(
"printLoadBalancingInfo"))
110 printLoadBalanceInfo =
true;
111 if (params->
isParameter(
"printCommInfo") && params->
get<
bool>(
"printCommInfo"))
112 printCommInfo =
true;
113 if (params->
isParameter(
"printEntryStats") && params->
get<
bool>(
"printEntryStats"))
114 printEntryStats =
true;
116 if (!printLoadBalanceInfo && !printCommInfo && !printEntryStats)
122 size_t numMyNnz = A.getLocalNumEntries(), numMyRows = A.getLocalNumRows();
126 bool activeProc =
true;
127 int numProc = origComm->getSize();
128 int numActiveProcs = 0;
131 MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
133 std::vector<size_t> numRowsPerProc(numProc);
134 Teuchos::gatherAll(*origComm, 1, &numMyRows, numProc, &numRowsPerProc[0]);
137 bool rootFlag =
true;
138 for (
int i = 0; i < numProc; i++) {
139 if (numRowsPerProc[i]) {
148 if (numMyRows == 0) {
153 if (numMyRows == 0) {
166 absList.
set(
"print abs",
true);
172 crsA = crsWrapA->getCrsMatrix();
173 if (printEntryStats && !crsA.is_null()) {
175 typedef typename STS::magnitudeType magnitudeType;
185 crsA->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
186 crsA->getLocalDiagOffsets(offsets_RCP);
187 rowptr = rowptr_RCP();
189 offsets = offsets_RCP();
191 Scalar val, minVal, maxVal;
192 magnitudeType absVal, minAbsVal, maxAbsVal;
194 minVal = STS::rmax();
195 maxVal = STS::rmin();
196 minAbsVal = MTS::rmax();
197 maxAbsVal = MTS::zero();
199 for (
int i = 0; i < offsets.
size(); i++) {
200 val = vals[rowptr[i] + offsets[i]];
201 if (STS::real(val) < STS::real(minVal))
203 if (STS::real(val) > STS::real(maxVal))
205 absVal = STS::magnitude(val);
206 minAbsVal = std::min(minAbsVal, absVal);
207 maxAbsVal = std::max(maxAbsVal, absVal);
210 ss << msgTag <<
" diag min : " << stringStats<Scalar>(origComm, numActiveProcs, minVal) << std::endl;
211 ss << msgTag <<
" diag max : " << stringStats<Scalar>(origComm, numActiveProcs, maxVal) << std::endl;
212 ss << msgTag <<
" abs(diag) min : " << stringStats<Scalar>(origComm, numActiveProcs, minAbsVal) << std::endl;
213 ss << msgTag <<
" abs(diag) max : " << stringStats<Scalar>(origComm, numActiveProcs, maxAbsVal) << std::endl;
217 minVal = STS::rmax();
218 maxVal = STS::rmin();
219 minAbsVal = MTS::rmax();
220 maxAbsVal = MTS::zero();
222 for (
int i = 0; i < vals.
size(); i++) {
224 if (STS::real(val) < STS::real(minVal))
226 if (STS::real(val) > STS::real(maxVal))
228 absVal = STS::magnitude(val);
229 minAbsVal = std::min(minAbsVal, absVal);
230 maxAbsVal = std::max(maxAbsVal, absVal);
233 ss << msgTag <<
" entry min : " << stringStats<Scalar>(origComm, numActiveProcs, minVal) << std::endl;
234 ss << msgTag <<
" entry max : " << stringStats<Scalar>(origComm, numActiveProcs, maxVal) << std::endl;
235 ss << msgTag <<
" abs(entry) min : " << stringStats<Scalar>(origComm, numActiveProcs, minAbsVal) << std::endl;
236 ss << msgTag <<
" abs(entry) max : " << stringStats<Scalar>(origComm, numActiveProcs, maxAbsVal) << std::endl;
240 if (printLoadBalanceInfo) {
241 ss << msgTag <<
" Load balancing info" << std::endl;
242 ss << msgTag <<
" # active processes: " << numActiveProcs <<
"/" << numProc << std::endl;
243 ss << msgTag <<
" # rows per proc : " << stringStats<global_size_t>(origComm, numActiveProcs, numMyRows) << std::endl;
244 ss << msgTag <<
" # nnz per proc : " << stringStats<global_size_t>(origComm, numActiveProcs, numMyNnz) << std::endl;
247 if (printCommInfo && numActiveProcs != 1) {
248 typedef std::map<int, size_t> map_type;
252 if (exportPIDs.
size())
253 for (
int i = 0; i < exportPIDs.
size(); i++)
254 neighMap[exportPIDs[i]]++;
258 size_t numExportSend = 0;
259 size_t numImportSend = 0;
265 numExportSend = (!exporter.
is_null() ? exporter->getNumExportIDs() : 0);
266 numImportSend = (!importer.
is_null() ? importer->getNumExportIDs() : 0);
267 numMsgs = neighMap.size();
268 map_type::const_iterator it = std::min_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
269 minMsg = (it != neighMap.end() ? it->second : 0);
270 it = std::max_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
271 maxMsg = (it != neighMap.end() ? it->second : 0);
274 ss << msgTag <<
" Communication info" << std::endl;
275 ss << msgTag <<
" # num export send : " << stringStats<global_size_t>(origComm, numActiveProcs, numExportSend) << std::endl;
276 ss << msgTag <<
" # num import send : " << stringStats<global_size_t>(origComm, numActiveProcs, numImportSend) << std::endl;
277 ss << msgTag <<
" # num msgs : " << stringStats<global_size_t>(origComm, numActiveProcs, numMsgs, rcpFromRef(absList)) << std::endl;
278 ss << msgTag <<
" # min msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, minMsg) << std::endl;
279 ss << msgTag <<
" # max msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, maxMsg) << std::endl;
285 int strLength = outstr.size();
286 MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
287 if (origComm->getRank() != root)
288 outstr.resize(strLength);
289 MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
295 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
299 std::ostringstream ss;
303 bool activeProc =
true;
304 int numActiveProcs = origComm->getSize();
307 MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
313 absList.
set(
"print abs",
true);
315 typedef std::map<int, size_t> map_type;
318 if (exportPIDs.
size())
319 for (
int i = 0; i < exportPIDs.
size(); i++)
320 neighMap[exportPIDs[i]]++;
323 size_t numImportSend = 0;
329 numImportSend = importer->getNumExportIDs();
330 numMsgs = neighMap.size();
331 map_type::const_iterator it = std::min_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
332 minMsg = (it != neighMap.end() ? it->second : 0);
333 it = std::max_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
334 maxMsg = (it != neighMap.end() ? it->second : 0);
337 ss << msgTag <<
" Communication info" << std::endl;
338 ss << msgTag <<
" # num import send : " << stringStats<global_size_t>(origComm, numActiveProcs, numImportSend) << std::endl;
339 ss << msgTag <<
" # num msgs : " << stringStats<global_size_t>(origComm, numActiveProcs, numMsgs, rcpFromRef(absList)) << std::endl;
340 ss << msgTag <<
" # min msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, minMsg) << std::endl;
341 ss << msgTag <<
" # max msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, maxMsg) << std::endl;
346 int strLength = outstr.size();
347 MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
348 if (origComm->getRank() != root)
349 outstr.resize(strLength);
350 MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
356 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
361 std::ostringstream out;
364 int myRank = comm->getRank();
366 out << msgTag <<
" " << myRank <<
":";
368 RCP<const Import> importer = (A.getCrsGraph() != Teuchos::null ? A.getCrsGraph()->getImporter() : Teuchos::null);
376 if (exportPIDs.
size()) {
378 int neigh = exportPIDs[0];
380 for (
int i = 1; i < exportPIDs.
size(); i++) {
381 if (exportPIDs[i] != exportPIDs[i - 1]) {
382 out <<
" " << neigh <<
"(" << weight <<
")";
384 neigh = exportPIDs[i];
391 out <<
" " << neigh <<
"(" << weight <<
")" << std::endl;
397 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
404 bool hasCrsGraph =
true;
417 #endif // MUELU_PERFUTILS_DEF_HPP
static bool CheckMatrix(const Matrix &A)
std::string stringStats(const RCP< const Teuchos::Comm< int > > &comm, int numActiveProcs, const Type &v, RCP< ParameterList > paramList=Teuchos::null)
#define MueLu_sumAll(rcpComm, in, out)
#define MueLu_maxAll(rcpComm, in, out)
T & get(const std::string &name, T def_value)
static magnitudeType real(T a)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define MueLu_minAll(rcpComm, in, out)
static std::string CommPattern(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
static std::string PrintImporterInfo(RCP< const Import > importer, const std::string &msgTag)
bool isParameter(const std::string &name) const
MueLu::DefaultScalar Scalar
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
void calculateStats(Type &minVal, Type &maxVal, double &avgVal, double &devVal, int &minProc, int &maxProc, const RCP< const Teuchos::Comm< int > > &comm, int numActiveProcs, const Type &v)
bool cmp_less(typename Map::value_type &v1, typename Map::value_type &v2)