Zoltan2  Version of the Day
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_MatrixInput.hpp>
56 #include <Zoltan2_GraphInput.hpp>
59 #include <Zoltan2_VectorInput.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;
80 #endif
81 
83  throw std::logic_error("a specific instantiation should be used");
84  }
85 
87  // The CoordinateModel interface.
89 
92  int getCoordinateDim() const { return 0; }
93 
96  size_t getLocalNumCoordinates() const { return 0; }
97 
101 
104  int getCoordinateWeightDim() const { return 0; }
105 
128  size_t getCoordinates(ArrayView<const gno_t> &Ids,
129  ArrayView<input_t> &xyz,
130  ArrayView<input_t> &wgts) const {return 0;}
131 
133  // The Model interface.
135 
136  size_t getLocalNumObjects() const
137  {
138  return getLocalNumCoordinates();
139  }
140 
141  size_t getGlobalNumObjects() const
142  {
143  return getGlobalNumCoordinates();
144  }
145 
146  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const { return ; }
147 };
148 
149 #ifndef DOXYGEN_SHOULD_SKIP_THIS
150 
152 // Coordinate model derived from CoordinateInput.
154 
155 template <typename User>
156 class CoordinateModel<CoordinateInput<User> > :
157  public Model<CoordinateInput<User> >
158 {
159 
160 public:
161 
162  typedef typename CoordinateInput<User>::scalar_t scalar_t;
163  typedef typename CoordinateInput<User>::gno_t gno_t;
164  typedef typename CoordinateInput<User>::lno_t lno_t;
165  typedef typename CoordinateInput<User>::gid_t gid_t;
166  typedef IdentifierMap<User> idmap_t;
168 
178  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
179  modelFlag_t flags);
180 
181  int getCoordinateDim() const { return coordinateDim_; }
182 
183  size_t getLocalNumCoordinates() const { return gids_.size(); }
184 
185  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
186 
187  int getCoordinateWeightDim() const { return userNumWeights_;}
188 
189  size_t getCoordinates(ArrayView<const gno_t> &Ids,
190  ArrayView<input_t> &xyz,
191  ArrayView<input_t> &wgts) const
192  {
193  xyz = xyz_.view(0, coordinateDim_);
194  wgts = weights_.view(0, userNumWeights_);
195 
196  size_t nCoord = getLocalNumCoordinates();
197  Ids = ArrayView<const gno_t>();
198 
199  if (nCoord){
200  if (gnosAreGids_)
201  Ids = Teuchos::arrayView<const gno_t>(
202  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
203  else
204  Ids = gnosConst_.view(0, nCoord);
205  }
206 
207  return nCoord;
208  }
209 
211  // The Model interface.
213 
214  size_t getLocalNumObjects() const
215  {
216  return getLocalNumCoordinates();
217  }
218 
219  size_t getGlobalNumObjects() const
220  {
221  return getGlobalNumCoordinates();
222  }
223 
224  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
225  {
226  ArrayView<input_t> xyz;
227  ArrayView<input_t> weights;
228  getCoordinates(gnos, xyz, weights);
229  }
230 
231 private:
232 
233  bool gnosAreGids_;
234  gno_t numGlobalCoordinates_;
235  const RCP<const Environment> env_;
236  const RCP<const Comm<int> > comm_;
237  int coordinateDim_;
238  ArrayRCP<const gid_t> gids_;
239  ArrayRCP<input_t> xyz_;
240  int userNumWeights_;
241  ArrayRCP<input_t> weights_;
242  ArrayRCP<gno_t> gnos_;
243  ArrayRCP<const gno_t> gnosConst_;
244 
245 };
246 
247 template <typename User>
249  const CoordinateInput<User> *ia,
250  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
251  modelFlag_t flags):
252  gnosAreGids_(false), numGlobalCoordinates_(), env_(env), comm_(comm),
253  coordinateDim_(), gids_(), xyz_(), userNumWeights_(0), weights_(),
254  gnos_(), gnosConst_()
255 {
256  bool consecutiveIds = flags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
257 
258  size_t nLocalIds = ia->getLocalNumberOfCoordinates();
259 
260  // Get coordinates and weights (if any)
261 
262  coordinateDim_ = ia->getCoordinateDimension();
263  userNumWeights_ = ia->getNumberOfWeights();
264 
265  Model<CoordinateInput<User> >::maxCount(*comm, coordinateDim_,
266  userNumWeights_);
267 
268  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
269  coordinateDim_ > 0, COMPLEX_ASSERTION);
270 
271  input_t *coordArray = new input_t [coordinateDim_];
272  input_t *weightArray = NULL;
273  if (userNumWeights_)
274  weightArray = new input_t [userNumWeights_];
275 
276  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
277  coordArray && (!userNumWeights_ || weightArray));
278 
279  Array<lno_t> arrayLengths(userNumWeights_, 0);
280 
281  if (nLocalIds){
282  for (int dim=0; dim < coordinateDim_; dim++){
283  int stride;
284  const gid_t *gids=NULL;
285  const scalar_t *coords=NULL;
286  try{
287  ia->getCoordinates(dim, gids, coords, stride);
288  }
290 
291  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
292  coordArray[dim] = input_t(cArray, stride);
293 
294  if (dim==0)
295  gids_ = arcp(gids, 0, nLocalIds, false);
296  }
297 
298  for (int dim=0; dim < userNumWeights_; dim++){
299  int stride;
300  const scalar_t *weights;
301  try{
302  ia->getCoordinateWeights(dim, weights, stride);
303  }
305 
306  if (weights){
307  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
308  weightArray[dim] = input_t(wArray, stride);
309  arrayLengths[dim] = nLocalIds;
310  }
311  }
312  }
313 
314  xyz_ = arcp(coordArray, 0, coordinateDim_);
315 
316  if (userNumWeights_)
317  weights_ = arcp(weightArray, 0, userNumWeights_);
318 
319  // Sets this->weightDim_, this->uniform_
320  this->setWeightArrayLengths(arrayLengths, *comm_);
321 
322  // Create identifier map.
323 
324  RCP<const idmap_t> idMap;
325 
326  try{
327  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIds));
328  }
330 
331  numGlobalCoordinates_ = idMap->getGlobalNumberOfIds();
332  gnosAreGids_ = idMap->gnosAreGids();
333 
334  this->setIdentifierMap(idMap);
335 
336  if (!gnosAreGids_ && nLocalIds>0){
337  gno_t *tmpGno = new gno_t [nLocalIds];
338  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
339  gnos_ = arcp(tmpGno, 0, nLocalIds);
340 
341  try{
342  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
343  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
345  }
347  }
348 
349  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
350 
351  env_->memory("After construction of coordinate model");
352 }
353 
355 // Coordinate model derived from MatrixInput.
357 
358 template <typename User>
359 class CoordinateModel<MatrixInput<User> > :
360  public Model<MatrixInput<User> >
361 {
362 public:
363 
364  typedef typename MatrixInput<User>::scalar_t scalar_t;
365  typedef typename MatrixInput<User>::gno_t gno_t;
366  typedef typename MatrixInput<User>::lno_t lno_t;
367  typedef typename MatrixInput<User>::gid_t gid_t;
368  typedef IdentifierMap<User> idmap_t;
370 
380  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
381  modelFlag_t flags);
382 
383  int getCoordinateDim() const { return coordinateDim_; }
384 
385  size_t getLocalNumCoordinates() const { return gids_.size(); }
386 
387  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
388 
389  /* \brief Weights are not implemented in MatrixInput.
390  *
391  * Whereas a computational model may create weights to use with
392  * a matrix problem, they are not inherent in the input.
393  */
394  int getCoordinateWeightDim() const { return 0; }
395 
396  size_t getCoordinates(ArrayView<const gno_t> &Ids,
397  ArrayView<input_t> &xyz,
398  ArrayView<input_t> &wgts) const
399  {
400  xyz = xyz_.view(0, coordinateDim_);
401  wgts = weights_.view(0, userNumWeights_);
402 
403  size_t nCoord = getLocalNumCoordinates();
404  Ids = ArrayView<const gno_t>();
405 
406  if (nCoord){
407  if (gnosAreGids_)
408  Ids = Teuchos::arrayView<const gno_t>(
409  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
410  else
411  Ids = gnosConst_.view(0, nCoord);
412  }
413 
414  return nCoord;
415  }
416 
418  // The Model interface.
420 
421  size_t getLocalNumObjects() const
422  {
423  return getLocalNumCoordinates();
424  }
425 
426  size_t getGlobalNumObjects() const
427  {
428  return getGlobalNumCoordinates();
429  }
430 
431  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
432  {
433  ArrayView<input_t> xyz;
434  ArrayView<input_t> weights;
435  getCoordinates(gnos, xyz, weights);
436  }
437 
438 private:
439 
440  bool gnosAreGids_;
441  gno_t numGlobalCoordinates_;
442  const RCP<const Environment> env_;
443  const RCP<const Comm<int> > comm_;
444  int coordinateDim_;
445  ArrayRCP<const gid_t> gids_;
446  ArrayRCP<input_t> xyz_;
447  int userNumWeights_;
448  ArrayRCP<input_t> weights_;
449  ArrayRCP<gno_t> gnos_;
450  ArrayRCP<const gno_t> gnosConst_;
451 };
452 
453 template <typename User>
455  const MatrixInput<User> *ia,
456  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
457  modelFlag_t flags):
458  gnosAreGids_(false), numGlobalCoordinates_(), env_(env), comm_(comm),
459  coordinateDim_(), gids_(), xyz_(), userNumWeights_(0), weights_(),
460  gnos_(), gnosConst_()
461 {
462  bool consecutiveIds = flags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
463 
464  userNumWeights_= 0; // matrix input does not have weights
465 
466  coordinateDim_ = ia->getCoordinateDimension();
467 
468  Model<MatrixInput<User> >::maxCount(*comm, coordinateDim_, userNumWeights_);
469 
470  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
471  coordinateDim_ > 0, COMPLEX_ASSERTION);
472 
473  size_t nLocalIds = (coordinateDim_ ? ia->getLocalNumRows() : 0);
474 
475  // Get coordinates
476 
477  input_t *coordArray = new input_t [coordinateDim_];
478  env_->localMemoryAssertion(__FILE__, __LINE__, coordinateDim_, coordArray);
479 
480  if (nLocalIds){
481  for (int dim=0; dim < coordinateDim_; dim++){
482  int stride;
483  const scalar_t *coords=NULL;
484  try{
485  ia->getRowCoordinates(dim, coords, stride);
486  }
488 
489  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
490  coordArray[dim] = input_t(cArray, stride);
491  }
492  }
493 
494  xyz_ = arcp(coordArray, 0, coordinateDim_);
495 
496  // Create identifier map.
497 
498  const gid_t *rowGids;
499  const lno_t *offsets;
500  const gid_t *colGids;
501 
502  ia->getRowListView(rowGids, offsets, colGids);
503 
504  gids_ = arcp<const gid_t>(rowGids, 0, nLocalIds, false);
505 
506  RCP<const idmap_t> idMap;
507 
508  try{
509  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIds));
510  }
512 
513  numGlobalCoordinates_ = idMap->getGlobalNumberOfIds();
514  gnosAreGids_ = idMap->gnosAreGids();
515 
516  this->setIdentifierMap(idMap);
517 
518  if (!gnosAreGids_ && nLocalIds>0){
519  gno_t *tmpGno = new gno_t [nLocalIds];
520  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
521  gnos_ = arcp(tmpGno, 0, nLocalIds);
522 
523  try{
524  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
525  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
527  }
529  }
530 
531  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
532 
533  env_->memory("After construction of coordinate model");
534 }
535 
537 // Coordinate model derived from VectorInput.
539 
540 template <typename User>
541 class CoordinateModel<VectorInput<User> > :
542  public Model<VectorInput<User> >
543 {
544 
545 public:
546 
547  typedef typename VectorInput<User>::scalar_t scalar_t;
548  typedef typename VectorInput<User>::gno_t gno_t;
549  typedef typename VectorInput<User>::gid_t gid_t;
550  typedef typename VectorInput<User>::lno_t lno_t;
552  typedef IdentifierMap<User> idmap_t;
553 
555  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
556  modelFlag_t flags);
557 
558  int getCoordinateDim() const { return coordinateDim_;}
559 
560  size_t getLocalNumCoordinates() const { return gids_.size();}
561 
562  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
563 
564  int getCoordinateWeightDim() const { return userNumWeights_;}
565 
566  size_t getCoordinates(ArrayView<const gno_t> &Ids,
567  ArrayView<input_t> &xyz,
568  ArrayView<input_t> &wgts) const
569  {
570  xyz = xyz_.view(0, coordinateDim_);
571  wgts = weights_.view(0, userNumWeights_);
572 
573  size_t nCoord = getLocalNumCoordinates();
574  Ids = ArrayView<const gno_t>();
575 
576  if (nCoord){
577  if (gnosAreGids_)
578  Ids = Teuchos::arrayView<const gno_t>(
579  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
580  else
581  Ids = gnosConst_.view(0, nCoord);
582  }
583 
584  return nCoord;
585  }
586 
588  // The Model interface.
590 
591  size_t getLocalNumObjects() const {return gids_.size();}
592 
593  size_t getGlobalNumObjects() const {return numGlobalCoordinates_;}
594 
595  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {
596  ArrayView<input_t> a, b;
597  getCoordinates(gnos, a, b);
598  return;
599  }
600 
601 private:
602 
603  bool gnosAreGids_;
604  gno_t numGlobalCoordinates_;
605  const RCP<const Environment> env_;
606  const RCP<const Comm<int> > comm_;
607  int coordinateDim_;
608  ArrayRCP<const gid_t> gids_;
609  ArrayRCP<input_t> xyz_;
610  int userNumWeights_;
611  ArrayRCP<input_t> weights_;
612  ArrayRCP<gno_t> gnos_;
613  ArrayRCP<const gno_t> gnosConst_;
614 };
615 
616 template <typename User>
618  const VectorInput<User> *ia,
619  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
620  modelFlag_t flags):
621  gnosAreGids_(false), numGlobalCoordinates_(), env_(env), comm_(comm),
622  coordinateDim_(), gids_(), xyz_(), userNumWeights_(0), weights_(),
623  gnos_(), gnosConst_()
624 {
625  bool consecutiveIds = flags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
626 
627  size_t nLocalIds = ia->getLocalLength();
628 
629  // Get coordinates and weights (if any)
630 
631  coordinateDim_ = ia->getNumberOfVectors();
632  userNumWeights_ = ia->getNumberOfWeights();
633 
634  Model<VectorInput<User> >::maxCount(*comm, coordinateDim_, userNumWeights_);
635 
636  env_->localBugAssertion(__FILE__, __LINE__, "coordinate dimension",
637  coordinateDim_ > 0, COMPLEX_ASSERTION);
638 
639  input_t *coordArray = new input_t [coordinateDim_];
640  input_t *weightArray = NULL;
641  if (userNumWeights_)
642  weightArray = new input_t [userNumWeights_];
643 
644  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
645  coordArray && (!userNumWeights_|| weightArray));
646 
647  Array<lno_t> arrayLengths(userNumWeights_, 0);
648 
649  if (nLocalIds){
650  for (int dim=0; dim < coordinateDim_; dim++){
651  int stride;
652  const gid_t *gids=NULL;
653  const scalar_t *coords=NULL;
654  try{
655  ia->getVector(dim, gids, coords, stride);
656  }
658 
659  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
660  coordArray[dim] = input_t(cArray, stride);
661 
662  if (dim==0)
663  gids_ = arcp(gids, 0, nLocalIds, false);
664  }
665 
666  for (int dim=0; dim < userNumWeights_; dim++){
667  int stride;
668  const scalar_t *weights;
669  try{
670  ia->getVectorWeights(dim, weights, stride);
671  }
673 
674  if (weights){
675  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
676  weightArray[dim] = input_t(wArray, stride);
677  arrayLengths[dim] = nLocalIds;
678  }
679  }
680  }
681 
682  xyz_ = arcp(coordArray, 0, coordinateDim_);
683 
684  if (userNumWeights_)
685  weights_ = arcp(weightArray, 0, userNumWeights_);
686 
687  // Sets this->weightDim_ and this->uniform_
688  this->setWeightArrayLengths(arrayLengths, *comm_);
689 
690  // Create identifier map.
691 
692  RCP<const idmap_t> idMap;
693 
694  try{
695  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIds));
696  }
698 
699  numGlobalCoordinates_ = idMap->getGlobalNumberOfIds();
700  gnosAreGids_ = idMap->gnosAreGids();
701 
702  this->setIdentifierMap(idMap);
703 
704  if (!gnosAreGids_ && nLocalIds>0){
705  gno_t *tmpGno = new gno_t [nLocalIds];
706  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
707  gnos_ = arcp(tmpGno, 0, nLocalIds);
708 
709  try{
710  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
711  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
713  }
715  }
716 
717  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
718 
719  env_->memory("After construction of coordinate model");
720 }
721 
723 // Coordinate model derived from IdentifierInput.
724 // A coordinate model can not be built from IdentifierInput.
725 // This specialization exists so that other code can compile.
727 
728 template <typename User>
729 class CoordinateModel<IdentifierInput<User> > :
730  public Model<IdentifierInput<User> >
731 {
732 public:
733 
734  typedef typename IdentifierInput<User>::scalar_t scalar_t;
735  typedef typename IdentifierInput<User>::gno_t gno_t;
736  typedef typename IdentifierInput<User>::lno_t lno_t;
738 
740  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
741  modelFlag_t flags)
742  {
743  throw logic_error(
744  "a coordinate model can not be build from identifier input");
745  }
746 
747  int getCoordinateDim() const { return 0;}
748  size_t getLocalNumCoordinates() const { return 0;}
749  global_size_t getGlobalNumCoordinates() const {return 0;}
750  int getCoordinateWeightDim() const { return 0;}
751  size_t getCoordinates(ArrayView<const gno_t> &Ids,
752  ArrayView<input_t> &xyz,
753  ArrayView<input_t> &wgts) const { return 0;}
754 
756  // The Model interface.
758 
759  size_t getLocalNumObjects() const {return 0;}
760  size_t getGlobalNumObjects() const {return 0;}
761  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {return;}
762 };
763 
764 
766 // Coordinate model derived from GraphInput.
768 
769 template <typename User>
770 class CoordinateModel<GraphInput<User> > :
771  public Model<GraphInput<User> >
772 {
773 
774 public:
775 
776  typedef typename GraphInput<User>::scalar_t scalar_t;
777  typedef typename GraphInput<User>::gno_t gno_t;
778  typedef typename GraphInput<User>::gid_t gid_t;
779  typedef typename GraphInput<User>::lno_t lno_t;
781  typedef IdentifierMap<User> idmap_t;
782 
783  CoordinateModel( const GraphInput<User> *ia,
784  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
785  modelFlag_t flags);
786 
787  int getCoordinateDim() const { return coordinateDim_;}
788 
789  size_t getLocalNumCoordinates() const { return gids_.size();}
790 
791  global_size_t getGlobalNumCoordinates() const {return numGlobalCoordinates_;}
792 
793  int getCoordinateWeightDim() const { return userNumWeights_;}
794 
795  size_t getCoordinates(ArrayView<const gno_t> &Ids,
796  ArrayView<input_t> &xyz,
797  ArrayView<input_t> &wgts) const
798  {
799  xyz = xyz_.view(0, coordinateDim_);
800  wgts = weights_.view(0, userNumWeights_);
801 
802  size_t nCoord = getLocalNumCoordinates();
803  Ids = ArrayView<const gno_t>();
804 
805  if (nCoord){
806  if (gnosAreGids_)
807  Ids = Teuchos::arrayView<const gno_t>(
808  reinterpret_cast<const gno_t *>(gids_.getRawPtr()), nCoord);
809  else
810  Ids = gnosConst_.view(0, nCoord);
811  }
812 
813  return nCoord;
814  }
815 
817  // The Model interface.
819 
820  size_t getLocalNumObjects() const {return gids_.size();}
821 
822  size_t getGlobalNumObjects() const {return numGlobalCoordinates_;}
823 
824  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {
825  ArrayView<input_t> a, b;
826  getCoordinates(gnos, a, b);
827  return;
828  }
829 
830 private:
831 
832  bool gnosAreGids_;
833  gno_t numGlobalCoordinates_;
834  const RCP<const Environment> env_;
835  const RCP<const Comm<int> > comm_;
836  int coordinateDim_;
837  ArrayRCP<const gid_t> gids_;
838  ArrayRCP<input_t> xyz_;
839  int userNumWeights_;
840  ArrayRCP<input_t> weights_;
841  ArrayRCP<gno_t> gnos_;
842  ArrayRCP<const gno_t> gnosConst_;
843 };
844 
845 template <typename User>
847  const GraphInput<User> *ia,
848  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
849  modelFlag_t flags):
850  gnosAreGids_(false), numGlobalCoordinates_(), env_(env), comm_(comm),
851  coordinateDim_(), gids_(), xyz_(), userNumWeights_(0), weights_(),
852  gnos_(), gnosConst_()
853 {
854  coordinateDim_ = ia->getCoordinateDimension();
855 
856  env->localInputAssertion(__FILE__, __LINE__,
857  "graph input does not have vertex coordinates",
858  coordinateDim_>0, BASIC_ASSERTION);
859 
860  // Get coordinates and weights (if any)
861 
862  userNumWeights_ = ia->getVertexWeightDimension();
863 
864  Model<GraphInput<User> >::maxCount(*comm, coordinateDim_, userNumWeights_);
865 
866  input_t *coordArray = new input_t [coordinateDim_];
867  input_t *weightArray = NULL;
868  if (userNumWeights_)
869  weightArray = new input_t [userNumWeights_];
870 
871  env_->localMemoryAssertion(__FILE__, __LINE__, userNumWeights_+coordinateDim_,
872  coordArray && (!userNumWeights_|| weightArray));
873 
874  Array<lno_t> arrayLengths(userNumWeights_, 0);
875 
876  size_t nLocalIds = ia->getLocalNumberOfVertices();
877 
878  if (nLocalIds){
879  const gid_t *globalIds=NULL;
880  const lno_t *offsets=NULL;
881  const gid_t *edgeIds=NULL;
882 
883  size_t numIds = ia->getVertexListView(globalIds, offsets, edgeIds);
884 
885  gids_ = arcp(globalIds, 0, nLocalIds, false);
886 
887  for (int dim=0; dim < coordinateDim_; dim++){
888  int stride;
889  const scalar_t *coords=NULL;
890  try{
891  ia->getVertexCoordinates(dim, coords, stride);
892  }
894 
895  ArrayRCP<const scalar_t> cArray(coords, 0, nLocalIds*stride, false);
896  coordArray[dim] = input_t(cArray, stride);
897  }
898 
899  for (int dim=0; dim < userNumWeights_; dim++){
900  int stride;
901  const scalar_t *weights;
902  try{
903  ia->getVertexWeights(dim, weights, stride);
904  }
906 
907  if (weights){
908  ArrayRCP<const scalar_t> wArray(weights, 0, nLocalIds*stride, false);
909  weightArray[dim] = input_t(wArray, stride);
910  arrayLengths[dim] = nLocalIds;
911  }
912  }
913  }
914 
915  xyz_ = arcp(coordArray, 0, coordinateDim_);
916 
917  if (userNumWeights_)
918  weights_ = arcp(weightArray, 0, userNumWeights_);
919 
920  // Sets this->weightDim_ and this->uniform_
921  this->setWeightArrayLengths(arrayLengths, *comm_);
922 
923  // Create identifier map.
924 
925  bool consecutiveIds = flags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
926 
927  RCP<const idmap_t> idMap;
928 
929  try{
930  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIds));
931  }
933 
934  numGlobalCoordinates_ = idMap->getGlobalNumberOfIds();
935  gnosAreGids_ = idMap->gnosAreGids();
936 
937  this->setIdentifierMap(idMap);
938 
939  if (!gnosAreGids_ && nLocalIds>0){
940  gno_t *tmpGno = new gno_t [nLocalIds];
941  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
942  gnos_ = arcp(tmpGno, 0, nLocalIds);
943 
944  try{
945  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
946  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
948  }
950  }
951 
952  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
953 
954  env_->memory("After construction of coordinate model");
955 }
956 #endif // DOXYGEN_SHOULD_SKIP_THIS
957 
958 } // namespace Zoltan2
959 
960 #endif
An IdentifierMap manages a global space of object identifiers.
void getGlobalObjectIds(ArrayView< const gno_t > &gnos) const
Get a list of the global Ids for the local objects.
global_size_t getGlobalNumCoordinates() const
Returns the global number coordinates.
Defines the VectorInput adapter interface.
fast typical checks for valid arguments
virtual size_t getVectorWeights(int dimension, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the weights corresponding to the elements returned in getVector().
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the Model interface.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
size_t global_size_t
IdentifierInput defines the interface for identifiers.
static void maxCount(const Comm< int > &comm, Array< T > &countValues)
Get the global maximum for each of an array of values.
virtual int getCoordinateDimension() const =0
Return dimension of the coordinates.
more involved, like validate a graph
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
algorithm requires consecutive ids
virtual size_t getVector(const gid_t *&ids, const scalar_t *&elements, int &stride) const =0
Provide a pointer to the vertex elements. If the VectorInput represents more than one vector...
virtual int getNumberOfWeights() const =0
Return the number of weights per vector element. Number of weights per element should be zero or grea...
Defines the IdentifierInput interface.
virtual size_t getLocalNumberOfVertices() const =0
Returns the number vertices on this process.
virtual int getNumberOfWeights() const =0
Return the number of weights per coordinate.
virtual size_t getLocalNumberOfCoordinates() const =0
Return the number of coordinates on this process.
virtual int getCoordinateDimension() const =0
Returns the dimension of the geometry, if any.
virtual size_t getVertexCoordinates(int coordDim, const scalar_t *&coords, int &stride) const =0
Provide a pointer to one dimension of vertex coordinates.
This class provides geometric coordinates with optional weights to the Zoltan2 algorithm.
convert user ids to internal ids
Defines the CoordinateInput interface.
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.
virtual int getNumberOfVectors() const =0
Return the number of vectors (typically one).
size_t getLocalNumObjects() const
Return the local number of objects.
virtual size_t getRowCoordinates(int coordDim, const scalar_t *&coords, int &stride) const =0
Provide a pointer to one dimension of row coordinates.
virtual int getCoordinateDimension() const =0
Returns the dimension of the geometry, if any.
The StridedData class manages lists of weights or coordinates.
size_t getGlobalNumObjects() const
Return the global number of objects.
virtual size_t getLocalLength() const =0
Return the length of the portion of the vector on this process.
MatrixInput defines the interface for input adapters for matrices.
virtual size_t getCoordinates(int coordDim, const gid_t *&gids, const scalar_t *&coords, int &stride) const =0
Provide a pointer to one dimension of this process&#39; coordinates.
double scalar_t
size_t getLocalNumCoordinates() const
Returns the number of coordinates on this process.
virtual size_t getVertexListView(const gid_t *&vertexIds, const lno_t *&offsets, const gid_t *&edgeIds) const =0
Sets pointers to this process&#39; graph entries.
int getCoordinateWeightDim() const
Returns the dimension (0 or greater) of coordinate weights.
The base class for all model classes.
Defines the MatrixInput adapter interface.
virtual int getVertexWeightDimension() const =0
Returns the dimension (0 or greater) of vertex weights.
virtual size_t getLocalNumRows() const =0
Returns the number rows on this process.
VectorInput defines the interface for vector input adapters.
int getCoordinateDim() const
Returns the dimension of the coordinates.
GraphInput defines the interface for graph input adapters.
void setWeightArrayLengths(const Array< lno_t > &len, const Teuchos::Comm< int > &comm)
Set the length of each weight array. The Model calls this in the constructor so we know which weights...
Defines the GraphInput interface.
virtual size_t getRowListView(const gid_t *&rowIds, const lno_t *&offsets, const gid_t *&colIds) const =0
Sets pointers to this process&#39; matrix entries. If this optional call is defined in the adapter...
CoordinateInput defines the interface for input geometric coordinates.
virtual size_t getCoordinateWeights(int weightDim, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the weights, if any, corresponding to the coordinates returned in getCoordinates...
virtual size_t getVertexWeights(int weightDim, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the vertex weights, if any.
void setIdentifierMap(RCP< const idmap_t > &map)
Set the IdentifierMap used by the model.
This file defines the StridedData class.