Zoltan2 Version of the Day
Zoltan2_BasicCoordinateInput.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_BASICCOORDINATEINPUT_HPP_
00051 #define _ZOLTAN2_BASICCOORDINATEINPUT_HPP_
00052 
00053 #include <Zoltan2_CoordinateInput.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 #include <vector>
00056 
00057 namespace Zoltan2 {
00058 
00092 template <typename User>
00093   class BasicCoordinateInput : public CoordinateInput<User> {
00094 
00095 public:
00096 
00097 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00098 
00099   typedef typename InputTraits<User>::scalar_t    scalar_t;
00100   typedef typename InputTraits<User>::lno_t    lno_t;
00101   typedef typename InputTraits<User>::gno_t    gno_t;
00102   typedef typename InputTraits<User>::gid_t    gid_t;
00103   typedef typename InputTraits<User>::node_t   node_t;
00104   typedef CoordinateInput<User>   base_adapter_t;
00105   typedef User user_t;
00106 
00107 #endif
00108 
00127   BasicCoordinateInput(lno_t numIds, const gid_t *ids,
00128     const scalar_t *x, const scalar_t *y, const scalar_t *z,
00129     int xStride=1, int yStride=1, int zStride=1);
00130 
00155   BasicCoordinateInput(lno_t numIds, const gid_t *ids, 
00156     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00157     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00158 
00161   ~BasicCoordinateInput() {};
00162 
00164   // The InputAdapter interface.
00166 
00167   string inputAdapterName() const {return string("BasicCoordinate");}
00168 
00169   size_t getLocalNumberOfObjects() const { return numIds_;}
00170 
00171   int getNumberOfWeightsPerObject() const { return numWeights_;}
00172 
00173   size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
00174   {
00175     return getCoordinateWeights(dim, wgt, stride);
00176   }
00177 
00179   // The CoordinateInput interface.
00181 
00182   int getCoordinateDimension() const { return dimension_;}
00183 
00184   int getNumberOfWeights() const { return numWeights_;}  
00185 
00186   size_t getLocalNumberOfCoordinates() const { return numIds_; }
00187 
00188   size_t getCoordinates(int dim, const gid_t *&gids, const scalar_t *&coords, 
00189     int &stride) const
00190   {
00191     env_->localInputAssertion(__FILE__, __LINE__, "invalid dimension",
00192       dim >= 0 && dim < dimension_, BASIC_ASSERTION);
00193 
00194     gids = idList_;
00195     
00196     size_t length;
00197 
00198     coords_[dim].getStridedList(length, coords, stride);
00199 
00200     return length;
00201   }
00202 
00203   size_t getCoordinateWeights(int dim, const scalar_t *&weights, 
00204     int &stride) const
00205   {
00206     env_->localInputAssertion(__FILE__, __LINE__, "invalid dimension",
00207       dim >= 0 && dim < numWeights_, BASIC_ASSERTION);
00208     
00209     size_t length;
00210 
00211     weights_[dim].getStridedList(length, weights, stride);
00212 
00213     return length;
00214   }
00215 
00216 private:
00217   void initializeData(
00218     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00219     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00220 
00221   // A default Environment for error handling.
00222   RCP<const Environment> env_;
00223 
00224   lno_t numIds_;
00225   const gid_t *idList_;
00226 
00227   int dimension_;
00228   ArrayRCP<StridedData<lno_t, scalar_t> > coords_;
00229 
00230   int numWeights_;
00231   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00232 };
00233 
00235 // Definitions
00237 
00238 template <typename User>
00239   BasicCoordinateInput<User>::BasicCoordinateInput( 
00240     lno_t numIds, const gid_t *ids,
00241     const scalar_t *x, const scalar_t *y, const scalar_t *z,
00242     int xStride, int yStride, int zStride):
00243       env_(rcp(new Environment)), 
00244       numIds_(numIds), idList_(ids), 
00245       dimension_(0), coords_(), 
00246       numWeights_(0), weights_()
00247 {
00248   vector<const scalar_t *> values;
00249   vector<int> strides;
00250   vector<const scalar_t *> emptyValues;
00251   vector<int> emptyStrides;
00252 
00253   if (x){
00254     values.push_back(x);
00255     strides.push_back(xStride);
00256     dimension_++;
00257     if (y){
00258       values.push_back(y);
00259       strides.push_back(yStride);
00260       dimension_++;
00261       if (z){
00262         values.push_back(z);
00263         strides.push_back(zStride);
00264         dimension_++;
00265       }
00266     }
00267   }
00268 
00269   initializeData(values, strides, emptyValues, emptyStrides);
00270 }
00271 
00272 template <typename User>
00273   BasicCoordinateInput<User>::BasicCoordinateInput( 
00274     lno_t numIds, const gid_t *ids, 
00275     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00276     vector<const scalar_t *> &weights, vector<int> &weightStrides):
00277       env_(rcp(new Environment)), 
00278       numIds_(numIds), idList_(ids), 
00279       dimension_(values.size()), coords_(),
00280       numWeights_(weights.size()), weights_()
00281 {
00282   initializeData(values, valueStrides, weights, weightStrides);
00283 }
00284 
00285 template <typename User>
00286   void BasicCoordinateInput<User>::initializeData(
00287     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00288     vector<const scalar_t *> &weights, vector<int> &weightStrides)
00289 {
00290   typedef StridedData<lno_t,scalar_t> input_t;
00291 
00292   coords_ = arcp(new input_t [dimension_], 0, dimension_, true);
00293 
00294   if (numWeights_ > 0)
00295     weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
00296 
00297   if (numIds_){
00298     int stride = 1;
00299     for (int x=0; x < dimension_; x++){
00300       if (valueStrides.size())
00301         stride = valueStrides[x];
00302       ArrayRCP<const scalar_t> coordV(values[x], 0, stride*numIds_, false); 
00303       coords_[x] = input_t(coordV, stride);
00304     }
00305 
00306     if (numWeights_){
00307       stride = 1;
00308       for (int w=0; w < numWeights_; w++){
00309         if (weightStrides.size())
00310           stride = weightStrides[w];
00311         ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false); 
00312         weights_[w] = input_t(wgtV, stride);
00313       }
00314     }
00315   }
00316 }
00317   
00318 }  //namespace Zoltan2
00319   
00320 #endif