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_BASICCOORDINATEADAPTER_HPP_
00051 #define _ZOLTAN2_BASICCOORDINATEADAPTER_HPP_
00052 
00053 #include <Zoltan2_VectorAdapter.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 #include <vector>
00056 
00057 namespace Zoltan2 {
00058 
00092 template <typename User>
00093   class BasicCoordinateAdapter : public VectorAdapter<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 VectorAdapter<User>   base_adapter_t;
00105   typedef User user_t;
00106 
00107 #endif
00108 
00127   BasicCoordinateAdapter(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   BasicCoordinateAdapter(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   ~BasicCoordinateAdapter() {};
00162 
00164   // The Adapter interface.
00166 
00167   size_t getLocalNumIDs() const { return numIds_;}
00168 
00169   void getIDsView(const gid_t *&ids) const {ids = idList_;}
00170 
00171   int getNumWeightsPerID() const { return numWeights_;}
00172 
00173   void getWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
00174   {
00175     if (idx < 0 || idx >= numWeights_) {
00176       std::ostringstream emsg;
00177       emsg << __FILE__ << ":" << __LINE__
00178            << "  Invalid weight index " << idx << std::endl;
00179       throw std::runtime_error(emsg.str());
00180     }
00181     size_t length;
00182     weights_[idx].getStridedList(length, weights, stride);
00183   }
00184 
00186   // The VectorAdapter interface.
00188 
00189   int getNumEntriesPerID() const { return dimension_;}
00190 
00191   void getEntriesView(const scalar_t *&coords, int &stride, int dim) const
00192   {
00193     if (dim < 0 || dim >= dimension_) {
00194       std::ostringstream emsg;
00195       emsg << __FILE__ << ":" << __LINE__
00196            << "  Invalid dimension " << dim << std::endl;
00197       throw std::runtime_error(emsg.str());
00198     }
00199 
00200     size_t length;
00201     coords_[dim].getStridedList(length, coords, stride);
00202   }
00203 
00204 private:
00205   void initializeData(
00206     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00207     vector<const scalar_t *> &weights, vector<int> &weightStrides);
00208 
00209   lno_t numIds_;
00210   const gid_t *idList_;
00211 
00212   int dimension_;
00213   ArrayRCP<StridedData<lno_t, scalar_t> > coords_;
00214 
00215   int numWeights_;
00216   ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
00217 };
00218 
00220 // Definitions
00222 
00223 template <typename User>
00224   BasicCoordinateAdapter<User>::BasicCoordinateAdapter( 
00225     lno_t numIds, const gid_t *ids,
00226     const scalar_t *x, const scalar_t *y, const scalar_t *z,
00227     int xStride, int yStride, int zStride):
00228       numIds_(numIds), idList_(ids), 
00229       dimension_(0), coords_(), 
00230       numWeights_(0), weights_()
00231 {
00232   vector<const scalar_t *> values;
00233   vector<int> strides;
00234   vector<const scalar_t *> emptyValues;
00235   vector<int> emptyStrides;
00236 
00237   if (x){
00238     values.push_back(x);
00239     strides.push_back(xStride);
00240     dimension_++;
00241     if (y){
00242       values.push_back(y);
00243       strides.push_back(yStride);
00244       dimension_++;
00245       if (z){
00246         values.push_back(z);
00247         strides.push_back(zStride);
00248         dimension_++;
00249       }
00250     }
00251   }
00252 
00253   initializeData(values, strides, emptyValues, emptyStrides);
00254 }
00255 
00256 template <typename User>
00257   BasicCoordinateAdapter<User>::BasicCoordinateAdapter( 
00258     lno_t numIds, const gid_t *ids, 
00259     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00260     vector<const scalar_t *> &weights, vector<int> &weightStrides):
00261       numIds_(numIds), idList_(ids), 
00262       dimension_(values.size()), coords_(),
00263       numWeights_(weights.size()), weights_()
00264 {
00265   initializeData(values, valueStrides, weights, weightStrides);
00266 }
00267 
00268 template <typename User>
00269   void BasicCoordinateAdapter<User>::initializeData(
00270     vector<const scalar_t *> &values,  vector<int> &valueStrides,
00271     vector<const scalar_t *> &weights, vector<int> &weightStrides)
00272 {
00273   typedef StridedData<lno_t,scalar_t> input_t;
00274 
00275   coords_ = arcp(new input_t [dimension_], 0, dimension_, true);
00276 
00277   if (numWeights_ > 0)
00278     weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
00279 
00280   if (numIds_){
00281     int stride = 1;
00282     for (int x=0; x < dimension_; x++){
00283       if (valueStrides.size())
00284         stride = valueStrides[x];
00285       ArrayRCP<const scalar_t> coordV(values[x], 0, stride*numIds_, false); 
00286       coords_[x] = input_t(coordV, stride);
00287     }
00288 
00289     if (numWeights_){
00290       stride = 1;
00291       for (int w=0; w < numWeights_; w++){
00292         if (weightStrides.size())
00293           stride = weightStrides[w];
00294         ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false); 
00295         weights_[w] = input_t(wgtV, stride);
00296       }
00297     }
00298   }
00299 }
00300   
00301 }  //namespace Zoltan2
00302   
00303 #endif