Zoltan2
Zoltan2_CoordinateModel.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 
00051 #ifndef _ZOLTAN2_COORDINATEMODEL_HPP_
00052 #define _ZOLTAN2_COORDINATEMODEL_HPP_
00053 
00054 // disable clang warnings
00055 #ifdef __clang__
00056 #pragma clang system_header
00057 #endif
00058 
00059 #include <Zoltan2_Model.hpp>
00060 #include <Zoltan2_MatrixAdapter.hpp>
00061 #include <Zoltan2_GraphAdapter.hpp>
00062 #include <Zoltan2_IdentifierAdapter.hpp>
00063 #include <Zoltan2_VectorAdapter.hpp>
00064 #include <Zoltan2_StridedData.hpp>
00065 
00066 namespace Zoltan2 {
00067 
00074 template <typename Adapter>
00075 class CoordinateModel : public Model<Adapter>
00076 {
00077 public:
00078 
00079 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00080   typedef typename Adapter::scalar_t    scalar_t;
00081   typedef typename Adapter::gno_t       gno_t;
00082   typedef typename Adapter::gid_t       gid_t;
00083   typedef typename Adapter::lno_t       lno_t;
00084   typedef typename Adapter::user_t      user_t;
00085   typedef typename Adapter::userCoord_t userCoord_t;
00086   typedef StridedData<lno_t, scalar_t>  input_t;
00087   typedef IdentifierMap<user_t>         idmap_t;
00088 #endif
00089 
00091   // Constructors for each Adapter type
00093 
00094   // VectorAdapter
00095   CoordinateModel(const VectorAdapter<user_t> *ia,
00096                   const RCP<const Environment> &env,
00097                   const RCP<const Comm<int> > &comm,
00098                   modelFlag_t &flags):
00099       gnosAreGids_(false), numGlobalCoordinates_(), env_(env), comm_(comm),
00100       coordinateDim_(), gids_(), xyz_(), userNumWeights_(0), weights_(),
00101       gnos_(), gnosConst_()
00102   {
00103     sharedConstructor(ia, env, comm, flags);
00104   }
00105 
00106   // MatrixAdapter
00107   CoordinateModel(const MatrixAdapter<user_t,userCoord_t> *ia,
00108                   const RCP<const Environment> &env,
00109                   const RCP<const Comm<int> > &comm,
00110                   modelFlag_t &flags) :
00111                   gnosAreGids_(false), numGlobalCoordinates_(),
00112                   env_(env), comm_(comm),
00113                   coordinateDim_(), gids_(), xyz_(),
00114                   userNumWeights_(0), weights_(),
00115                   gnos_(), gnosConst_()
00116   {
00117     if (!(ia->coordinatesAvailable()))
00118       throw std::logic_error("No coordinate info was provided to MatrixAdapter.");
00119     else {
00120       typedef VectorAdapter<userCoord_t> vectorAdapter_t;
00121       vectorAdapter_t *va = ia->getCoordinateInput();
00122       sharedConstructor(va, env, comm, flags);
00123     }
00124   }
00125 
00126   // GraphAdapter
00127   CoordinateModel(const GraphAdapter<user_t,userCoord_t> *ia,
00128                   const RCP<const Environment> &env,
00129                   const RCP<const Comm<int> > &comm,
00130                   modelFlag_t &flags) :
00131                   gnosAreGids_(false), numGlobalCoordinates_(),
00132                   env_(env), comm_(comm),
00133                   coordinateDim_(), gids_(), xyz_(),
00134                   userNumWeights_(0), weights_(),
00135                   gnos_(), gnosConst_()
00136   {
00137     if (!(ia->coordinatesAvailable()))
00138       throw std::logic_error("No coordinate info was provided to MatrixAdapter.");
00139     else {
00140       typedef VectorAdapter<userCoord_t> vectorAdapter_t;
00141       vectorAdapter_t *va = ia->getCoordinateInput();
00142       sharedConstructor(va, env, comm, flags);
00143     }
00144   }
00145 
00146   // IdentifierAdapter
00147   CoordinateModel(const IdentifierAdapter<user_t> *ia,
00148                   const RCP<const Environment> &env,
00149                   const RCP<const Comm<int> > &comm,
00150                   modelFlag_t &flags)
00151   {
00152     throw std::logic_error(
00153       "A coordinate model can not be build from an IdentifierAdapter");
00154   }
00155 
00157   // CoordinateModel interface.
00159 
00162   int getCoordinateDim() const { return coordinateDim_;}
00163 
00166   size_t getLocalNumCoordinates() const { return gids_.size();}
00167 
00170   global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
00171 
00174   int getNumWeightsPerCoordinate() const { return userNumWeights_;}
00175 
00198   size_t getCoordinates(ArrayView<const gno_t>  &Ids,
00199                         ArrayView<input_t> &xyz,
00200                         ArrayView<input_t> &wgts) const
00201   {
00202     xyz = xyz_.view(0, coordinateDim_);
00203     wgts = weights_.view(0, userNumWeights_);
00204 
00205     size_t nCoord = getLocalNumCoordinates();
00206     Ids =  ArrayView<const gno_t>();
00207 
00208     if (nCoord){
00209       if (gnosAreGids_)
00210         Ids = Teuchos::arrayView<const gno_t>(
00211           reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
00212       else
00213         Ids = gnosConst_.view(0, nCoord);
00214     }
00215 
00216     return nCoord;
00217   }
00218 
00220   // The Model interface.
00222 
00223   size_t getLocalNumObjects() const
00224   {
00225     return getLocalNumCoordinates();
00226   }
00227 
00228   size_t getGlobalNumObjects() const
00229   {
00230     return getGlobalNumCoordinates();
00231   }
00232 
00233 private:
00234   bool gnosAreGids_;
00235   gno_t numGlobalCoordinates_;
00236   const RCP<const Environment> env_;
00237   const RCP<const Comm<int> > comm_;
00238   int coordinateDim_;
00239   ArrayRCP<const gid_t> gids_;
00240   ArrayRCP<input_t> xyz_;
00241   int userNumWeights_;
00242   ArrayRCP<input_t> weights_;
00243   ArrayRCP<gno_t> gnos_;
00244   ArrayRCP<const gno_t> gnosConst_;
00245 
00246   void sharedConstructor(const VectorAdapter<userCoord_t> *ia,
00247                          const RCP<const Environment> &env,
00248                          const RCP<const Comm<int> > &comm,
00249                          modelFlag_t &flags);
00250 };
00251 
00252 
00254 
00255 template <typename Adapter>
00256 void CoordinateModel<Adapter>::sharedConstructor(
00257     const VectorAdapter<typename Adapter::userCoord_t> *ia,
00258     const RCP<const Environment> &env,
00259     const RCP<const Comm<int> > &comm,
00260     modelFlag_t &flags)
00261 {
00262   bool consecutiveIds = flags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
00263 
00264   size_t nLocalIds = ia->getLocalNumIDs();
00265 
00266   // Get coordinates and weights (if any)
00267 
00268   int tmp[2], gtmp[2];
00269   tmp[0] = ia->getNumEntriesPerID();
00270   tmp[1] = ia->getNumWeightsPerID();
00271   Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 2, tmp, gtmp);
00272   coordinateDim_ = gtmp[0];
00273   userNumWeights_ = gtmp[1];
00274 
00275   env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
00276     coordinateDim_ > 0, COMPLEX_ASSERTION);
00277 
00278   input_t *coordArray = new input_t [coordinateDim_];
00279   input_t *weightArray = NULL;
00280   if (userNumWeights_)
00281     weightArray = new input_t [userNumWeights_];
00282 
00283   env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
00284     coordArray && (!userNumWeights_|| weightArray));
00285 
00286 
00287   if (nLocalIds){
00288     const gid_t *gids=NULL;
00289     ia->getIDsView(gids);
00290     gids_ = arcp(gids, 0, nLocalIds, false);
00291 
00292     for (int dim=0; dim < coordinateDim_; dim++){
00293       int stride;
00294       const scalar_t *coords=NULL;
00295       try{
00296         ia->getEntriesView(coords, stride, dim);
00297       }
00298       Z2_FORWARD_EXCEPTIONS;
00299 
00300       ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
00301       coordArray[dim] = input_t(cArray, stride);
00302     }
00303 
00304     for (int idx=0; idx < userNumWeights_; idx++){
00305       int stride;
00306       const scalar_t *weights;
00307       try{
00308         ia->getWeightsView(weights, stride, idx);
00309       }
00310       Z2_FORWARD_EXCEPTIONS;
00311 
00312       ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
00313       weightArray[idx] = input_t(wArray, stride);
00314     }
00315   }
00316 
00317   xyz_ = arcp(coordArray, 0, coordinateDim_);
00318 
00319   if (userNumWeights_)
00320     weights_ = arcp(weightArray, 0, userNumWeights_);
00321 
00322   // Create identifier map.
00323   // TODO:  Why do coordinate models need an IdentifierMap?
00324 
00325   RCP<const idmap_t> idMap;
00326 
00327   try{
00328     idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIds));
00329   }
00330   Z2_FORWARD_EXCEPTIONS;
00331 
00332   numGlobalCoordinates_ = idMap->getGlobalNumberOfIds();
00333   gnosAreGids_ = idMap->gnosAreGids();
00334 
00335   this->setIdentifierMap(idMap);
00336 
00337   if (!gnosAreGids_ && nLocalIds>0){
00338     gno_t *tmpGno = new gno_t [nLocalIds];
00339     env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
00340     gnos_ = arcp(tmpGno, 0, nLocalIds);
00341 
00342     try{
00343       ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
00344       idMap->gidTranslate( gidsNonConst(0,nLocalIds),  gnos_(0,nLocalIds),
00345         TRANSLATE_APP_TO_LIB);
00346     }
00347     Z2_FORWARD_EXCEPTIONS;
00348   }
00349 
00350   gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
00351 
00352   env_->memory("After construction of coordinate model");
00353 }
00354 
00355 }   // namespace Zoltan2
00356 
00357 #endif