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 // ***********************************************************************
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 #define IMBALANCE_METRICS_TYPE_NAME "ImbalanceMetrics"
57 
58 namespace Zoltan2{
59 
61 template <typename scalar_t>
62  class ImbalanceMetrics : public BaseClassMetrics<scalar_t> {
63 
64 private:
65  multiCriteriaNorm mcnorm_; // store "actualNorm + 1"
66 
67 public:
69 ImbalanceMetrics(std::string mname) : BaseClassMetrics<scalar_t>(static_metricNames_.size(), mname),mcnorm_(multiCriteriaNorm(0)) {}
70 
73 
75 virtual const std::string & getMetricType() const { return static_metricTypeName_; }
76 
78 static void printHeader(std::ostream &os);
79 
81 virtual void printLine(std::ostream &os) const;
82 
84 void setNorm(multiCriteriaNorm normVal) { mcnorm_ = multiCriteriaNorm(normVal+1);}
85 
88 
90 void setLocalSum(scalar_t x) { this->setMetricValue("local sum", x);}
91 
93 void setGlobalSum(scalar_t x) { this->setMetricValue("global sum", x );}
94 
96 void setGlobalMin(scalar_t x) { this->setMetricValue("global minimum", x );}
97 
99 void setGlobalMax(scalar_t x) { this->setMetricValue("global maximum", x );}
100 
102 void setMaxImbalance(scalar_t x) { this->setMetricValue("maximum imbalance", x);}
103 
105 void setAvgImbalance(scalar_t x) { this->setMetricValue("average imbalance", x);}
106 
108 scalar_t getLocalSum() const { return this->getMetricValue("local sum");}
109 
111 scalar_t getGlobalSum() const { return this->getMetricValue("global sum");}
112 
114 scalar_t getGlobalMin() const { return this->getMetricValue("global minimum");}
115 
117 scalar_t getGlobalMax() const { return this->getMetricValue("global maximum");}
118 
122 scalar_t getMaxImbalance() const { return this->getMetricValue("maximum imbalance");}
123 
125 scalar_t getAvgImbalance() const { return this->getMetricValue("average imbalance");}
126 
128 virtual const std::vector<std::string> & getMetrics() const { return ImbalanceMetrics<scalar_t>::static_metricNames_; }
129 
131 static std::string static_metricTypeName_;
132 
134 static std::vector<std::string> static_metricNames_;
135 }; // end class
136 
139 
141 template <typename scalar_t>
142 std::vector<std::string> ImbalanceMetrics<scalar_t>::static_metricNames_ = {
143  "local sum",
144  "global sum",
145  "global minimum",
146  "global maximum",
147  "global average",
148  "average imbalance",
149  "maximum imbalance",
150 };
151 
152 template <typename scalar_t>
154 {
155  os << std::setw(20) << " ";
156  os << std::setw(11) << "min" << std::setw(11) << "max" << std::setw(11) << "avg";
157  os << std::setw(2) << " ";
158  os << std::setw(10) << "imbalance";
159  os << std::endl;
160 }
161 
162 template <typename scalar_t>
163  void ImbalanceMetrics<scalar_t>::printLine(std::ostream &os) const
164 {
165  std::string label( this->getName() );
166  if (mcnorm_ > 0){
167  multiCriteriaNorm realNorm = multiCriteriaNorm(mcnorm_ - 1);
168  std::ostringstream oss;
169  switch (realNorm) {
170  case normMinimizeTotalWeight: // 1-norm = Manhattan norm
171  oss << this->getName() << " (1)";
172  break;
173  case normBalanceTotalMaximum: // 2-norm = sqrt of sum of squares
174  oss << this->getName() << " (2)";
175  break;
176  case normMinimizeMaximumWeight: // inf-norm = maximum norm
177  oss << this->getName() << " (inf)";
178  break;
179  default:
180  oss << this->getName() << " (?)";
181  break;
182  }
183 
184  label = oss.str();
185  }
186 
187  os << std::setw(20) << label;
188  os << std::setw(11) << std::setprecision(4)
189  << this->getMetricValue("global minimum");
190  os << std::setw(11) << std::setprecision(4)
191  << this->getMetricValue("global maximum");
192  os << std::setw(11) << std::setprecision(4)
193  << this->getMetricValue("global average");
194 
195  os << std::setw(2) << " ";
196  os << std::setw(10) << std::setprecision(4)
197  << this->getMetricValue("maximum imbalance");
198 
199  os << std::endl;
200 }
201 } // namespace Zoltan2
202 #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.