Zoltan2  Version of the Day
Zoltan2_XpetraVectorInput.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_XPETRAVECTORINPUT_HPP_
51 #define _ZOLTAN2_XPETRAVECTORINPUT_HPP_
52 
53 #include <Zoltan2_VectorInput.hpp>
54 #include <Zoltan2_XpetraTraits.hpp>
55 #include <Zoltan2_StridedData.hpp>
56 #include <Zoltan2_Util.hpp>
57 
58 #include <Xpetra_EpetraVector.hpp>
59 #include <Xpetra_TpetraVector.hpp>
60 
61 namespace Zoltan2 {
62 
63 
83 template <typename User>
84  class XpetraVectorInput : public VectorInput<User> {
85 public:
86 
87 #ifndef DOXYGEN_SHOULD_SKIP_THIS
88  typedef typename InputTraits<User>::scalar_t scalar_t;
89  typedef typename InputTraits<User>::lno_t lno_t;
90  typedef typename InputTraits<User>::gno_t gno_t;
91  typedef typename InputTraits<User>::gid_t gid_t;
92  typedef typename InputTraits<User>::node_t node_t;
93  typedef VectorInput<User> base_adapter_t;
94  typedef User user_t;
95 
96  typedef Xpetra::Vector<
97  scalar_t, lno_t, gno_t, node_t> x_vector_t;
98  typedef Xpetra::TpetraVector<
99  scalar_t, lno_t, gno_t, node_t> xt_vector_t;
100  typedef Xpetra::EpetraVector xe_vector_t;
101 #endif
102 
106 
121  XpetraVectorInput( const RCP<const User> &invector,
122  vector<const scalar_t *> &weights, vector<int> &weightStrides);
123 
127  const RCP<const x_vector_t> &getVector() const
128  {
129  return vector_;
130  }
131 
133  // The InputAdapter interface.
135 
136  string inputAdapterName()const { return string("XpetraVector");}
137 
138  size_t getLocalNumberOfObjects() const { return getLocalLength();}
139 
140  int getNumberOfWeightsPerObject() const { return numWeights_;}
141 
142  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
143  {
144  return getVectorWeights(dim, wgt, stride);
145  }
146 
148  // The VectorInput interface.
150 
151  int getNumberOfVectors() const { return 1; }
152 
153  int getNumberOfWeights() const {return numWeights_;}
154 
155  size_t getLocalLength() const {return vector_->getLocalLength();}
156 
157  size_t getGlobalLength() const {return vector_->getGlobalLength();}
158 
159  size_t getVector(const gid_t *&Ids, const scalar_t *&elements,
160  int &stride) const;
161 
162  size_t getVector(int vectorNumber, const gid_t *&Ids,
163  const scalar_t *&elements, int &stride) const
164  {
165  env_->localInputAssertion(__FILE__, __LINE__, "invalid vector",
166  vectorNumber==0, BASIC_ASSERTION);
167 
168  return getVector(Ids, elements, stride);
169  }
170 
171  size_t getVectorWeights(int dim, const scalar_t *&weights, int &stride) const
172  {
173  env_->localInputAssertion(__FILE__, __LINE__, "invalid dimension",
174  dim >= 0 && dim < numWeights_, BASIC_ASSERTION);
175 
176  size_t length;
177 
178  weights_[dim].getStridedList(length, weights, stride);
179 
180  return length;
181  }
182 
183  template <typename Adapter>
184  size_t applyPartitioningSolution(const User &in, User *&out,
185  const PartitioningSolution<Adapter> &solution) const;
186 
187 private:
188 
189  RCP<const User> invector_;
190  RCP<const x_vector_t> vector_;
191  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > map_;
192  RCP<Environment> env_;
193  lno_t base_;
194 
195  int numWeights_;
196  ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
197 };
198 
200 // Definitions
202 
203 template <typename User>
205  const RCP<const User> &invector,
206  vector<const scalar_t *> &weights, vector<int> &weightStrides):
207  invector_(invector), vector_(), map_(),
208  env_(rcp(new Environment)), base_(),
209  numWeights_(weights.size()), weights_()
210 {
212 
213  vector_ = XpetraTraits<User>::convertToXpetra(invector);
214  map_ = vector_->getMap();
215  base_ = map_->getIndexBase();
216 
217  size_t length = vector_->getLocalLength();
218 
219  if (numWeights_ > 0)
220  weights_ = arcp(new input_t [numWeights_], 0, numWeights_, true);
221 
222  if (length > 0 && numWeights_ > 0){
223  int stride = 1;
224  for (int w=0; w < numWeights_; w++){
225  if (weightStrides.size())
226  stride = weightStrides[w];
227  ArrayRCP<const scalar_t> wtArray(weights[w], 0, stride*length, false);
228  weights_[w] = input_t(wtArray, stride);
229  }
230  }
231 }
232 
233 template <typename User>
234  size_t XpetraVectorInput<User>::getVector(const gid_t *&Ids,
235  const scalar_t *&elements, int &stride) const
236 {
237  stride = 1;
238  elements = NULL;
239  const x_vector_t *vec = vector_.get();
240 
241  if (map_->lib() == Xpetra::UseTpetra){
242  const xt_vector_t *tvector = dynamic_cast<const xt_vector_t *>(vec);
243 
244  if (tvector->getLocalLength() > 0){
245  // getData hangs if vector length is 0
246  ArrayRCP<const scalar_t> data = tvector->getData(0);
247  elements = data.get();
248  }
249  }
250  else if (map_->lib() == Xpetra::UseEpetra){
251  const xe_vector_t *evector = dynamic_cast<const xe_vector_t *>(vec);
252 
253  if (evector->getLocalLength() > 0){
254  // getData hangs if vector length is 0
255  ArrayRCP<const double> data = evector->getData(0);
256 
257  // Cast so this will compile when scalar_t is not double,
258  // a case when this code should never execute.
259  elements = reinterpret_cast<const scalar_t *>(data.get());
260  }
261  }
262  else{
263  throw logic_error("invalid underlying lib");
264  }
265 
266  ArrayView<const gid_t> gids = map_->getNodeElementList();
267  Ids = gids.getRawPtr();
268 
269  return getLocalLength();
270 }
271 
272 template <typename User>
273  template <typename Adapter>
275  const User &in, User *&out,
276  const PartitioningSolution<Adapter> &solution) const
277 {
278  // Get an import list
279 
280  size_t len = solution.getLocalNumberOfIds();
281  const gid_t *gids = solution.getIdList();
282  const partId_t *parts = solution.getPartList();
283  ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
284  ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len,
285  false);
286 
287  ArrayRCP<lno_t> dummyIn;
288  ArrayRCP<gid_t> importList;
289  ArrayRCP<lno_t> dummyOut;
290  size_t numNewRows;
291 
292  const RCP<const Comm<int> > comm = map_->getComm();
293 
294  try{
295  numNewRows = solution.convertSolutionToImportList(
296  0, dummyIn, importList, dummyOut);
297  }
299 
300  RCP<const User> inPtr = rcp(&in, false);
301  lno_t localNumElts = numNewRows;
302 
303  RCP<const User> outPtr = XpetraTraits<User>::doMigration(
304  inPtr, localNumElts, importList.get());
305 
306  out = const_cast<User *>(outPtr.get());
307  outPtr.release();
308  return numNewRows;
309 }
310 
311 } //namespace Zoltan2
312 
313 #endif
int getNumberOfWeights() const
Return the number of weights per vector element. Number of weights per element should be zero or grea...
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
Defines the VectorInput adapter interface.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
zoltan2_partId_t partId_t
Traits of Xpetra classes, including migration method.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
size_t getLocalLength() const
Return the length of the portion of the vector on this process.
size_t convertSolutionToImportList(int numExtra, ArrayRCP< Extra > &xtraInfo, ArrayRCP< typename Adapter::gid_t > &imports, ArrayRCP< Extra > &newXtraInfo) const
Create an import list from the export list.
Provides access for Zoltan2 to an Xpetra::Vector.
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
A PartitioningSolution is a solution to a partitioning problem.
size_t applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
The StridedData class manages lists of weights or coordinates.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
static RCP< const User > convertToXpetra(const RCP< const User > &a)
Convert the object to its Xpetra wrapped version.
const RCP< const x_vector_t > & getVector() const
Access to the xpetra-wrapped vector.
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...
XpetraVectorInput(const RCP< const User > &invector, vector< const scalar_t * > &weights, vector< int > &weightStrides)
Constructor.
string inputAdapterName() const
Returns a descriptive name that identifies the concrete adapter.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
const gid_t * getIdList() const
Returns the user&#39;s global ID list.
size_t getVectorWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the weights corresponding to the elements returned in getVector().
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
VectorInput defines the interface for vector input adapters.
size_t getVector(int vectorNumber, const gid_t *&Ids, const scalar_t *&elements, int &stride) const
Provide a pointer to the elements of the specified vector.
A gathering of useful namespace methods.
int getNumberOfVectors() const
Return the number of vectors (typically one).
default_scalar_t scalar_t
The data type for weights and coordinates.
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
size_t getLocalNumberOfIds() const
Returns the local number of Ids.