Zoltan2
Zoltan2_PartitioningSolutionQuality.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #ifndef ZOLTAN2_SOLUTIONQUALITY_HPP
00051 #define ZOLTAN2_SOLUTIONQUALITY_HPP
00052 
00053 #include <Zoltan2_Metric.hpp>
00054 #include <Zoltan2_PartitioningSolution.hpp>
00055 
00056 namespace Zoltan2{
00057 
00065 template <typename Adapter>
00066   class PartitioningSolutionQuality {
00067 
00068 private:
00069 
00070   typedef typename Adapter::lno_t lno_t;
00071   typedef typename Adapter::gid_t gid_t;
00072   typedef typename Adapter::part_t part_t;
00073   typedef typename Adapter::scalar_t scalar_t;
00074 
00075   const RCP<const Environment> env_;
00076 
00077   part_t numGlobalParts_;           // desired
00078   part_t targetGlobalParts_;        // actual
00079   part_t numNonEmpty_;              // of actual
00080 
00081   ArrayRCP<MetricValues<scalar_t> > metrics_;
00082   ArrayRCP<const MetricValues<scalar_t> > metricsConst_;
00083 
00084 public:
00085 
00095   PartitioningSolutionQuality(const RCP<const Environment> &env,
00096     const RCP<const Comm<int> > &problemComm,
00097     const RCP<const Adapter> &ia, 
00098     const RCP<const PartitioningSolution<Adapter> > &soln);
00099 
00103   ArrayRCP<const MetricValues<scalar_t> > getMetrics() const{
00104     return metricsConst_;
00105   }
00106 
00110   void getObjectCountImbalance(scalar_t &imbalance) const{
00111     imbalance = metrics_[0].getMaxImbalance();
00112   }
00113 
00119   void getNormedImbalance(scalar_t &imbalance) const{
00120     if (metrics_.size() > 1)
00121       imbalance = metrics_[1].getMaxImbalance();
00122     else 
00123       imbalance = metrics_[0].getMaxImbalance();
00124   }
00125 
00132   void getWeightImbalance(scalar_t &imbalance, int idx=0) const{
00133     imbalance = 0;
00134     if (metrics_.size() > 2)  // idx of multiple weights
00135       imbalance = metrics_[idx+2].getMaxImbalance();
00136     else if (metrics_.size() == 2)   //  only one weight
00137       imbalance = metrics_[1].getMaxImbalance();
00138     else                       // no weights, return object count imbalance
00139       imbalance = metrics_[0].getMaxImbalance();
00140   }  
00141 
00144   void printMetrics(std::ostream &os) const {
00145     Zoltan2::printMetrics<scalar_t, part_t>(os, 
00146       targetGlobalParts_, numGlobalParts_, numNonEmpty_, 
00147       metrics_.view(0, metrics_.size()));
00148   }
00149 };
00150 
00151 template <typename Adapter>
00152   PartitioningSolutionQuality<Adapter>::PartitioningSolutionQuality(
00153   const RCP<const Environment> &env,
00154   const RCP<const Comm<int> > &problemComm,
00155   const RCP<const Adapter> &ia, 
00156   const RCP<const PartitioningSolution<Adapter> > &soln):
00157     env_(env), numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0),
00158     metrics_(),  metricsConst_()
00159 {
00160 
00161   env->debug(DETAILED_STATUS, std::string("Entering PartitioningSolutionQuality"));
00162   env->timerStart(MACRO_TIMERS, "Computing metrics");
00163 
00164   // When we add parameters for which weights to use, we
00165   // should check those here.  For now we compute metrics
00166   // using all weights.
00167 
00168   const Teuchos::ParameterList &pl = env->getParameters();
00169   multiCriteriaNorm mcnorm = normBalanceTotalMaximum;
00170 
00171   const Teuchos::ParameterEntry *pe = pl.getEntryPtr("partitioning_objective");
00172 
00173   if (pe){
00174     std::string strChoice = pe->getValue<std::string>(&strChoice);
00175     if (strChoice == std::string("multicriteria_minimize_total_weight"))
00176       mcnorm = normMinimizeTotalWeight;
00177     else if (strChoice == std::string("multicriteria_minimize_maximum_weight"))
00178       mcnorm = normMinimizeMaximumWeight;
00179   } 
00180 
00181   try{
00182     objectMetrics<Adapter>(env, problemComm, mcnorm, ia, soln,
00183       numGlobalParts_, numNonEmpty_, metrics_);
00184   }
00185   Z2_FORWARD_EXCEPTIONS;
00186 
00187   targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
00188 
00189   env->timerStop(MACRO_TIMERS, "Computing metrics");
00190   env->debug(DETAILED_STATUS, std::string("Exiting PartitioningSolutionQuality"));
00191 }
00192 
00193 }   // namespace Zoltan2
00194 
00195 #endif