Zoltan2 Version of the Day
Zoltan2_BasicVectorInput.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_BASICVECTORADAPTER_HPP_
00051 #define _ZOLTAN2_BASICVECTORADAPTER_HPP_
00052 
00053 #include <Zoltan2_VectorAdapter.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 
00056 namespace Zoltan2 {
00057 
00102 template <typename User>
00103   class BasicVectorAdapter : public VectorAdapter<User> {
00104 
00105 public:
00106 
00107 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00108 
00109   typedef typename InputTraits<User>::scalar_t    scalar_t;
00110   typedef typename InputTraits<User>::lno_t    lno_t;
00111   typedef typename InputTraits<User>::gno_t    gno_t;
00112   typedef typename InputTraits<User>::gid_t    gid_t;
00113   typedef typename InputTraits<User>::node_t   node_t;
00114   typedef VectorAdapter<User> base_adapter_t;
00115   typedef User user_t;
00116 
00117 #endif
00118 
00131   BasicVectorAdapter(lno_t numIds, const gid_t *ids, const scalar_t *elements, 
00132     int elementStride=1):
00133       env_(rcp(new Environment)), 
00134       numIds_(numIds), globalNumIds_(0), idList_(ids),
00135       numVectors_(1), elements_(), numWeights_(0), weights_()
00136   {
00137     vector<const scalar_t *> values;
00138     vector<int> strides;
00139     vector<const scalar_t *> emptyValues;
00140     vector<int> emptyStrides;
00141 
00142     values.push_back(elements);
00143     strides.push_back(elementStride);
00144 
00145     createBasicVector(values, strides, emptyValues, emptyStrides);
00146   }
00147 
00167   BasicVectorAdapter(lno_t numIds, const gid_t *ids, 
00168     const scalar_t *elements, int elementStride,
00169     vector<const scalar_t *> &weights, vector<int> &weightStrides):
00170       env_(rcp(new Environment)), 
00171       numIds_(numIds), globalNumIds_(0), idList_(ids),
00172       numVectors_(1), elements_(), 
00173       numWeights_(weights.size()), weights_()
00174   {
00175     vector<const scalar_t *> values;
00176     vector<int> strides;
00177 
00178     values.push_back(elements);
00179     strides.push_back(elementStride);
00180 
00181     createBasicVector(values, strides, weights, weightStrides);
00182   }
00183 
00208   BasicVectorAdapter(lno_t numIds, const gid_t *ids, 
00209     vector<const scalar_t *> &elements,  vector<int> &elementStrides,
00210     vector<const scalar_t *> &weights, vector<int> &weightStrides):
00211       env_(rcp(new Environment)), 
00212       numIds_(numIds), globalNumIds_(0), idList_(ids),
00213       numVectors_(elements.size()), elements_(),
00214       numWeights_(weights.size()), weights_()
00215   {
00216     createBasicVector(elements, elementStrides, weights, weightStrides);
00217   }
00218 
00221   ~BasicVectorAdapter() {};
00222 
00224   // The Adapter interface.
00226 
00227   size_t getLocalNumIDs() const { return numIds_;}
00228 
00229   void getIDsView(const gid_t *&ids) const {ids = idList_;}
00230 
00231   int getNumWeightsPerID() const { return numWeights_;}
00232 
00233   void getWeightsView(const scalar_t *&weights, int &stride, int idx) const
00234   {
00235     if (idx < 0 || idx >= numWeights_) {
00236       std::ostringstream emsg;
00237       emsg << __FILE__ << ":" << __LINE__
00238            << "  Invalid vector index " << idx << std::endl;
00239       throw std::runtime_error(emsg.str());
00240     }
00241     size_t length;
00242     weights_[idx].getStridedList(length, weights, stride);
00243   }
00244 
00246   // The VectorAdapter interface.
00248 
00249   int getNumEntriesPerID() const { return numVectors_;}
00250 
00251   void getEntriesView(const scalar_t *&element, int &stride, int idx = 0) const
00252   {
00253     if (idx < 0 || idx >= numVectors_) {
00254       std::ostringstream emsg;
00255       emsg << __FILE__ << ":" << __LINE__
00256            << "  Invalid vector index " << idx << std::endl;
00257       throw std::runtime_error(emsg.str());
00258     }
00259     size_t length;
00260     elements_[idx].getStridedList(length, element, stride);
00261   }
00262 
00263 private:
00264 
00265   // A default environment.  An Environment is an internal Zoltan2
00266   // class, so adapters don't usually have one.  But we create
00267   // one here so we can use it for error handling.
00268 
00269   RCP<const Environment> env_;
00270 
00271   lno_t numIds_;
00272   gno_t globalNumIds_;
00273 
00274   const gid_t *idList_;
00275 
00276   int numVectors_;
00277   ArrayRCP<StridedData<lno_t, scalar_t> > elements_ ;
00278 
00279   int numWeights_;
00280   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00281 
00282   void createBasicVector(
00283     vector<const scalar_t *> &elements,  vector<int> &elementStrides,
00284     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00285 
00286 };
00287 
00289 // Definitions
00291 
00292 template <typename User>
00293   void BasicVectorAdapter<User>::createBasicVector(
00294    vector<const scalar_t *> &elements,  vector<int> &elementStrides,
00295    vector<const scalar_t *> &weights, vector<int> &weightStrides)
00296 {
00297   typedef StridedData<lno_t,scalar_t> input_t;
00298 
00299   gno_t tmp = numIds_;
00300   try{
00301     reduceAll<int, gno_t>(*(env_->comm_), Teuchos::REDUCE_SUM, 1, 
00302        &tmp, &globalNumIds_);
00303   }
00304   Z2_THROW_OUTSIDE_ERROR(*env_);
00305 
00306   if (numIds_){
00307     int stride = 1;
00308     elements_ = arcp(new input_t [numVectors_], 0, numVectors_, true);
00309     for (int v=0; v < numVectors_; v++){
00310       if (elementStrides.size())
00311         stride = elementStrides[v];
00312       ArrayRCP<const scalar_t> eltV(elements[v], 0, stride*numIds_, false); 
00313       elements_[v] = input_t(eltV, stride);
00314     }
00315 
00316     if (numWeights_){
00317       stride = 1;
00318       weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
00319       for (int w=0; w < numWeights_; w++){
00320         if (weightStrides.size())
00321           stride = weightStrides[w];
00322         ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false); 
00323         weights_[w] = input_t(wgtV, stride);
00324       }
00325     }
00326   }
00327 }
00328   
00329 }  //namespace Zoltan2
00330   
00331 #endif