Zoltan2  Version of the Day
Zoltan2_BasicVectorInput.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_BASICVECTORINPUT_HPP_
51 #define _ZOLTAN2_BASICVECTORINPUT_HPP_
52 
53 #include <Zoltan2_VectorInput.hpp>
54 #include <Zoltan2_StridedData.hpp>
55 
56 namespace Zoltan2 {
57 
102 template <typename User>
103  class BasicVectorInput : public VectorInput<User> {
104 
105 public:
106 
107 #ifndef DOXYGEN_SHOULD_SKIP_THIS
108 
109  typedef typename InputTraits<User>::scalar_t scalar_t;
110  typedef typename InputTraits<User>::lno_t lno_t;
111  typedef typename InputTraits<User>::gno_t gno_t;
112  typedef typename InputTraits<User>::gid_t gid_t;
113  typedef typename InputTraits<User>::node_t node_t;
114  typedef VectorInput<User> base_adapter_t;
115  typedef User user_t;
116 
117 #endif
118 
131  BasicVectorInput(lno_t numIds, const gid_t *ids, const scalar_t *elements,
132  int elementStride=1):
133  env_(rcp(new Environment)),
134  numIds_(numIds), globalNumIds_(0), idList_(ids),
135  numVectors_(1), elements_(), numWeights_(0), weights_()
136  {
137  vector<const scalar_t *> values;
138  vector<int> strides;
139  vector<const scalar_t *> emptyValues;
140  vector<int> emptyStrides;
141 
142  values.push_back(elements);
143  strides.push_back(elementStride);
144 
145  createBasicVector(values, strides, emptyValues, emptyStrides);
146  }
147 
167  BasicVectorInput(lno_t numIds, const gid_t *ids,
168  const scalar_t *elements, int elementStride,
169  vector<const scalar_t *> &weights, vector<int> &weightStrides):
170  env_(rcp(new Environment)),
171  numIds_(numIds), globalNumIds_(0), idList_(ids),
172  numVectors_(1), elements_(),
173  numWeights_(weights.size()), weights_()
174  {
175  vector<const scalar_t *> values;
176  vector<int> strides;
177 
178  values.push_back(elements);
179  strides.push_back(elementStride);
180 
181  createBasicVector(values, strides, weights, weightStrides);
182  }
183 
208  BasicVectorInput(lno_t numIds, const gid_t *ids,
209  vector<const scalar_t *> &elements, vector<int> &elementStrides,
210  vector<const scalar_t *> &weights, vector<int> &weightStrides):
211  env_(rcp(new Environment)),
212  numIds_(numIds), globalNumIds_(0), idList_(ids),
213  numVectors_(elements.size()), elements_(),
214  numWeights_(weights.size()), weights_()
215  {
216  createBasicVector(elements, elementStrides, weights, weightStrides);
217  }
218 
222 
224  // The InputAdapter interface.
226 
227  string inputAdapterName() const {return string("BasicVector");}
228 
229  size_t getLocalNumberOfObjects() const { return numIds_;}
230 
231  int getNumberOfWeightsPerObject() const { return numWeights_;}
232 
233  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
234  {
235  return getVectorWeights(dim, wgt, stride);
236  }
237 
239  // The VectorInput interface.
241 
242  int getNumberOfVectors() const { return numVectors_;}
243 
244  int getNumberOfWeights() const { return numWeights_;}
245 
246  size_t getLocalLength() const { return numIds_; }
247 
248  size_t getGlobalLength() const { return globalNumIds_;}
249 
250  size_t getVector(const gid_t *&ids,
251  const scalar_t *&element, int &stride) const
252  {
253  return getVector(0, ids, element, stride);
254  }
255 
256  size_t getVector(int i, const gid_t *&ids,
257  const scalar_t *&element, int &stride) const;
258 
259  size_t getVectorWeights(int dimension,
260  const scalar_t *&weights, int &stride) const;
261 
262 private:
263 
264  // A default environment. An Environment is an internal Zoltan2
265  // class, so input adapters don't usually have one. But we create
266  // one here so we can use it for error handling.
267 
268  RCP<const Environment> env_;
269 
270  lno_t numIds_;
271  gno_t globalNumIds_;
272 
273  const gid_t *idList_;
274 
275  int numVectors_;
276  ArrayRCP<StridedData<lno_t, scalar_t> > elements_ ;
277 
278  int numWeights_;
279  ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
280 
281  void createBasicVector(
282  vector<const scalar_t *> &elements, vector<int> &elementStrides,
283  vector<const scalar_t *> &weights, vector<int> &weightStrides);
284 
285 };
286 
288 // Definitions
290 
291 template <typename User>
292  size_t BasicVectorInput<User>::getVector(int i, const gid_t *&ids,
293  const scalar_t *&element, int &stride) const
294 {
295  env_->localInputAssertion(__FILE__, __LINE__, "invalid vector number",
296  i >= 0 && i < numVectors_, BASIC_ASSERTION);
297 
298  ids = idList_;
299 
300  size_t length;
301 
302  elements_[i].getStridedList(length, element, stride);
303 
304  return length;
305 }
306 
307 template <typename User>
309  const scalar_t *&weights, int &stride) const
310 {
311  env_->localInputAssertion(__FILE__, __LINE__, "invalid weight dimension",
312  dimension >= 0 && dimension < numWeights_, BASIC_ASSERTION);
313 
314  size_t length;
315 
316  weights_[dimension].getStridedList(length, weights, stride);
317 
318  return length;
319 }
320 
321 template <typename User>
323  vector<const scalar_t *> &elements, vector<int> &elementStrides,
324  vector<const scalar_t *> &weights, vector<int> &weightStrides)
325 {
327 
328  gno_t tmp = numIds_;
329  try{
330  reduceAll<int, gno_t>(*(env_->comm_), Teuchos::REDUCE_SUM, 1,
331  &tmp, &globalNumIds_);
332  }
333  Z2_THROW_OUTSIDE_ERROR(*env_);
334 
335  if (numIds_){
336  int stride = 1;
337  elements_ = arcp(new input_t [numVectors_], 0, numVectors_, true);
338  for (int v=0; v < numVectors_; v++){
339  if (elementStrides.size())
340  stride = elementStrides[v];
341  ArrayRCP<const scalar_t> eltV(elements[v], 0, stride*numIds_, false);
342  elements_[v] = input_t(eltV, stride);
343  }
344 
345  if (numWeights_){
346  stride = 1;
347  weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
348  for (int w=0; w < numWeights_; w++){
349  if (weightStrides.size())
350  stride = weightStrides[w];
351  ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*numIds_, false);
352  weights_[w] = input_t(wgtV, stride);
353  }
354  }
355  }
356 }
357 
358 } //namespace Zoltan2
359 
360 #endif
size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
Defines the VectorInput adapter interface.
fast typical checks for valid arguments
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
size_t getVector(const gid_t *&ids, const scalar_t *&element, int &stride) const
Provide a pointer to the vertex elements. If the VectorInput represents more than one vector...
BasicVectorInput(lno_t numIds, const gid_t *ids, const scalar_t *elements, int elementStride=1)
Constructor for one vector with no weights.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
BasicVectorInput(lno_t numIds, const gid_t *ids, const scalar_t *elements, int elementStride, vector< const scalar_t * > &weights, vector< int > &weightStrides)
Constructor for one vector.
BasicVectorInput represents a vector (plus optional weights) supplied by the user as pointers to stri...
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
int getNumberOfVectors() const
Return the number of vectors (typically one).
BasicVectorInput(lno_t numIds, const gid_t *ids, vector< const scalar_t * > &elements, vector< int > &elementStrides, vector< const scalar_t * > &weights, vector< int > &weightStrides)
Constructor for a set of vectors.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
The StridedData class manages lists of weights or coordinates.
default_gno_t gid_t
The data type that the user uses for global Identifiers.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
size_t getLocalLength() const
Return the length of the portion of the vector on this process.
string inputAdapterName() const
Returns a descriptive name that identifies the concrete adapter.
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
VectorInput defines the interface for vector input adapters.
int getNumberOfWeights() const
Return the number of weights per vector element. Number of weights per element should be zero or grea...
default_scalar_t scalar_t
The data type for weights and coordinates.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
size_t getVectorWeights(int dimension, const scalar_t *&weights, int &stride) const
Provide a pointer to the weights corresponding to the elements returned in getVector().