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);
197 ids = graph_->getRowMap()->getNodeElementList().getRawPtr();
204 offsets = offs_.getRawPtr();
213 env_->localInputAssertion(__FILE__, __LINE__,
"invalid weight index",
216 vertexWeights_[idx].getStridedList(length,
weights, stride);
225 env_->localInputAssertion(__FILE__, __LINE__,
"invalid weight index",
228 edgeWeights_[idx].getStridedList(length,
weights, stride);
232 template <
typename Adapter>
236 template <
typename Adapter>
242 RCP<const Environment> env_;
244 RCP<const User> graph_;
246 ArrayRCP<const lno_t> offs_;
247 ArrayRCP<const gno_t> adjids_;
249 int nWeightsPerVertex_;
250 ArrayRCP<StridedData<lno_t, scalar_t> > vertexWeights_;
251 ArrayRCP<bool> vertexDegreeWeight_;
253 int nWeightsPerEdge_;
254 ArrayRCP<StridedData<lno_t, scalar_t> > edgeWeights_;
257 ArrayRCP<StridedData<lno_t, scalar_t> > coords_;
259 RCP<User> doMigration(
const User &from,
size_t numLocalRows,
260 const gno_t *myNewRows)
const;
267 template <
typename User,
typename UserCoord>
269 const RCP<const User> &ingraph,
int nVtxWgts,
int nEdgeWgts):
271 graph_(ingraph), offs_(),
273 nWeightsPerVertex_(nVtxWgts), vertexWeights_(), vertexDegreeWeight_(),
274 nWeightsPerEdge_(nEdgeWgts), edgeWeights_(),
275 coordinateDim_(0), coords_()
279 size_t nvtx = graph_->getNodeNumRows();
280 size_t nedges = graph_->getNodeNumEntries();
281 size_t maxnumentries =
282 graph_->getNodeMaxNumRowEntries();
289 env_->localMemoryAssertion(__FILE__, __LINE__, n, offs);
291 gno_t *adjids = NULL;
293 adjids =
new gno_t [nedges];
294 env_->localMemoryAssertion(__FILE__, __LINE__, nedges, adjids);
297 ArrayRCP<lno_t> nbors(maxnumentries);
300 for (
size_t v=0; v < nvtx; v++){
301 graph_->getLocalRowCopy(v, nbors(), nedges);
302 offs[v+1] = offs[v] + nedges;
303 for (
lno_t e=offs[v], i=0; e < offs[v+1]; e++) {
304 adjids[e] = graph_->getColMap()->getGlobalElement(nbors[i++]);
308 offs_ = arcp(offs, 0, n,
true);
309 adjids_ = arcp(adjids, 0, nedges,
true);
311 if (nWeightsPerVertex_ > 0) {
313 arcp(
new input_t[nWeightsPerVertex_], 0, nWeightsPerVertex_,
true);
314 vertexDegreeWeight_ =
315 arcp(
new bool[nWeightsPerVertex_], 0, nWeightsPerVertex_,
true);
316 for (
int i=0; i < nWeightsPerVertex_; i++)
317 vertexDegreeWeight_[i] =
false;
320 if (nWeightsPerEdge_ > 0)
321 edgeWeights_ = arcp(
new input_t[nWeightsPerEdge_], 0, nWeightsPerEdge_,
true);
325 template <
typename User,
typename UserCoord>
327 const scalar_t *weightVal,
int stride,
int idx)
330 setVertexWeights(weightVal, stride, idx);
332 setEdgeWeights(weightVal, stride, idx);
336 template <
typename User,
typename UserCoord>
338 const scalar_t *weightVal,
int stride,
int idx)
341 env_->localInputAssertion(__FILE__, __LINE__,
"invalid vertex weight index",
343 size_t nvtx = getLocalNumVertices();
344 ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx*stride,
false);
345 vertexWeights_[idx] = input_t(weightV, stride);
349 template <
typename User,
typename UserCoord>
354 setVertexWeightIsDegree(idx);
356 std::ostringstream emsg;
357 emsg << __FILE__ <<
"," << __LINE__
358 <<
" error: setWeightIsNumberOfNonZeros is supported only for" 359 <<
" vertices" << std::endl;
360 throw std::runtime_error(emsg.str());
365 template <
typename User,
typename UserCoord>
369 env_->localInputAssertion(__FILE__, __LINE__,
"invalid vertex weight index",
372 vertexDegreeWeight_[idx] =
true;
376 template <
typename User,
typename UserCoord>
378 const scalar_t *weightVal,
int stride,
int idx)
381 env_->localInputAssertion(__FILE__, __LINE__,
"invalid edge weight index",
383 size_t nedges = getLocalNumEdges();
384 ArrayRCP<const scalar_t> weightV(weightVal, 0, nedges*stride,
false);
385 edgeWeights_[idx] = input_t(weightV, stride);
389 template <
typename User,
typename UserCoord>
390 template<
typename Adapter>
392 const User &in, User *&out,
397 ArrayRCP<gno_t> importList;
401 (solution,
this, importList);
406 RCP<User> outPtr = doMigration(in, numNewVtx, importList.getRawPtr());
412 template <
typename User,
typename UserCoord>
413 template<
typename Adapter>
415 const User &in, RCP<User> &out,
420 ArrayRCP<gno_t> importList;
424 (solution,
this, importList);
429 out = doMigration(in, numNewVtx, importList.getRawPtr());
434 template <
typename User,
typename UserCoord>
438 const gno_t *myNewRows
441 typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
442 typedef Tpetra::CrsGraph<lno_t, gno_t, node_t> tcrsgraph_t;
443 typedef Tpetra::RowGraph<lno_t, gno_t, node_t> trowgraph_t;
454 const tcrsgraph_t *pCrsGraphSrc =
dynamic_cast<const tcrsgraph_t *
>(&from);
457 throw std::logic_error(
"TpetraRowGraphAdapter cannot migrate data for " 458 "your RowGraph; it can migrate data only for " 460 "You can inherit from TpetraRowGraphAdapter and " 461 "implement migration for your RowGraph.");
467 const RCP<const map_t> &smap = from.getRowMap();
468 int oldNumElts = smap->getNodeNumElements();
469 gno_t numGlobalRows = smap->getGlobalNumElements();
472 ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
473 const RCP<const Teuchos::Comm<int> > &comm = from.getComm();
474 RCP<const map_t> tmap = rcp(
new map_t(numGlobalRows, rowList, base, comm));
477 Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
480 typedef Tpetra::Vector<gno_t, lno_t, gno_t, node_t> vector_t;
481 vector_t numOld(smap);
482 vector_t numNew(tmap);
483 for (
int lid=0; lid < oldNumElts; lid++){
484 numOld.replaceGlobalValue(smap->getGlobalElement(lid),
485 from.getNumEntriesInLocalRow(lid));
487 numNew.doImport(numOld, importer, Tpetra::INSERT);
489 size_t numElts = tmap->getNodeNumElements();
490 ArrayRCP<const gno_t> nnz;
492 nnz = numNew.getData(0);
494 ArrayRCP<const size_t> nnz_size_t;
496 if (numElts &&
sizeof(gno_t) !=
sizeof(
size_t)){
497 size_t *
vals =
new size_t [numElts];
498 nnz_size_t = arcp(
vals, 0, numElts,
true);
499 for (
size_t i=0; i < numElts; i++){
500 vals[i] =
static_cast<size_t>(nnz[i]);
504 nnz_size_t = arcp_reinterpret_cast<
const size_t>(nnz);
508 RCP<tcrsgraph_t> G = rcp(
new tcrsgraph_t(tmap, nnz_size_t,
509 Tpetra::StaticProfile));
511 G->doImport(*pCrsGraphSrc, importer, Tpetra::INSERT);
513 return Teuchos::rcp_dynamic_cast<User>(G);
InputTraits< User >::scalar_t scalar_t
Helper functions for Partitioning Problems.
fast typical checks for valid arguments
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
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
size_t getLocalNumVertices() const
Returns the number of vertices on this process.
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.