Zoltan2  Version of the Day
Zoltan2_XpetraCrsGraphInput.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_XPETRACRSGRAPHINPUT_HPP_
51 #define _ZOLTAN2_XPETRACRSGRAPHINPUT_HPP_
52 
53 #include <Zoltan2_GraphInput.hpp>
54 #include <Zoltan2_StridedData.hpp>
55 #include <Zoltan2_XpetraTraits.hpp>
56 #include <Zoltan2_Util.hpp>
57 
58 #include <Xpetra_CrsGraph.hpp>
59 
60 namespace Zoltan2 {
61 
83 template <typename User>
84  class XpetraCrsGraphInput : public GraphInput<User> {
85 
86 public:
87 
88 #ifndef DOXYGEN_SHOULD_SKIP_THIS
89  typedef typename InputTraits<User>::scalar_t scalar_t;
90  typedef typename InputTraits<User>::lno_t lno_t;
91  typedef typename InputTraits<User>::gno_t gno_t;
92  typedef typename InputTraits<User>::gid_t gid_t;
93  typedef typename InputTraits<User>::node_t node_t;
94  typedef Xpetra::CrsGraph<lno_t, gno_t, node_t> xgraph_t;
95  typedef GraphInput<User> base_adapter_t;
96  typedef User user_t;
97 #endif
98 
102 
110  XpetraCrsGraphInput(const RCP<const User> &ingraph);
111 
152  XpetraCrsGraphInput(const RCP<const User> &ingraph,
153  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
154  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides);
155 
202  XpetraCrsGraphInput(const RCP<const User> &ingraph,
203  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
204  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides,
205  vector<const scalar_t *> &coords, vector<int> &coordStrides);
206 
222  void setVertexWeights(int dim, const scalar_t *val, int stride);
223 
246  void setEdgeWeights(int dim, const scalar_t *val, int stride);
247 
263  void setVertexCoordinates(int dim, const scalar_t *val, int stride);
264 
267  RCP<const xgraph_t> getXpetraGraph() const
268  {
269  return graph_;
270  }
271 
274  RCP<const User> getUserGraph() const
275  {
276  return ingraph_;
277  }
278 
280  // The InputAdapter interface.
282 
283  string inputAdapterName()const { return string("XpetraCrsGraph");}
284 
286 
287  int getNumberOfWeightsPerObject() const { return 0;}
288 
289  size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
290  {
291  return getVertexWeights(dim, wgt, stride);
292  }
293 
295  // The GraphInput interface.
297 
298  size_t getLocalNumberOfVertices() const {
299  return graph_->getNodeNumRows();
300  }
301 
302  size_t getLocalNumberOfEdges() const {
303  return graph_->getNodeNumEntries();
304  }
305 
307  return vertexWeightDim_;
308  }
309 
310  int getEdgeWeightDimension() const {
311  return edgeWeightDim_;
312  }
313 
314  int getCoordinateDimension() const {
315  return coordinateDim_;
316  }
317 
318  size_t getVertexListView(const gid_t *&ids,
319  const lno_t *&offsets, const gid_t *& edgeId) const
320  {
321  size_t nvtx = getLocalNumberOfVertices();
322  ids = edgeId = NULL;
323  offsets = NULL;
324 
325  if (nvtx){
326  ids = graph_->getRowMap()->getNodeElementList().getRawPtr();
327  offsets = offs_.getRawPtr();
328  edgeId = eids_.getRawPtr();
329  }
330 
331  return nvtx;
332  }
333 
334  size_t getVertexWeights(int dim,
335  const scalar_t *&weights, int &stride) const
336  {
337  env_->localInputAssertion(__FILE__, __LINE__, "invalid weight dimension",
338  dim >= 0 && dim < vertexWeightDim_, BASIC_ASSERTION);
339 
340  size_t length;
341  vertexWeights_[dim].getStridedList(length, weights, stride);
342  return length;
343  }
344 
345  size_t getEdgeWeights(int dim,
346  const scalar_t *&weights, int &stride) const
347  {
348  env_->localInputAssertion(__FILE__, __LINE__, "invalid weight dimension",
349  dim >= 0 && dim < edgeWeightDim_, BASIC_ASSERTION);
350 
351  size_t length;
352  edgeWeights_[dim].getStridedList(length, weights, stride);
353  return length;
354  }
355 
356  size_t getVertexCoordinates(int dim,
357  const scalar_t *&coords, int &stride) const
358  {
359  env_->localInputAssertion(__FILE__, __LINE__,
360  "invalid coordinate dimension",
361  dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
362 
363  size_t length;
364  coords_[dim].getStridedList(length, coords, stride);
365  return length;
366  }
367 
371  template<typename Adapter>
372  size_t applyPartitioningSolution(const User &in, User *&out,
373  const PartitioningSolution<Adapter> &solution) const;
374 
375 private:
376 
377  void initializeData(
378  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
379  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides,
380  vector<const scalar_t *> &coords, vector<int> &coordStrides);
381 
382  RCP<const User > ingraph_;
383  RCP<const xgraph_t > graph_;
384  RCP<const Comm<int> > comm_;
385 
386  ArrayRCP<const lno_t> offs_;
387  ArrayRCP<const gid_t> eids_;
388 
389  int vertexWeightDim_;
390  ArrayRCP<StridedData<lno_t, scalar_t> > vertexWeights_;
391 
392  int edgeWeightDim_;
393  ArrayRCP<StridedData<lno_t, scalar_t> > edgeWeights_;
394 
395  int coordinateDim_;
396  ArrayRCP<StridedData<lno_t, scalar_t> > coords_;
397 
398  // A default Environment for error messages. User-written
399  // InputAdapter classes can use some other error return convention
400  // if desired.
401  RCP<const Environment> env_;
402 };
403 
405 // Definitions
407 
408 template <typename User>
410  const RCP<const User> &ingraph):
411  ingraph_(ingraph), graph_(), comm_() , offs_(), eids_(),
412  vertexWeightDim_(0), vertexWeights_(),
413  edgeWeightDim_(0), edgeWeights_(),
414  coordinateDim_(0), coords_(),
415  env_(rcp(new Environment))
416 {
417  vector<const scalar_t *> emptyValues;
418  vector<int> emptyStrides;
419 
420  initializeData(emptyValues, emptyStrides, emptyValues, emptyStrides,
421  emptyValues, emptyStrides);
422 }
423 
424 template <typename User>
426  const RCP<const User> &ingraph,
427  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
428  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides):
429  ingraph_(ingraph), graph_(), comm_() , offs_(), eids_(),
430  vertexWeightDim_(vWeights.size()), vertexWeights_(),
431  edgeWeightDim_(eWeights.size()), edgeWeights_(),
432  coordinateDim_(0), coords_(),
433  env_(rcp(new Environment))
434 {
435  vector<const scalar_t *> emptyValues;
436  vector<int> emptyStrides;
437 
438  initializeData(vWeights, vWeightStrides, eWeights, eWeightStrides,
439  emptyValues, emptyStrides);
440 }
441 
442 template <typename User>
444  const RCP<const User> &ingraph,
445  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
446  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides,
447  vector<const scalar_t *> &coords, vector<int> &coordStrides):
448  ingraph_(ingraph), graph_(), comm_() , offs_(), eids_(),
449  vertexWeightDim_(vWeights.size()), vertexWeights_(),
450  edgeWeightDim_(eWeights.size()), edgeWeights_(),
451  coordinateDim_(coords.size()), coords_(),
452  env_(rcp(new Environment))
453 {
454  initializeData(vWeights, vWeightStrides, eWeights, eWeightStrides,
455  coords, coordStrides);
456 }
457 
458 template <typename User>
460  vector<const scalar_t *> &vWeights, vector<int> &vWeightStrides,
461  vector<const scalar_t *> &eWeights, vector<int> &eWeightStrides,
462  vector<const scalar_t *> &coords, vector<int> &coordStrides)
463 {
464  env_->localInputAssertion(__FILE__, __LINE__,
465  "invalid number of dimensions",
466  vertexWeightDim_ >= 0 && edgeWeightDim_ >= 0 && coordinateDim_ >= 0,
468 
470 
471  if (vertexWeightDim_)
472  vertexWeights_ =
473  arcp(new input_t [vertexWeightDim_], 0, vertexWeightDim_, true);
474 
475  if (edgeWeightDim_)
476  edgeWeights_ =
477  arcp(new input_t [edgeWeightDim_], 0, edgeWeightDim_, true);
478 
479  if (coordinateDim_)
480  coords_ =
481  arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
482 
483  graph_ = XpetraTraits<User>::convertToXpetra(ingraph_);
484  comm_ = graph_->getComm();
485  size_t nvtx = graph_->getNodeNumRows();
486  size_t nedges = graph_->getNodeNumEntries();
487 
488  // Unfortunately we have to copy the offsets and edge Ids
489  // because edge Ids are not usually stored in vertex id order.
490 
491  size_t n = nvtx + 1;
492  lno_t *offs = new lno_t [n];
493  env_->localMemoryAssertion(__FILE__, __LINE__, n, offs);
494 
495  gid_t *eids = NULL;
496  if (nedges){
497  eids = new gid_t [nedges];
498  env_->localMemoryAssertion(__FILE__, __LINE__, nedges, eids);
499  }
500 
501  offs[0] = 0;
502  for (size_t v=0; v < nvtx; v++){
503  ArrayView<const lno_t> nbors;
504  graph_->getLocalRowView(v, nbors);
505  offs[v+1] = offs[v] + nbors.size();
506  for (lno_t e=offs[v], i=0; e < offs[v+1]; e++)
507  eids[e] = graph_->getColMap()->getGlobalElement(nbors[i++]);
508  }
509 
510  offs_ = arcp(offs, 0, n, true);
511  eids_ = arcp(eids, 0, nedges, true);
512 
513  int stride = 1;
514  for (int dim=0; dim < coordinateDim_; dim++){
515  if (coordStrides.size())
516  stride = coordStrides[dim];
517  ArrayRCP<const scalar_t> coordV(coords[dim], 0, nvtx, false);
518  coords_[dim] = input_t(coordV, stride);
519  }
520 
521  stride = 1;
522  for (int dim=0; dim < vertexWeightDim_; dim++){
523  if (vWeightStrides.size())
524  stride = vWeightStrides[dim];
525  ArrayRCP<const scalar_t> wgtV(vWeights[dim], 0, nvtx, false);
526  vertexWeights_[dim] = input_t(wgtV, stride);
527  }
528 
529  stride = 1;
530  for (int dim=0; dim < edgeWeightDim_; dim++){
531  if (eWeightStrides.size())
532  stride = eWeightStrides[dim];
533  ArrayRCP<const scalar_t> ewgtV(eWeights[dim], 0, nedges, false);
534  edgeWeights_[dim] = input_t(ewgtV, stride);
535  }
536 }
537 
538 template <typename User>
539  template<typename Adapter>
541  const User &in, User *&out,
542  const PartitioningSolution<Adapter> &solution) const
543 {
544  // Get an import list
545 
547  size_t len = solution.getLocalNumberOfIds();
548  const gid_t *gids = solution.getIdList();
549  const partId_t *parts = solution.getPartList();
550  ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false);
551  ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len,
552  false);
553 
554  ArrayRCP<lno_t> dummyIn;
555  ArrayRCP<gid_t> importList;
556  ArrayRCP<lno_t> dummyOut;
557  size_t numNewVtx;
558  const RCP<const Comm<int> > comm = graph_->getComm();
559 
560  try{
561  numNewVtx = solution.convertSolutionToImportList(
562  0, dummyIn, importList, dummyOut);
563  }
565 
566  gno_t lsum = numNewVtx;
567  gno_t gsum = 0;
568  reduceAll<int, gno_t>(*comm_, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
569 
570  RCP<const User> inPtr = rcp(&in, false);
571 
572  RCP<const User> outPtr = XpetraTraits<User>::doMigration(
573  inPtr, lsum, importList.getRawPtr());
574 
575  out = const_cast<User *>(outPtr.get());
576  outPtr.release();
577  return numNewVtx;
578 }
579 
580 } //namespace Zoltan2
581 
582 #endif
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
string inputAdapterName() const
Returns a descriptive name that identifies the concrete adapter.
Xpetra::CrsGraph< lno_t, gno_t, node_t > xgraph_t
RCP< const User > getUserGraph() const
Access to user&#39;s graph.
XpetraCrsGraphInput(const RCP< const User > &ingraph)
Constructor for graph with no weights or coordinates.
size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
Provide pointer to a weight array with stride.
size_t getVertexListView(const gid_t *&ids, const lno_t *&offsets, const gid_t *&edgeId) const
Sets pointers to this process&#39; graph entries.
zoltan2_partId_t partId_t
RCP< const xgraph_t > getXpetraGraph() const
Access to Xpetra-wrapped user&#39;s graph.
Traits of Xpetra classes, including migration method.
size_t getVertexWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the vertex weights, if any.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
size_t applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
Repartition a graph that has the same structure as the graph that instantiated this input adapter...
size_t convertSolutionToImportList(int numExtra, ArrayRCP< Extra > &xtraInfo, ArrayRCP< typename Adapter::gid_t > &imports, ArrayRCP< Extra > &newXtraInfo) const
Create an import list from the export list.
void setEdgeWeights(int dim, const scalar_t *val, int stride)
Provide a pointer to one dimension of the edge weights.
void setVertexCoordinates(int dim, const scalar_t *val, int stride)
Provide a pointer to one dimension of the vertex coordinates.
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
A PartitioningSolution is a solution to a partitioning problem.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
The StridedData class manages lists of weights or coordinates.
static RCP< const User > doMigration(const RCP< const User > &from, lno_t numLocalRows, const gno_t *myNewRows)
Migrate the object Given a user object and a new row distribution, create and return a new user objec...
static RCP< const User > convertToXpetra(const RCP< const User > &a)
Convert the object to its Xpetra wrapped version.
int getCoordinateDimension() const
Returns the dimension of the geometry, if any.
default_gno_t gid_t
The data type that the user uses for global Identifiers.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
double scalar_t
const gid_t * getIdList() const
Returns the user&#39;s global ID list.
size_t getLocalNumberOfObjects() const
Returns the number of objects in the input.
int getNumberOfWeightsPerObject() const
Returns the number of weights per object. Number of weights per object should be zero or greater...
size_t getLocalNumberOfEdges() const
Returns the number edges on this process.
size_t getVertexCoordinates(int dim, const scalar_t *&coords, int &stride) const
Provide a pointer to one dimension of vertex coordinates.
Provides access for Zoltan2 to Xpetra::CrsGraph data.
Kokkos::DefaultNode::DefaultNodeType node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
void setVertexWeights(int dim, const scalar_t *val, int stride)
Provide a pointer to one dimension of the vertex weights.
GraphInput defines the interface for graph input adapters.
A gathering of useful namespace methods.
Defines the GraphInput interface.
int getEdgeWeightDimension() const
Returns the dimension (0 or greater) of edge weights.
int getVertexWeightDimension() const
Returns the dimension (0 or greater) of vertex weights.
size_t getLocalNumberOfVertices() const
Returns the number vertices on this process.
default_scalar_t scalar_t
The data type for weights and coordinates.
size_t getEdgeWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the edge weights, if any.
Kokkos::DefaultNode::DefaultNodeType node_t
This file defines the StridedData class.
size_t getLocalNumberOfIds() const
Returns the local number of Ids.