50 #ifndef _ZOLTAN2_TPETRAROWGRAPHADAPTER_HPP_ 51 #define _ZOLTAN2_TPETRAROWGRAPHADAPTER_HPP_ 56 #include <Tpetra_RowGraph.hpp> 81 template <
typename User,
typename UserCoord=User>
86 #ifndef DOXYGEN_SHOULD_SKIP_THIS 93 typedef UserCoord userCoord_t;
110 int nVtxWeights=0,
int nEdgeWeights=0);
124 void setWeights(
const scalar_t *val,
int stride,
int idx);
197 ids = graph_->getRowMap()->getNodeElementList().getRawPtr();
204 offsets = offs_.getRawPtr();
213 if(idx<0 || idx >= nWeightsPerVertex_)
215 std::ostringstream emsg;
216 emsg << __FILE__ <<
":" << __LINE__
217 <<
" Invalid vertex weight index " << idx << std::endl;
218 throw std::runtime_error(emsg.str());
222 vertexWeights_[idx].getStridedList(length, weights, stride);
231 if(idx<0 || idx >= nWeightsPerEdge_)
233 std::ostringstream emsg;
234 emsg << __FILE__ <<
":" << __LINE__
235 <<
" Invalid edge weight index " << idx << std::endl;
236 throw std::runtime_error(emsg.str());
240 edgeWeights_[idx].getStridedList(length, weights, stride);
244 template <
typename Adapter>
248 template <
typename Adapter>
254 RCP<const User> graph_;
256 ArrayRCP<const lno_t> offs_;
257 ArrayRCP<const gno_t> adjids_;
259 int nWeightsPerVertex_;
260 ArrayRCP<StridedData<lno_t, scalar_t> > vertexWeights_;
261 ArrayRCP<bool> vertexDegreeWeight_;
263 int nWeightsPerEdge_;
264 ArrayRCP<StridedData<lno_t, scalar_t> > edgeWeights_;
267 ArrayRCP<StridedData<lno_t, scalar_t> > coords_;
269 RCP<User> doMigration(
const User &from,
size_t numLocalRows,
270 const gno_t *myNewRows)
const;
277 template <
typename User,
typename UserCoord>
279 const RCP<const User> &ingraph,
int nVtxWgts,
int nEdgeWgts):
280 graph_(ingraph), offs_(),
282 nWeightsPerVertex_(nVtxWgts), vertexWeights_(), vertexDegreeWeight_(),
283 nWeightsPerEdge_(nEdgeWgts), edgeWeights_(),
284 coordinateDim_(0), coords_()
288 size_t nvtx = graph_->getNodeNumRows();
289 size_t nedges = graph_->getNodeNumEntries();
290 size_t maxnumentries =
291 graph_->getNodeMaxNumRowEntries();
301 std::cerr <<
"Error: " << __FILE__ <<
", " << __LINE__<< std::endl;
302 std::cerr << n <<
" objects" << std::endl;
303 throw std::bad_alloc();
306 gno_t *adjids = NULL;
309 adjids =
new gno_t [nedges];
313 std::cerr <<
"Error: " << __FILE__ <<
", " << __LINE__<< std::endl;
314 std::cerr << nedges <<
" objects" << std::endl;
315 throw std::bad_alloc();
319 ArrayRCP<lno_t> nbors(maxnumentries);
322 for (
size_t v=0; v < nvtx; v++){
323 graph_->getLocalRowCopy(v, nbors(), nedges);
324 offs[v+1] = offs[v] + nedges;
325 for (
lno_t e=offs[v], i=0; e < offs[v+1]; e++) {
326 adjids[e] = graph_->getColMap()->getGlobalElement(nbors[i++]);
330 offs_ = arcp(offs, 0, n,
true);
331 adjids_ = arcp(adjids, 0, nedges,
true);
333 if (nWeightsPerVertex_ > 0) {
335 arcp(
new input_t[nWeightsPerVertex_], 0, nWeightsPerVertex_,
true);
336 vertexDegreeWeight_ =
337 arcp(
new bool[nWeightsPerVertex_], 0, nWeightsPerVertex_,
true);
338 for (
int i=0; i < nWeightsPerVertex_; i++)
339 vertexDegreeWeight_[i] =
false;
342 if (nWeightsPerEdge_ > 0)
343 edgeWeights_ = arcp(
new input_t[nWeightsPerEdge_], 0, nWeightsPerEdge_,
true);
347 template <
typename User,
typename UserCoord>
349 const scalar_t *weightVal,
int stride,
int idx)
358 template <
typename User,
typename UserCoord>
360 const scalar_t *weightVal,
int stride,
int idx)
363 if(idx<0 || idx >= nWeightsPerVertex_)
365 std::ostringstream emsg;
366 emsg << __FILE__ <<
":" << __LINE__
367 <<
" Invalid vertex weight index " << idx << std::endl;
368 throw std::runtime_error(emsg.str());
372 ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx*stride,
false);
373 vertexWeights_[idx] = input_t(weightV, stride);
377 template <
typename User,
typename UserCoord>
384 std::ostringstream emsg;
385 emsg << __FILE__ <<
"," << __LINE__
386 <<
" error: setWeightIsNumberOfNonZeros is supported only for" 387 <<
" vertices" << std::endl;
388 throw std::runtime_error(emsg.str());
393 template <
typename User,
typename UserCoord>
397 if(idx<0 || idx >= nWeightsPerVertex_)
399 std::ostringstream emsg;
400 emsg << __FILE__ <<
":" << __LINE__
401 <<
" Invalid vertex weight index " << idx << std::endl;
402 throw std::runtime_error(emsg.str());
405 vertexDegreeWeight_[idx] =
true;
409 template <
typename User,
typename UserCoord>
411 const scalar_t *weightVal,
int stride,
int idx)
415 if(idx<0 || idx >= nWeightsPerEdge_)
417 std::ostringstream emsg;
418 emsg << __FILE__ <<
":" << __LINE__
419 <<
" Invalid edge weight index " << idx << std::endl;
420 throw std::runtime_error(emsg.str());
424 ArrayRCP<const scalar_t> weightV(weightVal, 0, nedges*stride,
false);
425 edgeWeights_[idx] = input_t(weightV, stride);
429 template <
typename User,
typename UserCoord>
430 template<
typename Adapter>
432 const User &in, User *&out,
437 ArrayRCP<gno_t> importList;
441 (solution,
this, importList);
446 RCP<User> outPtr = doMigration(in, numNewVtx, importList.getRawPtr());
452 template <
typename User,
typename UserCoord>
453 template<
typename Adapter>
455 const User &in, RCP<User> &out,
460 ArrayRCP<gno_t> importList;
464 (solution,
this, importList);
469 out = doMigration(in, numNewVtx, importList.getRawPtr());
474 template <
typename User,
typename UserCoord>
478 const gno_t *myNewRows
481 typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
482 typedef Tpetra::CrsGraph<lno_t, gno_t, node_t> tcrsgraph_t;
493 const tcrsgraph_t *pCrsGraphSrc =
dynamic_cast<const tcrsgraph_t *
>(&from);
496 throw std::logic_error(
"TpetraRowGraphAdapter cannot migrate data for " 497 "your RowGraph; it can migrate data only for " 499 "You can inherit from TpetraRowGraphAdapter and " 500 "implement migration for your RowGraph.");
504 const RCP<const map_t> &smap = from.getRowMap();
505 int oldNumElts = smap->getNodeNumElements();
506 gno_t numGlobalRows = smap->getGlobalNumElements();
507 gno_t base = smap->getMinAllGlobalIndex();
510 ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
511 const RCP<const Teuchos::Comm<int> > &comm = from.getComm();
512 RCP<const map_t> tmap = rcp(
new map_t(numGlobalRows, rowList, base, comm));
515 Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
518 typedef Tpetra::Vector<gno_t, lno_t, gno_t, node_t> vector_t;
519 vector_t numOld(smap);
520 vector_t numNew(tmap);
521 for (
int lid=0; lid < oldNumElts; lid++){
522 numOld.replaceGlobalValue(smap->getGlobalElement(lid),
523 from.getNumEntriesInLocalRow(lid));
525 numNew.doImport(numOld, importer, Tpetra::INSERT);
527 size_t numElts = tmap->getNodeNumElements();
528 ArrayRCP<const gno_t> nnz;
530 nnz = numNew.getData(0);
532 ArrayRCP<const size_t> nnz_size_t;
534 if (numElts &&
sizeof(
gno_t) !=
sizeof(
size_t)){
535 size_t *
vals =
new size_t [numElts];
536 nnz_size_t = arcp(vals, 0, numElts,
true);
537 for (
size_t i=0; i < numElts; i++){
538 vals[i] =
static_cast<size_t>(nnz[i]);
542 nnz_size_t = arcp_reinterpret_cast<
const size_t>(nnz);
546 RCP<tcrsgraph_t> G = rcp(
new tcrsgraph_t(tmap, nnz_size_t,
547 Tpetra::StaticProfile));
549 G->doImport(*pCrsGraphSrc, importer, Tpetra::INSERT);
551 return Teuchos::rcp_dynamic_cast<User>(G);
InputTraits< User >::scalar_t scalar_t
Helper functions for Partitioning Problems.
void setWeightIsDegree(int idx)
Specify an index for which the weight should be the degree of the entity.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
size_t getLocalNumEdges() const
Returns the number of edges on this process.
InputTraits< User >::gno_t gno_t
bool useDegreeAsVertexWeight(int idx) const
Indicate whether vertex weight with index idx should be the global degree of the vertex.
GraphAdapter defines the interface for graph-based user data.
void setVertexWeights(const scalar_t *val, int stride, int idx)
Provide a pointer to vertex weights.
InputTraits< User >::lno_t lno_t
size_t getImportList(const PartitioningSolution< SolutionAdapter > &solution, const DataAdapter *const data, ArrayRCP< typename DataAdapter::gno_t > &imports)
From a PartitioningSolution, get a list of IDs to be imported. Assumes part numbers in PartitioningSo...
void getEdgeWeightsView(const scalar_t *&weights, int &stride, int idx) const
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
int getNumWeightsPerEdge() const
Returns the number (0 or greater) of edge weights.
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
InputTraits< User >::part_t part_t
size_t getLocalNumVertices() const
Returns the number of vertices on this process.
enum GraphEntityType getPrimaryEntityType() const
Returns the entity to be partitioned, ordered, colored, etc. Valid values are GRAPH_VERTEX or GRAPH_E...
void setVertexWeightIsDegree(int idx)
Specify an index for which the vertex weight should be the degree of the vertex.
TpetraRowGraphAdapter(const RCP< const User > &ingraph, int nVtxWeights=0, int nEdgeWeights=0)
Constructor for graph with no weights or coordinates.
void setEdgeWeights(const scalar_t *val, int stride, int idx)
Provide a pointer to edge weights.
void getVertexIDsView(const gno_t *&ids) const
Sets pointers to this process' graph entries.
Defines the GraphAdapter interface.
void getVertexWeightsView(const scalar_t *&weights, int &stride, int idx) const
void setWeights(const scalar_t *val, int stride, int idx)
Provide a pointer to weights for the primary entity type.
~TpetraRowGraphAdapter()
Destructor.
void getEdgesView(const lno_t *&offsets, const gno_t *&adjIds) const
Gets adjacency lists for all vertices in a compressed sparse row (CSR) format.
int getNumWeightsPerVertex() const
Returns the number (0 or greater) of weights per vertex.
Provides access for Zoltan2 to Tpetra::RowGraph data.
This file defines the StridedData class.