MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_PerfUtils_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NodeT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DIScalarLAIMED. IN Node EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NodeT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GlobalOrdinalODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_PERFUTILS_DEF_HPP
47 #define MUELU_PERFUTILS_DEF_HPP
48 
49 #include <algorithm>
50 #include <string>
51 
52 #ifdef HAVE_MPI
53 #include <Teuchos_CommHelpers.hpp>
54 #endif
55 
56 #include <Xpetra_Export.hpp>
57 #include <Xpetra_Import.hpp>
58 #include <Xpetra_Matrix.hpp>
59 
60 #include "MueLu_PerfUtils_decl.hpp"
61 
62 //#include "MueLu_Utilities.hpp"
63 
64 namespace MueLu {
65 
66  template<class Type>
67  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) {
68 
69  Type sumVal, sum2Val;
70 
71  MueLu_sumAll(comm, v, sumVal);
72  MueLu_sumAll(comm, v*v, sum2Val);
73  MueLu_minAll(comm, v, minVal);
74  MueLu_maxAll(comm, v, maxVal);
75 
76  int w;
77  w = (minVal == v) ? comm->getRank() : -1;
78  MueLu_maxAll(comm, w, maxProc);
79  w = (maxVal == v) ? comm->getRank() : -1;
80  MueLu_maxAll(comm, w, minProc);
81 
82  avgVal = (numActiveProcs > 0 ? as<double>(sumVal) / numActiveProcs : 0);
83  devVal = (numActiveProcs > 1 ? sqrt((sum2Val - sumVal*avgVal)/(numActiveProcs-1)) : 0);
84  }
85 
86  template<class Type>
87  std::string stringStats(const RCP<const Teuchos::Comm<int> >& comm, int numActiveProcs, const Type& v, RCP<ParameterList> paramList = Teuchos::null) {
88  Type minVal, maxVal;
89  double avgVal, devVal;
90  int minProc, maxProc;
91  calculateStats<Type>(minVal, maxVal, avgVal, devVal, minProc, maxProc, comm, numActiveProcs, v);
92 
93  char buf[256];
94  if (avgVal && (paramList.is_null() || !paramList->isParameter("print abs") || paramList->get<bool>("print abs") == false))
95  sprintf(buf, "avg = %.2e, dev = %5.1f%%, min = %+6.1f%% (%8.2f on %4d), max = %+6.1f%% (%8.2f on %4d)", avgVal,
96  (devVal/avgVal)*100, (minVal/avgVal-1)*100, as<double>(minVal), minProc, (maxVal/avgVal-1)*100, as<double>(maxVal), maxProc);
97  else
98  sprintf(buf, "avg = %8.2f, dev = %6.2f, min = %6.1f (on %4d), max = %6.1f (on %4d)", avgVal,
99  devVal, as<double>(minVal), minProc, as<double>(maxVal), maxProc);
100  return buf;
101  }
102 
103  template<class Map>
104  bool cmp_less(typename Map::value_type& v1, typename Map::value_type& v2) {
105  return v1.second < v2.second;
106  }
107 
108  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
109  std::string PerfUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::PrintMatrixInfo(const Matrix& A, const std::string& msgTag, RCP<const ParameterList> params) {
110  if (!CheckMatrix(A))
111  return "";
112 
113  typedef Xpetra::global_size_t global_size_t;
114 
115  std::ostringstream ss;
116 
117  ss << msgTag << " size = " << A.getGlobalNumRows() << " x " << A.getGlobalNumCols();
118  if(A.haveGlobalConstants())
119  ss << ", nnz = " << A.getGlobalNumEntries();
120  ss << std::endl;
121 
122  if (params.is_null())
123  return ss.str();
124 
125  bool printLoadBalanceInfo = false, printCommInfo = false;
126  if (params->isParameter("printLoadBalancingInfo") && params->get<bool>("printLoadBalancingInfo"))
127  printLoadBalanceInfo = true;
128  if (params->isParameter("printCommInfo") && params->get<bool>("printCommInfo"))
129  printCommInfo = true;
130 
131  if (!printLoadBalanceInfo && !printCommInfo)
132  return ss.str();
133 
134  RCP<const Import> importer = A.getCrsGraph()->getImporter();
135  RCP<const Export> exporter = A.getCrsGraph()->getExporter();
136 
137  size_t numMyNnz = A.getNodeNumEntries(), numMyRows = A.getNodeNumRows();
138 
139  // Create communicator only for active processes
140  RCP<const Teuchos::Comm<int> > origComm = A.getRowMap()->getComm();
141  bool activeProc = true;
142  int numProc = origComm->getSize();
143  int numActiveProcs = 0;
144 #ifdef HAVE_MPI
145  RCP<const Teuchos::MpiComm<int> > mpiComm = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(origComm);
146  MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
147 
148  std::vector<size_t> numRowsPerProc(numProc);
149  Teuchos::gatherAll(*origComm, 1, &numMyRows, numProc, &numRowsPerProc[0]);
150 
151  int root = 0;
152  bool rootFlag = true;
153  for (int i = 0; i < numProc; i++) {
154  if (numRowsPerProc[i]) {
155  ++numActiveProcs;
156  if(rootFlag) {
157  root = i;
158  rootFlag = false;
159  }
160  }
161  }
162 
163  if(numMyRows == 0) {activeProc = false; numMyNnz = 0;} // Reset numMyNnz to avoid adding it up in reduceAll
164 #else
165  if(numMyRows == 0) {
166  //FIXME JJH 10-May-2017 Is there any case in serial where numMyRows would be zero?
167  // Reset numMyNnz to avoid adding it up in reduceAll
168  numActiveProcs = 0;
169  activeProc = false;
170  numMyNnz = 0;
171  } else {
172  numActiveProcs = 1;
173  }
174 #endif
175 
176  std::string outstr;
177  ParameterList absList;
178  absList.set("print abs", true);
179 
180  if (printLoadBalanceInfo) {
181  ss << msgTag << " Load balancing info" << std::endl;
182  ss << msgTag << " # active processes: " << numActiveProcs << "/" << numProc << std::endl;
183  ss << msgTag << " # rows per proc : " << stringStats<global_size_t>(origComm, numActiveProcs, numMyRows) << std::endl;
184  ss << msgTag << " # nnz per proc : " << stringStats<global_size_t>(origComm, numActiveProcs, numMyNnz) << std::endl;
185  }
186 
187  if (printCommInfo && numActiveProcs != 1) {
188  typedef std::map<int,size_t> map_type;
189  map_type neighMap;
190  if (!importer.is_null()) {
191  ArrayView<const int> exportPIDs = importer->getExportPIDs();
192  if (exportPIDs.size())
193  for (int i = 0; i < exportPIDs.size(); i++)
194  neighMap[exportPIDs[i]]++;
195  }
196 
197  // Communication volume
198  size_t numExportSend = 0;
199  size_t numImportSend = 0;
200  size_t numMsgs = 0;
201  size_t minMsg = 0;
202  size_t maxMsg = 0;
203 
204  if(activeProc) {
205  numExportSend = (!exporter.is_null() ? exporter->getNumExportIDs() : 0);
206  numImportSend = (!importer.is_null() ? importer->getNumExportIDs() : 0);
207  numMsgs = neighMap.size();
208  map_type::const_iterator it = std::min_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
209  minMsg = (it != neighMap.end() ? it->second : 0);
210  it = std::max_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
211  maxMsg = (it != neighMap.end() ? it->second : 0);
212  }
213 
214  ss << msgTag << " Communication info" << std::endl;
215  ss << msgTag << " # num export send : " << stringStats<global_size_t>(origComm, numActiveProcs, numExportSend) << std::endl;
216  ss << msgTag << " # num import send : " << stringStats<global_size_t>(origComm, numActiveProcs, numImportSend) << std::endl;
217  ss << msgTag << " # num msgs : " << stringStats<global_size_t>(origComm, numActiveProcs, numMsgs, rcpFromRef(absList)) << std::endl;
218  ss << msgTag << " # min msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, minMsg) << std::endl;
219  ss << msgTag << " # max msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, maxMsg) << std::endl;
220  }
221 
222  outstr = ss.str();
223 
224 #ifdef HAVE_MPI
225  int strLength = outstr.size();
226  MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
227  if (origComm->getRank() != root)
228  outstr.resize(strLength);
229  MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
230 #endif
231 
232  return outstr;
233  }
234 
235  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
237 
238  typedef Xpetra::global_size_t global_size_t;
239 
240  std::ostringstream ss;
241 
242  // Create communicator only for active processes
243  RCP<const Teuchos::Comm<int> > origComm = importer->getSourceMap()->getComm();
244  bool activeProc = true;
245  int numActiveProcs = origComm->getSize();
246 #ifdef HAVE_MPI
247  RCP<const Teuchos::MpiComm<int> > mpiComm = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(origComm);
248  MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
249  int root = 0;
250 #endif
251 
252  std::string outstr;
253  ParameterList absList;
254  absList.set("print abs", true);
255 
256  typedef std::map<int,size_t> map_type;
257  map_type neighMap;
258  ArrayView<const int> exportPIDs = importer->getExportPIDs();
259  if (exportPIDs.size())
260  for (int i = 0; i < exportPIDs.size(); i++)
261  neighMap[exportPIDs[i]]++;
262 
263  // Communication volume
264  size_t numImportSend = 0;
265  size_t numMsgs = 0;
266  size_t minMsg = 0;
267  size_t maxMsg = 0;
268 
269  if(activeProc) {
270  numImportSend = importer->getNumExportIDs();
271  numMsgs = neighMap.size();
272  map_type::const_iterator it = std::min_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
273  minMsg = (it != neighMap.end() ? it->second : 0);
274  it = std::max_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
275  maxMsg = (it != neighMap.end() ? it->second : 0);
276  }
277 
278  ss << msgTag << " Communication info" << std::endl;
279  ss << msgTag << " # num import send : " << stringStats<global_size_t>(origComm, numActiveProcs, numImportSend) << std::endl;
280  ss << msgTag << " # num msgs : " << stringStats<global_size_t>(origComm, numActiveProcs, numMsgs, rcpFromRef(absList)) << std::endl;
281  ss << msgTag << " # min msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, minMsg) << std::endl;
282  ss << msgTag << " # max msg size : " << stringStats<global_size_t>(origComm, numActiveProcs, maxMsg) << std::endl;
283 
284 
285  outstr = ss.str();
286 
287 #ifdef HAVE_MPI
288  int strLength = outstr.size();
289  MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
290  if (origComm->getRank() != root)
291  outstr.resize(strLength);
292  MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
293 #endif
294 
295  return outstr;
296  }
297 
298  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
299  std::string PerfUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CommPattern(const Matrix& A, const std::string& msgTag, RCP<const ParameterList> /* params */) {
300  if (!CheckMatrix(A))
301  return "";
302 
303  std::ostringstream out;
304 
305  RCP<const Teuchos::Comm<int> > comm = A.getRowMap()->getComm();
306  int myRank = comm->getRank();
307 
308  out << msgTag << " " << myRank << ":";
309 
310  RCP<const Import> importer = (A.getCrsGraph() != Teuchos::null ? A.getCrsGraph()->getImporter() : Teuchos::null);
311  if (importer.is_null()) {
312  out << std::endl;
313  return out.str();
314  }
315 
316  ArrayView<const int> exportPIDs = importer->getExportPIDs();
317 
318  if (exportPIDs.size()) {
319  // NodeTE: exportPIDs is sorted but not unique ( 1 1 1 2 2 3 4 4 4 )
320  int neigh = exportPIDs[0];
321  GO weight = 1;
322  for (int i = 1; i < exportPIDs.size(); i++) {
323  if (exportPIDs[i] != exportPIDs[i-1]) {
324  out << " " << neigh << "(" << weight << ")";
325 
326  neigh = exportPIDs[i];
327  weight = 1;
328 
329  } else {
330  weight += 1;
331  }
332  }
333  out << " " << neigh << "(" << weight << ")" << std::endl;
334  }
335 
336  return out.str();
337  }
338 
339  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
341  // We can only print statistics for matrices that have a crs graph. A
342  // potential issue is regarding Xpetra::TpetraBlockCrsMatrix which has no
343  // CrsGraph. It is held as a private data member by Xpetra::CrsMatrix,
344  // which itself is an Xpetra::Matrix. So we check directly whether the
345  // request for the graph throws.
346  bool hasCrsGraph = true;
347  try {
348  A.getCrsGraph();
349 
350  } catch (...) {
351  hasCrsGraph = false;
352  }
353 
354  return hasCrsGraph;
355  }
356 
357 } //namespace MueLu
358 
359 #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)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
size_type size() const
#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
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)
bool is_null() const