Zoltan2
Zoltan2_ImbalanceMetrics.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
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 NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS 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 Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
49 #ifndef ZOLTAN2_IMBALANCEMETRICS_HPP
50 #define ZOLTAN2_IMBALANCEMETRICS_HPP
51 
53 #include <Zoltan2_GraphModel.hpp>
55 
56 namespace Zoltan2{
57 
59 template <typename scalar_t>
60  class ImbalanceMetrics : public BaseClassMetrics<scalar_t> {
61 
62 private:
63  multiCriteriaNorm mcnorm_; // store "actualNorm + 1"
64 
65 public:
67 ImbalanceMetrics(std::string mname) : BaseClassMetrics<scalar_t>(static_metricNames_.size(), mname),mcnorm_(multiCriteriaNorm(0)) {}
68 
71 
73 virtual const std::string & getMetricType() const { return static_metricTypeName_; }
74 
76 static void printHeader(std::ostream &os);
77 
79 virtual void printLine(std::ostream &os) const;
80 
82 void setNorm(multiCriteriaNorm normVal) { mcnorm_ = multiCriteriaNorm(normVal+1);}
83 
86 
88 void setLocalSum(scalar_t x) { this->setMetricValue("local sum", x);}
89 
91 void setGlobalSum(scalar_t x) { this->setMetricValue("global sum", x );}
92 
94 void setGlobalMin(scalar_t x) { this->setMetricValue("global minimum", x );}
95 
97 void setGlobalMax(scalar_t x) { this->setMetricValue("global maximum", x );}
98 
100 void setMaxImbalance(scalar_t x) { this->setMetricValue("maximum imbalance", x);}
101 
103 void setAvgImbalance(scalar_t x) { this->setMetricValue("average imbalance", x);}
104 
106 scalar_t getLocalSum() const { return this->getMetricValue("local sum");}
107 
109 scalar_t getGlobalSum() const { return this->getMetricValue("global sum");}
110 
112 scalar_t getGlobalMin() const { return this->getMetricValue("global minimum");}
113 
115 scalar_t getGlobalMax() const { return this->getMetricValue("global maximum");}
116 
120 scalar_t getMaxImbalance() const { return this->getMetricValue("maximum imbalance");}
121 
123 scalar_t getAvgImbalance() const { return this->getMetricValue("average imbalance");}
124 
126 virtual const std::vector<std::string> & getMetrics() const { return ImbalanceMetrics<scalar_t>::static_metricNames_; }
127 
129 static std::string static_metricTypeName_;
130 
132 static std::vector<std::string> static_metricNames_;
133 }; // end class
134 
137 
139 template <typename scalar_t>
140 std::vector<std::string> ImbalanceMetrics<scalar_t>::static_metricNames_ = {
141  "local sum",
142  "global sum",
143  "global minimum",
144  "global maximum",
145  "global average",
146  "average imbalance",
147  "maximum imbalance",
148 };
149 
150 template <typename scalar_t>
152 {
153  os << std::setw(20) << " ";
154  os << std::setw(11) << "min" << std::setw(11) << "max" << std::setw(11) << "avg";
155  os << std::setw(2) << " ";
156  os << std::setw(10) << "imbalance";
157  os << std::endl;
158 }
159 
160 template <typename scalar_t>
161  void ImbalanceMetrics<scalar_t>::printLine(std::ostream &os) const
162 {
163  std::string label( this->getName() );
164  if (mcnorm_ > 0){
165  multiCriteriaNorm realNorm = multiCriteriaNorm(mcnorm_ - 1);
166  std::ostringstream oss;
167  switch (realNorm) {
168  case normMinimizeTotalWeight: // 1-norm = Manhattan norm
169  oss << this->getName() << " (1)";
170  break;
171  case normBalanceTotalMaximum: // 2-norm = sqrt of sum of squares
172  oss << this->getName() << " (2)";
173  break;
174  case normMinimizeMaximumWeight: // inf-norm = maximum norm
175  oss << this->getName() << " (inf)";
176  break;
177  default:
178  oss << this->getName() << " (?)";
179  break;
180  }
181 
182  label = oss.str();
183  }
184 
185  os << std::setw(20) << label;
186  os << std::setw(11) << std::setprecision(4)
187  << this->getMetricValue("global minimum");
188  os << std::setw(11) << std::setprecision(4)
189  << this->getMetricValue("global maximum");
190  os << std::setw(11) << std::setprecision(4)
191  << this->getMetricValue("global average");
192 
193  os << std::setw(2) << " ";
194  os << std::setw(10) << std::setprecision(4)
195  << this->getMetricValue("maximum imbalance");
196 
197  os << std::endl;
198 }
199 } // namespace Zoltan2
200 #endif
scalar_t getGlobalMin() const
Get the global minimum across all parts.
scalar_t getMetricValue(const std::string &metric_name) const
scalar_t getGlobalMax() const
Get the global maximum across all parts.
void setLocalSum(scalar_t x)
Set the sum on the local process.
#define IMBALANCE_METRICS_TYPE_NAME
void setGlobalMin(scalar_t x)
Set the global minimum across parts.
void setGlobalSum(scalar_t x)
Set the global sum.
virtual void printLine(std::ostream &os) const
Print a standard line of data that fits under the header.
void setAvgImbalance(scalar_t x)
Set the average imbalance of all parts.
void setMetricValue(const std::string &metric_name, scalar_t value) const
scalar_t getLocalSum() const
Get the sum on the local process.
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.
multiCriteriaNorm getNorm()
Get the norm.
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
virtual const std::vector< std::string > & getMetrics() const
Defines the GraphModel interface.
scalar_t getAvgImbalance() const
Get the average of the part imbalances.
virtual const std::string & getMetricType() const
Get the class type of the metric.
scalar_t getMaxImbalance() const
Get the imbalance of the most imbalanced part. This is what we normally call the imbalance of a parti...
ImbalanceMetrics(std::string mname)
Constructor.
scalar_t getGlobalSum() const
Get the global sum for all parts.