Zoltan2
Zoltan2_PartitioningSolutionQuality.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 
50 #ifndef ZOLTAN2_SOLUTIONQUALITY_HPP
51 #define ZOLTAN2_SOLUTIONQUALITY_HPP
52 
53 #include <Zoltan2_Metric.hpp>
55 
56 namespace Zoltan2{
57 
65 template <typename Adapter>
67 
68 private:
69 
70  typedef typename Adapter::lno_t lno_t;
71  typedef typename Adapter::part_t part_t;
72  typedef typename Adapter::scalar_t scalar_t;
73 
74  const RCP<const Environment> env_;
75 
76  part_t numGlobalParts_; // desired
77  part_t targetGlobalParts_; // actual
78  part_t numNonEmpty_; // of actual
79 
80  ArrayRCP<MetricValues<scalar_t> > metrics_;
81  ArrayRCP<const MetricValues<scalar_t> > metricsConst_;
82 
83 public:
84 
94  PartitioningSolutionQuality(const RCP<const Environment> &env,
95  const RCP<const Comm<int> > &problemComm,
96  const RCP<const Adapter> &ia,
97  const RCP<const PartitioningSolution<Adapter> > &soln);
98 
102  ArrayRCP<const MetricValues<scalar_t> > getMetrics() const{
103  //BDD return metricsConst_;
104  if(metricsConst_.is_null()) return metrics_;
105  return metricsConst_;
106  }
107 
111  void getObjectCountImbalance(scalar_t &imbalance) const{
112  imbalance = metrics_[0].getMaxImbalance();
113  }
114 
120  void getNormedImbalance(scalar_t &imbalance) const{
121  if (metrics_.size() > 1)
122  imbalance = metrics_[1].getMaxImbalance();
123  else
124  imbalance = metrics_[0].getMaxImbalance();
125  }
126 
133  void getWeightImbalance(scalar_t &imbalance, int idx=0) const{
134  imbalance = 0;
135  if (metrics_.size() > 2) // idx of multiple weights
136  imbalance = metrics_[idx+2].getMaxImbalance();
137  else if (metrics_.size() == 2) // only one weight
138  imbalance = metrics_[1].getMaxImbalance();
139  else // no weights, return object count imbalance
140  imbalance = metrics_[0].getMaxImbalance();
141  }
142 
145  void printMetrics(std::ostream &os) const {
146  Zoltan2::printMetrics<scalar_t, part_t>(os,
147  targetGlobalParts_, numGlobalParts_, numNonEmpty_,
148  metrics_.view(0, metrics_.size()));
149  }
150 };
151 
152 template <typename Adapter>
154 
155 private:
156 
157  typedef typename Adapter::lno_t lno_t;
158  typedef typename Adapter::part_t part_t;
159  typedef typename Adapter::scalar_t scalar_t;
160 
161  const RCP<const Environment> env_;
162 
163  part_t numGlobalParts_; // desired
164  part_t targetGlobalParts_; // actual
165 
166  ArrayRCP<GraphMetricValues<scalar_t> > metrics_;
167  ArrayRCP<const GraphMetricValues<scalar_t> > metricsConst_;
168 
169 public:
170 
180  GraphPartitioningSolutionQuality(const RCP<const Environment> &env,
181  const RCP<const Comm<int> > &problemComm,
182  const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graph,
183  const RCP<const Adapter> &ia,
184  const RCP<const PartitioningSolution<Adapter> > &soln);
185 
189  ArrayRCP<const GraphMetricValues<scalar_t> > getGraphMetrics() const{
190  if(metricsConst_.is_null()) return metrics_;
191  return metricsConst_;
192  }
193 
200  void getWeightCut(scalar_t &cut, int idx=0) const{
201  if (metrics_.size() < idx) // idx too high
202  cut = metrics_[metrics_.size()-1].getGlobalMax();
203  else if (idx < 0) // idx too low
204  cut = metrics_[0].getGlobalMax();
205  else // idx weight
206  cut = metrics_[idx].getGlobalMax();
207  }
208 };
209 
210 template <typename Adapter>
212  const RCP<const Environment> &env,
213  const RCP<const Comm<int> > &problemComm,
214  const RCP<const Adapter> &ia,
215  const RCP<const PartitioningSolution<Adapter> > &soln):
216  env_(env), numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0),
217  metrics_(), metricsConst_()
218 {
219 
220  env->debug(DETAILED_STATUS, std::string("Entering PartitioningSolutionQuality"));
221  env->timerStart(MACRO_TIMERS, "Computing metrics");
222 
223  // When we add parameters for which weights to use, we
224  // should check those here. For now we compute metrics
225  // using all weights.
226 
227  const Teuchos::ParameterList &pl = env->getParameters();
229 
230  const Teuchos::ParameterEntry *pe = pl.getEntryPtr("partitioning_objective");
231 
232  if (pe){
233  std::string strChoice = pe->getValue<std::string>(&strChoice);
234  if (strChoice == std::string("multicriteria_minimize_total_weight"))
235  mcnorm = normMinimizeTotalWeight;
236  else if (strChoice == std::string("multicriteria_minimize_maximum_weight"))
237  mcnorm = normMinimizeMaximumWeight;
238  }
239 
240  try{
241  objectMetrics<Adapter>(env, problemComm, mcnorm, ia, soln,
242  numGlobalParts_, numNonEmpty_, metrics_);
243  }
245 
246  targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
247 
248  env->timerStop(MACRO_TIMERS, "Computing metrics");
249  env->debug(DETAILED_STATUS, std::string("Exiting PartitioningSolutionQuality"));
250 }
251 
252 template <typename Adapter>
254  const RCP<const Environment> &env,
255  const RCP<const Comm<int> > &problemComm,
256  const RCP<const GraphModel<typename Adapter::base_adapter_t> > &graph,
257  const RCP<const Adapter> &ia,
258  const RCP<const PartitioningSolution<Adapter> > &soln):
259  env_(env), numGlobalParts_(0), targetGlobalParts_(0),
260  metrics_(), metricsConst_()
261 {
262 
263  env->debug(DETAILED_STATUS,
264  std::string("Entering GraphPartitioningSolutionQuality"));
265  env->timerStart(MACRO_TIMERS, "Computing graph metrics");
266  // When we add parameters for which weights to use, we
267  // should check those here. For now we compute graph metrics
268  // using all weights.
269 
270  typedef typename Adapter::part_t part_t;
271 
272  // Local number of objects.
273 
274  size_t numLocalObjects = ia->getLocalNumIDs();
275 
276  // Parts to which objects are assigned.
277 
278  const part_t *parts = soln->getPartListView();
279  env->localInputAssertion(__FILE__, __LINE__, "parts not set",
280  ((numLocalObjects == 0) || parts), BASIC_ASSERTION);
281  ArrayView<const part_t> partArray(parts, numLocalObjects);
282 
283  ArrayRCP<scalar_t> globalSums;
284 
285  try{
286  globalWeightedCutsByPart<Adapter>(env,
287  problemComm, graph, partArray, numGlobalParts_, metrics_, globalSums);
288  }
290 
291  targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
292 
293  env->timerStop(MACRO_TIMERS, "Computing graph metrics");
294  env->debug(DETAILED_STATUS,
295  std::string("Exiting GraphPartitioningSolutionQuality"));
296 }
297 
298 } // namespace Zoltan2
299 
300 #endif
void getWeightImbalance(scalar_t &imbalance, int idx=0) const
Return the imbalance for the requested weight.
void printMetrics(std::ostream &os) const
Print all the metrics.
Time an algorithm (or other entity) as a whole.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Metric class and namespace methods to compute quality metrics.
void getObjectCountImbalance(scalar_t &imbalance) const
Return the object count imbalance.
void getWeightCut(scalar_t &cut, int idx=0) const
Return the max cut for the requested weight.
Defines the PartitioningSolution class.
sub-steps, each method&#39;s entry and exit
ArrayRCP< const GraphMetricValues< scalar_t > > getGraphMetrics() const
Return the graph metric values.
A class that computes and returns quality metrics.
A PartitioningSolution is a solution to a partitioning problem.
GraphPartitioningSolutionQuality(const RCP< const Environment > &env, const RCP< const Comm< int > > &problemComm, const RCP< const GraphModel< typename Adapter::base_adapter_t > > &graph, const RCP< const Adapter > &ia, const RCP< const PartitioningSolution< Adapter > > &soln)
Constructor.
GraphModel defines the interface required for graph models.
multiCriteriaNorm
Enumerator used in code for multicriteria norm choice.
void getNormedImbalance(scalar_t &imbalance) const
Return the object normed weight imbalance.
ArrayRCP< const MetricValues< scalar_t > > getMetrics() const
Return the metric values.
PartitioningSolutionQuality(const RCP< const Environment > &env, const RCP< const Comm< int > > &problemComm, const RCP< const Adapter > &ia, const RCP< const PartitioningSolution< Adapter > > &soln)
Constructor.