Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_CoordinateModel.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #ifndef _ZOLTAN2_COORDINATEMODEL_HPP_
16 #define _ZOLTAN2_COORDINATEMODEL_HPP_
17 
18 #include <Zoltan2_Model.hpp>
19 #include <Zoltan2_MeshAdapter.hpp>
21 #include <Zoltan2_GraphAdapter.hpp>
24 #include <Zoltan2_StridedData.hpp>
25 
26 namespace Zoltan2 {
27 
34 template <typename Adapter>
35 class CoordinateModel : public Model<Adapter>
36 {
37 public:
38 
39 #ifndef DOXYGEN_SHOULD_SKIP_THIS
40  typedef typename Adapter::scalar_t scalar_t;
41  typedef typename Adapter::gno_t gno_t;
42  typedef typename Adapter::lno_t lno_t;
43  typedef typename Adapter::node_t node_t;
44  typedef typename Adapter::user_t user_t;
45  typedef typename Adapter::userCoord_t userCoord_t;
46  typedef StridedData<lno_t, scalar_t> input_t;
47 #endif
48 
50  // Constructors for each Adapter type
52 
53  // VectorAdapter
54  CoordinateModel(const RCP<const VectorAdapter<user_t> > &ia,
55  const RCP<const Environment> &env,
56  const RCP<const Comm<int> > &comm,
57  modelFlag_t &flags):
58  numGlobalCoordinates_(), env_(env), comm_(comm),
59  coordinateDim_(), gids_(),
60  xyz_(), userNumWeights_(0), weights_()
61  {
62  this->ia_ = ia;
63  typedef VectorAdapter<user_t> adapterWithCoords_t;
64  sharedConstructor<adapterWithCoords_t>(&(*ia), env, comm, flags);
65  }
66 
67  // MatrixAdapter
69  const RCP<const Environment> &env,
70  const RCP<const Comm<int> > &comm,
71  modelFlag_t &flags) :
72  numGlobalCoordinates_(), env_(env), comm_(comm),
73  coordinateDim_(), gids_(),
74  xyz_(), userNumWeights_(0), weights_()
75  {
76  if (!(ia->coordinatesAvailable()))
77  throw std::logic_error("No coordinate info provided to MatrixAdapter.");
78  else {
79  this->ia_ = ia;
80  typedef VectorAdapter<userCoord_t> adapterWithCoords_t;
81  adapterWithCoords_t *va = ia->getCoordinateInput();
82  // this->ia = va;
83  sharedConstructor<adapterWithCoords_t>(va, env, comm, flags);
84  }
85  }
86 
87  // GraphAdapter
89  const RCP<const Environment> &env,
90  const RCP<const Comm<int> > &comm,
91  modelFlag_t &flags) :
92  numGlobalCoordinates_(), env_(env), comm_(comm),
93  coordinateDim_(), gids_(),
94  xyz_(), userNumWeights_(0), weights_()
95  {
96  if (!(ia->coordinatesAvailable()))
97  throw std::logic_error("No coordinate info provided to GraphAdapter.");
98  else {
99  this->ia_ = ia;
100  typedef VectorAdapter<userCoord_t> adapterWithCoords_t;
101  adapterWithCoords_t *va = ia->getCoordinateInput();
102 
103  sharedConstructor<adapterWithCoords_t>(va, env, comm, flags);
104  }
105  }
106 
107  // MeshAdapter
108  CoordinateModel(const RCP<const MeshAdapter<user_t> > &ia,
109  const RCP<const Environment> &env,
110  const RCP<const Comm<int> > &comm,
111  modelFlag_t &flags) :
112  numGlobalCoordinates_(), env_(env), comm_(comm),
113  coordinateDim_(), gids_(),
114  xyz_(), userNumWeights_(0), weights_()
115  {
116  this->ia_ = ia;
117  typedef MeshAdapter<user_t> adapterWithCoords_t;
118  sharedConstructor<adapterWithCoords_t>(&(*ia), env, comm, flags);
119  }
120 
121  // IdentifierAdapter
123  const RCP<const Environment> &env,
124  const RCP<const Comm<int> > &comm,
125  modelFlag_t &flags)
126  {
127  throw std::logic_error(
128  "A coordinate model can not be build from an IdentifierAdapter");
129  }
130 
132  // CoordinateModel interface.
134 
137  int getCoordinateDim() const { return coordinateDim_;}
138 
141  size_t getLocalNumCoordinates() const { return gids_.size();}
142 
145  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
146 
149  int getNumWeightsPerCoordinate() const { return userNumWeights_;}
150 
173  size_t getCoordinates(ArrayView<const gno_t> &Ids,
174  ArrayView<input_t> &xyz,
175  ArrayView<input_t> &wgts) const
176  {
177  xyz = xyz_.view(0, coordinateDim_);
178  wgts = weights_.view(0, userNumWeights_);
179 
180  size_t nCoord = getLocalNumCoordinates();
181  Ids = ArrayView<const gno_t>();
182 
183  if (nCoord){
184  Ids = Teuchos::arrayView<const gno_t>(
185  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
186  }
187 
188  return nCoord;
189  }
190 
199  Kokkos::View<const gno_t *, typename node_t::device_type> &Ids,
200  // coordinates in MJ are LayoutLeft since Tpetra Multivector gives LayoutLeft
201  Kokkos::View<scalar_t **,
202  Kokkos::LayoutLeft, typename node_t::device_type> &xyz,
203  Kokkos::View<scalar_t **, typename node_t::device_type> &wgts) const
204  {
205  const auto type = ia_->adapterType();
206 
207  if (type == VectorAdapterType or type == MeshAdapterType)
208  {
209  auto adapterWithCoords = dynamic_cast<const AdapterWithCoords<user_t>*>(&(*ia_));
210  adapterWithCoords->getCoordinatesKokkosView(xyz);
211  }
212  else if (type == MatrixAdapterType or type == GraphAdapterType)
213  {
214  auto wrapper = dynamic_cast<const AdapterWithCoordsWrapper<user_t, userCoord_t>*>(&(*ia_));
216  }
217 
218  ia_->getIDsKokkosView(Ids);
219 
220  if(userNumWeights_ > 0) {
221  ia_->getWeightsKokkosView(wgts);
222  }
223 
224  return getLocalNumCoordinates();
225  }
226 
228  // The Model interface.
230 
231  size_t getLocalNumObjects() const
232  {
233  return getLocalNumCoordinates();
234  }
235 
236  size_t getGlobalNumObjects() const
237  {
238  return getGlobalNumCoordinates();
239  }
240 
241 private:
242  RCP<const BaseAdapter<user_t>> ia_;
243 
244  size_t numGlobalCoordinates_;
245  const RCP<const Environment> env_;
246  const RCP<const Comm<int> > comm_;
247  int coordinateDim_;
248 
249  ArrayRCP<const gno_t> gids_;
250  ArrayRCP<input_t> xyz_;
251  int userNumWeights_;
252  ArrayRCP<input_t> weights_;
253 
254  template <typename AdapterWithCoords_>
255  void sharedConstructor(const AdapterWithCoords_ *ia,
256  const RCP<const Environment> &env,
257  const RCP<const Comm<int> > &comm,
258  modelFlag_t &flags);
259 
260 };
261 
262 
264 
265 // sharedConstructor
266 template <typename Adapter>
267 template <typename AdapterWithCoords_>
268 void CoordinateModel<Adapter>::sharedConstructor(
269  const AdapterWithCoords_ *ia,
270  const RCP<const Environment> &/* env */,
271  const RCP<const Comm<int> > &comm,
272  modelFlag_t &/* flags */)
273 {
274  size_t nLocalIds = ia_->getLocalNumIDs();
275 
276  // Get coordinates and weights (if any)
277 
278  int tmp[2], gtmp[2];
279  tmp[0] = ia->getDimension();
280  tmp[1] = ia->getNumWeightsPerID();
281  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 2, tmp, gtmp);
282  coordinateDim_ = gtmp[0];
283  userNumWeights_ = gtmp[1];
284 
285  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
286  coordinateDim_ > 0, COMPLEX_ASSERTION);
287 
288  input_t *coordArray = new input_t [coordinateDim_];
289  input_t *weightArray = NULL;
290  if (userNumWeights_)
291  weightArray = new input_t [userNumWeights_];
292 
293  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
294  coordArray && (!userNumWeights_|| weightArray));
295 
296 
297  if (nLocalIds){
298  const gno_t *gids=NULL;
299 
300  ia->getIDsView(gids);
301  gids_ = arcp(gids, 0, nLocalIds, false);
302 
303  for (int dim=0; dim < coordinateDim_; dim++){
304  int stride;
305  const scalar_t *coords=NULL;
306  try{
307  ia->getCoordinatesView(coords, stride, dim);
308  }
310 
311  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
312  coordArray[dim] = input_t(cArray, stride);
313  }
314 
315  for (int idx=0; idx < userNumWeights_; idx++){
316  int stride;
317  const scalar_t *weights;
318  try{
319  ia->getWeightsView(weights, stride, idx);
320  }
322 
323  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
324  weightArray[idx] = input_t(wArray, stride);
325  }
326  }
327 
328  xyz_ = arcp(coordArray, 0, coordinateDim_);
329 
330  if (userNumWeights_)
331  weights_ = arcp(weightArray, 0, userNumWeights_);
332 
333  Teuchos::reduceAll<int, size_t>(*comm, Teuchos::REDUCE_SUM, 1,
334  &nLocalIds, &numGlobalCoordinates_);
335 
336  env_->memory("After construction of coordinate model");
337 }
338 
339 } // namespace Zoltan2
340 
341 #endif
virtual VectorAdapter< UserCoord > * getCoordinateInput() const =0
global_size_t getGlobalNumCoordinates() const
Returns the global number coordinates.
IdentifierAdapter defines the interface for identifiers.
CoordinateModel(const RCP< const MatrixAdapter< user_t, userCoord_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
CoordinateModel(const RCP< const MeshAdapter< user_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
MatrixAdapter defines the adapter interface for matrices.
Defines the Model interface.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
void getCoordinatesKokkosView(typename AdapterWithCoords< User >::CoordsDeviceView &elements) const override
GraphAdapter defines the interface for graph-based user data.
Defines the MeshAdapter interface.
MeshAdapter defines the interface for mesh input.
more involved, like validate a graph
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
map_t::global_ordinal_type gno_t
Definition: mapRemotes.cpp:27
Defines the IdentifierAdapter interface.
Defines the VectorAdapter interface.
typename Zoltan2::InputTraits< ztcrsmatrix_t >::node_t node_t
This class provides geometric coordinates with optional weights to the Zoltan2 algorithm.
size_t getCoordinates(ArrayView< const gno_t > &Ids, ArrayView< input_t > &xyz, ArrayView< input_t > &wgts) const
Returns the coordinate ids, values and optional weights.
CoordinateModel(const RCP< const GraphAdapter< user_t, userCoord_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
size_t getCoordinatesKokkos(Kokkos::View< const gno_t *, typename node_t::device_type > &Ids, Kokkos::View< scalar_t **, Kokkos::LayoutLeft, typename node_t::device_type > &xyz, Kokkos::View< scalar_t **, typename node_t::device_type > &wgts) const
Returns the coordinate ids, values and optional weights.
VectorAdapter defines the interface for vector input.
size_t getLocalNumObjects() const
Return the local number of objects.
The StridedData class manages lists of weights or coordinates.
size_t getGlobalNumObjects() const
Return the global number of objects.
map_t::local_ordinal_type lno_t
Definition: mapRemotes.cpp:26
size_t getLocalNumCoordinates() const
Returns the number of coordinates on this process.
int getNumWeightsPerCoordinate() const
Returns the number (0 or greater) of weights per coordinate.
Defines the MatrixAdapter interface.
The base class for all model classes.
Tpetra::global_size_t global_size_t
CoordinateModel(const RCP< const VectorAdapter< user_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
Defines the GraphAdapter interface.
int getCoordinateDim() const
Returns the dimension of the coordinates.
virtual void getCoordinatesKokkosView(CoordsDeviceView &elements) const =0
CoordinateModel(const RCP< const IdentifierAdapter< user_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
This file defines the StridedData class.
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
Definition: Metric.cpp:39