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_(),
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_(),
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_(),
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_(),
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<input_t> xyz_;
241  int userNumWeights_;
242  ArrayRCP<input_t> weights_;
243 
244  template <typename AdapterWithCoords>
245  void sharedConstructor(const AdapterWithCoords *ia,
246  const RCP<const Environment> &env,
247  const RCP<const Comm<int> > &comm,
248  modelFlag_t &flags);
249 
250 };
251 
252 
254 
255 // sharedConstructor
256 template <typename Adapter>
257 template <typename AdapterWithCoords>
258 void CoordinateModel<Adapter>::sharedConstructor(
259  const AdapterWithCoords *ia,
260  const RCP<const Environment> &env,
261  const RCP<const Comm<int> > &comm,
262  modelFlag_t &flags)
263 {
264  size_t nLocalIds = ia->getLocalNumIDs();
265 
266  // Get coordinates and weights (if any)
267 
268  int tmp[2], gtmp[2];
269  tmp[0] = ia->getDimension();
270  tmp[1] = ia->getNumWeightsPerID();
271  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_MAX, 2, tmp, gtmp);
272  coordinateDim_ = gtmp[0];
273  userNumWeights_ = gtmp[1];
274 
275  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
276  coordinateDim_ > 0, COMPLEX_ASSERTION);
277 
278  input_t *coordArray = new input_t [coordinateDim_];
279  input_t *weightArray = NULL;
280  if (userNumWeights_)
281  weightArray = new input_t [userNumWeights_];
282 
283  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
284  coordArray && (!userNumWeights_|| weightArray));
285 
286 
287  if (nLocalIds){
288  const gno_t *gids=NULL;
289  ia->getIDsView(gids);
290  gids_ = arcp(gids, 0, nLocalIds, false);
291 
292  for (int dim=0; dim < coordinateDim_; dim++){
293  int stride;
294  const scalar_t *coords=NULL;
295  try{
296  ia->getCoordinatesView(coords, stride, dim);
297  }
299 
300  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
301  coordArray[dim] = input_t(cArray, stride);
302  }
303 
304  for (int idx=0; idx < userNumWeights_; idx++){
305  int stride;
306  const scalar_t *weights;
307  try{
308  ia->getWeightsView(weights, stride, idx);
309  }
311 
312  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
313  weightArray[idx] = input_t(wArray, stride);
314  }
315  }
316 
317  xyz_ = arcp(coordArray, 0, coordinateDim_);
318 
319  if (userNumWeights_)
320  weights_ = arcp(weightArray, 0, userNumWeights_);
321 
322  Teuchos::reduceAll<int, size_t>(*comm, Teuchos::REDUCE_SUM, 1,
323  &nLocalIds, &numGlobalCoordinates_);
324 
325  env_->memory("After construction of coordinate model");
326 }
327 
328 } // namespace Zoltan2
329 
330 #endif
int getCoordinateDim() const
Returns the dimension of the coordinates.
size_t getLocalNumCoordinates() const
Returns the number of coordinates on this process.
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.
static ArrayRCP< ArrayRCP< zscalar_t > > weights
size_t getGlobalNumObjects() const
Return the global number of objects.
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.
The StridedData class manages lists of weights or coordinates.
Defines the MatrixAdapter interface.
The base class for all model classes.
size_t getLocalNumObjects() const
Return the local number of objects.
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 getNumWeightsPerCoordinate() const
Returns the number (0 or greater) of weights per coordinate.
CoordinateModel(const RCP< const IdentifierAdapter< user_t > > &ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &flags)
global_size_t getGlobalNumCoordinates() const
Returns the global number coordinates.
This file defines the StridedData class.