Zoltan2  Version of the Day
Zoltan2_GraphModel.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_GRAPHMODEL_HPP_
51 #define _ZOLTAN2_GRAPHMODEL_HPP_
52 
53 #include <Zoltan2_Model.hpp>
54 #include <Zoltan2_InputTraits.hpp>
55 #include <Zoltan2_MatrixInput.hpp>
56 #include <Zoltan2_GraphInput.hpp>
59 #include <Zoltan2_VectorInput.hpp>
60 #include <Zoltan2_StridedData.hpp>
61 
62 #include <vector>
63 #include <Teuchos_Hashtable.hpp>
64 
65 namespace Zoltan2 {
66 
67 
104 template <typename User> size_t removeUndesiredEdges(
105  const RCP<const Environment> &env,
106  int myRank,
107  bool removeSelfEdges,
108  bool removeOffProcessEdges,
109  bool removeOffGroupEdges,
110  ArrayView<const typename InputTraits<User>::gid_t> &gids,
111  ArrayView<const typename InputTraits<User>::gid_t> &gidNbors,
112  ArrayView<const int> &procIds,
113  ArrayView<StridedData<typename InputTraits<User>::lno_t,
114  typename InputTraits<User>::scalar_t> > &edgeWeights,
115  ArrayView<const typename InputTraits<User>::lno_t> &offsets,
116  ArrayRCP<const typename InputTraits<User>::gid_t> &newGidNbors, // out
117  typename InputTraits<User>::scalar_t **&newWeights, // out
118  ArrayRCP<const typename InputTraits<User>::lno_t> &newOffsets) // out
119 {
120  typedef typename InputTraits<User>::gid_t gid_t;
121  typedef typename InputTraits<User>::scalar_t scalar_t;
122  typedef typename InputTraits<User>::lno_t lno_t;
123  size_t numKeep = 0;
124 
125  size_t numVtx = offsets.size() - 1;
126  size_t numNbors = gidNbors.size();
127 
128  env->localInputAssertion(__FILE__, __LINE__, "need more input",
129  (!removeSelfEdges ||
130  gids.size() >=
131  static_cast<typename ArrayView<const gid_t>::size_type>(numVtx))
132  &&
133  (!removeOffProcessEdges ||
134  procIds.size() >=
135  static_cast<typename ArrayView<const int>::size_type>(numNbors)) &&
136  (!removeOffGroupEdges ||
137  procIds.size() >=
138  static_cast<typename ArrayView<const int>::size_type>(numNbors)),
140 
141  // initialize edge weight array
142 
143  newWeights = NULL;
144  int eDim = edgeWeights.size();
145  std::vector<bool> uniformWeight;
146  if (eDim > 0){
147  for (int i=0; i < eDim; i++)
148  uniformWeight[i] = (edgeWeights[i].size() == 0);
149  }
150 
151  // count desired edges
152 
153  lno_t *offs = new lno_t [numVtx + 1];
154  env->localMemoryAssertion(__FILE__, __LINE__, numVtx+1, offs);
155  ArrayRCP<const lno_t> offArray = arcp(offs, 0, numVtx+1, true);
156 
157  const lno_t *allOffs = offsets.getRawPtr();
158  const gid_t *allIds = gidNbors.getRawPtr();
159 
160  const gid_t *vtx = NULL;
161  const int *proc = NULL;
162 
163  if (gids.size() > 0)
164  vtx = gids.getRawPtr();
165 
166  if (procIds.size() > 0)
167  proc = procIds.getRawPtr();
168 
169  offs[0] = 0;
170  for (size_t i=0; i < numVtx; i++){
171  offs[i+1] = 0;
172  int vid = vtx ? vtx[i] : 0;
173  for (lno_t j=allOffs[i]; j < allOffs[i+1]; j++){
174  int owner = proc ? proc[j] : 0;
175  bool keep = (!removeSelfEdges || vid != allIds[j]) &&
176  (!removeOffProcessEdges || owner == myRank) &&
177  (!removeOffGroupEdges || owner >= 0);
178 
179  if (keep)
180  offs[i+1]++;
181  }
182  }
183 
184  // from counters to offsets
185 
186  for (size_t i=1; i < numVtx; i++)
187  offs[i+1] += offs[i];
188 
189  numKeep = offs[numVtx];
190 
191  // do we need a new neighbor list?
192 
193  if (numNbors == numKeep){
194  newGidNbors = Teuchos::arcpFromArrayView(gidNbors);
195  newOffsets = Teuchos::arcpFromArrayView(offsets);
196  return numNbors;
197  }
198  else if (numKeep == 0){
199  newGidNbors = ArrayRCP<const gid_t>(Teuchos::null);
200  newOffsets = ArrayRCP<const lno_t>(Teuchos::null);
201  return 0;
202  }
203 
204  // Build the subset neighbor lists (id, weight, and offset).
205 
206  gid_t *newGids = new gid_t [numKeep];
207  env->localMemoryAssertion(__FILE__, __LINE__, numKeep, newGids);
208 
209  newGidNbors = arcp(newGids, 0, numKeep, true);
210  newOffsets = offArray;
211 
212  if (eDim > 0){
213  newWeights = new scalar_t * [eDim];
214  env->localMemoryAssertion(__FILE__, __LINE__, eDim, newWeights);
215 
216  for (int w=0; w < eDim; w++){
217  if (uniformWeight[w])
218  newWeights[w] = NULL; // implies uniform
219  else{
220  newWeights[w] = new scalar_t [numKeep];
221  env->localMemoryAssertion(__FILE__, __LINE__, numKeep, newWeights[w]);
222  }
223  }
224  }
225 
226  size_t next = 0;
227  for (size_t i=0; i < numVtx && next < numKeep; i++){
228  int vid = vtx ? vtx[i] : 0;
229  for (lno_t j=allOffs[i]; j < allOffs[i+1]; j++){
230  int owner = proc ? proc[j] : 0;
231  bool keep = (!removeSelfEdges || vid != allIds[j]) &&
232  (!removeOffProcessEdges || owner == myRank) &&
233  (!removeOffGroupEdges || owner >= 0);
234 
235  if (keep){
236  newGids[next] = allIds[j];
237  if (eDim > 0){
238  for (int w=0; w < eDim; w++){
239  if (!uniformWeight[w])
240  newWeights[w][next] = edgeWeights[w][j];
241  }
242  }
243  next++;
244  if (next == numKeep)
245  break;
246 
247  } // if (keep)
248  }
249  }
250 
251  return numKeep;
252 }
253 
258 template <typename User> size_t computeLocalEdgeList(
259  const RCP<const Environment> &env, int rank,
260  size_t numLocalEdges, // local edges
261  size_t numLocalGraphEdges, // edges in "local" graph
262  RCP<const IdentifierMap<User> > &idMap,
263  ArrayRCP<const typename InputTraits<User>::gid_t> &allEdgeIds, // in
264  ArrayRCP<int> &allProcs, // in
265  ArrayRCP<const typename InputTraits<User>::lno_t> &allOffs, // in
266  ArrayRCP<StridedData<typename InputTraits<User>::lno_t,
267  typename InputTraits<User>::scalar_t> > &allWeights, // in
268  ArrayRCP<const typename InputTraits<User>::lno_t> &edgeLocalIds, //
269  ArrayRCP<const typename InputTraits<User>::lno_t> &offsets, // out
270  ArrayRCP<StridedData<typename InputTraits<User>::lno_t,
271  typename InputTraits<User>::scalar_t> > &eWeights) // out
272 {
273  typedef typename InputTraits<User>::gid_t gid_t;
274  typedef typename InputTraits<User>::gno_t gno_t;
275  typedef typename InputTraits<User>::scalar_t scalar_t;
276  typedef typename InputTraits<User>::lno_t lno_t;
278 
279  bool gnosAreGids = idMap->gnosAreGids();
280 
281  edgeLocalIds = ArrayRCP<const lno_t>(Teuchos::null);
282  offsets = ArrayRCP<const lno_t>(Teuchos::null);
283  eWeights = ArrayRCP<input_t>(Teuchos::null);
284 
285  if (numLocalGraphEdges == 0)
286  return 0;
287 
288  if (numLocalGraphEdges == numLocalEdges){
289 
290  // Entire graph is local.
291 
292  lno_t *lnos = new lno_t [numLocalEdges];
293  env->localMemoryAssertion(__FILE__, __LINE__,numLocalEdges, lnos);
294  for (size_t i=0; i < numLocalEdges; i++)
295  lnos[i] = i;
296  edgeLocalIds = arcp(lnos, 0, numLocalEdges, true);
297  offsets = allOffs;
298  eWeights = allWeights;
299  }
300  else{
301 
302  // Create subset list of local graph edges, offsets and weights.
303 
304  int eWeightDim = allWeights.size();
305 
306  ArrayRCP<const gid_t> newEgids;
307  scalar_t **newWeights = NULL;
308 
309  ArrayView<const gid_t> dummyVtx;
310  ArrayView<const gid_t> nborView= allEdgeIds.view(0, numLocalEdges);
311  ArrayView<const int> nborOwner = allProcs.view(0, numLocalEdges);
312  ArrayView<input_t> eWgts = allWeights.view(0, eWeightDim);
313  ArrayView<const lno_t> offView = allOffs.view(0, allOffs.size());
314 
315  try{
316  numLocalEdges = removeUndesiredEdges<User>(
317  env, rank,
318  false, true, false,
319  dummyVtx,
320  nborView,
321  nborOwner,
322  eWgts,
323  offView,
324  newEgids,
325  newWeights,
326  offsets);
327  }
329 
330  env->localBugAssertion(__FILE__, __LINE__, "local graph miscalculation",
331  numLocalEdges == numLocalGraphEdges, BASIC_ASSERTION);
332 
333  // offsets array was set by removeUndesiredEdges. Create weight array.
334 
335  if (eWeightDim > 0){
336  input_t *wgts = new input_t [eWeightDim];
337  for (int w=0; w < eWeightDim; w++){
338  if (newWeights[w]){
339  ArrayRCP<const scalar_t> wgtArray(
340  newWeights[w], 0, numLocalGraphEdges, true);
341  wgts[w] = input_t(wgtArray, 1);
342  }
343  }
344  eWeights = arcp(wgts, 0, eWeightDim);
345  }
346 
347  // Create local ID array. First translate gid to gno.
348 
349  ArrayRCP<gno_t> gnoArray;
350 
351  if (gnosAreGids){
352  ArrayRCP<const gno_t> gnosConst =
353  arcp_reinterpret_cast<const gno_t>(newEgids);
354  gnoArray = arcp_const_cast<gno_t>(gnosConst);
355  }
356  else{
357 
358  ArrayRCP<gid_t> gidArray = arcp_const_cast<gid_t>(newEgids);
359  gno_t *gnoList= new gno_t [numLocalGraphEdges];
360  env->localMemoryAssertion(__FILE__, __LINE__, numLocalGraphEdges,
361  gnoList);
362  gnoArray = arcp(gnoList, 0, numLocalGraphEdges, true);
363 
364  try {
365  idMap->gidTranslate(
366  gidArray.view(0,numLocalGraphEdges),
367  gnoArray.view(0,numLocalGraphEdges),
369  }
371  }
372 
373  // translate gno to lno
374 
375  lno_t *lnoList = new lno_t [numLocalGraphEdges];
376  env->localMemoryAssertion(__FILE__, __LINE__, numLocalGraphEdges,
377  lnoList);
378  ArrayView<lno_t> lnoView(lnoList, numLocalGraphEdges);
379 
380  try {
381  idMap->lnoTranslate(
382  lnoView,
383  gnoArray.view(0,numLocalGraphEdges),
385  }
387  edgeLocalIds = arcp<const lno_t>(lnoList, 0, numLocalGraphEdges, true);
388  }
389 
390  return numLocalGraphEdges;
391 }
392 
402 template <typename Adapter>
403 class GraphModel : public Model<Adapter>
404 {
405 public:
406 
407 #ifndef DOXYGEN_SHOULD_SKIP_THIS
408  typedef typename Adapter::scalar_t scalar_t;
409  typedef typename Adapter::gno_t gno_t;
410  typedef typename Adapter::lno_t lno_t;
412 #endif
413 
425  GraphModel(const Adapter *ia,
426  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
427  modelFlag_t &modelFlags)
428  {
429  throw std::logic_error("in non-specialized GraphModel");
430  }
431 
434  size_t getLocalNumVertices() const { return 0; }
435 
438  size_t getGlobalNumVertices() const { return 0; }
439 
443  size_t getLocalNumGlobalEdges() const { return 0; }
444 
448  size_t getLocalNumLocalEdges() const { return 0; }
449 
452  size_t getGlobalNumEdges() const { return 0; }
453 
456  int getVertexWeightDim() const { return 0; }
457 
460  int getEdgeWeightDim() const { return 0; }
461 
464  int getCoordinateDim() const { return 0; }
465 
476  size_t getVertexList( ArrayView<const gno_t> &Ids,
477  ArrayView<input_t> &xyz,
478  ArrayView<input_t> &wgts) const {return 0;}
479 
493  // Implied Vertex LNOs from getVertexList are used as indices to offsets
494  // array.
495  // Vertex GNOs are returned as neighbors in edgeIds.
496 
497  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
498  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
499  ArrayView<input_t> &wgts) const {return 0;}
500 
524  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
525  ArrayView<const lno_t> &offsets,
526  ArrayView<input_t> &wgts) {return 0;}
527 
529  // The Model interface.
531 
532  size_t getLocalNumObjects() const { return 0;}
533 
534  size_t getGlobalNumObjects() const { return 0;}
535 
536  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const { return; }
537 };
538 
539 #ifndef DOXYGEN_SHOULD_SKIP_THIS
540 
542 // Graph model derived from MatrixInput.
544 
545 template <typename User>
546 class GraphModel<MatrixInput<User> > : public Model<MatrixInput<User> >
547 {
548 public:
549 
550  typedef typename InputTraits<User>::scalar_t scalar_t;
551  typedef typename InputTraits<User>::gno_t gno_t;
552  typedef typename InputTraits<User>::lno_t lno_t;
553  typedef typename InputTraits<User>::gid_t gid_t;
555  typedef IdentifierMap<User> idmap_t;
556 
566  GraphModel(const MatrixInput<User> *ia,
567  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
568  modelFlag_t &modelFlags);
569 
571  ~GraphModel() { }
572 
573  // // // // // // // // // // // // // // // // // // // // // /
574  // The GraphModel interface.
575  // // // // // // // // // // // // // // // // // // // // // /
576 
577  size_t getLocalNumVertices() const { return numLocalVertices_; }
578 
579  size_t getGlobalNumVertices() const { return numGlobalVertices_; }
580 
581  size_t getLocalNumGlobalEdges() const { return numLocalEdges_; }
582 
583  size_t getLocalNumLocalEdges() const { return numLocalGraphEdges_; }
584 
585  size_t getGlobalNumEdges() const { return numGlobalEdges_; }
586 
587  int getVertexWeightDim() const { return vWeightDim_; }
588 
589  int getEdgeWeightDim() const { return eWeightDim_; }
590 
591  int getCoordinateDim() const { return vCoordDim_; }
592 
593  size_t getVertexList( ArrayView<const gno_t> &Ids,
594  ArrayView<input_t> &xyz, ArrayView<input_t> &wgts) const;
595 
596  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
597  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
598  ArrayView<input_t> &wgts) const;
599 
600  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
601  ArrayView<const lno_t> &offsets,
602  ArrayView<input_t> &wgts){
603 
604  if (localGraphEdgeLnos_.size() <
605  static_cast<typename ArrayRCP<const lno_t>::size_type>(numLocalGraphEdges_)){
606 
607  RCP<const IdentifierMap<User> > idmap = this->getIdentifierMap();
608 
609  computeLocalEdgeList<User>(env_, comm_->getRank(),
610  numLocalEdges_, numLocalGraphEdges_,
611  idmap, edgeGids_, procIds_, offsets_, eWeights_,
612  localGraphEdgeLnos_, localGraphEdgeOffsets_, localGraphEdgeWeights_);
613  }
614 
615  edgeIds = localGraphEdgeLnos_();
616  offsets = localGraphEdgeOffsets_();
617  wgts = localGraphEdgeWeights_();
618 
619  return numLocalGraphEdges_;
620  }
621 
623  // The Model interface.
625 
626  size_t getLocalNumObjects() const { return numLocalVertices_; }
627 
628  size_t getGlobalNumObjects() const { return numGlobalVertices_; }
629 
630  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
631  {
632  ArrayView<input_t> xyz, wgts;
633  getVertexList(gnos, xyz, wgts);
634  }
635 
636 private:
637 
638  const RCP<const Environment > env_;
639  const RCP<const Comm<int> > comm_;
640 
641  ArrayRCP<const gid_t> gids_; // rows
642  ArrayRCP<gno_t> gnos_;
643 
644  int vWeightDim_;
645  ArrayRCP<input_t> vWeights_;
646 
647  int vCoordDim_;
648  ArrayRCP<input_t> vCoords_;
649 
650  // Note: in case of graph subsetting, size of these arrays
651  // may be larger than numLocalEdges_. So do not use .size().
652 
653  ArrayRCP<const gid_t> edgeGids_;
654  ArrayRCP<gno_t> edgeGnos_;
655  ArrayRCP<int> procIds_;
656  ArrayRCP<const lno_t> offsets_;
657 
658  int eWeightDim_;
659  ArrayRCP<input_t> eWeights_;
660 
661  ArrayRCP<const gno_t> gnosConst_;
662  ArrayRCP<const gno_t> edgeGnosConst_;
663  ArrayRCP<const int> procIdsConst_;
664 
665  bool gnosAreGids_;
666 
667  // For local graphs (graph restricted to local process). We only
668  // create these arrays if required by the algorithm.
669 
670  ArrayRCP<const lno_t> localGraphEdgeLnos_;
671  ArrayRCP<const lno_t> localGraphEdgeOffsets_;
672  ArrayRCP<input_t> localGraphEdgeWeights_;
673 
674  // For convenience
675 
676  size_t numLocalVertices_;
677  size_t numGlobalVertices_;
678  size_t numLocalEdges_;
679  size_t numGlobalEdges_;
680  size_t numLocalGraphEdges_;
681 };
682 
683 template <typename User>
685  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
686  modelFlag_t &modelFlags):
687  env_(env), comm_(comm),
688  gids_(), gnos_(),
689  vWeightDim_(0), vWeights_(),
690  vCoordDim_(0), vCoords_(),
691  edgeGids_(), edgeGnos_(), procIds_(), offsets_(),
692  eWeightDim_(0), eWeights_(),
693  gnosConst_(), edgeGnosConst_(), procIdsConst_(),
694  gnosAreGids_(false),
695  localGraphEdgeLnos_(), localGraphEdgeOffsets_(), localGraphEdgeWeights_(),
696  numLocalVertices_(0), numGlobalVertices_(0), numLocalEdges_(0),
697  numGlobalEdges_(0), numLocalGraphEdges_(0)
698 {
699  // Model creation flags
700 
701  bool symTranspose = modelFlags.test(SYMMETRIZE_INPUT_TRANSPOSE);
702  bool symBipartite = modelFlags.test(SYMMETRIZE_INPUT_BIPARTITE);
703  bool vertexCols = modelFlags.test(VERTICES_ARE_MATRIX_COLUMNS);
704  bool vertexNz = modelFlags.test(VERTICES_ARE_MATRIX_NONZEROS);
705  bool consecutiveIdsRequired =
706  modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
707  bool removeSelfEdges = modelFlags.test(SELF_EDGES_MUST_BE_REMOVED);
708  bool subsetGraph = modelFlags.test(GRAPH_IS_A_SUBSET_GRAPH);
709 
710  if (symTranspose || symBipartite || vertexCols || vertexNz){
711  throw std::runtime_error("graph build option not yet implemented");
712  }
713 
714  // Get the matrix from the input adapter
715 
716  gid_t const *vtxIds=NULL, *nborIds=NULL;
717  lno_t const *offsets=NULL;
718  try{
719  numLocalVertices_ = ia->getRowListView(vtxIds, offsets, nborIds);
720  }
722 
723  numLocalEdges_ = offsets[numLocalVertices_];
724 
725  gids_ = arcp<const gid_t>(vtxIds, 0, numLocalVertices_, false);
726  edgeGids_ = arcp<const gid_t>(nborIds, 0, numLocalEdges_, false);
727  offsets_ = arcp<const lno_t>(offsets, 0, numLocalVertices_ + 1, false);
728 
729  eWeightDim_ = 0; // no edge weights from a matrix
730 
731  // A subset graph is special in that it may contain neighbor
732  // vertices that are not owned by processes in this communicator.
733  // We remove these.
734 
735  ArrayRCP<const int> nborProcs;
736 
737  if (subsetGraph){
738  RCP<const idmap_t> idMap;
739  try{
740  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
741  }
743 
744  ArrayRCP<int> procArray;
745 
746  if (numLocalEdges_ > 0){
747  int *pids = new int [numLocalEdges_];
748  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, pids);
749  procArray = arcp(pids, 0, numLocalEdges_, true);
750  }
751 
752  ArrayView<gno_t> dummyGno;
753 
754  try{
755  // All processes must make this call.
756  // procOwner will be -1 if edge Id is not in our communicator.
757 
758  idMap->gidGlobalTranslate( edgeGids_.view(0, numLocalEdges_),
759  dummyGno, procArray.view(0, numLocalEdges_));
760  }
762 
763  int outOfSubset = 0;
764  for (size_t i=0; i < numLocalEdges_; i++){
765  if (procArray[i] < 0){
766  outOfSubset++;
767  break;
768  }
769  }
770 
771  if (outOfSubset == 0){
772  procArray.clear();
773  subsetGraph = false;
774  }
775  else{
776  nborProcs = arcp_const_cast<const int>(procArray);
777  }
778  }
779 
780  // Now remove undesired edges.
781 
782  if (subsetGraph || removeSelfEdges){
783 
784  ArrayRCP<input_t> noEdgeWeights;
785  ArrayRCP<const gid_t> newEdges;
786  ArrayRCP<const lno_t> newOffs;
787  scalar_t **newWeights = NULL;
788  size_t numNewEdges = 0;
789 
790  // Compiler complained of an error if gids_.view(0, n), etc
791  // was listed directly as a parameter in removeUndesiredEdges.
792  // So we have to create the ArraView before before the call.
793 
794  ArrayView<const gid_t> vtxView= gids_.view(0, numLocalVertices_);
795  ArrayView<const gid_t> nborView= edgeGids_.view(0, numLocalEdges_);
796  ArrayView<const int> nborOwner = nborProcs.view(0, nborProcs.size());
797  ArrayView<input_t> eWgts = noEdgeWeights.view(0, 0);
798  ArrayView<const lno_t> offView = offsets_.view(0, numLocalVertices_ + 1);
799 
800  try{
801  numNewEdges = Zoltan2::removeUndesiredEdges<User>(env_, comm_->getRank(),
802  removeSelfEdges,
803  false,
804  subsetGraph,
805  vtxView,
806  nborView,
807  nborOwner,
808  eWgts,
809  offView,
810  newEdges,
811  newWeights,
812  newOffs);
813  }
815 
816  nborProcs.clear();
817 
818  if (numNewEdges < numLocalEdges_){
819  edgeGids_ = newEdges;
820  offsets_ = newOffs;
821  numLocalEdges_ = numNewEdges;
822  }
823  }
824 
825  // Create an IdentifierMap, which will map the user's global IDs to
826  // Zoltan2 internal global numbers if necessary.
827  // The map can also give us owners of our vertex neighbors.
828 
829  RCP<const idmap_t> idMap;
830 
831  try{
832  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIdsRequired));
833  }
835 
836  // Model base class needs to have IdentifierMap.
837 
838  this->setIdentifierMap(idMap);
839 
840  numGlobalVertices_ = idMap->getGlobalNumberOfIds();
841  gnosAreGids_ = idMap->gnosAreGids();
842 
843  // Compute internal global numbers if we can not use the
844  // user's global Ids. Also find the process owning each
845  // neighboring vertex.
846 
847  ArrayView<const gid_t> gidArray(Teuchos::null); // edge gid
848  ArrayView<gno_t> gnoArray(Teuchos::null); // edge gno
849  ArrayView<int> procArray(Teuchos::null); // edge owner
850 
851  if (numLocalVertices_){
852 
853  if (!gnosAreGids_){ // need vertex global numbers, edge global numbers
854  gno_t *tmp = new gno_t [numLocalVertices_];
855  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalVertices_, tmp);
856  gnos_ = arcp(tmp, 0, numLocalVertices_);
857 
858  try{
859  ArrayRCP<gid_t> tmpGids = arcp_const_cast<gid_t>(gids_);
860 
861  idMap->gidTranslate(tmpGids(0,numLocalVertices_),
862  gnos_(0,numLocalVertices_), TRANSLATE_APP_TO_LIB);
863  }
865 
866  if (numLocalEdges_){
867  tmp = new gno_t [numLocalEdges_];
868  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, tmp);
869  edgeGnos_ = arcp(tmp, 0, numLocalEdges_);
870  gnoArray = edgeGnos_.view(0, numLocalEdges_);
871  }
872  }
873 
874  if (numLocalEdges_){
875  gidArray = edgeGids_.view(0, numLocalEdges_);
876 
877  int *p = new int [numLocalEdges_];
878  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, p);
879  procIds_ = arcp(p, 0, numLocalEdges_);
880  procArray = procIds_.view(0, numLocalEdges_);
881  }
882  }
883 
884  try{
885  // All processes must make this call.
886  idMap->gidGlobalTranslate(gidArray, gnoArray, procArray);
887  }
889 
890  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
891  edgeGnosConst_ = arcp_const_cast<const gno_t>(edgeGnos_);
892  procIdsConst_ = arcp_const_cast<const int>(procIds_);
893 
894  // Number of edges such that neighbor is on the local process.
895  // We only create the list of local graph edges if the user
896  // calls getLocalEdgeList().
897 
898  numLocalGraphEdges_ = 0;
899  int *pids = procArray.getRawPtr();
900  for (size_t i=0; i < numLocalEdges_; i++)
901  if (pids[i] == comm_->getRank())
902  numLocalGraphEdges_++;
903 
904  // Vertex weights
905 
906  vWeightDim_ = ia->getRowWeightDimension();
907 
908  if (vWeightDim_ > 0){
909  input_t *weightInfo = new input_t [vWeightDim_];
910  env_->localMemoryAssertion(__FILE__, __LINE__, vWeightDim_, weightInfo);
911 
912  for (int dim=0; dim < vWeightDim_; dim++){
913  bool useNumNZ = ia->getRowWeightIsNumberOfNonZeros(dim);
914  if (useNumNZ){
915  scalar_t *wgts = new scalar_t [numLocalVertices_];
916  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalVertices_, wgts);
917  ArrayRCP<const scalar_t> wgtArray =
918  arcp(wgts, 0, numLocalVertices_, true);
919  for (size_t i=0; i < numLocalVertices_; i++){
920  wgts[i] = offsets_[i+1] - offsets_[i];
921  }
922  weightInfo[dim] = input_t(wgtArray, 1);
923  }
924  else{
925  const scalar_t *weights=NULL;
926  int stride=0;
927  size_t len = ia->getRowWeights(dim, weights, stride);
928  // If weights is NULL, user wants to use uniform weights
929  if (weights != NULL){
930  ArrayRCP<const scalar_t> wgtArray = arcp(weights, 0, len, false);
931  weightInfo[dim] = input_t(wgtArray, stride);
932  }
933  }
934  }
935 
936  vWeights_ = arcp<input_t>(weightInfo, 0, vWeightDim_, true);
937  }
938 
939  // Model base class needs to know if any weights are uniform.
940 
941  Array<lno_t> weightArrayLengths(vWeightDim_);
942  for (int dim=0; dim < vWeightDim_; dim++){
943  weightArrayLengths[dim] = vWeights_[dim].size();
944  }
945  this->setWeightArrayLengths(weightArrayLengths, *comm_);
946 
947  // Vertex coordinates
948 
949  vCoordDim_ = ia->getCoordinateDimension();
950 
951  if (vCoordDim_ > 0){
952  input_t *coordInfo = new input_t [vCoordDim_];
953  env_->localMemoryAssertion(__FILE__, __LINE__, vCoordDim_, coordInfo);
954 
955  for (int dim=0; dim < vCoordDim_; dim++){
956  const scalar_t *coords=NULL;
957  int stride=0;
958  size_t len = ia->getRowCoordinates(dim, coords, stride);
959  ArrayRCP<const scalar_t> coordArray = arcp(coords, 0, len, false);
960  coordInfo[dim] = input_t(coordArray, stride);
961  }
962 
963  vCoords_ = arcp<input_t>(coordInfo, 0, vCoordDim_, true);
964  }
965 
966  reduceAll<int, size_t>(*comm_, Teuchos::REDUCE_SUM, 1,
967  &numLocalEdges_, &numGlobalEdges_);
968 
969  env_->memory("After construction of graph model");
970 }
971 
972 template <typename User>
974  ArrayView<const gno_t> &Ids, ArrayView<input_t> &xyz,
975  ArrayView<input_t> &wgts) const
976  {
977  size_t nv = gids_.size();
978 
979  if (gnosAreGids_)
980  Ids = gids_.view(0, nv);
981  else
982  Ids = gnosConst_.view(0, nv);
983 
984  xyz = vCoords_.view(0, vWeightDim_);
985  wgts = vWeights_.view(0, vCoordDim_);
986 
987  return nv;
988  }
989 
990 template <typename User>
992  ArrayView<const gno_t> &edgeIds, ArrayView<const int> &procIds,
993  ArrayView<const lno_t> &offsets, ArrayView<input_t> &wgts) const
994 {
995  if (gnosAreGids_)
996  edgeIds = edgeGids_.view(0, numLocalEdges_);
997  else
998  edgeIds = edgeGnosConst_.view(0, numLocalEdges_);
999 
1000  procIds = procIdsConst_.view(0, numLocalEdges_);
1001  offsets = offsets_.view(0, numLocalVertices_+1);
1002  wgts = eWeights_.view(0, eWeightDim_);
1003 
1004  return numLocalEdges_;
1005 }
1006 
1008 // Graph model derived from GraphInput.
1010 
1011 template <typename User>
1012 class GraphModel<GraphInput<User> > : public Model<GraphInput<User> >
1013 {
1014 public:
1015 
1016  typedef typename GraphInput<User>::scalar_t scalar_t;
1017  typedef typename GraphInput<User>::gno_t gno_t;
1018  typedef typename GraphInput<User>::lno_t lno_t;
1019  typedef typename GraphInput<User>::gid_t gid_t;
1020  typedef typename GraphInput<User>::node_t node_t;
1021  typedef IdentifierMap<User> idmap_t;
1023 
1033  GraphModel(const GraphInput<User> *ia,
1034  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
1035  modelFlag_t &modelFlags);
1036 
1038  ~GraphModel() { }
1039 
1040  // // // // // // // // // // // // // // // // // // // // // /
1041  // The GraphModel interface.
1042  // // // // // // // // // // // // // // // // // // // // // /
1043 
1044  size_t getLocalNumVertices() const { return numLocalVertices_; }
1045 
1046  size_t getGlobalNumVertices() const { return numGlobalVertices_; }
1047 
1048  size_t getLocalNumGlobalEdges() const { return numLocalEdges_; }
1049 
1050  size_t getLocalNumLocalEdges() const { return numLocalGraphEdges_; }
1051 
1052  size_t getGlobalNumEdges() const { return numGlobalEdges_; }
1053 
1054  int getVertexWeightDim() const { return vWeightDim_; }
1055 
1056  int getEdgeWeightDim() const { return eWeightDim_; }
1057 
1058  int getCoordinateDim() const { return vCoordDim_; }
1059 
1060  size_t getVertexList( ArrayView<const gno_t> &Ids,
1061  ArrayView<input_t> &xyz, ArrayView<input_t> &wgts) const;
1062 
1063  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
1064  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
1065  ArrayView<input_t> &wgts) const;
1066 
1067  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
1068  ArrayView<const lno_t> &offsets,
1069  ArrayView<input_t> &wgts){
1070 
1071  if (localGraphEdgeLnos_.size() < numLocalGraphEdges_){
1072 
1073  RCP<const IdentifierMap<User> > idmap = this->getIdentifierMap();
1074 
1075  computeLocalEdgeList(env_, comm_->getRank(),
1076  numLocalEdges_, numLocalGraphEdges_,
1077  idmap, edgeGids_, procIds_, offsets_, eWeights_,
1078  localGraphEdgeLnos_, localGraphEdgeOffsets_, localGraphEdgeWeights_);
1079  }
1080 
1081  edgeIds = localGraphEdgeLnos_;
1082  offsets = localGraphEdgeOffsets_;
1083  wgts = localGraphEdgeWeights_;
1084 
1085  return numLocalGraphEdges_;
1086  }
1087 
1089  // The Model interface.
1091 
1092  size_t getLocalNumObjects() const { return numLocalVertices_; }
1093 
1094  size_t getGlobalNumObjects() const { return numGlobalVertices_; }
1095 
1096  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const
1097  {
1098  ArrayView<input_t> xyz, wgts;
1099  getVertexList(gnos, xyz, wgts);
1100  }
1101 
1102 private:
1103 
1104  const RCP<const Environment > env_;
1105  const RCP<const Comm<int> > comm_;
1106 
1107  ArrayRCP<const gid_t> gids_; // vertices of input graph
1108  ArrayRCP<gno_t> gnos_;
1109 
1110  int vWeightDim_;
1111  ArrayRCP<input_t> vWeights_;
1112 
1113  int vCoordDim_;
1114  ArrayRCP<input_t> vCoords_;
1115 
1116  // Note: in case of graph subsetting, size of these arrays
1117  // may be larger than numLocalEdges_. So do not use .size().
1118 
1119  ArrayRCP<const gid_t> edgeGids_;
1120  ArrayRCP<gno_t> edgeGnos_;
1121  ArrayRCP<int> procIds_;
1122  ArrayRCP<const lno_t> offsets_;
1123 
1124  int eWeightDim_;
1125  ArrayRCP<input_t> eWeights_;
1126 
1127  ArrayRCP<const gno_t> gnosConst_;
1128  ArrayRCP<const gno_t> edgeGnosConst_;
1129  ArrayRCP<const int> procIdsConst_;
1130 
1131  bool gnosAreGids_;
1132 
1133  // For local graphs (graph restricted to local process). We only
1134  // create these arrays if required by the algorithm.
1135 
1136  ArrayRCP<const lno_t> localGraphEdgeLnos_;
1137  ArrayRCP<const lno_t> localGraphEdgeOffsets_;
1138  ArrayRCP<input_t> localGraphEdgeWeights_;
1139 
1140  // For convenience
1141 
1142  size_t numLocalVertices_;
1143  size_t numGlobalVertices_;
1144  size_t numLocalEdges_;
1145  size_t numGlobalEdges_;
1146  size_t numLocalGraphEdges_;
1147 
1148 };
1149 
1150 template <typename User>
1152  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
1153  modelFlag_t &modelFlags):
1154  env_(env), comm_(comm),
1155  gids_(), gnos_(),
1156  vWeightDim_(0), vWeights_(),
1157  vCoordDim_(0), vCoords_(),
1158  edgeGids_(), edgeGnos_(), procIds_(), offsets_(),
1159  eWeightDim_(0), eWeights_(),
1160  gnosConst_(), edgeGnosConst_(), procIdsConst_(),
1161  gnosAreGids_(false),
1162  localGraphEdgeLnos_(), localGraphEdgeOffsets_(), localGraphEdgeWeights_(),
1163  numLocalVertices_(0), numGlobalVertices_(0), numLocalEdges_(0),
1164  numGlobalEdges_(0), numLocalGraphEdges_(0)
1165 {
1166  // Model creation flags
1167 
1168  bool consecutiveIdsRequired =
1169  modelFlags.test(IDS_MUST_BE_GLOBALLY_CONSECUTIVE);
1170  bool removeSelfEdges = modelFlags.test(SELF_EDGES_MUST_BE_REMOVED);
1171  bool subsetGraph = modelFlags.test(GRAPH_IS_A_SUBSET_GRAPH);
1172 
1173  // Get the graph from the input adapter
1174 
1175  gid_t const *vtxIds=NULL, *nborIds=NULL;
1176  lno_t const *offsets=NULL;
1177  try{
1178  numLocalVertices_ = ia->getVertexListView(vtxIds, offsets, nborIds);
1179  }
1181 
1182  numLocalEdges_ = offsets[numLocalVertices_];
1183 
1184  gids_ = arcp<const gid_t>(vtxIds, 0, numLocalVertices_, false);
1185  edgeGids_ = arcp<const gid_t>(nborIds, 0, numLocalEdges_, false);
1186  offsets_ = arcp<const lno_t>(offsets, 0, numLocalVertices_ + 1, false);
1187 
1188  eWeightDim_ = ia->getEdgeWeightDimension();
1189 
1190  if (eWeightDim_ > 0){
1191  input_t *wgts = new input_t [eWeightDim_];
1192  eWeights_ = arcp(wgts, 0, eWeightDim_, true);
1193  }
1194 
1195  for (int w=0; w < eWeightDim_; w++){
1196  const scalar_t *ewgts=NULL;
1197  int stride=0;
1198 
1199  ia->getEdgeWeights(w, ewgts, stride);
1200 
1201  ArrayRCP<const scalar_t> wgtArray(ewgts, 0, numLocalEdges_, false);
1202  eWeights_[w] = input_t(wgtArray, stride);
1203  }
1204 
1205  // A subset graph is special in that it may contain neighbor
1206  // vertices that are not owned by processes in this communicator.
1207  // We remove these.
1208 
1209  ArrayRCP<const int> nborProcs;
1210 
1211  if (subsetGraph){
1212  RCP<const idmap_t> idMap;
1213  try{
1214  idMap = rcp(new idmap_t(env_, comm_, gids_, false));
1215  }
1217 
1218  ArrayRCP<int> procArray;
1219 
1220  if (numLocalEdges_ > 0){
1221  int *pids = new int [numLocalEdges_];
1222  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, pids);
1223  procArray = arcp(pids, 0, numLocalEdges_, true);
1224  }
1225 
1226  ArrayView<gno_t> dummyGno;
1227 
1228  try{
1229  // All processes must make this call.
1230  // procOwner will be -1 if edge Id is not in our communicator.
1231 
1232  idMap->gidGlobalTranslate( edgeGids_.view(0, numLocalEdges_),
1233  dummyGno, procArray.view(0, numLocalEdges_));
1234  }
1236 
1237  int outOfSubset = 0;
1238  for (size_t i=0; i < numLocalEdges_; i++){
1239  if (procArray[i] < 0){
1240  outOfSubset++;
1241  break;
1242  }
1243  }
1244 
1245  if (outOfSubset == 0){
1246  procArray.clear();
1247  subsetGraph = false;
1248  }
1249  else{
1250  nborProcs = arcp_const_cast<const int>(procArray);
1251  }
1252  }
1253 
1254  // Now remove undesired edges.
1255 
1256  if (subsetGraph || removeSelfEdges){
1257 
1258  ArrayRCP<const gid_t> newEdges;
1259  ArrayRCP<const lno_t> newOffs;
1260  scalar_t **newWeights = NULL;
1261  size_t numNewEdges = 0;
1262 
1263  ArrayView<const gid_t> vtxView = gids_.view(0, numLocalVertices_);
1264  ArrayView<const gid_t> nborView= edgeGids_.view(0, numLocalEdges_);
1265  ArrayView<const int> nborOwner = nborProcs.view(0, nborProcs.size());
1266  ArrayView<input_t> eWgts = eWeights_.view(0, eWeightDim_);
1267  ArrayView<const lno_t> offView = offsets_.view(0, numLocalVertices_ + 1);
1268 
1269  try{
1270  numNewEdges = removeUndesiredEdges<User>(env_, comm_->getRank(),
1271  removeSelfEdges,
1272  false,
1273  subsetGraph,
1274  vtxView,
1275  nborView,
1276  nborOwner,
1277  eWgts,
1278  offView,
1279  newEdges,
1280  newWeights,
1281  newOffs);
1282  }
1284 
1285  nborProcs.clear();
1286 
1287  if (numNewEdges < numLocalEdges_){
1288  edgeGids_ = newEdges;
1289  offsets_ = newOffs;
1290  numLocalEdges_ = numNewEdges;
1291 
1292  for (int w=0; w < eWeightDim_; w++){
1293  if (newWeights[w] != NULL){ // non-uniform weights
1294  ArrayRCP<const scalar_t> wgtArray(newWeights[w],
1295  0, numNewEdges, true);
1296  eWeights_[w] = input_t(wgtArray, 1);
1297  }
1298  }
1299  }
1300  }
1301 
1302  // Create an IdentifierMap, which will map the user's global IDs to
1303  // Zoltan2 internal global numbers if necessary.
1304  // The map can also give us owners of our vertex neighbors.
1305 
1306  RCP<const idmap_t> idMap;
1307 
1308  try{
1309  idMap = rcp(new idmap_t(env_, comm_, gids_, consecutiveIdsRequired));
1310  }
1312 
1313  // Model base class needs to have IdentifierMap.
1314 
1315  this->setIdentifierMap(idMap);
1316 
1317  numGlobalVertices_ = idMap->getGlobalNumberOfIds();
1318  gnosAreGids_ = idMap->gnosAreGids();
1319 
1320  // Compute internal global numbers if we can not use the
1321  // user's global Ids. Also find the process owning each
1322  // neighboring vertex.
1323 
1324  ArrayView<const gid_t> gidArray(Teuchos::null); // edge gid
1325  ArrayView<gno_t> gnoArray(Teuchos::null); // edge gno
1326  ArrayView<int> procArray(Teuchos::null); // edge owner
1327 
1328  if (numLocalVertices_){
1329 
1330  if (!gnosAreGids_){ // need vertex global numbers, edge global numbers
1331  gno_t *tmp = new gno_t [numLocalVertices_];
1332  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalVertices_, tmp);
1333  gnos_ = arcp(tmp, 0, numLocalVertices_);
1334 
1335  try{
1336  ArrayRCP<gid_t> tmpGids = arcp_const_cast<gid_t>(gids_);
1337 
1338  idMap->gidTranslate(tmpGids(0,numLocalVertices_),
1339  gnos_(0,numLocalVertices_), TRANSLATE_APP_TO_LIB);
1340  }
1342 
1343  if (numLocalEdges_){
1344  tmp = new gno_t [numLocalEdges_];
1345  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, tmp);
1346  edgeGnos_ = arcp(tmp, 0, numLocalEdges_);
1347  gnoArray = edgeGnos_.view(0, numLocalEdges_);
1348  }
1349  }
1350 
1351  if (numLocalEdges_){
1352  gidArray = edgeGids_.view(0, numLocalEdges_);
1353 
1354  int *p = new int [numLocalEdges_];
1355  env_->localMemoryAssertion(__FILE__, __LINE__, numLocalEdges_, p);
1356  procIds_ = arcp(p, 0, numLocalEdges_);
1357  procArray = procIds_.view(0, numLocalEdges_);
1358  }
1359  }
1360 
1361  try{
1362  // All processes must make this call.
1363  idMap->gidGlobalTranslate(gidArray, gnoArray, procArray);
1364  }
1366 
1367  gnosConst_ = arcp_const_cast<const gno_t>(gnos_);
1368  edgeGnosConst_ = arcp_const_cast<const gno_t>(edgeGnos_);
1369  procIdsConst_ = arcp_const_cast<const int>(procIds_);
1370 
1371  // Number of edges such that neighbor is on the local process.
1372  // We only create the list of local graph edges if the user
1373  // calls getLocalEdgeList().
1374 
1375  numLocalGraphEdges_ = 0;
1376  int *pids = procArray.getRawPtr();
1377  for (lno_t i=0; i < numLocalEdges_; i++)
1378  if (pids[i] == comm_->getRank())
1379  numLocalGraphEdges_++;
1380 
1381  // Vertex weights
1382 
1383  vWeightDim_ = ia->getVertexWeightDimension();
1384 
1385  if (vWeightDim_ > 0){
1386  input_t *weightInfo = new input_t [vWeightDim_];
1387  env_->localMemoryAssertion(__FILE__, __LINE__, vWeightDim_, weightInfo);
1388 
1389  for (int dim=0; dim < vWeightDim_; dim++){
1390  const scalar_t *weights=NULL;
1391  int stride=0;
1392  size_t len = ia->getVertexWeights(dim, weights, stride);
1393  // If weights is NULL, user wants to use uniform weights
1394  if (weights != NULL){
1395  ArrayRCP<const scalar_t> wgtArray = arcp(weights, 0, len, false);
1396  weightInfo[dim] = input_t(wgtArray, stride);
1397  }
1398  }
1399 
1400  vWeights_ = arcp<input_t>(weightInfo, 0, vWeightDim_, true);
1401  }
1402 
1403  // Model base class needs to know if any weights are uniform.
1404 
1405  Array<lno_t> weightArrayLengths(vWeightDim_);
1406  for (int dim=0; dim < vWeightDim_; dim++){
1407  weightArrayLengths[dim] = vWeights_[dim].size();
1408  }
1409  this->setWeightArrayLengths(weightArrayLengths, *comm_);
1410 
1411  // Vertex coordinates
1412 
1413  vCoordDim_ = ia->getCoordinateDimension();
1414 
1415  if (vCoordDim_ > 0){
1416  input_t *coordInfo = new input_t [vCoordDim_];
1417  env_->localMemoryAssertion(__FILE__, __LINE__, vCoordDim_, coordInfo);
1418 
1419  for (int dim=0; dim < vCoordDim_; dim++){
1420  const scalar_t *coords=NULL;
1421  int stride=0;
1422  size_t len = ia->getVertexCoordinates(dim, coords, stride);
1423  ArrayRCP<const scalar_t> coordArray = arcp(coords, 0, len, false);
1424  coordInfo[dim] = input_t(coordArray, stride);
1425  }
1426 
1427  vCoords_ = arcp<input_t>(coordInfo, 0, vCoordDim_, true);
1428  }
1429 
1430  reduceAll<int, size_t>(*comm_, Teuchos::REDUCE_SUM, 1,
1431  &numLocalEdges_, &numGlobalEdges_);
1432 
1433  env_->memory("After construction of graph model");
1434 }
1435 
1436 template <typename User>
1438  ArrayView<const gno_t> &Ids, ArrayView<input_t> &xyz,
1439  ArrayView<input_t> &wgts) const
1440  {
1441  size_t nv = gids_.size();
1442 
1443  if (gnosAreGids_)
1444  Ids = gids_.view(0, nv);
1445  else
1446  Ids = gnosConst_.view(0, nv);
1447 
1448  xyz = vCoords_.view(0, vWeightDim_);
1449  wgts = vWeights_.view(0, vCoordDim_);
1450 
1451  return nv;
1452  }
1453 
1454 template <typename User>
1456  ArrayView<const gno_t> &edgeIds, ArrayView<const int> &procIds,
1457  ArrayView<const lno_t> &offsets, ArrayView<input_t> &wgts) const
1458 {
1459  if (gnosAreGids_)
1460  edgeIds = edgeGids_.view(0, numLocalEdges_);
1461  else
1462  edgeIds = edgeGnosConst_.view(0, numLocalEdges_);
1463 
1464  procIds = procIdsConst_.view(0, numLocalEdges_);
1465  offsets = offsets_.view(0, numLocalVertices_+1);
1466  wgts = eWeights_.view(0, eWeightDim_);
1467 
1468  return numLocalEdges_;
1469 }
1470 
1472 // Graph model derived from CoordinateInput.
1473 //
1474 // We do not build a graph model from coordinates. We include
1475 // this definition so that other code will compile.
1477 
1478 template <typename User>
1479 class GraphModel<CoordinateInput<User> > : public Model<CoordinateInput<User> >
1480 {
1481 public:
1482 
1483  typedef typename CoordinateInput<User>::scalar_t scalar_t;
1484  typedef typename CoordinateInput<User>::gno_t gno_t;
1485  typedef typename CoordinateInput<User>::lno_t lno_t;
1487 
1489  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
1490  modelFlag_t &flags)
1491  {
1492  throw std::runtime_error("may not build a graph with identifiers");
1493  }
1494 
1495  // GraphModel interface
1496 
1497  size_t getLocalNumVertices() const { return 0;}
1498  size_t getGlobalNumVertices() const { return 0;}
1499  size_t getLocalNumGlobalEdges() const { return 0;}
1500  size_t getLocalNumLocalEdges() const { return 0;}
1501  size_t getGlobalNumEdges() const {return 0;}
1502  int getVertexWeightDim() const { return 0; }
1503  int getEdgeWeightDim() const { return 0; }
1504  int getCoordinateDim() const { return 0; }
1505  size_t getVertexList( ArrayView<const gno_t> &Ids,
1506  ArrayView<input_t> &xyz,
1507  ArrayView<input_t> &wgts) const { return 0; }
1508  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
1509  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
1510  ArrayView<input_t> &wgts) const { return 0; }
1511  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
1512  ArrayView<const lno_t> &offsets,
1513  ArrayView<input_t> &wgts) { return 0; }
1514 
1515  // Model interface
1516 
1517  size_t getLocalNumObjects() const { return 0; }
1518  size_t getGlobalNumObjects() const { return 0; }
1519  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {}
1520 
1521 };
1522 
1524 // Graph model derived from VectorInput.
1525 //
1526 // We do not build a graph model from a vector. We include
1527 // this definition so that other code will compile.
1529 
1530 template <typename User>
1531 class GraphModel<VectorInput<User> > : public Model<VectorInput<User> >
1532 {
1533 public:
1534 
1535  typedef typename VectorInput<User>::scalar_t scalar_t;
1536  typedef typename VectorInput<User>::gno_t gno_t;
1537  typedef typename VectorInput<User>::lno_t lno_t;
1539 
1540  GraphModel(const VectorInput<User> *ia,
1541  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
1542  modelFlag_t &flags)
1543  {
1544  throw std::runtime_error("can not build a graph from a vector");
1545  }
1546 
1547  // GraphModel interface
1548 
1549  size_t getLocalNumVertices() const { return 0;}
1550  size_t getGlobalNumVertices() const { return 0;}
1551  size_t getLocalNumGlobalEdges() const { return 0;}
1552  size_t getLocalNumLocalEdges() const { return 0;}
1553  size_t getGlobalNumEdges() const {return 0;}
1554  int getVertexWeightDim() const { return 0; }
1555  int getEdgeWeightDim() const { return 0; }
1556  int getCoordinateDim() const { return 0; }
1557  size_t getVertexList( ArrayView<const gno_t> &Ids,
1558  ArrayView<input_t> &xyz,
1559  ArrayView<input_t> &wgts) const { return 0; }
1560  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
1561  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
1562  ArrayView<input_t> &wgts) const { return 0; }
1563  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
1564  ArrayView<const lno_t> &offsets,
1565  ArrayView<input_t> &wgts) { return 0; }
1566 
1567  // Model interface
1568 
1569  size_t getLocalNumObjects() const { return 0; }
1570  size_t getGlobalNumObjects() const { return 0; }
1571  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {}
1572 
1573 };
1574 
1576 // Graph model derived from IdentifierInput.
1577 //
1578 // We do not build a graph model from identifiers. We include
1579 // this definition so that other code will compile.
1581 
1582 template <typename User>
1583 class GraphModel<IdentifierInput<User> > : public Model<IdentifierInput<User> >
1584 {
1585 public:
1586 
1587  typedef typename IdentifierInput<User>::scalar_t scalar_t;
1588  typedef typename IdentifierInput<User>::gno_t gno_t;
1589  typedef typename IdentifierInput<User>::lno_t lno_t;
1591 
1593  const RCP<const Environment> &env, const RCP<const Comm<int> > &comm,
1594  modelFlag_t &flags)
1595  {
1596  throw std::runtime_error("can not build a graph with identifiers");
1597  }
1598 
1599  // GraphModel interface
1600 
1601  size_t getLocalNumVertices() const { return 0;}
1602  size_t getGlobalNumVertices() const { return 0;}
1603  size_t getLocalNumGlobalEdges() const { return 0;}
1604  size_t getLocalNumLocalEdges() const { return 0;}
1605  size_t getGlobalNumEdges() const {return 0;}
1606  int getVertexWeightDim() const { return 0; }
1607  int getEdgeWeightDim() const { return 0; }
1608  int getCoordinateDim() const { return 0; }
1609  size_t getVertexList( ArrayView<const gno_t> &Ids,
1610  ArrayView<input_t> &xyz,
1611  ArrayView<input_t> &wgts) const { return 0; }
1612  size_t getEdgeList( ArrayView<const gno_t> &edgeIds,
1613  ArrayView<const int> &procIds, ArrayView<const lno_t> &offsets,
1614  ArrayView<input_t> &wgts) const { return 0; }
1615  size_t getLocalEdgeList( ArrayView<const lno_t> &edgeIds,
1616  ArrayView<const lno_t> &offsets,
1617  ArrayView<input_t> &wgts) { return 0; }
1618 
1619  // Model interface
1620 
1621  size_t getLocalNumObjects() const { return 0; }
1622  size_t getGlobalNumObjects() const { return 0; }
1623  void getGlobalObjectIds(ArrayView<const gno_t> &gnos) const {}
1624 
1625 };
1626 
1627 #endif // DOXYGEN_SHOULD_SKIP_THIS
1628 
1629 } // namespace Zoltan2
1630 
1631 #endif
1632 
use columns as graph vertices
An IdentifierMap manages a global space of object identifiers.
int getEdgeWeightDim() const
Returns the dimension (0 or greater) of edge weights.
virtual size_t getEdgeWeights(int weightDim, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the edge weights, if any.
int getVertexWeightDim() const
Returns the dimension (0 or greater) of vertex weights.
Defines the VectorInput adapter interface.
size_t getGlobalNumObjects() const
Return the global number of objects.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
size_t getLocalNumLocalEdges() const
Returns the number of local edges on this process. Does not include edges to off-process vertices...
Defines the Model interface.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
convert internal ids to user ids
size_t removeUndesiredEdges(const RCP< const Environment > &env, int myRank, bool removeSelfEdges, bool removeOffProcessEdges, bool removeOffGroupEdges, ArrayView< const typename InputTraits< User >::gid_t > &gids, ArrayView< const typename InputTraits< User >::gid_t > &gidNbors, ArrayView< const int > &procIds, ArrayView< StridedData< typename InputTraits< User >::lno_t, typename InputTraits< User >::scalar_t > > &edgeWeights, ArrayView< const typename InputTraits< User >::lno_t > &offsets, ArrayRCP< const typename InputTraits< User >::gid_t > &newGidNbors, typename InputTraits< User >::scalar_t **&newWeights, ArrayRCP< const typename InputTraits< User >::lno_t > &newOffsets)
Helper function to remove undesired edges from a graph.
IdentifierInput defines the interface for identifiers.
size_t getLocalNumGlobalEdges() const
Returns the number of global edges on this process. Includes remote edges.
std::bitset< NUM_MODEL_FLAGS > modelFlag_t
algorithm requires consecutive ids
model must symmetrize input
Defines the IdentifierInput interface.
model must symmetrize input
GraphModel(const Adapter *ia, const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, modelFlag_t &modelFlags)
Constructor.
size_t getGlobalNumVertices() const
Returns the global number vertices.
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.
convert user ids to internal ids
Defines the CoordinateInput interface.
size_t getEdgeList(ArrayView< const gno_t > &edgeIds, ArrayView< const int > &procIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; edge (neighbor) global Ids, including off-process edges.
int getCoordinateDim() const
Returns the dimension (0 to 3) of vertex coordinates.
use nonzeros as graph vertices
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
size_t getGlobalNumEdges() const
Returns the global number edges.
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.
virtual int getEdgeWeightDimension() const =0
Returns the dimension (0 or greater) of edge weights.
default_gno_t gid_t
The data type that the user uses for global Identifiers.
Traits for application input objects.
size_t computeLocalEdgeList(const RCP< const Environment > &env, int rank, size_t numLocalEdges, size_t numLocalGraphEdges, RCP< const IdentifierMap< User > > &idMap, ArrayRCP< const typename InputTraits< User >::gid_t > &allEdgeIds, ArrayRCP< int > &allProcs, ArrayRCP< const typename InputTraits< User >::lno_t > &allOffs, ArrayRCP< StridedData< typename InputTraits< User >::lno_t, typename InputTraits< User >::scalar_t > > &allWeights, ArrayRCP< const typename InputTraits< User >::lno_t > &edgeLocalIds, ArrayRCP< const typename InputTraits< User >::lno_t > &offsets, ArrayRCP< StridedData< typename InputTraits< User >::lno_t, typename InputTraits< User >::scalar_t > > &eWeights)
Helper function to create new edges lists containing only edges connected to a neighbor on this proce...
MatrixInput defines the interface for input adapters for matrices.
size_t getLocalNumVertices() const
Returns the number vertices on this process.
virtual int getRowWeightDimension() const =0
Returns the dimension (0 or greater) of row weights.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
double scalar_t
size_t getLocalEdgeList(ArrayView< const lno_t > &edgeIds, ArrayView< const lno_t > &offsets, ArrayView< input_t > &wgts)
Sets pointers to this process&#39; local-only edge (neighbor) LNOs, using the same implied vertex LNOs re...
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.
GraphModel defines the interface required for graph models.
virtual size_t getRowWeights(int weightDim, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the row weights, if any.
size_t getLocalNumObjects() const
Return the local number of objects.
The base class for all model classes.
Defines the MatrixInput adapter interface.
algorithm requires no self edges
RCP< const idmap_t > getIdentifierMap() const
Return the map from user global identifiers to internal Zoltan2 global numbers.
virtual int getVertexWeightDimension() const =0
Returns the dimension (0 or greater) of vertex weights.
VectorInput defines the interface for vector input adapters.
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...
void getGlobalObjectIds(ArrayView< const gno_t > &gnos) const
Get a list of the global Ids for the local objects.
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.
size_t getVertexList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &xyz, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; vertex Ids and their weights.
virtual size_t getVertexWeights(int weightDim, const scalar_t *&weights, int &stride) const =0
Provide a pointer to the vertex weights, if any.
default_scalar_t scalar_t
The data type for weights and coordinates.
ignore invalid neighbors
void setIdentifierMap(RCP< const idmap_t > &map)
Set the IdentifierMap used by the model.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
virtual bool getRowWeightIsNumberOfNonZeros(int dim) const =0
Is the row weight for a dimension the number of row non-zeros?