Zoltan2 Version of the Day
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::scalar_t scalar_t;
00073 
00074   const RCP<const Environment> env_;
00075 
00076   partId_t numGlobalParts_;           // desired
00077   partId_t targetGlobalParts_;        // actual
00078   partId_t numNonEmpty_;              // of actual
00079 
00080   ArrayRCP<MetricValues<scalar_t> > metrics_;
00081   ArrayRCP<const MetricValues<scalar_t> > metricsConst_;
00082 
00083 public:
00084 
00094   PartitioningSolutionQuality(const RCP<const Environment> &env,
00095     const RCP<const Comm<int> > &problemComm,
00096     const RCP<const Adapter> &ia, 
00097     const RCP<const PartitioningSolution<Adapter> > &soln);
00098 
00102   ArrayRCP<const MetricValues<scalar_t> > getMetrics() const{
00103     return metricsConst_;
00104   }
00105 
00109   void getObjectCountImbalance(scalar_t &imbalance) const{
00110     imbalance = metrics_[0].getMaxImbalance();
00111   }
00112 
00118   void getNormedImbalance(scalar_t &imbalance) const{
00119     if (metrics_.size() > 1)
00120       imbalance = metrics_[1].getMaxImbalance();
00121     else 
00122       imbalance = metrics_[0].getMaxImbalance();
00123   }
00124 
00131   void getWeightImbalance(scalar_t &imbalance, int dim=0) const{
00132     imbalance = 0;
00133     if (metrics_.size() > 2)  // dimension dim of multiple weights
00134       imbalance = metrics_[dim+2].getMaxImbalance();
00135     else if (metrics_.size() == 2)   //  only one weight
00136       imbalance = metrics_[1].getMaxImbalance();
00137     else                       // no weights, return object count imbalance
00138       imbalance = metrics_[0].getMaxImbalance();
00139   }  
00140 
00143   void printMetrics(ostream &os) const {
00144     Zoltan2::printMetrics<scalar_t>(os, 
00145       targetGlobalParts_, numGlobalParts_, numNonEmpty_, 
00146       metrics_.view(0, metrics_.size()));
00147   }
00148 };
00149 
00150 template <typename Adapter>
00151   PartitioningSolutionQuality<Adapter>::PartitioningSolutionQuality(
00152   const RCP<const Environment> &env,
00153   const RCP<const Comm<int> > &problemComm,
00154   const RCP<const Adapter> &ia, 
00155   const RCP<const PartitioningSolution<Adapter> > &soln):
00156     env_(env), numGlobalParts_(0), targetGlobalParts_(0), numNonEmpty_(0),
00157     metrics_(),  metricsConst_()
00158 {
00159 
00160   env->debug(DETAILED_STATUS, string("Entering PartitioningSolutionQuality"));
00161   env->timerStart(MACRO_TIMERS, "Computing metrics");
00162 
00163   // When we add parameters for which weights to use, we
00164   // should check those here.  For now we compute metrics
00165   // using all weights.
00166 
00167   const Teuchos::ParameterList &pl = env->getParameters();
00168   multiCriteriaNorm mcnorm = normBalanceTotalMaximum;
00169   bool balanceCountOnly=false;
00170 
00171   const Teuchos::ParameterEntry *pe = pl.getEntryPtr("partitioning_objective");
00172 
00173   if (pe){
00174     string strChoice = pe->getValue<string>(&strChoice);
00175     if (strChoice == string("balance_object_count"))
00176       balanceCountOnly=true;
00177     else if (strChoice == string("multicriteria_minimize_total_weight"))
00178       mcnorm = normMinimizeTotalWeight;
00179     else if (strChoice == string("multicriteria_minimize_maximum_weight"))
00180       mcnorm = normMinimizeMaximumWeight;
00181   } 
00182 
00183   try{
00184     objectMetrics<Adapter>(env, problemComm, mcnorm, ia, soln,
00185       numGlobalParts_, numNonEmpty_, metrics_);
00186   }
00187   Z2_FORWARD_EXCEPTIONS;
00188 
00189   targetGlobalParts_ = soln->getTargetGlobalNumberOfParts();
00190 
00191   env->timerStop(MACRO_TIMERS, "Computing metrics");
00192   env->debug(DETAILED_STATUS, string("Exiting PartitioningSolutionQuality"));
00193 }
00194 
00195 }   // namespace Zoltan2
00196 
00197 #endif