Zoltan2
Zoltan2_CoordinateModel.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 
51 #ifndef _ZOLTAN2_COORDINATEMODEL_HPP_
52 #define _ZOLTAN2_COORDINATEMODEL_HPP_
53 
54 #include <Zoltan2_Model.hpp>
55 #include <Zoltan2_MeshAdapter.hpp>
57 #include <Zoltan2_GraphAdapter.hpp>
60 #include <Zoltan2_StridedData.hpp>
61 
62 namespace Zoltan2 {
63 
70 template <typename Adapter>
71 class CoordinateModel : public Model<Adapter>
72 {
73 public:
74 
75 #ifndef DOXYGEN_SHOULD_SKIP_THIS
76  typedef typename Adapter::scalar_t scalar_t;
77  typedef typename Adapter::gno_t gno_t;
78  typedef typename Adapter::lno_t lno_t;
79  typedef typename Adapter::user_t user_t;
80  typedef typename Adapter::userCoord_t userCoord_t;
81  typedef StridedData<lno_t, scalar_t> input_t;
82 #endif
83 
85  // Constructors for each Adapter type
87 
88  // VectorAdapter
89  CoordinateModel(const RCP<const VectorAdapter<user_t> > &ia,
90  const RCP<const Environment> &env,
91  const RCP<const Comm<int> > &comm,
92  modelFlag_t &flags):
93  numGlobalCoordinates_(), env_(env), comm_(comm),
94  coordinateDim_(), gids_(), gidsConst_(),
95  xyz_(), userNumWeights_(0), weights_()
96  {
97  typedef VectorAdapter<user_t> adapterWithCoords_t;
98  sharedConstructor<adapterWithCoords_t>(&(*ia), env, comm, flags);
99  }
100 
101  // MatrixAdapter
103  const RCP<const Environment> &env,
104  const RCP<const Comm<int> > &comm,
105  modelFlag_t &flags) :
106  numGlobalCoordinates_(), env_(env), comm_(comm),
107  coordinateDim_(), gids_(), gidsConst_(),
108  xyz_(), userNumWeights_(0), weights_()
109  {
110  if (!(ia->coordinatesAvailable()))
111  throw std::logic_error("No coordinate info provided to MatrixAdapter.");
112  else {
113  typedef VectorAdapter<userCoord_t> adapterWithCoords_t;
114  adapterWithCoords_t *va = ia->getCoordinateInput();
115  sharedConstructor<adapterWithCoords_t>(va, env, comm, flags);
116  }
117  }
118 
119  // GraphAdapter
121  const RCP<const Environment> &env,
122  const RCP<const Comm<int> > &comm,
123  modelFlag_t &flags) :
124  numGlobalCoordinates_(), env_(env), comm_(comm),
125  coordinateDim_(), gids_(), gidsConst_(),
126  xyz_(), userNumWeights_(0), weights_()
127  {
128  if (!(ia->coordinatesAvailable()))
129  throw std::logic_error("No coordinate info provided to GraphAdapter.");
130  else {
131  typedef VectorAdapter<userCoord_t> adapterWithCoords_t;
132  adapterWithCoords_t *va = ia->getCoordinateInput();
133  sharedConstructor<adapterWithCoords_t>(va, env, comm, flags);
134  }
135  }
136 
137  // MeshAdapter
138  CoordinateModel(const RCP<const MeshAdapter<user_t> > &ia,
139  const RCP<const Environment> &env,
140  const RCP<const Comm<int> > &comm,
141  modelFlag_t &flags) :
142  numGlobalCoordinates_(), env_(env), comm_(comm),
143  coordinateDim_(), gids_(), gidsConst_(),
144  xyz_(), userNumWeights_(0), weights_()
145  {
146  typedef MeshAdapter<user_t> adapterWithCoords_t;
147  sharedConstructor<adapterWithCoords_t>(&(*ia), env, comm, flags);
148  }
149 
150  // IdentifierAdapter
152  const RCP<const Environment> &env,
153  const RCP<const Comm<int> > &comm,
154  modelFlag_t &flags)
155  {
156  throw std::logic_error(
157  "A coordinate model can not be build from an IdentifierAdapter");
158  }
159 
161  // CoordinateModel interface.
163 
166  int getCoordinateDim() const { return coordinateDim_;}
167 
170  size_t getLocalNumCoordinates() const { return gids_.size();}
171 
174  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
175 
178  int getNumWeightsPerCoordinate() const { return userNumWeights_;}
179 
202  size_t getCoordinates(ArrayView<const gno_t> &Ids,
203  ArrayView<input_t> &xyz,
204  ArrayView<input_t> &wgts) const
205  {
206  xyz = xyz_.view(0, coordinateDim_);
207  wgts = weights_.view(0, userNumWeights_);
208 
209  size_t nCoord = getLocalNumCoordinates();
210  Ids = ArrayView<const gno_t>();
211 
212  if (nCoord){
213  Ids = Teuchos::arrayView<const gno_t>(
214  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
215  }
216 
217  return nCoord;
218  }
219 
221  // The Model interface.
223 
224  size_t getLocalNumObjects() const
225  {
226  return getLocalNumCoordinates();
227  }
228 
229  size_t getGlobalNumObjects() const
230  {
231  return getGlobalNumCoordinates();
232  }
233 
234 private:
235  size_t numGlobalCoordinates_;
236  const RCP<const Environment> env_;
237  const RCP<const Comm<int> > comm_;
238  int coordinateDim_;
239  ArrayRCP<const gno_t> gids_;
240  ArrayRCP<const gno_t> gidsConst_;
241  ArrayRCP<input_t> xyz_;
242  int userNumWeights_;
243  ArrayRCP<input_t> weights_;
244 
245  template <typename AdapterWithCoords>
246  void sharedConstructor(const AdapterWithCoords *ia,
247  const RCP<const Environment> &env,
248  const RCP<const Comm<int> > &comm,
249  modelFlag_t &flags);
250 
251 };
252 
253 
255 
256 // sharedConstructor
257 template <typename Adapter>
258 template <typename AdapterWithCoords>
260  const AdapterWithCoords *ia,
261  const RCP<const Environment> &env,
262  const RCP<const Comm<int> > &comm,
263  modelFlag_t &flags)
264 {
265  size_t nLocalIds = ia->getLocalNumIDs();
266 
267  // Get coordinates and weights (if any)
268 
269  int tmp[2], gtmp[2];
270  tmp[0] = ia->getDimension();
271  tmp[1] = ia->getNumWeightsPerID();
272  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 2, tmp, gtmp);
273  coordinateDim_ = gtmp[0];
274  userNumWeights_ = gtmp[1];
275 
276  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
277  coordinateDim_ > 0, COMPLEX_ASSERTION);
278 
279  input_t *coordArray = new input_t [coordinateDim_];
280  input_t *weightArray = NULL;
281  if (userNumWeights_)
282  weightArray = new input_t [userNumWeights_];
283 
284  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
285  coordArray && (!userNumWeights_|| weightArray));
286 
287 
288  if (nLocalIds){
289  const gno_t *gids=NULL;
290  ia->getIDsView(gids);
291  gids_ = arcp(gids, 0, nLocalIds, false);
292 
293  for (int dim=0; dim < coordinateDim_; dim++){
294  int stride;
295  const scalar_t *coords=NULL;
296  try{
297  ia->getCoordinatesView(coords, stride, dim);
298  }
300 
301  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
302  coordArray[dim] = input_t(cArray, stride);
303  }
304 
305  for (int idx=0; idx < userNumWeights_; idx++){
306  int stride;
307  const scalar_t *weights;
308  try{
309  ia->getWeightsView(weights, stride, idx);
310  }
312 
313  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
314  weightArray[idx] = input_t(wArray, stride);
315  }
316  }
317 
318  xyz_ = arcp(coordArray, 0, coordinateDim_);
319 
320  if (userNumWeights_)
321  weights_ = arcp(weightArray, 0, userNumWeights_);
322 
323  Teuchos::reduceAll<int, size_t>(*comm, Teuchos::REDUCE_SUM, 1,
324  &nLocalIds, &numGlobalCoordinates_);
325 
326  env_->memory("After construction of coordinate model");
327 }
328 
329 } // namespace Zoltan2
330 
331 #endif
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.
GraphAdapter defines the interface for graph-based user data.
size_t global_size_t
Defines the MeshAdapter interface.
MeshAdapter defines the interface for mesh input.
more involved, like validate a graph
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
Defines the IdentifierAdapter interface.
Defines the VectorAdapter interface.
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)
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.
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.
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.
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.