Zoltan2  Version of the Day
Zoltan2_IdentifierModel.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_IDENTIFIERMODEL_HPP_
51 #define _ZOLTAN2_IDENTIFIERMODEL_HPP_
52 
53 #include <Zoltan2_Model.hpp>
54 #include <Zoltan2_MatrixInput.hpp>
55 #include <Zoltan2_VectorInput.hpp>
56 #include <Zoltan2_GraphInput.hpp>
59 #include <Zoltan2_StridedData.hpp>
60 
61 namespace Zoltan2 {
62 
81 template <typename Adapter>
82 class IdentifierModel : public Model<Adapter>
83 {
84 private:
85 
86 public:
87 
88 #ifndef DOXYGEN_SHOULD_SKIP_THIS
89  typedef typename Adapter::scalar_t scalar_t;
90  typedef typename Adapter::gno_t gno_t;
91  typedef typename Adapter::lno_t lno_t;
93 #endif
94 
96  throw std::logic_error("a specific instantiation should be used");
97  }
98 
100  // The IdentifierModel interface.
102 
105  size_t getLocalNumIdentifiers() const { return 0; }
106 
110 
113  int getIdentifierWeightDim() const { return 0; }
114 
126  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
127  ArrayView<input_t> &wgts) const {return 0;}
128 
130  // The Model interface.
132 
133  size_t getLocalNumObjects() const
134  {
135  return getLocalNumIdentifiers();
136  }
137 
138  size_t getGlobalNumObjects() const
139  {
140  return getGlobalNumIdentifiers();
141  }
142 
143  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const { return ; }
144 };
145 
146 #ifndef DOXYGEN_SHOULD_SKIP_THIS
147 
149 // Identifier model derived from IdentifierInput.
151 
152 template <typename User>
153 class IdentifierModel<IdentifierInput<User> > : public Model<IdentifierInput<User> >
154 {
155 public:
156 
157  typedef typename IdentifierInput<User>::scalar_t scalar_t;
158  typedef typename IdentifierInput<User>::gno_t gno_t;
159  typedef typename IdentifierInput<User>::lno_t lno_t;
160  typedef typename IdentifierInput<User>::gid_t gid_t;
161  typedef IdentifierMap<User> idmap_t;
162  typedef StridedData<lno_t, scalar_t> input_t;
163 
172  IdentifierModel( const IdentifierInput<User> *ia,
173  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
174  modelFlag_t &modelFlags);
175 
178  size_t getLocalNumIdentifiers() const { return gids_.size(); }
179 
182  global_size_t getGlobalNumIdentifiers() const {return numGlobalIdentifiers_;}
183 
186  int getIdentifierWeightDim() const { return this->getNumWeights(); }
187 
198  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
199  ArrayView<input_t> &wgts) const
200  {
201  Ids = ArrayView<const gno_t>();
202  wgts = weights_.view(0, userWeightDim_);
203 
204  size_t n = getLocalNumIdentifiers();
205  if (n){
206  if (gnosAreGids_)
207  Ids = gids_(0, n);
208  else
209  Ids = gnosConst_(0, n);
210  }
211 
212  return n;
213  }
214 
216  // The Model interface.
218 
219  size_t getLocalNumObjects() const
220  {
221  return getLocalNumIdentifiers();
222  }
223 
224  size_t getGlobalNumObjects() const
225  {
226  return getGlobalNumIdentifiers();
227  }
228 
229  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
230  {
231  ArrayView<input_t> weights;
232  getIdentifierList(gnos, weights);
233  }
234 
235 private:
236 
237  bool gnosAreGids_;
238  gno_t numGlobalIdentifiers_;
239  const RCP<const Environment> env_;
240  const RCP<const Comm<int> > comm_;
241  ArrayRCP<const gid_t> gids_;
242  int userWeightDim_;
243  ArrayRCP<input_t> weights_;
244  ArrayRCP<gno_t> gnos_;
245  ArrayRCP<const gno_t> gnosConst_;
246 };
247 
248 template <typename User>
249  IdentifierModel<IdentifierInput<User> >::IdentifierModel(
250  const IdentifierInput<User> *ia,
251  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
252  modelFlag_t &modelFlags):
253  gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
254  gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_()
255 {
256  userWeightDim_ = ia->getNumberOfWeights();
257  size_t nLocalIds = ia->getLocalNumberOfIdentifiers();
258 
259  Model<IdentifierInput<User> >::maxCount(*comm, userWeightDim_);
260 
261  Array<const scalar_t *> wgts(userWeightDim_, (const scalar_t *)NULL);
262  Array<int> wgtStrides(userWeightDim_, 0);
263  Array<lno_t> weightArrayLengths(userWeightDim_, 0);
264 
265  if (userWeightDim_ > 0){
266  input_t *w = new input_t [userWeightDim_];
267  weights_ = arcp<input_t>(w, 0, userWeightDim_);
268  }
269 
270  const gid_t *gids=NULL;
271 
272  try{
273  ia->getIdentifierList(gids);
274  for (int dim=0; dim < userWeightDim_; dim++)
275  ia->getIdentifierWeights(dim, wgts[dim], wgtStrides[dim]);
276  }
278 
279  if (nLocalIds){
280  gids_ = arcp(gids, 0, nLocalIds, false);
281 
282  if (userWeightDim_ > 0){
283  for (int i=0; i < userWeightDim_; i++){
284  if (wgts[i] != NULL){
285  ArrayRCP<const scalar_t> wgtArray(
286  wgts[i], 0, nLocalIds*wgtStrides[i], false);
287  weights_[i] = input_t(wgtArray, wgtStrides[i]);
288  weightArrayLengths[i] = nLocalIds;
289  }
290  }
291  }
292  }
293 
294  this->setWeightArrayLengths(weightArrayLengths, *comm_);
295 
296  RCP<const idmap_t> idMap;
297 
298  try{
299  if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE))
300  idMap = rcp(new idmap_t(env_, comm_, gids_, true));
301  else
302  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
303  }
305 
306  gnosAreGids_ = idMap->gnosAreGids();
307 
308  this->setIdentifierMap(idMap);
309 
310  gno_t lsum = nLocalIds;
311  reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum,
312  &numGlobalIdentifiers_);
313 
314  if (!gnosAreGids_ && nLocalIds>0){
315  gno_t *tmpGno = new gno_t [nLocalIds];
316  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
317  gnos_ = arcp(tmpGno, 0, nLocalIds);
318 
319  try{
320  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
321  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
323  }
325  }
326 
327  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
328 
329  env_->memory("After construction of identifier model");
330 }
331 
333 // Identifier model derived from CoordinateInput.
335 
336 template <typename User>
337  class IdentifierModel<CoordinateInput<User> > :
338  public Model<CoordinateInput<User> >
339 {
340 public:
341 
342  typedef typename CoordinateInput<User>::scalar_t scalar_t;
343  typedef typename CoordinateInput<User>::gno_t gno_t;
344  typedef typename CoordinateInput<User>::lno_t lno_t;
345  typedef typename CoordinateInput<User>::gid_t gid_t;
346  typedef IdentifierMap<User> idmap_t;
347  typedef StridedData<lno_t, scalar_t> input_t;
348 
349  IdentifierModel( const CoordinateInput<User> *ia,
350  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
351  modelFlag_t &modelFlags);
352 
355  size_t getLocalNumIdentifiers() const { return gids_.size(); }
356 
359  global_size_t getGlobalNumIdentifiers() const {return numGlobalIdentifiers_;}
360 
363  int getIdentifierWeightDim() const { return this->getNumWeights();}
364 
375  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
376  ArrayView<input_t> &wgts) const
377  {
378  Ids = ArrayView<const gno_t>();
379  wgts = weights_.view(0, userWeightDim_);
380 
381  size_t n = getLocalNumIdentifiers();
382  if (n){
383  if (gnosAreGids_)
384  Ids = gids_(0, n);
385  else
386  Ids = gnosConst_(0, n);
387  }
388 
389  return n;
390  }
391 
393  // The Model interface.
395 
396  size_t getLocalNumObjects() const
397  {
398  return getLocalNumIdentifiers();
399  }
400 
401  size_t getGlobalNumObjects() const
402  {
403  return getGlobalNumIdentifiers();
404  }
405 
406  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
407  {
408  ArrayView<input_t> weights;
409  getIdentifierList(gnos, weights);
410  }
411 
412 private:
413 
414  bool gnosAreGids_;
415  gno_t numGlobalIdentifiers_;
416  const RCP<const Environment> env_;
417  const RCP<const Comm<int> > comm_;
418  ArrayRCP<const gid_t> gids_;
419  int userWeightDim_;
420  ArrayRCP<input_t> weights_;
421  ArrayRCP<gno_t> gnos_;
422  ArrayRCP<const gno_t> gnosConst_;
423 };
424 
425 template <typename User>
426  IdentifierModel<CoordinateInput<User> >::IdentifierModel(
427  const CoordinateInput<User> *ia,
428  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
429  modelFlag_t &modelFlags):
430  gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
431  gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_()
432 {
434  // Get global IDs.
435 
436  size_t nLocalIds = ia->getLocalNumberOfCoordinates();
437  const gid_t *gids=NULL;
438 
439  if (nLocalIds > 0){
440  try{
441  const scalar_t *coords;
442  int stride;
443  ia->getCoordinates(0, gids, coords, stride);
444  }
446 
447  gids_ = arcp(gids, 0, nLocalIds, false);
448  }
449 
450  RCP<const idmap_t> idMap;
451 
452  try{
453  if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE) )
454  idMap = rcp(new idmap_t(env_, comm_, gids_, true));
455  else
456  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
457  }
459 
460  numGlobalIdentifiers_ = idMap->getGlobalNumberOfIds();
461  gnosAreGids_ = idMap->gnosAreGids();
462  this->setIdentifierMap(idMap); // Base Model method
463 
465  // Get weights.
466 
467  userWeightDim_ = ia->getNumberOfWeights();
468  Array<lno_t> weightListSizes(userWeightDim_, 0);
469 
470  Model<CoordinateInput<User> >::maxCount(*comm, userWeightDim_);
471 
472  if (userWeightDim_ > 0){
473  input_t *weightObj = new input_t [userWeightDim_];
474  weights_ = arcp(weightObj, 0, userWeightDim_);
475 
476  if (nLocalIds > 0){
477  const scalar_t *wgts=NULL;
478  int stride = 0;
479  size_t wgtListSize;
480 
481  for (int wdim=0; wdim < userWeightDim_; wdim++){
482  wgtListSize = ia->getCoordinateWeights(wdim, wgts, stride);
483 
484  if (wgtListSize > 0){ // non-uniform weights
485  ArrayRCP<const scalar_t> wgtArray(wgts, 0, wgtListSize, false);
486  weightObj[wdim] = StridedData<lno_t, scalar_t>(wgtArray, stride);
487  weightListSizes[wdim] = wgtListSize;
488  }
489  }
490  }
491  }
492 
493  this->setWeightArrayLengths(weightListSizes, *comm_);
494 
496  // Get internal global numbers if necessary.
497 
498  if (!gnosAreGids_ && nLocalIds>0){
499  gno_t *tmpGno = new gno_t [nLocalIds];
500  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
501  gnos_ = arcp(tmpGno, 0, gids_.size());
502 
503  try{
504  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
505  idMap->gidTranslate(gidsNonConst(0, nLocalIds), gnos_(0, nLocalIds),
507  }
509  }
510 
511  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
512  env_->memory("After construction of identifier model");
513 }
514 
515 
517 // Identifier model derived from MatrixInput.
519 
520 template <typename User>
521 class IdentifierModel<MatrixInput<User> > : public Model<MatrixInput<User> >
522 {
523 public:
524 
525  typedef typename MatrixInput<User>::scalar_t scalar_t;
526  typedef typename MatrixInput<User>::gno_t gno_t;
527  typedef typename MatrixInput<User>::lno_t lno_t;
528  typedef typename MatrixInput<User>::gid_t gid_t;
529  typedef IdentifierMap<User> idmap_t;
530  typedef StridedData<lno_t, scalar_t> input_t;
531 
532  IdentifierModel( const MatrixInput<User> *ia,
533  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
534  modelFlag_t &modelFlags);
535 
538  size_t getLocalNumIdentifiers() const { return gids_.size(); }
539 
542  global_size_t getGlobalNumIdentifiers() const {return numGlobalIdentifiers_;}
543 
547  int getIdentifierWeightDim() const { return 0; }
548 
559  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
560  ArrayView<input_t> &wgts) const
561  {
562  Ids = ArrayView<const gno_t>();
563  wgts = weights_.view(0, userWeightDim_);
564 
565  size_t n = getLocalNumIdentifiers();
566  if (n){
567  if (gnosAreGids_)
568  Ids = gids_(0, n);
569  else
570  Ids = gnosConst_(0, n);
571  }
572 
573  return n;
574  }
575 
577  // The Model interface.
579 
580  size_t getLocalNumObjects() const
581  {
582  return getLocalNumIdentifiers();
583  }
584 
585  size_t getGlobalNumObjects() const
586  {
587  return getGlobalNumIdentifiers();
588  }
589 
590  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
591  {
592  ArrayView<input_t> weights;
593  getIdentifierList(gnos, weights);
594  }
595 
596 private:
597 
598  bool gnosAreGids_;
599  gno_t numGlobalIdentifiers_;
600  const RCP<const Environment> env_;
601  const RCP<const Comm<int> > comm_;
602  ArrayRCP<const gid_t> gids_;
603  int userWeightDim_;
604  ArrayRCP<input_t> weights_;
605  ArrayRCP<gno_t> gnos_;
606  ArrayRCP<const gno_t> gnosConst_;
607 };
608 
609 
610 template <typename User>
611  IdentifierModel<MatrixInput<User> >::IdentifierModel(
612  const MatrixInput<User> *ia,
613  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
614  modelFlag_t &modelFlags):
615  gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
616  gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_()
617 {
618  size_t nLocalIds;
619  const gid_t *gids;
620  const gid_t *colIds;
621  const lno_t *offsets;
622 
623  try{
624  nLocalIds = ia->getRowListView(gids, offsets, colIds);
625  }
627 
628  if (nLocalIds){
629  gids_ = arcp(gids, 0, nLocalIds, false);
630  }
631 
632  RCP<const idmap_t> idMap;
633 
634  try{
635  if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE) )
636  idMap = rcp(new idmap_t(env_, comm_, gids_, true));
637  else
638  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
639  }
641 
642  numGlobalIdentifiers_ = idMap->getGlobalNumberOfIds();
643  gnosAreGids_ = idMap->gnosAreGids();
644 
645  this->setIdentifierMap(idMap); // Base Model methods
646  Array<lno_t> weightListSizes;
647  this->setWeightArrayLengths(weightListSizes, *comm_);
648 
649  if (!gnosAreGids_ && nLocalIds>0){
650  gno_t *tmpGno = new gno_t [nLocalIds];
651  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
652  gnos_ = arcp(tmpGno, 0, gids_.size());
653 
654  try{
655  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
656  idMap->gidTranslate(gidsNonConst(0, nLocalIds),
657  gnos_(0, nLocalIds), TRANSLATE_APP_TO_LIB);
658  }
660  }
661 
662  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
663  env_->memory("After construction of identifier model");
664 }
665 
667 // Identifier model derived from VectorInput.
669 
670 template <typename User>
671 class IdentifierModel<VectorInput<User> > : public Model<VectorInput<User> >
672 {
673 public:
674 
675  typedef typename VectorInput<User>::scalar_t scalar_t;
676  typedef typename VectorInput<User>::gno_t gno_t;
677  typedef typename VectorInput<User>::lno_t lno_t;
678  typedef typename VectorInput<User>::gid_t gid_t;
679  typedef IdentifierMap<User> idmap_t;
680  typedef StridedData<lno_t, scalar_t> input_t;
681 
682  IdentifierModel( const VectorInput<User> *ia,
683  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
684  modelFlag_t &modelFlags);
685 
688  size_t getLocalNumIdentifiers() const { return gids_.size(); }
689 
692  global_size_t getGlobalNumIdentifiers() const {return numGlobalIdentifiers_;}
693 
697  int getIdentifierWeightDim() const { return 0; }
698 
709  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
710  ArrayView<input_t> &wgts) const
711  {
712  Ids = ArrayView<const gno_t>();
713  wgts = weights_.view(0, userWeightDim_);
714 
715  size_t n = getLocalNumIdentifiers();
716  if (n){
717  if (gnosAreGids_)
718  Ids = gids_(0, n);
719  else
720  Ids = gnosConst_(0, n);
721  }
722 
723  return n;
724  }
725 
727  // The Model interface.
729 
730  size_t getLocalNumObjects() const
731  {
732  return getLocalNumIdentifiers();
733  }
734 
735  size_t getGlobalNumObjects() const
736  {
737  return getGlobalNumIdentifiers();
738  }
739 
740  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
741  {
742  ArrayView<input_t> weights;
743  getIdentifierList(gnos, weights);
744  }
745 
746 private:
747 
748  bool gnosAreGids_;
749  gno_t numGlobalIdentifiers_;
750  const RCP<const Environment> env_;
751  const RCP<const Comm<int> > comm_;
752  ArrayRCP<const gid_t> gids_;
753  int userWeightDim_;
754  ArrayRCP<input_t> weights_;
755  ArrayRCP<gno_t> gnos_;
756  ArrayRCP<const gno_t> gnosConst_;
757 };
758 
759 
760 template <typename User>
761  IdentifierModel<VectorInput<User> >::IdentifierModel(
762  const VectorInput<User> *ia,
763  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
764  modelFlag_t &modelFlags):
765  gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
766  gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_()
767 {
768  size_t nLocalIds;
769  const gid_t *gids;
770  const scalar_t *elements;
771  int stride;
772 
773  try{
774  nLocalIds = ia->getVector(gids, elements, stride);
775  }
777 
778  if (nLocalIds){
779  gids_ = arcp(gids, 0, nLocalIds, false);
780  }
781 
782  RCP<const idmap_t> idMap;
783 
784  try{
785  if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE) )
786  idMap = rcp(new idmap_t(env_, comm_, gids_, true));
787  else
788  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
789  }
791 
792  numGlobalIdentifiers_ = idMap->getGlobalNumberOfIds();
793  gnosAreGids_ = idMap->gnosAreGids();
794 
795  this->setIdentifierMap(idMap); // Base Model methods
796  Array<lno_t> weightListSizes;
797  this->setWeightArrayLengths(weightListSizes, *comm_);
798 
799  if (!gnosAreGids_ && nLocalIds>0){
800  gno_t *tmpGno = new gno_t [nLocalIds];
801  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
802  gnos_ = arcp(tmpGno, 0, gids_.size());
803 
804  try{
805  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
806  idMap->gidTranslate(gidsNonConst(0, nLocalIds),
807  gnos_(0, nLocalIds), TRANSLATE_APP_TO_LIB);
808  }
810  }
811 
812  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
813  env_->memory("After construction of identifier model");
814 }
815 
817 // Identifier model derived from GraphInput.
819 
820 template <typename User>
821 class IdentifierModel<GraphInput<User> > : public Model<GraphInput<User> >
822 {
823 public:
824 
825  typedef typename GraphInput<User>::scalar_t scalar_t;
826  typedef typename GraphInput<User>::gno_t gno_t;
827  typedef typename GraphInput<User>::lno_t lno_t;
828  typedef typename GraphInput<User>::gid_t gid_t;
829  typedef IdentifierMap<User> idmap_t;
830  typedef StridedData<lno_t, scalar_t> input_t;
831 
840  IdentifierModel( const GraphInput<User> *ia,
841  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
842  modelFlag_t &modelFlags);
843 
846  size_t getLocalNumIdentifiers() const { return gids_.size(); }
847 
850  global_size_t getGlobalNumIdentifiers() const {return numGlobalIdentifiers_;}
851 
854  int getIdentifierWeightDim() const { return this->getNumWeights(); }
855 
866  size_t getIdentifierList(ArrayView<const gno_t> &Ids,
867  ArrayView<input_t> &wgts) const
868  {
869  Ids = ArrayView<const gno_t>();
870  wgts = weights_.view(0, userWeightDim_);
871 
872  size_t n = getLocalNumIdentifiers();
873  if (n){
874  if (gnosAreGids_)
875  Ids = gids_(0, n);
876  else
877  Ids = gnosConst_(0, n);
878  }
879 
880  return n;
881  }
882 
884  // The Model interface.
886 
887  size_t getLocalNumObjects() const
888  {
889  return getLocalNumIdentifiers();
890  }
891 
892  size_t getGlobalNumObjects() const
893  {
894  return getGlobalNumIdentifiers();
895  }
896 
897  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
898  {
899  ArrayView<input_t> weights;
900  getIdentifierList(gnos, weights);
901  }
902 
903 private:
904 
905  bool gnosAreGids_;
906  gno_t numGlobalIdentifiers_;
907  const RCP<const Environment> env_;
908  const RCP<const Comm<int> > comm_;
909  ArrayRCP<const gid_t> gids_;
910  int userWeightDim_;
911  ArrayRCP<input_t> weights_;
912  ArrayRCP<gno_t> gnos_;
913  ArrayRCP<const gno_t> gnosConst_;
914 };
915 
916 template <typename User>
917  IdentifierModel<GraphInput<User> >::IdentifierModel(
918  const GraphInput<User> *ia,
919  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
920  modelFlag_t &modelFlags):
921  gnosAreGids_(false), numGlobalIdentifiers_(), env_(env), comm_(comm),
922  gids_(), userWeightDim_(0), weights_(), gnos_(), gnosConst_()
923 {
924  userWeightDim_ = ia->getVertexWeightDimension();
925  size_t nLocalIds = ia->getLocalNumberOfVertices();
926 
927  Model<GraphInput<User> >::maxCount(*comm, userWeightDim_);
928 
929  Array<const scalar_t *> wgts(userWeightDim_, NULL);
930  Array<int> wgtStrides(userWeightDim_, 0);
931  Array<lno_t> weightArrayLengths(userWeightDim_, 0);
932 
933  if (userWeightDim_ > 0){
934  input_t *w = new input_t [userWeightDim_];
935  weights_ = arcp<input_t>(w, 0, userWeightDim_);
936  }
937 
938  const gid_t *gids=NULL;
939 
940  try{
941  const lno_t *offsets;
942  const gid_t *nbors;
943  ia->getVertexListView(gids, offsets, nbors);
944  for (int dim=0; dim < userWeightDim_; dim++)
945  ia->getVertexWeights(dim, wgts[dim], wgtStrides[dim]);
946  }
948 
949  if (nLocalIds){
950  gids_ = arcp(gids, 0, nLocalIds, false);
951 
952  if (userWeightDim_ > 0){
953  for (int i=0; i < userWeightDim_; i++){
954  if (wgts[i] != NULL){
955  ArrayRCP<const scalar_t> wgtArray(
956  wgts[i], 0, nLocalIds*wgtStrides[i], false);
957  weights_[i] = input_t(wgtArray, wgtStrides[i]);
958  weightArrayLengths[i] = nLocalIds;
959  }
960  }
961  }
962  }
963 
964  this->setWeightArrayLengths(weightArrayLengths, *comm_);
965 
966  RCP<const idmap_t> idMap;
967 
968  try{
969  if (modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE))
970  idMap = rcp(new idmap_t(env_, comm_, gids_, true));
971  else
972  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
973  }
975 
976  gnosAreGids_ = idMap->gnosAreGids();
977 
978  this->setIdentifierMap(idMap);
979 
980  gno_t lsum = nLocalIds;
981  reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum,
982  &numGlobalIdentifiers_);
983 
984  if (!gnosAreGids_ && nLocalIds>0){
985  gno_t *tmpGno = new gno_t [nLocalIds];
986  env_->localMemoryAssertion(__FILE__, __LINE__, nLocalIds, tmpGno);
987  gnos_ = arcp(tmpGno, 0, nLocalIds);
988 
989  try{
990  ArrayRCP<gid_t> gidsNonConst = arcp_const_cast<gid_t>(gids_);
991  idMap->gidTranslate( gidsNonConst(0,nLocalIds), gnos_(0,nLocalIds),
993  }
995  }
996 
997  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
998 
999  env_->memory("After construction of identifier model");
1000 }
1001 
1002 #endif // DOXYGEN_SHOULD_SKIP_THIS
1003 
1004 } // namespace Zoltan2
1005 
1006 #endif
Defines the VectorInput adapter interface.
#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
static void maxCount(const Comm< int > &comm, Array< T > &countValues)
Get the global maximum for each of an array of values.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
algorithm requires consecutive ids
void getGlobalObjectIds(ArrayView< const gno_t > &gnos) const
Get a list of the global Ids for the local objects.
Defines the IdentifierInput interface.
int getNumWeights() const
Return the number of weights supplied for each object. If the user supplied no weights, dimension one is returned, because one dimension of uniform weights is implied.
int getIdentifierWeightDim() const
Returns the dimension (0 or greater) of identifier weights.
convert user ids to internal ids
Defines the CoordinateInput interface.
The StridedData class manages lists of weights or coordinates.
size_t getLocalNumObjects() const
Return the local number of objects.
double scalar_t
The base class for all model classes.
Defines the MatrixInput adapter interface.
size_t getLocalNumIdentifiers() const
Returns the number identifiers on this process.
IdentifierModel defines the interface for all identifier models.
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...
size_t getGlobalNumObjects() const
Return the global number of objects.
Defines the GraphInput interface.
global_size_t getGlobalNumIdentifiers() const
Returns the global number identifiers.
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; identifier Ids and their weights.
void setIdentifierMap(RCP< const idmap_t > &map)
Set the IdentifierMap used by the model.
This file defines the StridedData class.