Zoltan2
Zoltan2_IdentifierModel.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_IDENTIFIERMODEL_HPP_
00051 #define _ZOLTAN2_IDENTIFIERMODEL_HPP_
00052 
00053 #include <Zoltan2_Model.hpp>
00054 #include <Zoltan2_Adapter.hpp>
00055 #include <Zoltan2_StridedData.hpp>
00056 
00057 namespace Zoltan2 {
00058 
00069 template <typename Adapter>
00070 class IdentifierModel : public Model<Adapter> 
00071 {
00072 public:
00073 
00074 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00075   typedef typename Adapter::scalar_t  scalar_t;
00076   typedef typename Adapter::gno_t     gno_t;
00077   typedef typename Adapter::lno_t     lno_t;
00078   typedef typename Adapter::gid_t     gid_t;
00079   typedef IdentifierMap<typename Adapter::user_t> idmap_t;
00080   typedef StridedData<lno_t, scalar_t> input_t;
00081 #endif
00082 
00090   IdentifierModel(const Adapter *ia, const RCP<const Environment> &env,
00091                   const RCP<const Comm<int> > &comm, modelFlag_t &modelFlags);
00092 
00095   inline size_t getLocalNumIdentifiers() const { return gids_.size(); }
00096 
00099   inline global_size_t getGlobalNumIdentifiers() const {
00100     return numGlobalIdentifiers_;
00101   }
00102 
00111   inline size_t getIdentifierList(ArrayView<const gno_t> &Ids,
00112                                   ArrayView<input_t> &wgts) const 
00113   {
00114     Ids = ArrayView<const gno_t>();
00115     wgts = weights_.view(0, nUserWeights_);
00116     size_t n = getLocalNumIdentifiers();
00117     if (n){
00118       if (gnosAreGids_) Ids = gids_(0, n);
00119       else              Ids = gnosConst_(0, n);
00120     }
00121     return n;
00122   }
00123 
00125   // The Model interface.
00127 
00128   inline size_t getLocalNumObjects() const {return getLocalNumIdentifiers();}
00129 
00130   inline size_t getGlobalNumObjects() const {return getGlobalNumIdentifiers();}
00131 
00132 private:
00133 
00134   bool gnosAreGids_;
00135   gno_t numGlobalIdentifiers_;
00136   const RCP<const Environment> env_;
00137   const RCP<const Comm<int> > comm_;
00138   ArrayRCP<const gid_t> gids_;
00139   int nUserWeights_;
00140   ArrayRCP<input_t> weights_;
00141   ArrayRCP<gno_t> gnos_;
00142   ArrayRCP<const gno_t> gnosConst_;
00143 };
00144 
00146 template <typename Adapter>
00147   IdentifierModel<Adapter>::IdentifierModel( 
00148     const Adapter *ia,
00149     const RCP<const Environment> &env,
00150     const RCP<const Comm<int> > &comm,
00151     modelFlag_t &modelFlags):
00152       gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
00153       gids_(), nUserWeights_(0), weights_(), gnos_(), gnosConst_()
00154 {
00155   // Get the local and global problem size
00156   size_t nLocalIds = ia->getLocalNumIDs();
00157   gno_t lsum = nLocalIds;
00158   reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum,
00159     &numGlobalIdentifiers_);
00160 
00161   // Get the number of weights
00162   // Use max number of weights over all processes as nUserWeights_
00163   int tmp = ia->getNumWeightsPerID();
00164   Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 1,
00165       &tmp, &nUserWeights_);
00166 
00167   // Prepare to store views from input adapter
00168   // TODO:  Do we have to store these views, or can we get them on an 
00169   // TODO:  as-needed basis?
00170   Array<const scalar_t *> wgts(nUserWeights_, (const scalar_t *)NULL);
00171   Array<int> wgtStrides(nUserWeights_, 0);
00172 
00173   if (nUserWeights_ > 0){
00174     input_t *w = new input_t [nUserWeights_];
00175     weights_ = arcp<input_t>(w, 0, nUserWeights_);
00176   }
00177 
00178   const gid_t *gids=NULL;
00179   
00180   // Get the input adapter's views
00181   try{
00182     ia->getIDsView(gids);
00183     for (int idx=0; idx < nUserWeights_; idx++)
00184       ia->getWeightsView(wgts[idx], wgtStrides[idx], idx);
00185   }
00186   Z2_FORWARD_EXCEPTIONS;
00187 
00188   if (nLocalIds){
00189     gids_ = arcp(gids, 0, nLocalIds, false);
00190 
00191     if (nUserWeights_ > 0){
00192       for (int idx=0; idx < nUserWeights_; idx++){
00193         ArrayRCP<const scalar_t> wgtArray(wgts[idx], 0,
00194                                           nLocalIds*wgtStrides[idx], false);
00195         weights_[idx] = input_t(wgtArray, wgtStrides[idx]);
00196       }
00197     }
00198   }
00199 
00200 
00201   // TODO:  Why does an IdentifierModel need an IdentifierMap?
00202   // TODO:  Currently is useful only if gid_t is not Teuchos::Ordinal
00203   RCP<const idmap_t> idMap;
00204   try{
00205     if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE))
00206       idMap = rcp(new idmap_t(env_, comm_, gids_, true));
00207     else
00208       idMap = rcp(new idmap_t(env_, comm_, gids_, false));
00209   }
00210   Z2_FORWARD_EXCEPTIONS;
00211 
00212   gnosAreGids_ = idMap->gnosAreGids();
00213 
00214   this->setIdentifierMap(idMap);
00215 
00216   if (!gnosAreGids_ && nLocalIds>0){
00217     gno_t *tmpGno = new gno_t [nLocalIds];
00218     env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
00219     gnos_ = arcp(tmpGno, 0, nLocalIds);
00220 
00221     try{
00222       ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
00223       idMap->gidTranslate( gidsNonConst(0,nLocalIds),  gnos_(0,nLocalIds),
00224         TRANSLATE_APP_TO_LIB);
00225     }
00226     Z2_FORWARD_EXCEPTIONS;
00227   }
00228 
00229   gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
00230 
00231   env_->memory("After construction of identifier model");
00232 }
00233 
00234 }  // namespace Zoltan2
00235 
00236 #endif