Zoltan2
Zoltan2_BasicVectorAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_BASICVECTORADAPTER_HPP_
51 #define _ZOLTAN2_BASICVECTORADAPTER_HPP_
52 
54 #include <Zoltan2_StridedData.hpp>
55 
56 namespace Zoltan2 {
57 
80 template <typename User>
81  class BasicVectorAdapter : public VectorAdapter<User> {
82 
83 public:
84 
85 #ifndef DOXYGEN_SHOULD_SKIP_THIS
86 
87  typedef typename InputTraits<User>::scalar_t scalar_t;
88  typedef typename InputTraits<User>::lno_t lno_t;
89  typedef typename InputTraits<User>::gno_t gno_t;
90  typedef typename InputTraits<User>::part_t part_t;
91  typedef typename InputTraits<User>::node_t node_t;
92  typedef User user_t;
93 
94 #endif
95 
111  BasicVectorAdapter(lno_t numIds, const gno_t *ids,
112  const scalar_t *entries, int entryStride=1,
113  bool usewgts=false,
114  const scalar_t *wgts=NULL, int wgtStride=1):
115  numIds_(numIds), idList_(ids),
116  numEntriesPerID_(1), entries_(),
117  numWeights_(usewgts==true), weights_()
118  {
119  std::vector<const scalar_t *> values;
120  std::vector<int> strides;
121  std::vector<const scalar_t *> weightValues;
122  std::vector<int> weightStrides;
123 
124  values.push_back(entries);
125  strides.push_back(entryStride);
126  if (usewgts) {
127  weightValues.push_back(wgts);
128  weightStrides.push_back(wgtStride);
129  }
130 
131  createBasicVector(values, strides, weightValues, weightStrides);
132  }
133 
159  BasicVectorAdapter(lno_t numIds, const gno_t *ids,
160  std::vector<const scalar_t *> &entries, std::vector<int> &entryStride,
161  std::vector<const scalar_t *> &weights, std::vector<int> &weightStrides):
162  numIds_(numIds), idList_(ids),
163  numEntriesPerID_(entries.size()), entries_(),
164  numWeights_(weights.size()), weights_()
165  {
166  createBasicVector(entries, entryStride, weights, weightStrides);
167  }
168 
195  BasicVectorAdapter(lno_t numIds, const gno_t *ids,
196  const scalar_t *x, const scalar_t *y,
197  const scalar_t *z,
198  int xStride=1, int yStride=1, int zStride=1,
199  bool usewgts=false, const scalar_t *wgts=NULL,
200  int wgtStride=1) :
201  numIds_(numIds), idList_(ids), numEntriesPerID_(0), entries_(),
202  numWeights_(usewgts==true), weights_()
203  {
204  std::vector<const scalar_t *> values, weightValues;
205  std::vector<int> strides, weightStrides;
206 
207  if (x){
208  values.push_back(x);
209  strides.push_back(xStride);
210  numEntriesPerID_++;
211  if (y){
212  values.push_back(y);
213  strides.push_back(yStride);
214  numEntriesPerID_++;
215  if (z){
216  values.push_back(z);
217  strides.push_back(zStride);
218  numEntriesPerID_++;
219  }
220  }
221  }
222  if (usewgts) {
223  weightValues.push_back(wgts);
224  weightStrides.push_back(wgtStride);
225  }
226  createBasicVector(values, strides, weightValues, weightStrides);
227  }
228 
229 
231 
233  // The Adapter interface.
235 
236  size_t getLocalNumIDs() const { return numIds_;}
237 
238  void getIDsView(const gno_t *&ids) const {ids = idList_;}
239 
240  int getNumWeightsPerID() const { return numWeights_;}
241 
242  void getWeightsView(const scalar_t *&weights, int &stride, int idx) const
243  {
244  if (idx < 0 || idx >= numWeights_) {
245  std::ostringstream emsg;
246  emsg << __FILE__ << ":" << __LINE__
247  << " Invalid vector index " << idx << std::endl;
248  throw std::runtime_error(emsg.str());
249  }
250  size_t length;
251  weights_[idx].getStridedList(length, weights, stride);
252  }
253 
255  // The VectorAdapter interface.
257 
258  int getNumEntriesPerID() const { return numEntriesPerID_;}
259 
260  void getEntriesView(const scalar_t *&entries, int &stride, int idx = 0) const
261  {
262  if (idx < 0 || idx >= numEntriesPerID_) {
263  std::ostringstream emsg;
264  emsg << __FILE__ << ":" << __LINE__
265  << " Invalid vector index " << idx << std::endl;
266  throw std::runtime_error(emsg.str());
267  }
268  size_t length;
269  entries_[idx].getStridedList(length, entries, stride);
270  }
271 
272 private:
273 
274  lno_t numIds_;
275  const gno_t *idList_;
276 
277  int numEntriesPerID_;
278  ArrayRCP<StridedData<lno_t, scalar_t> > entries_ ;
279 
280  int numWeights_;
281  ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
282 
283  void createBasicVector(
284  std::vector<const scalar_t *> &entries, std::vector<int> &entryStride,
285  std::vector<const scalar_t *> &weights, std::vector<int> &weightStrides)
286  {
287  typedef StridedData<lno_t,scalar_t> input_t;
288 
289  if (numIds_){
290  int stride = 1;
291  entries_ = arcp(new input_t[numEntriesPerID_], 0, numEntriesPerID_, true);
292  for (int v=0; v < numEntriesPerID_; v++) {
293  if (entryStride.size()) stride = entryStride[v];
294  ArrayRCP<const scalar_t> eltV(entries[v], 0, stride*numIds_, false);
295  entries_[v] = input_t(eltV, stride);
296  }
297  }
298 
299  if (numWeights_) {
300  int stride = 1;
301  weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
302  for (int w=0; w < numWeights_; w++){
303  if (weightStrides.size()) stride = weightStrides[w];
304  ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false);
305  weights_[w] = input_t(wgtV, stride);
306  }
307  }
308  }
309 };
310 
311 } //namespace Zoltan2
312 
313 #endif
InputTraits< User >::scalar_t scalar_t
size_t getLocalNumIDs() const
Returns the number of objects on this process.
BasicVectorAdapter(lno_t numIds, const gno_t *ids, const scalar_t *entries, int entryStride=1, bool usewgts=false, const scalar_t *wgts=NULL, int wgtStride=1)
Constructor for one vector with (optionally) one weight.
InputTraits< User >::gno_t gno_t
default_part_t part_t
The data type to represent part numbers.
InputTraits< User >::lno_t lno_t
Defines the VectorAdapter interface.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
int getNumEntriesPerID() const
Return the number of vectors (typically one).
void getWeightsView(const scalar_t *&weights, int &stride, int idx) const
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
VectorAdapter defines the interface for vector input.
The StridedData class manages lists of weights or coordinates.
BasicVectorAdapter represents a vector (plus optional weights) supplied by the user as pointers to st...
InputTraits< User >::part_t part_t
BasicVectorAdapter(lno_t numIds, const gno_t *ids, std::vector< const scalar_t * > &entries, std::vector< int > &entryStride, std::vector< const scalar_t * > &weights, std::vector< int > &weightStrides)
Constructor for multivector (a set of vectors sharing the same global numbering and data distribution...
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
void getIDsView(const gno_t *&ids) const
Provide a pointer to this process&#39; identifiers.
int getNumWeightsPerID() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
void getEntriesView(const scalar_t *&entries, int &stride, int idx=0) const
BasicVectorAdapter(lno_t numIds, const gno_t *ids, const scalar_t *x, const scalar_t *y, const scalar_t *z, int xStride=1, int yStride=1, int zStride=1, bool usewgts=false, const scalar_t *wgts=NULL, int wgtStride=1)
A simple constructor for coordinate-based problems with dimension 1, 2 or 3 and (optionally) one weig...
default_scalar_t scalar_t
The data type for weights and coordinates.
This file defines the StridedData class.