Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_ImbalanceMetrics.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 #ifndef ZOLTAN2_IMBALANCEMETRICS_HPP
14 #define ZOLTAN2_IMBALANCEMETRICS_HPP
15 
17 #include <Zoltan2_GraphModel.hpp>
19 
20 #define IMBALANCE_METRICS_TYPE_NAME "ImbalanceMetrics"
21 
22 namespace Zoltan2{
23 
25 template <typename scalar_t>
26  class ImbalanceMetrics : public BaseClassMetrics<scalar_t> {
27 
28 private:
29  multiCriteriaNorm mcnorm_; // store "actualNorm + 1"
30 
31 public:
33 ImbalanceMetrics(std::string mname) : BaseClassMetrics<scalar_t>(static_metricNames_.size(), mname),mcnorm_(multiCriteriaNorm(0)) {}
34 
37 
39 virtual const std::string & getMetricType() const { return static_metricTypeName_; }
40 
42 static void printHeader(std::ostream &os);
43 
45 virtual void printLine(std::ostream &os) const;
46 
48 void setNorm(multiCriteriaNorm normVal) { mcnorm_ = multiCriteriaNorm(normVal+1);}
49 
52 
54 void setLocalSum(scalar_t x) { this->setMetricValue("local sum", x);}
55 
57 void setGlobalSum(scalar_t x) { this->setMetricValue("global sum", x );}
58 
60 void setGlobalMin(scalar_t x) { this->setMetricValue("global minimum", x );}
61 
63 void setGlobalMax(scalar_t x) { this->setMetricValue("global maximum", x );}
64 
66 void setMaxImbalance(scalar_t x) { this->setMetricValue("maximum imbalance", x);}
67 
69 void setAvgImbalance(scalar_t x) { this->setMetricValue("average imbalance", x);}
70 
72 scalar_t getLocalSum() const { return this->getMetricValue("local sum");}
73 
75 scalar_t getGlobalSum() const { return this->getMetricValue("global sum");}
76 
78 scalar_t getGlobalMin() const { return this->getMetricValue("global minimum");}
79 
81 scalar_t getGlobalMax() const { return this->getMetricValue("global maximum");}
82 
86 scalar_t getMaxImbalance() const { return this->getMetricValue("maximum imbalance");}
87 
89 scalar_t getAvgImbalance() const { return this->getMetricValue("average imbalance");}
90 
92 virtual const std::vector<std::string> & getMetrics() const { return ImbalanceMetrics<scalar_t>::static_metricNames_; }
93 
95 static std::string static_metricTypeName_;
96 
98 static std::vector<std::string> static_metricNames_;
99 }; // end class
100 
103 
105 template <typename scalar_t>
106 std::vector<std::string> ImbalanceMetrics<scalar_t>::static_metricNames_ = {
107  "local sum",
108  "global sum",
109  "global minimum",
110  "global maximum",
111  "global average",
112  "average imbalance",
113  "maximum imbalance",
114 };
115 
116 template <typename scalar_t>
118 {
119  os << std::setw(20) << " ";
120  os << std::setw(15) << "min" << std::setw(15) << "max" << std::setw(15) << "avg";
121  os << std::setw(2) << " ";
122  os << std::setw(10) << "imbalance";
123  os << std::endl;
124 }
125 
126 template <typename scalar_t>
127  void ImbalanceMetrics<scalar_t>::printLine(std::ostream &os) const
128 {
129  std::string label( this->getName() );
130  if (mcnorm_ > 0){
131  multiCriteriaNorm realNorm = multiCriteriaNorm(mcnorm_ - 1);
132  std::ostringstream oss;
133  switch (realNorm) {
134  case normMinimizeTotalWeight: // 1-norm = Manhattan norm
135  oss << this->getName() << " (1)";
136  break;
137  case normBalanceTotalMaximum: // 2-norm = sqrt of sum of squares
138  oss << this->getName() << " (2)";
139  break;
140  case normMinimizeMaximumWeight: // inf-norm = maximum norm
141  oss << this->getName() << " (inf)";
142  break;
143  default:
144  oss << this->getName() << " (?)";
145  break;
146  }
147 
148  label = oss.str();
149  }
150 
151  auto min = this->getMetricValue("global minimum");
152  auto max = this->getMetricValue("global maximum");
153  auto avg = this->getMetricValue("global average");
154  int precision = 4;
155  if( min > 999 ) { precision = 0; }
156  else if( min > 99 ) { precision = 2; }
157 
158  os << std::setw(20) << label;
159  os << std::setw(15) << std::setprecision(precision) << min;
160  os << std::setw(15) << std::setprecision(precision) << max;
161  os << std::setw(15) << std::setprecision(precision) << avg;
162 
163  os << std::setw(2) << " ";
164  os << std::setw(10) << std::setprecision(4)
165  << this->getMetricValue("maximum imbalance");
166 
167  os << std::endl;
168 }
169 } // namespace Zoltan2
170 #endif
scalar_t getLocalSum() const
Get the sum on the local process.
void setLocalSum(scalar_t x)
Set the sum on the local process.
void setGlobalMin(scalar_t x)
Set the global minimum across parts.
virtual const std::string & getMetricType() const
Get the class type of the metric.
virtual const std::vector< std::string > & getMetrics() const
virtual void printLine(std::ostream &os) const
Print a standard line of data that fits under the header.
void setGlobalSum(scalar_t x)
Set the global sum.
scalar_t getAvgImbalance() const
Get the average of the part imbalances.
void setAvgImbalance(scalar_t x)
Set the average imbalance of all parts.
scalar_t getMetricValue(const std::string &metric_name) const
scalar_t getGlobalMin() const
Get the global minimum across all parts.
#define IMBALANCE_METRICS_TYPE_NAME
void setMetricValue(const std::string &metric_name, scalar_t value) const
scalar_t getGlobalMax() const
Get the global maximum across all parts.
static void printHeader(std::ostream &os)
Print a standard header.
static std::vector< std::string > static_metricNames_
void setGlobalMax(scalar_t x)
Set the global maximum across parts.
void setMaxImbalance(scalar_t x)
Set the imbalance of the worst imbalanced part. This is what we normally call the imbalance of a part...
void setNorm(multiCriteriaNorm normVal)
Set or reset the norm.
scalar_t getMaxImbalance() const
Get the imbalance of the most imbalanced part. This is what we normally call the imbalance of a parti...
multiCriteriaNorm getNorm()
Get the norm.
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
Defines the GraphModel interface.
ImbalanceMetrics(std::string mname)
Constructor.
scalar_t getGlobalSum() const
Get the global sum for all parts.