Zoltan2
Zoltan2_BasicVectorAdapter.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 
00081 template <typename User>
00082   class BasicVectorAdapter : public VectorAdapter<User> {
00083 
00084 public:
00085 
00086 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00087 
00088   typedef typename InputTraits<User>::scalar_t scalar_t;
00089   typedef typename InputTraits<User>::lno_t lno_t;
00090   typedef typename InputTraits<User>::gno_t gno_t;
00091   typedef typename InputTraits<User>::zgid_t zgid_t;
00092   typedef typename InputTraits<User>::part_t   part_t;
00093   typedef typename InputTraits<User>::node_t node_t;
00094   typedef VectorAdapter<User> base_adapter_t;
00095   typedef User user_t;
00096 
00097 #endif
00098 
00114   BasicVectorAdapter(lno_t numIds, const zgid_t *ids,
00115                      const scalar_t *entries, int entryStride=1,
00116                      bool usewgts=false,
00117                      const scalar_t *wgts=NULL, int wgtStride=1):
00118       numIds_(numIds), idList_(ids),
00119       numEntriesPerID_(1), entries_(),
00120       numWeights_(usewgts==true), weights_()
00121   {
00122     std::vector<const scalar_t *> values;
00123     std::vector<int> strides;
00124     std::vector<const scalar_t *> weightValues;
00125     std::vector<int> weightStrides;
00126 
00127     values.push_back(entries);
00128     strides.push_back(entryStride);
00129     if (usewgts) {
00130       weightValues.push_back(wgts);
00131       weightStrides.push_back(wgtStride);
00132     }
00133 
00134     createBasicVector(values, strides, weightValues, weightStrides);
00135   }
00136 
00162   BasicVectorAdapter(lno_t numIds, const zgid_t *ids,
00163     std::vector<const scalar_t *> &entries,  std::vector<int> &entryStride,
00164     std::vector<const scalar_t *> &weights, std::vector<int> &weightStrides):
00165       numIds_(numIds), idList_(ids),
00166       numEntriesPerID_(entries.size()), entries_(),
00167       numWeights_(weights.size()), weights_()
00168   {
00169     createBasicVector(entries, entryStride, weights, weightStrides);
00170   }
00171 
00198   BasicVectorAdapter(lno_t numIds, const zgid_t *ids,
00199                      const scalar_t *x, const scalar_t *y,
00200                      const scalar_t *z,
00201                      int xStride=1, int yStride=1, int zStride=1,
00202                      bool usewgts=false, const scalar_t *wgts=NULL,
00203                      int wgtStride=1) :
00204       numIds_(numIds), idList_(ids), numEntriesPerID_(0), entries_(),
00205       numWeights_(usewgts==true), weights_()
00206   {
00207     std::vector<const scalar_t *> values, weightValues;
00208     std::vector<int> strides, weightStrides;
00209 
00210     if (x){
00211       values.push_back(x);
00212       strides.push_back(xStride);
00213       numEntriesPerID_++;
00214       if (y){
00215         values.push_back(y);
00216         strides.push_back(yStride);
00217         numEntriesPerID_++;
00218         if (z){
00219           values.push_back(z);
00220           strides.push_back(zStride);
00221           numEntriesPerID_++;
00222         }
00223       }
00224     }
00225     if (usewgts) {
00226       weightValues.push_back(wgts);
00227       weightStrides.push_back(wgtStride);
00228     }
00229     createBasicVector(values, strides, weightValues, weightStrides);
00230   }
00231 
00232 
00233   ~BasicVectorAdapter() {};
00234 
00236   // The Adapter interface.
00238 
00239   size_t getLocalNumIDs() const { return numIds_;}
00240 
00241   void getIDsView(const zgid_t *&ids) const {ids = idList_;}
00242 
00243   int getNumWeightsPerID() const { return numWeights_;}
00244 
00245   void getWeightsView(const scalar_t *&weights, int &stride, int idx) const
00246   {
00247     if (idx < 0 || idx >= numWeights_) {
00248       std::ostringstream emsg;
00249       emsg << __FILE__ << ":" << __LINE__
00250            << "  Invalid vector index " << idx << std::endl;
00251       throw std::runtime_error(emsg.str());
00252     }
00253     size_t length;
00254     weights_[idx].getStridedList(length, weights, stride);
00255   }
00256 
00258   // The VectorAdapter interface.
00260 
00261   int getNumEntriesPerID() const { return numEntriesPerID_;}
00262 
00263   void getEntriesView(const scalar_t *&entries, int &stride, int idx = 0) const
00264   {
00265     if (idx < 0 || idx >= numEntriesPerID_) {
00266       std::ostringstream emsg;
00267       emsg << __FILE__ << ":" << __LINE__
00268            << "  Invalid vector index " << idx << std::endl;
00269       throw std::runtime_error(emsg.str());
00270     }
00271     size_t length;
00272     entries_[idx].getStridedList(length, entries, stride);
00273   }
00274 
00275 private:
00276 
00277   lno_t numIds_;
00278   const zgid_t *idList_;
00279 
00280   int numEntriesPerID_;
00281   ArrayRCP<StridedData<lno_t, scalar_t> > entries_ ;
00282 
00283   int numWeights_;
00284   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00285 
00286   void createBasicVector(
00287     std::vector<const scalar_t *> &entries,  std::vector<int> &entryStride,
00288     std::vector<const scalar_t *> &weights, std::vector<int> &weightStrides)
00289   {
00290     typedef StridedData<lno_t,scalar_t> input_t;
00291 
00292     if (numIds_){
00293       int stride = 1;
00294       entries_ = arcp(new input_t[numEntriesPerID_], 0, numEntriesPerID_, true);
00295       for (int v=0; v < numEntriesPerID_; v++) {
00296         if (entryStride.size()) stride = entryStride[v];
00297         ArrayRCP<const scalar_t> eltV(entries[v], 0, stride*numIds_, false);
00298         entries_[v] = input_t(eltV, stride);
00299       }
00300 
00301       if (numWeights_) {
00302         stride = 1;
00303         weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
00304         for (int w=0; w < numWeights_; w++){
00305           if (weightStrides.size()) stride = weightStrides[w];
00306           ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false);
00307           weights_[w] = input_t(wgtV, stride);
00308         }
00309       }
00310     }
00311   }
00312 };
00313 
00314 }  //namespace Zoltan2
00315 
00316 #endif