Zoltan2  Version of the Day
Zoltan2_XpetraMultiVectorInput.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_XPETRAMULTIVECTORINPUT_HPP_
51 #define _ZOLTAN2_XPETRAMULTIVECTORINPUT_HPP_
52 
53 #include <Zoltan2_XpetraTraits.hpp>
54 #include <Zoltan2_VectorInput.hpp>
55 #include <Zoltan2_StridedData.hpp>
56 #include <Zoltan2_Util.hpp>
57 
58 #include <Xpetra_EpetraMultiVector.hpp>
59 #include <Xpetra_TpetraMultiVector.hpp>
60 
61 namespace Zoltan2 {
62 
80 template <typename User>
81  class XpetraMultiVectorInput : public VectorInput<User> {
82 public:
83 
84 #ifndef DOXYGEN_SHOULD_SKIP_THIS
85  typedef typename InputTraits<User>::scalar_t scalar_t;
86  typedef typename InputTraits<User>::lno_t lno_t;
87  typedef typename InputTraits<User>::gno_t gno_t;
88  typedef typename InputTraits<User>::gid_t gid_t;
89  typedef typename InputTraits<User>::node_t node_t;
90  typedef VectorInput<User> base_adapter_t;
91  typedef User user_t;
92 
93  typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> x_mvector_t;
94  typedef Xpetra::TpetraMultiVector<
95  scalar_t, lno_t, gno_t, node_t> xt_mvector_t;
96  typedef Xpetra::EpetraMultiVector xe_mvector_t;
97 #endif
98 
102 
118  XpetraMultiVectorInput(const RCP<const User> &invector,
119  vector<const scalar_t *> &weights, vector<int> &weightStrides);
120 
126  XpetraMultiVectorInput(const RCP<const User> &invector);
127 
131  const RCP<const x_mvector_t> &getVector() const
132  {
133  return vector_;
134  }
135 
137  // The InputAdapter interface.
139 
140  string inputAdapterName()const {return string("XpetraMultiVector");}
141 
142  size_t getLocalNumberOfObjects() const { return getLocalLength();}
143 
144  int getNumberOfWeightsPerObject() const { return numWeights_;}
145 
146  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
147  {
148  return getVectorWeights(dim, wgt, stride);
149  }
150 
152  // The VectorInput interface.
154 
155  int getNumberOfVectors() const {return vector_->getNumVectors();}
156 
157  int getNumberOfWeights() const {return numWeights_;}
158 
159  size_t getLocalLength() const {return vector_->getLocalLength();}
160 
161  size_t getGlobalLength() const {return vector_->getGlobalLength();}
162 
163  size_t getVector(const gid_t *&Ids,
164  const scalar_t *&elements, int &stride) const
165  {
166  return getVector(0, Ids, elements, stride);
167  }
168 
169  size_t getVector(int i, const gid_t *&Ids,
170  const scalar_t *&elements, int &stride) const;
171 
172  size_t getVectorWeights(int dim, const scalar_t *&weights, int &stride) const
173  {
174  env_->localInputAssertion(__FILE__, __LINE__, "invalid dimension",
175  dim >= 0 && dim < numWeights_, BASIC_ASSERTION);
176 
177  size_t length;
178 
179  weights_[dim].getStridedList(length, weights, stride);
180 
181  return length;
182  }
183 
184  template <typename Adapter>
185  size_t applyPartitioningSolution(const User &in, User *&out,
186  const PartitioningSolution<Adapter> &solution) const;
187 
188 private:
189 
190  RCP<const User> invector_;
191  RCP<const x_mvector_t> vector_;
192  RCP<const Xpetra::Map<lno_t, gno_t, node_t> > map_;
193  RCP<Environment> env_; // for error messages, etc.
194  lno_t base_;
195 
196  int numWeights_;
197  ArrayRCP<StridedData<lno_t, scalar_t> > weights_;
198 };
199 
201 // Definitions
203 
204 template <typename User>
206  const RCP<const User> &invector,
207  vector<const scalar_t *> &weights, vector<int> &weightStrides):
208  invector_(invector), vector_(), map_(),
209  env_(rcp(new Environment)), base_(),
210  numWeights_(weights.size()), weights_(weights.size())
211 {
213 
214  vector_ = XpetraTraits<User>::convertToXpetra(invector);
215  map_ = vector_->getMap();
216  base_ = map_->getIndexBase();
217 
218  size_t length = vector_->getLocalLength();
219 
220  if (length > 0 && numWeights_ > 0){
221  int stride = 1;
222  for (int w=0; w < numWeights_; w++){
223  if (weightStrides.size())
224  stride = weightStrides[w];
225  ArrayRCP<const scalar_t> wgtV(weights[w], 0, stride*length, false);
226  weights_[w] = input_t(wgtV, stride);
227  }
228  }
229 }
230 
231 
232 template <typename User>
234  const RCP<const User> &invector):
235  invector_(invector), vector_(), map_(),
236  env_(rcp(new Environment)), base_(),
237  numWeights_(0), weights_()
238 {
239  vector_ = XpetraTraits<User>::convertToXpetra(invector);
240  map_ = vector_->getMap();
241  base_ = map_->getIndexBase();
242 }
243 
244 template <typename User>
246  int i, const gid_t *&Ids, const scalar_t *&elements, int &stride) const
247 {
248  stride = 1;
249  elements = NULL;
250  if (map_->lib() == Xpetra::UseTpetra){
251  const xt_mvector_t *tvector =
252  dynamic_cast<const xt_mvector_t *>(vector_.get());
253 
254  if (tvector->getLocalLength() > 0){
255  ArrayRCP<const scalar_t> data = tvector->getData(i);
256  elements = data.get();
257  }
258  }
259  else if (map_->lib() == Xpetra::UseEpetra){
260  const xe_mvector_t *evector =
261  dynamic_cast<const xe_mvector_t *>(vector_.get());
262 
263  if (evector->getLocalLength() > 0){
264  ArrayRCP<const double> data = evector->getData(i);
265 
266  // Cast so this will compile when scalar_t is not double,
267  // a case when this code should never execute.
268  elements = reinterpret_cast<const scalar_t *>(data.get());
269  }
270  }
271  else{
272  throw logic_error("invalid underlying lib");
273  }
274 
275  ArrayView<const gid_t> gids = map_->getNodeElementList();
276  Ids = gids.getRawPtr();
277  return gids.size();
278 }
279 
280 template <typename User>
281  template <typename Adapter>
283  const User &in, User *&out,
284  const PartitioningSolution<Adapter> &solution) const
285 {
286  size_t len = solution.getLocalNumberOfIds();
287  const gid_t *gids = solution.getIdList();
288  const partId_t *parts = solution.getPartList();
289  ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
290  ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len,
291  false);
292  ArrayRCP<lno_t> dummyIn;
293  ArrayRCP<gid_t> importList;
294  ArrayRCP<lno_t> dummyOut;
295  size_t numNewRows;
296 
297  const RCP<const Comm<int> > comm = map_->getComm();
298 
299  try{
300  // Get an import list
301  numNewRows = solution.convertSolutionToImportList(
302  0, dummyIn, importList, dummyOut);
303  }
305 
306  RCP<const User> inPtr = rcp(&in, false);
307  lno_t localNumElts = numNewRows;
308 
309  RCP<const User> outPtr = XpetraTraits<User>::doMigration(
310  inPtr, localNumElts, importList.get());
311 
312  out = const_cast<User *>(outPtr.get());
313  outPtr.release();
314  return numNewRows;
315 }
316 
317 } //namespace Zoltan2
318 
319 #endif
size_t applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
An input adapter for Xpetra::MultiVector.
int getNumberOfVectors() const
Return the number of vectors (typically one).
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
size_t getVector(const gid_t *&Ids, const scalar_t *&elements, int &stride) const
Provide a pointer to the vertex elements. If the VectorInput represents more than one vector...
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().
zoltan2_partId_t partId_t
Traits of Xpetra classes, including migration method.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
const RCP< const x_mvector_t > & getVector() const
Access to xpetra wrapper multivector.
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.
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.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
int getNumberOfWeights() const
Return the number of weights per vector element. Number of weights per element should be zero or grea...
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.
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...
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 getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
size_t getLocalLength() const
Return the length of the portion of the vector on this process.
VectorInput defines the interface for vector input adapters.
A gathering of useful namespace methods.
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
default_scalar_t scalar_t
The data type for weights and coordinates.
XpetraMultiVectorInput(const RCP< const User > &invector, vector< const scalar_t * > &weights, vector< int > &weightStrides)
Constructor.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
size_t getLocalNumberOfIds() const
Returns the local number of Ids.