Zoltan2
Zoltan2_AlgBlock.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 #ifndef _ZOLTAN2_ALGBLOCK_HPP_
00046 #define _ZOLTAN2_ALGBLOCK_HPP_
00047 
00048 #include <Zoltan2_IdentifierModel.hpp>
00049 #include <Zoltan2_PartitioningSolution.hpp>
00050 #include <Zoltan2_Algorithm.hpp>
00051 
00052 #include <sstream>
00053 #include <string>
00054 #include <bitset>
00055 
00060 namespace Zoltan2{
00061 
00064 enum blockParams{
00065   block_balanceCount,            
00066   block_balanceWeight,          
00067   block_minTotalWeight,      
00068   block_minMaximumWeight,  
00069   block_balanceTotalMaximum, 
00070   NUM_BLOCK_PARAMS
00071 };
00072 
00090 template <typename Adapter>
00091 class AlgBlock : public Algorithm<Adapter>
00092 {
00093 
00094 private:
00095   const RCP<const Environment> env;
00096   const RCP<Comm<int> > problemComm;
00097   const RCP<const IdentifierModel<typename Adapter::base_adapter_t> > ids;
00098 
00099 public:
00100   typedef typename Adapter::lno_t lno_t;     // local ids
00101   typedef typename Adapter::gno_t gno_t;     // global ids
00102   typedef typename Adapter::scalar_t scalar_t;   // scalars
00103   typedef typename Adapter::part_t part_t;   // part numbers
00104 
00105   // Constructor
00106   AlgBlock(
00107     const RCP<const Environment> &env_,
00108     const RCP<Comm<int> > &problemComm_,
00109     const RCP<const IdentifierModel<typename Adapter::base_adapter_t> > &ids_
00110   ) : 
00111     env(env_), problemComm(problemComm_), ids(ids_)
00112   {}
00113 
00114   // Partitioning method
00115   void partition(const RCP<PartitioningSolution<Adapter> > &solution)
00116   {
00117     using std::string;
00118     using std::ostringstream;
00119 
00120     env->debug(DETAILED_STATUS, string("Entering AlgBlock"));
00121 
00122     int rank = env->myRank_;
00123     int nprocs = env->numProcs_;
00124 
00126     // From the IdentifierModel we need:
00127     //    the number of gnos
00128     //    number of weights per gno
00129     //    the weights
00130 
00131     size_t numGnos = ids->getLocalNumIdentifiers();
00132 
00133     ArrayView<const gno_t> idList;
00134     typedef StridedData<lno_t, scalar_t> input_t;
00135     ArrayView<input_t> wgtList;
00136   
00137     ids->getIdentifierList(idList, wgtList);
00138 
00139     // If user supplied no weights, we use uniform weights.
00140     bool uniformWeights = (wgtList.size() == 0);
00141 
00143     // Partitioning problem parameters of interest:
00144     //    objective
00145     //    imbalance_tolerance
00146 
00147     const Teuchos::ParameterList &pl = env->getParameters();
00148     const Teuchos::ParameterEntry *pe;
00149 
00150     pe = pl.getEntryPtr("partitioning_objective");
00151     if (pe) {
00152       string po = pe->getValue<string>(&po);
00153       if (po == string("balance_object_count"))
00154         uniformWeights = true;    // User requests that we ignore weights
00155     }
00156 
00157     double imbalanceTolerance=1.1;
00158     pe = pl.getEntryPtr("imbalance_tolerance");
00159     if (pe) imbalanceTolerance = pe->getValue<double>(&imbalanceTolerance);
00160 
00162     // From the Solution we get part information:
00163     // number of parts and part sizes
00164 
00165     size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
00166 
00167     Array<scalar_t> part_sizes(numGlobalParts);
00168 
00169     if (solution->criteriaHasUniformPartSizes(0))
00170       for (unsigned int i=0; i<numGlobalParts; i++)
00171         part_sizes[i] = 1.0 / numGlobalParts;
00172     else
00173       for (unsigned int i=0; i<numGlobalParts; i++)
00174         part_sizes[i] = solution->getCriteriaPartSize(0, i);
00175 
00176     for (unsigned int i=1; i<numGlobalParts; i++)
00177       part_sizes[i] += part_sizes[i-1];
00178 
00179     // TODO assertion that last part sizes is about equal to 1.0
00180 
00181 
00183     // The algorithm
00184     //
00185     // Block partitioning algorithm lifted from zoltan/src/simple/block.c
00186     // The solution is:
00187     //    a list of part numbers in gno order
00188     //    an imbalance for each weight 
00189 
00190     scalar_t wtsum(0);
00191 
00192     if (!uniformWeights) {
00193       for (size_t i=0; i<numGnos; i++)
00194         wtsum += wgtList[0][i];          // [] operator knows stride
00195     }
00196     else
00197       wtsum = static_cast<scalar_t>(numGnos);
00198 
00199     Array<scalar_t> scansum(nprocs+1, 0);
00200 
00201     Teuchos::gatherAll<int, scalar_t>(*problemComm, 1, &wtsum, nprocs,
00202       scansum.getRawPtr()+1);
00203 
00204     /* scansum = sum of weights on lower processors, excluding self. */
00205 
00206     for (int i=2; i<=nprocs; i++)
00207       scansum[i] += scansum[i-1];
00208 
00209     scalar_t globalTotalWeight = scansum[nprocs];
00210 
00211     if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS) {
00212       ostringstream oss("Part sizes: ");
00213       for (unsigned int i=0; i < numGlobalParts; i++)
00214         oss << part_sizes[i] << " ";
00215       oss << std::endl << std::endl << "Weights : ";
00216       for (int i=0; i <= nprocs; i++)
00217         oss << scansum[i] << " ";
00218       oss << std::endl;
00219       env->debug(VERBOSE_DETAILED_STATUS, oss.str());
00220     }
00221 
00222     /* Loop over objects and assign part. */
00223     part_t part = 0;
00224     wtsum = scansum[rank];
00225     Array<scalar_t> partTotal(numGlobalParts, 0);
00226     ArrayRCP<part_t> gnoPart= arcp(new part_t[numGnos], 0, numGnos);
00227 
00228     env->memory("Block algorithm memory");
00229 
00230     for (size_t i=0; i<numGnos; i++){
00231       scalar_t gnoWeight = (uniformWeights ? 1.0 : wgtList[0][i]);
00232       /* wtsum is now sum of all lower-ordered object */
00233       /* determine new part number for this object,
00234          using the "center of gravity" */
00235       while (unsigned(part)<numGlobalParts-1 && 
00236              (wtsum+0.5*gnoWeight) > part_sizes[part]*globalTotalWeight)
00237         part++;
00238       gnoPart[i] = part;
00239       partTotal[part] += gnoWeight;
00240       wtsum += gnoWeight;
00241     }
00242 
00244     // Done
00245 
00246     ArrayRCP<const gno_t> gnos = arcpFromArrayView(idList);
00247     solution->setParts(gnos, gnoPart, true);
00248 
00249     env->debug(DETAILED_STATUS, string("Exiting AlgBlock"));
00250   }
00251 };
00252 
00253 }   // namespace Zoltan2
00254 
00255 #endif