Zoltan2 Version of the Day
Zoltan2_XpetraMultiVectorInput.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_XPETRAMULTIVECTORINPUT_HPP_
00051 #define _ZOLTAN2_XPETRAMULTIVECTORINPUT_HPP_
00052 
00053 #include <Zoltan2_XpetraTraits.hpp>
00054 #include <Zoltan2_VectorInput.hpp>
00055 #include <Zoltan2_StridedData.hpp>
00056 #include <Zoltan2_Util.hpp>
00057 
00058 #include <Xpetra_EpetraMultiVector.hpp>
00059 #include <Xpetra_TpetraMultiVector.hpp>
00060 
00061 namespace Zoltan2 {
00062 
00080 template <typename User>
00081   class XpetraMultiVectorInput : public VectorInput<User> {
00082 public:
00083 
00084 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00085   typedef typename InputTraits<User>::scalar_t    scalar_t;
00086   typedef typename InputTraits<User>::lno_t    lno_t;
00087   typedef typename InputTraits<User>::gno_t    gno_t;
00088   typedef typename InputTraits<User>::gid_t    gid_t;
00089   typedef typename InputTraits<User>::node_t   node_t;
00090   typedef VectorInput<User>       base_adapter_t;
00091   typedef User user_t;
00092 
00093   typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_mvector_t;
00094   typedef Xpetra::TpetraMultiVector<
00095     scalar_t, lno_t, gno_t, node_t> xt_mvector_t;
00096   typedef Xpetra::EpetraMultiVector xe_mvector_t;
00097 #endif
00098 
00101   ~XpetraMultiVectorInput() { }
00102 
00118   XpetraMultiVectorInput(const RCP<const User> &invector,
00119     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00120 
00126   XpetraMultiVectorInput(const RCP<const User> &invector);
00127 
00131   const RCP<const x_mvector_t> &getVector() const
00132   {
00133     return vector_;
00134   }
00135 
00137   // The InputAdapter interface.
00139 
00140   string inputAdapterName()const {return string("XpetraMultiVector");}
00141 
00142   size_t getLocalNumberOfObjects() const { return getLocalLength();}
00143 
00144   int getNumberOfWeightsPerObject() const { return numWeights_;}
00145 
00146   size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
00147   {
00148     return getVectorWeights(dim, wgt, stride);
00149   }
00150 
00152   // The VectorInput interface.
00154 
00155   int getNumberOfVectors() const {return vector_->getNumVectors();}
00156 
00157   int getNumberOfWeights() const {return numWeights_;}
00158   
00159   size_t getLocalLength() const {return vector_->getLocalLength();}
00160   
00161   size_t getGlobalLength() const {return vector_->getGlobalLength();}
00162 
00163   size_t getVector(const gid_t *&Ids, 
00164     const scalar_t *&elements, int &stride) const
00165   {
00166     return getVector(0, Ids, elements, stride);
00167   }
00168 
00169   size_t getVector(int i, const gid_t *&Ids, 
00170     const scalar_t *&elements, int &stride) const;
00171 
00172   size_t getVectorWeights(int dim, const scalar_t *&weights, int &stride) const
00173   {
00174     env_->localInputAssertion(__FILE__, __LINE__, "invalid dimension",
00175       dim >= 0 && dim < numWeights_, BASIC_ASSERTION);
00176 
00177     size_t length;
00178 
00179     weights_[dim].getStridedList(length, weights, stride);
00180 
00181     return length;
00182   }
00183 
00184   template <typename Adapter>
00185     size_t applyPartitioningSolution(const User &in, User *&out,
00186          const PartitioningSolution<Adapter> &solution) const;
00187 
00188 private:
00189 
00190   RCP<const User> invector_;
00191   RCP<const x_mvector_t> vector_;
00192   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > map_;
00193   RCP<Environment> env_;    // for error messages, etc.
00194   lno_t base_;
00195 
00196   int numWeights_;
00197   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00198 };
00199 
00201 // Definitions
00203 
00204 template <typename User>
00205   XpetraMultiVectorInput<User>::XpetraMultiVectorInput(
00206     const RCP<const User> &invector,
00207     vector<const scalar_t *> &weights, vector<int> &weightStrides):
00208       invector_(invector), vector_(), map_(), 
00209       env_(rcp(new Environment)), base_(),
00210       numWeights_(weights.size()), weights_(weights.size())
00211 {
00212   typedef StridedData<lno_t, scalar_t> input_t;
00213 
00214   vector_ = XpetraTraits<User>::convertToXpetra(invector);
00215   map_ = vector_->getMap();
00216   base_ = map_->getIndexBase();
00217 
00218   size_t length = vector_->getLocalLength();
00219 
00220   if (length > 0 && numWeights_ > 0){
00221     int stride = 1;
00222     for (int w=0; w < numWeights_; w++){
00223       if (weightStrides.size())
00224         stride = weightStrides[w];
00225       ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*length, false); 
00226       weights_[w] = input_t(wgtV, stride);
00227     }
00228   }
00229 }
00230 
00231 
00232 template <typename User>
00233   XpetraMultiVectorInput<User>::XpetraMultiVectorInput(
00234     const RCP<const User> &invector):
00235       invector_(invector), vector_(), map_(), 
00236       env_(rcp(new Environment)), base_(),
00237       numWeights_(0), weights_()
00238 {
00239   typedef StridedData<lno_t, scalar_t> input_t;
00240 
00241   vector_ = XpetraTraits<User>::convertToXpetra(invector);
00242   map_ = vector_->getMap();
00243   base_ = map_->getIndexBase();
00244 }
00245 
00246 template <typename User>
00247   size_t XpetraMultiVectorInput<User>::getVector(
00248     int i, const gid_t *&Ids, const scalar_t *&elements, int &stride) const
00249 {
00250   stride = 1;
00251   elements = NULL;
00252   if (map_->lib() == Xpetra::UseTpetra){
00253     const xt_mvector_t *tvector = 
00254       dynamic_cast<const xt_mvector_t *>(vector_.get());
00255      
00256     if (tvector->getLocalLength() > 0){
00257       ArrayRCP<const scalar_t> data = tvector->getData(i);
00258       elements = data.get();
00259     }
00260   }
00261   else if (map_->lib() == Xpetra::UseEpetra){
00262     const xe_mvector_t *evector = 
00263       dynamic_cast<const xe_mvector_t *>(vector_.get());
00264       
00265     if (evector->getLocalLength() > 0){
00266       ArrayRCP<const double> data = evector->getData(i);
00267 
00268       // Cast so this will compile when scalar_t is not double,
00269       // a case when this code should never execute.
00270       elements = reinterpret_cast<const scalar_t *>(data.get());
00271     }
00272   }
00273   else{
00274     throw logic_error("invalid underlying lib");
00275   }
00276 
00277   ArrayView<const gid_t> gids = map_->getNodeElementList();
00278   Ids = gids.getRawPtr();
00279   return gids.size();
00280 }
00281 
00282 template <typename User>
00283   template <typename Adapter>
00284     size_t XpetraMultiVectorInput<User>::applyPartitioningSolution(
00285       const User &in, User *&out, 
00286       const PartitioningSolution<Adapter> &solution) const
00287 {
00288   size_t len = solution.getLocalNumberOfIds();
00289   const gid_t *gids = solution.getIdList();
00290   const partId_t *parts = solution.getPartList();
00291   ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
00292   ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len, 
00293     false);
00294   ArrayRCP<lno_t> dummyIn;
00295   ArrayRCP<gid_t> importList;
00296   ArrayRCP<lno_t> dummyOut;
00297   size_t numNewRows;
00298 
00299   const RCP<const Comm<int> > comm = map_->getComm();
00300 
00301   try{
00302     // Get an import list
00303     numNewRows = solution.convertSolutionToImportList(
00304       0, dummyIn, importList, dummyOut);
00305   }
00306   Z2_FORWARD_EXCEPTIONS;
00307 
00308   RCP<const User> inPtr = rcp(&in, false);
00309   lno_t localNumElts = numNewRows;
00310 
00311   RCP<const User> outPtr = XpetraTraits<User>::doMigration(
00312    inPtr, localNumElts, importList.get());
00313 
00314   out = const_cast<User *>(outPtr.get());
00315   outPtr.release();
00316   return numNewRows;
00317 }
00318   
00319 }  //namespace Zoltan2
00320   
00321 #endif