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_BASICVECTORINPUT_HPP_
00051 #define _ZOLTAN2_BASICVECTORINPUT_HPP_
00052 
00053 #include <Zoltan2_VectorInput.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 
00056 namespace Zoltan2 {
00057 
00102 template <typename User>
00103   class BasicVectorInput : public VectorInput<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 VectorInput<User>   base_adapter_t;
00115   typedef User user_t;
00116 
00117 #endif
00118 
00131   BasicVectorInput(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   BasicVectorInput(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   BasicVectorInput(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   ~BasicVectorInput() {};
00222 
00224   // The InputAdapter interface.
00226 
00227   string inputAdapterName() const {return string("BasicVector");}
00228 
00229   size_t getLocalNumberOfObjects() const { return numIds_;}
00230 
00231   int getNumberOfWeightsPerObject() const { return numWeights_;}
00232 
00233   size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
00234   {
00235     return getVectorWeights(dim, wgt, stride);
00236   }
00237 
00239   // The VectorInput interface.
00241 
00242   int getNumberOfVectors() const { return numVectors_;}
00243 
00244   int getNumberOfWeights() const { return numWeights_;}
00245 
00246   size_t getLocalLength() const { return numIds_; }
00247 
00248   size_t getGlobalLength() const { return globalNumIds_;}
00249 
00250   size_t getVector(const gid_t *&ids, 
00251      const scalar_t *&element, int &stride) const
00252   {
00253     return getVector(0, ids, element, stride);
00254   }
00255 
00256   size_t getVector(int i, const gid_t *&ids, 
00257      const scalar_t *&element, int &stride) const;
00258 
00259   size_t getVectorWeights(int dimension, 
00260      const scalar_t *&weights, int &stride) const;
00261 
00262 private:
00263 
00264   // A default environment.  An Environment is an internal Zoltan2
00265   // class, so input adapters don't usually have one.  But we create
00266   // one here so we can use it for error handling.
00267 
00268   RCP<const Environment> env_;
00269 
00270   lno_t numIds_;
00271   gno_t globalNumIds_;
00272 
00273   const gid_t *idList_;
00274 
00275   int numVectors_;
00276   ArrayRCP<StridedData<lno_t, scalar_t> > elements_ ;
00277 
00278   int numWeights_;
00279   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00280 
00281   void createBasicVector(
00282     vector<const scalar_t *> &elements,  vector<int> &elementStrides,
00283     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00284 
00285 };
00286 
00288 // Definitions
00290 
00291 template <typename User>
00292   size_t BasicVectorInput<User>::getVector(int i, const gid_t *&ids, 
00293     const scalar_t *&element, int &stride) const
00294 {
00295   env_->localInputAssertion(__FILE__, __LINE__, "invalid vector number",
00296     i >= 0 && i < numVectors_, BASIC_ASSERTION);
00297   
00298   ids = idList_;
00299 
00300   size_t length;
00301 
00302   elements_[i].getStridedList(length, element, stride);
00303 
00304   return length;
00305 }
00306 
00307 template <typename User>
00308   size_t BasicVectorInput<User>::getVectorWeights(int dimension, 
00309     const scalar_t *&weights, int &stride) const
00310 {
00311   env_->localInputAssertion(__FILE__, __LINE__,  "invalid weight dimension",
00312     dimension >= 0 && dimension < numWeights_, BASIC_ASSERTION);
00313 
00314   size_t length;
00315 
00316   weights_[dimension].getStridedList(length, weights, stride);
00317 
00318   return length;
00319 }
00320 
00321 template <typename User>
00322   void BasicVectorInput<User>::createBasicVector(
00323    vector<const scalar_t *> &elements,  vector<int> &elementStrides,
00324    vector<const scalar_t *> &weights, vector<int> &weightStrides)
00325 {
00326   typedef StridedData<lno_t,scalar_t> input_t;
00327 
00328   gno_t tmp = numIds_;
00329   try{
00330     reduceAll<int, gno_t>(*(env_->comm_), Teuchos::REDUCE_SUM, 1, 
00331        &tmp, &globalNumIds_);
00332   }
00333   Z2_THROW_OUTSIDE_ERROR(*env_);
00334 
00335   if (numIds_){
00336     int stride = 1;
00337     elements_ = arcp(new input_t [numVectors_], 0, numVectors_, true);
00338     for (int v=0; v < numVectors_; v++){
00339       if (elementStrides.size())
00340         stride = elementStrides[v];
00341       ArrayRCP<const scalar_t> eltV(elements[v], 0, stride*numIds_, false); 
00342       elements_[v] = input_t(eltV, stride);
00343     }
00344 
00345     if (numWeights_){
00346       stride = 1;
00347       weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
00348       for (int w=0; w < numWeights_; w++){
00349         if (weightStrides.size())
00350           stride = weightStrides[w];
00351         ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false); 
00352         weights_[w] = input_t(wgtV, stride);
00353       }
00354     }
00355   }
00356 }
00357   
00358 }  //namespace Zoltan2
00359   
00360 #endif