Zoltan 2 Version 0.5
Zoltan2_OrderingProblem.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_ORDERINGPROBLEM_HPP_
00051 #define _ZOLTAN2_ORDERINGPROBLEM_HPP_
00052 
00053 #include <Zoltan2_Problem.hpp>
00054 //#include <Zoltan2_OrderingAlgorithms.hpp> // TODO: Fix include path?
00055 #include "algorithms/order/Zoltan2_OrderingAlgorithms.hpp"
00056 #include <Zoltan2_OrderingSolution.hpp>
00057 
00058 #include <Zoltan2_GraphModel.hpp>
00059 #include <string>
00060 #ifdef HAVE_ZOLTAN2_OVIS
00061 #include <ovis.h>
00062 #endif
00063 
00064 #include <bitset>
00065 
00066 using Teuchos::rcp_dynamic_cast;
00067 using namespace std;
00068 
00069 namespace Zoltan2{
00070 
00072 
00088 template<typename Adapter>
00089 class OrderingProblem : public Problem<Adapter>
00090 {
00091 public:
00092 
00093   typedef typename Adapter::gid_t gid_t;
00094   typedef typename Adapter::lno_t lno_t;
00095 
00096 #ifdef HAVE_ZOLTAN2_MPI
00097    typedef Teuchos::OpaqueWrapper<MPI_Comm> mpiWrapper_t;
00098 #endif
00099 
00102   virtual ~OrderingProblem() {};
00103 
00104 
00105 #ifdef HAVE_ZOLTAN2_MPI
00106 
00108   OrderingProblem(Adapter *A, ParameterList *p, MPI_Comm comm) 
00109                       : Problem<Adapter>(A, p, comm) 
00110   {
00111     HELLO;
00112     createOrderingProblem();
00113   };
00114 #endif
00115 
00118   OrderingProblem(Adapter *A, ParameterList *p) : Problem<Adapter>(A, p) 
00119   {
00120     HELLO;
00121     createOrderingProblem();
00122   };
00123 
00125   //
00126   //    \param updateInputData   If true this indicates that either
00127   //          this is the first attempt at solution, or that we
00128   //          are computing a new solution and the input data has
00129   //          changed since the previous solution was computed.
00130   //          If false, this indicates that we are computing a
00131   //          new solution using the same input data was used for
00132   //          the previous solution, even though the parameters
00133   //          may have been changed.
00134   //
00135   //  For the sake of performance, we ask the caller to set \c updateInputData
00136   //  to false if he/she is computing a new solution using the same input data,
00137   //  but different problem parameters, than that which was used to compute
00138   //  the most recent solution.
00139   
00140   void solve(bool updateInputData=true);
00141 
00143   //
00144   //   \return  a reference to the solution to the most recent solve().
00145 
00146   OrderingSolution<gid_t, lno_t> *getSolution() {
00147     return solution_.getRawPtr();
00148   };
00149 
00150 private:
00151   void createOrderingProblem();
00152 
00153   RCP<OrderingSolution<gid_t, lno_t> > solution_;
00154 
00155   RCP<Comm<int> > problemComm_;
00156   RCP<const Comm<int> > problemCommConst_;
00157 
00158 #ifdef HAVE_ZOLTAN2_MPI
00159   MPI_Comm mpiComm_;
00160 #endif
00161 };
00162 
00164 template <typename Adapter>
00165 void OrderingProblem<Adapter>::solve(bool newData)
00166 {
00167   HELLO;
00168 
00169   size_t nVtx = this->graphModel_->getLocalNumVertices();
00170 
00171   // TODO: Assuming one MPI process now. nVtx = ngids = nlids
00172   try
00173   {
00174       this->solution_ = rcp(new OrderingSolution<gid_t, lno_t>(nVtx, nVtx));
00175   }
00176   Z2_FORWARD_EXCEPTIONS;
00177 
00178   // Determine which algorithm to use based on defaults and parameters.
00179   // TODO: Use rcm if graph model is defined, otherwise use natural.
00180   // Need some exception handling here, too.
00181 
00182   string method = this->params_->template get<string>("order_method", "rcm");
00183   typedef typename Adapter::base_adapter_t base_adapter_t;
00184 
00185   // TODO: Ignore case
00186   try
00187   {
00188   if (method.compare("rcm") == 0)
00189   {
00190       AlgRCM<base_adapter_t>(this->graphModel_, this->solution_, this->params_,
00191                       problemComm_);
00192   }
00193   else if (method.compare("natural") == 0)
00194   {
00195       AlgNatural<base_adapter_t>(this->identifierModel_, this->solution_, this->params_, problemComm_);
00196   }
00197   else if (method.compare("random") == 0)
00198   {
00199       AlgRandom<base_adapter_t>(this->identifierModel_, this->solution_, this->params_, problemComm_);
00200   }
00201   else if (method.compare("minimum_degree") == 0)
00202   {
00203       string pkg = this->params_->template get<string>("order_package", "amd");
00204       if (pkg.compare("amd") == 0)
00205       {
00206           AlgAMD<base_adapter_t>(this->graphModel_, this->solution_, this->params_,
00207                           problemComm_);
00208       }
00209   }
00210   }
00211   Z2_FORWARD_EXCEPTIONS;
00212 
00213 #ifdef HAVE_ZOLTAN2_MPI
00214 
00215   // The algorithm may have changed the communicator.  Change it back.
00216 
00217   RCP<const mpiWrapper_t > wrappedComm = rcp(new mpiWrapper_t(mpiComm_));
00218   problemComm_ = rcp(new Teuchos::MpiComm<int>(wrappedComm));
00219   problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
00220 
00221 #endif
00222 
00223 }
00224 
00226 //template <typename Adapter>
00227 //void OrderingProblem<Adapter>::redistribute()
00228 //{
00229 //  HELLO;
00230 //}
00231 
00234 //  Method with common functionality for creating a OrderingProblem.
00235 //  Individual constructors do appropriate conversions of input, etc.
00236 //  This method does everything that all constructors must do.
00237 
00238 template <typename Adapter>
00239 void OrderingProblem<Adapter>::createOrderingProblem()
00240 {
00241   HELLO;
00242   using Teuchos::ParameterList;
00243 
00244 //  cout << __func__ << " input adapter type " 
00245 //       << this->inputAdapter_->inputAdapterType() << " " 
00246 //       << this->inputAdapter_->inputAdapterName() << endl;
00247 
00248 #ifdef HAVE_ZOLTAN2_OVIS
00249   ovis_enabled(this->comm_->getRank());
00250 #endif
00251 
00252   // Create a copy of the user's communicator.
00253 
00254   problemComm_ = this->comm_->duplicate();
00255   problemCommConst_ = rcp_const_cast<const Comm<int> > (problemComm_);
00256 
00257 
00258 #ifdef HAVE_ZOLTAN2_MPI
00259 
00260   // TPLs may want an MPI communicator
00261 
00262   Comm<int> *c = problemComm_.getRawPtr();
00263   Teuchos::MpiComm<int> *mc = dynamic_cast<Teuchos::MpiComm<int> *>(c);
00264   if (mc){
00265     RCP<const mpiWrapper_t> wrappedComm = mc->getRawMpiComm();
00266     mpiComm_ = (*wrappedComm.getRawPtr())();
00267   }
00268   else{
00269     mpiComm_ = MPI_COMM_SELF;   // or would this be an error?
00270   }
00271 
00272 #endif
00273 
00274   // Determine which parameters are relevant here.
00275   // For now, assume parameters similar to Zoltan:
00276   //   MODEL = graph, hypergraph, geometric, ids
00277   //   ALGORITHM = rcm, random, amd
00278 
00279   ModelType modelType = GraphModelType;
00280 
00281   typedef typename Adapter::base_adapter_t base_adapter_t;
00282 
00283   // Select Model based on parameters and InputAdapter type
00284 
00285   std::bitset<NUM_MODEL_FLAGS> graphFlags;
00286   std::bitset<NUM_MODEL_FLAGS> idFlags;
00287 
00288   switch (modelType) {
00289 
00290   case GraphModelType:
00291     graphFlags.set(SELF_EDGES_MUST_BE_REMOVED);
00292     this->graphModel_ = rcp(new GraphModel<base_adapter_t>(
00293       this->baseInputAdapter_, this->envConst_, problemCommConst_, graphFlags));
00294 
00295     this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
00296       this->graphModel_);
00297 
00298     break;
00299 
00300 
00301 
00302   case IdentifierModelType:
00303     idFlags.set(SELF_EDGES_MUST_BE_REMOVED);
00304     this->identifierModel_ = rcp(new IdentifierModel<base_adapter_t>(
00305       this->baseInputAdapter_, this->envConst_, problemCommConst_, idFlags));
00306 
00307     this->baseModel_ = rcp_implicit_cast<const Model<base_adapter_t> >(
00308       this->identifierModel_);
00309 
00310     break;
00311 
00312   case HypergraphModelType:
00313   case CoordinateModelType:
00314     cout << __func__ << " Model type " << modelType << " not yet supported." 
00315          << endl;
00316     break;
00317 
00318   default:
00319     cout << __func__ << " Invalid model" << modelType << endl;
00320     break;
00321   }
00322 }
00323 } //namespace Zoltan2
00324 #endif