Zoltan2
Zoltan2_AlgWolf.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_ALGWOLF_HPP_
00046 #define _ZOLTAN2_ALGWOLF_HPP_
00047 
00048 #include <Zoltan2_IdentifierModel.hpp>
00049 #include <Zoltan2_PartitioningSolution.hpp>
00050 #include <Zoltan2_Algorithm.hpp>
00051 #include <Zoltan2_AlgRCB.hpp>
00052 
00053 #include <sstream>
00054 #include <string>
00055 #include <bitset>
00056 
00061 namespace Zoltan2
00062 {
00063 
00064 // /*! \brief The boolean parameters of interest to the Block algorithm.
00065 //  */
00066 // enum blockParams{
00067 //   block_balanceCount,            /*!< objective = balance_object_count */
00068 //   block_balanceWeight,          /*!< objective = balance_object_weight */
00069 //   block_minTotalWeight,      /*!< objective = mc_minimize_total_weight */
00070 //   block_minMaximumWeight,  /*!< objective = mc_minimize_maximum_weight */
00071 //   block_balanceTotalMaximum, /*!< objective = mc_balance_total_maximum */
00072 //   NUM_BLOCK_PARAMS
00073 // };
00074 
00076 
00087 
00088 template <typename Adapter>
00089 class AlgWolf : public Algorithm<Adapter>
00090 {
00091 
00092 private:
00093   const RCP<const Environment> mEnv;
00094   const RCP<Comm<int> > mProblemComm;
00095 
00096   const RCP<const GraphModel<typename Adapter::base_adapter_t> > &mGraphModel;
00097   const RCP<const CoordinateModel<typename Adapter::base_adapter_t> > mIds;
00098 
00099   RCP<PartitioningSolution<Adapter> > mSolution; //Not sure if this should be saved
00100 
00101 public:
00102   // Constructor
00103   AlgWolf(const RCP<const Environment> &env_,
00104     const RCP<Comm<int> > &problemComm_,
00105     const RCP<const GraphModel<typename Adapter::base_adapter_t> > &gModel_,
00106     const RCP<const CoordinateModel<typename Adapter::base_adapter_t> > &cModel_)
00107     :mEnv(env_), mProblemComm(problemComm_), mGraphModel(gModel_), mIds(cModel_)
00108   {
00109 #ifndef INCLUDE_ZOLTAN2_EXPERIMENTAL
00110     Z2_THROW_EXPERIMENTAL("Zoltan2 Wolf is strictly experimental software ")
00111 #endif
00112 
00113 #ifndef INCLUDE_ZOLTAN2_EXPERIMENTAL_WOLF
00114     Z2_THROW_EXPERIMENTAL_WOLF("Zoltan2 Wolf is strictly experimental software ")
00115 #endif
00116 
00117   }
00118 
00119   // Partitioning method
00120   void partition(const RCP<PartitioningSolution<Adapter> > &solution_);
00121 
00122 };
00124 
00127 template <typename Adapter>
00128 void AlgWolf<Adapter>::partition(
00129   const RCP<PartitioningSolution<Adapter> > &solution_
00130 )
00131 {
00132     // using std::string;
00133     // using std::ostringstream;
00134 
00135     // typedef typename Adapter::lno_t lno_t;     // local ids
00136     // typedef typename Adapter::gno_t gno_t;     // global ids
00137     // typedef typename Adapter::scalar_t scalar_t;   // scalars
00138 
00139     mEnv->debug(DETAILED_STATUS, std::string("Entering AlgWolf"));
00140 
00141     // int rank = env->myRank_;
00142     // int nprocs = env->numProcs_;
00143 
00144     // ////////////////////////////////////////////////////////
00145     // // From the CoordinateModel we need:
00146     // //    the number of gnos
00147     // //    number of weights per gno
00148     // //    the weights
00149 
00150     // size_t numGnos = ids->getLocalNumIdentifiers();
00151 
00152     // ArrayView<const gno_t> idList;
00153     // typedef StridedData<lno_t, scalar_t> input_t;
00154     // ArrayView<input_t> wgtList;
00155   
00156     // ids->getIdentifierList(idList, wgtList);
00157 
00158     // // If user supplied no weights, we use uniform weights.
00159     // bool uniformWeights = (wgtList.size() == 0);
00160 
00161 
00162    
00163 
00164     // First, let's partition with RCB
00165 
00166     // Q: can I use solution passed into alg or do I need to create a different one?
00167     //    For now using the one passed into alg
00168 
00169     {
00170     AlgRCB<Adapter> algrcb(this->mEnv, mProblemComm, this->mIds);
00171     algrcb.partition(solution_);
00172     }
00173 
00174 
00175     // ////////////////////////////////////////////////////////
00176     // // Partitioning problem parameters of interest:
00177     // //    objective
00178     // //    imbalance_tolerance
00179 
00180     // const Teuchos::ParameterList &pl = env->getParameters();
00181     // const Teuchos::ParameterEntry *pe;
00182 
00183     // pe = pl.getEntryPtr("partitioning_objective");
00184     // if (pe) {
00185     //   string po = pe->getValue<string>(&po);
00186     //   if (po == string("balance_object_count"))
00187     //     uniformWeights = true;    // User requests that we ignore weights
00188     // }
00189 
00190     // double imbalanceTolerance=1.1;
00191     // pe = pl.getEntryPtr("imbalance_tolerance");
00192     // if (pe) imbalanceTolerance = pe->getValue<double>(&imbalanceTolerance);
00193 
00194     // ////////////////////////////////////////////////////////
00195     // // From the Solution we get part information:
00196     // // number of parts and part sizes
00197 
00198     // size_t numGlobalParts = solution->getTargetGlobalNumberOfParts();
00199 
00200     // Array<scalar_t> part_sizes(numGlobalParts);
00201 
00202     // if (solution->criteriaHasUniformPartSizes(0))
00203     //   for (unsigned int i=0; i<numGlobalParts; i++)
00204     //     part_sizes[i] = 1.0 / numGlobalParts;
00205     // else
00206     //   for (unsigned int i=0; i<numGlobalParts; i++)
00207     //     part_sizes[i] = solution->getCriteriaPartSize(0, i);
00208 
00209     // for (unsigned int i=1; i<numGlobalParts; i++)
00210     //   part_sizes[i] += part_sizes[i-1];
00211 
00212     // // TODO assertion that last part sizes is about equal to 1.0
00213 
00214 
00215     // ////////////////////////////////////////////////////////
00216     // // The algorithm
00217     // //
00218     // // Wolf partitioning algorithm lifted from zoltan/src/simple/block.c
00219     // // The solution is:
00220     // //    a list of part numbers in gno order
00221     // //    an imbalance for each weight 
00222 
00223     // scalar_t wtsum(0);
00224 
00225     // if (!uniformWeights) {
00226     //   for (size_t i=0; i<numGnos; i++)
00227     //     wtsum += wgtList[0][i];          // [] operator knows stride
00228     // }
00229     // else
00230     //   wtsum = static_cast<scalar_t>(numGnos);
00231 
00232     // Array<scalar_t> scansum(nprocs+1, 0);
00233 
00234     // Teuchos::gatherAll<int, scalar_t>(*problemComm, 1, &wtsum, nprocs,
00235     //   scansum.getRawPtr()+1);
00236 
00237     // /* scansum = sum of weights on lower processors, excluding self. */
00238 
00239     // for (int i=2; i<=nprocs; i++)
00240     //   scansum[i] += scansum[i-1];
00241 
00242     // scalar_t globalTotalWeight = scansum[nprocs];
00243 
00244     // if (env->getDebugLevel() >= VERBOSE_DETAILED_STATUS) {
00245     //   ostringstream oss("Part sizes: ");
00246     //   for (unsigned int i=0; i < numGlobalParts; i++)
00247     //     oss << part_sizes[i] << " ";
00248     //   oss << std::endl << std::endl << "Weights : ";
00249     //   for (int i=0; i <= nprocs; i++)
00250     //     oss << scansum[i] << " ";
00251     //   oss << std::endl;
00252     //   env->debug(VERBOSE_DETAILED_STATUS, oss.str());
00253     // }
00254 
00255     // /* Loop over objects and assign part. */
00256     // partId_t part = 0;
00257     // wtsum = scansum[rank];
00258     // Array<scalar_t> partTotal(numGlobalParts, 0);
00259     // ArrayRCP<partId_t> gnoPart= arcp(new partId_t [numGnos], 0, numGnos);
00260 
00261     // env->memory("Wolf algorithm memory");
00262 
00263     // for (size_t i=0; i<numGnos; i++){
00264     //   scalar_t gnoWeight = (uniformWeights ? 1.0 : wgtList[0][i]);
00265     //   /* wtsum is now sum of all lower-ordered object */
00266     //   /* determine new part number for this object,
00267     //      using the "center of gravity" */
00268     //   while (unsigned(part)<numGlobalParts-1 && 
00269     //          (wtsum+0.5*gnoWeight) > part_sizes[part]*globalTotalWeight)
00270     //     part++;
00271     //   gnoPart[i] = part;
00272     //   partTotal[part] += gnoWeight;
00273     //   wtsum += gnoWeight;
00274     // }
00275 
00276     // ////////////////////////////////////////////////////////////
00277     // // Done
00278 
00279     // ArrayRCP<const gno_t> gnos = arcpFromArrayView(idList);
00280     // solution->setParts(gnos, gnoPart, true);
00281 
00282     mEnv->debug(DETAILED_STATUS, std::string("Exiting AlgWolf"));
00283 }
00284 
00285 
00286 
00287 
00288 
00289 
00290 }   // namespace Zoltan2
00291 
00292 #endif