MueLu  Version of the Day
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, const RCP<const Teuchos::Comm<int> >& comm, const Type& v) {
68  int numProcs = comm->getSize();
69 
70  Type sumVal, sum2Val;
71 
72  MueLu_sumAll(comm, v, sumVal);
73  MueLu_sumAll(comm, v*v, sum2Val);
74  MueLu_minAll(comm, v, minVal);
75  MueLu_maxAll(comm, v, maxVal);
76 
77  avgVal = as<double>(sumVal) / numProcs;
78  devVal = (numProcs != 1 ? sqrt((sum2Val - sumVal*avgVal)/(numProcs-1)) : 0);
79  }
80 
81  template<class Type>
82  std::string stringStats(const RCP<const Teuchos::Comm<int> >& comm, const Type& v, RCP<ParameterList> paramList = Teuchos::null) {
83  Type minVal, maxVal;
84  double avgVal, devVal;
85  calculateStats<Type>(minVal, maxVal, avgVal, devVal, comm, v);
86 
87  char buf[256];
88  if (avgVal && (paramList.is_null() || !paramList->isParameter("print abs") || paramList->get<bool>("print abs") == false))
89  sprintf(buf, "avg = %.2e, dev = %5.1f%%, min = %+6.1f%%, max = %+6.1f%%", avgVal,
90  (devVal/avgVal)*100, (minVal/avgVal-1)*100, (maxVal/avgVal-1)*100);
91  else
92  sprintf(buf, "avg = %8.2f, dev = %6.2f, min = %6.1f , max = %6.1f", avgVal,
93  devVal, as<double>(minVal), as<double>(maxVal));
94  return buf;
95  }
96 
97  template<class Map>
98  bool cmp_less(typename Map::value_type& v1, typename Map::value_type& v2) {
99  return v1.second < v2.second;
100  }
101 
102  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103  std::string PerfUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::PrintMatrixInfo(const Matrix& A, const std::string& msgTag, RCP<const ParameterList> params) {
104  typedef Xpetra::global_size_t global_size_t;
105 
106  std::ostringstream ss;
107 
108  ss << msgTag << " size = " << A.getGlobalNumRows() << " x " << A.getGlobalNumCols()
109  << ", nnz = " << A.getGlobalNumEntries() << std::endl;
110 
111  if (params.is_null())
112  return ss.str();
113 
114  bool printLoadBalanceInfo = false, printCommInfo = false;
115  if (params->isParameter("printLoadBalancingInfo") && params->get<bool>("printLoadBalancingInfo"))
116  printLoadBalanceInfo = true;
117  if (params->isParameter("printCommInfo") && params->get<bool>("printCommInfo"))
118  printCommInfo = true;
119 
120  if (!printLoadBalanceInfo && !printCommInfo)
121  return ss.str();
122 
123  RCP<const Import> importer = A.getCrsGraph()->getImporter();
124  RCP<const Export> exporter = A.getCrsGraph()->getExporter();
125 
126  size_t numMyNnz = A.getNodeNumEntries(), numMyRows = A.getNodeNumRows();
127 
128  // Create communicator only for active processes
129  RCP<const Teuchos::Comm<int> > origComm = A.getRowMap()->getComm();
130 #ifdef HAVE_MPI
131  RCP<const Teuchos::MpiComm<int> > mpiComm = rcp_dynamic_cast<const Teuchos::MpiComm<int> >(origComm);
132  int numProc = origComm->getSize();
133  MPI_Comm rawComm = (*mpiComm->getRawMpiComm())();
134 
135  std::vector<size_t> numRowsPerProc(numProc);
136  Teuchos::gatherAll(*origComm, 1, &numMyRows, numProc, &numRowsPerProc[0]);
137 
138  int root = 0;
139  for (int i = 0; i < numProc; i++)
140  if (numRowsPerProc[i]) {
141  root = i;
142  break;
143  }
144 
145  RCP<const Teuchos::Comm<int> > comm = origComm->split((numMyRows > 0) ? 0 : MPI_UNDEFINED, 0);
146 #else
147  RCP<const Teuchos::Comm<int> > comm = origComm->split((numMyRows > 0) ? 0 : -1, 0);
148 #endif
149 
150  std::string outstr;
151  if (!comm.is_null()) {
152  ParameterList absList;
153  absList.set("print abs", true);
154 
155  if (printLoadBalanceInfo) {
156  ss << msgTag << " Load balancing info" << std::endl;
157  ss << msgTag << " # active processes: " << comm->getSize() << "/" << origComm->getSize() << std::endl;
158  ss << msgTag << " # rows per proc : " << stringStats<global_size_t>(comm, numMyRows) << std::endl;
159  ss << msgTag << " # nnz per proc : " << stringStats<global_size_t>(comm, numMyNnz) << std::endl;
160  }
161 
162  if (printCommInfo && comm->getSize() != 1) {
163  typedef std::map<int,size_t> map_type;
164  map_type neighMap;
165  if (!importer.is_null()) {
166  ArrayView<const int> exportPIDs = importer->getExportPIDs();
167  if (exportPIDs.size())
168  for (int i = 0; i < exportPIDs.size(); i++)
169  neighMap[exportPIDs[i]]++;
170  }
171 
172  // Communication volume
173  size_t numExportSend = (!exporter.is_null() ? exporter->getNumExportIDs() : 0);
174  size_t numImportSend = (!importer.is_null() ? importer->getNumExportIDs() : 0);
175  size_t numMsgs = neighMap.size();
176 
177  map_type::const_iterator it = std::min_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
178  size_t minMsg = (it != neighMap.end() ? it->second : 0);
179  it = std::max_element(neighMap.begin(), neighMap.end(), cmp_less<map_type>);
180  size_t maxMsg = (it != neighMap.end() ? it->second : 0);
181 
182  ss << msgTag << " Communication info" << std::endl;
183  ss << msgTag << " # num export send : " << stringStats<global_size_t>(comm, numExportSend) << std::endl;
184  ss << msgTag << " # num import send : " << stringStats<global_size_t>(comm, numImportSend) << std::endl;
185  ss << msgTag << " # num msgs : " << stringStats<global_size_t>(comm, numMsgs, rcpFromRef(absList)) << std::endl;
186  ss << msgTag << " # min msg size : " << stringStats<global_size_t>(comm, minMsg) << std::endl;
187  ss << msgTag << " # max msg size : " << stringStats<global_size_t>(comm, maxMsg) << std::endl;
188  }
189 
190  outstr = ss.str();
191  }
192 
193 #ifdef HAVE_MPI
194  int strLength = outstr.size();
195  MPI_Bcast(&strLength, 1, MPI_INT, root, rawComm);
196  if (origComm->getRank() != root)
197  outstr.resize(strLength);
198  MPI_Bcast(&outstr[0], strLength, MPI_CHAR, root, rawComm);
199 #endif
200 
201  return outstr;
202  }
203 
204  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
205  std::string PerfUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CommPattern(const Matrix& A, const std::string& msgTag, RCP<const ParameterList> params) {
206  std::ostringstream out;
207 
208  RCP<const Teuchos::Comm<int> > comm = A.getRowMap()->getComm();
209  int myRank = comm->getRank();
210 
211  out << msgTag << " " << myRank << ":";
212 
213  RCP<const Import> importer = (A.getCrsGraph() != Teuchos::null ? A.getCrsGraph()->getImporter() : Teuchos::null);
214  if (importer.is_null()) {
215  out << std::endl;
216  return out.str();
217  }
218 
219  ArrayView<const int> exportPIDs = importer->getExportPIDs();
220 
221  if (exportPIDs.size()) {
222  // NodeTE: exportPIDs is sorted but not unique ( 1 1 1 2 2 3 4 4 4 )
223  int neigh = exportPIDs[0];
224  GO weight = 1;
225  for (int i = 1; i < exportPIDs.size(); i++) {
226  if (exportPIDs[i] != exportPIDs[i-1]) {
227  out << " " << neigh << "(" << weight << ")";
228 
229  neigh = exportPIDs[i];
230  weight = 1;
231 
232  } else {
233  weight += 1;
234  }
235  }
236  out << " " << neigh << "(" << weight << ")" << std::endl;
237  }
238 
239  return out.str();
240  }
241 
242 } //namespace MueLu
243 
244 #endif // MUELU_PERFUTILS_DEF_HPP
#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
Namespace for MueLu classes and methods.
#define MueLu_minAll(rcpComm, in, out)
static std::string CommPattern(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
bool is_null() const
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
bool cmp_less(typename Map::value_type &v1, typename Map::value_type &v2)
bool isParameter(const std::string &name) const
void calculateStats(Type &minVal, Type &maxVal, double &avgVal, double &devVal, const RCP< const Teuchos::Comm< int > > &comm, const Type &v)
std::string stringStats(const RCP< const Teuchos::Comm< int > > &comm, const Type &v, RCP< ParameterList > paramList=Teuchos::null)