Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Zoltan2_TpetraCrsMatrixAdapter.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
14 #ifndef _ZOLTAN2_TPETRACRSMATRIXADAPTER_HPP_
15 #define _ZOLTAN2_TPETRACRSMATRIXADAPTER_HPP_
16 
19 #include <Zoltan2_StridedData.hpp>
20 #include <Zoltan2_InputTraits.hpp>
22 
23 #include <Tpetra_CrsMatrix.hpp>
24 
25 #include <vector>
26 
27 namespace Zoltan2 {
28 
30 
49 template <typename User, typename UserCoord = User>
50  class TpetraCrsMatrixAdapter : public TpetraRowMatrixAdapter<User,UserCoord> {
51 
52 public:
53 #ifndef DOXYGEN_SHOULD_SKIP_THIS
54  using scalar_t = typename InputTraits<User>::scalar_t;
55  using lno_t = typename InputTraits<User>::lno_t;
56  using gno_t = typename InputTraits<User>::gno_t;
57  using part_t = typename InputTraits<User>::part_t;
58  using node_t = typename InputTraits<User>::node_t;
59  using offset_t = typename InputTraits<User>::offset_t;
60  using tmatrix_t = Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t>;
61  using device_t = typename node_t::device_type;
62  using host_t = typename Kokkos::HostSpace::memory_space;
63  using user_t = User;
64  using userCoord_t = UserCoord;
65 
66  using Base = MatrixAdapter<User, UserCoord>;
68  #endif
69 
75  TpetraCrsMatrixAdapter(const RCP<const User> &inmatrix,
76  int nWeightsPerRow=0);
77 
80  RCP<const User> getUserMatrix() const { return this->matrix_; }
81 
82  template <typename Adapter>
83  void applyPartitioningSolution(const User &in, User *&out,
84  const PartitioningSolution<Adapter> &solution) const;
85 
86  template <typename Adapter>
87  void applyPartitioningSolution(const User &in, RCP<User> &out,
88  const PartitioningSolution<Adapter> &solution) const;
89 };
90 
92 // Definitions
94 
95  template <typename User, typename UserCoord>
97  const RCP<const User> &inmatrix, int nWeightsPerRow):
98  RowMatrix(nWeightsPerRow, inmatrix) {
99 
100  auto colIdsHost = inmatrix->getLocalIndicesHost();
101 
102  auto colIdsGlobalHost =
103  typename Base::IdsHostView("colIdsGlobalHost", colIdsHost.extent(0));
104  auto colMap = inmatrix->getColMap();
105 
106  // Convert to global IDs using Tpetra::Map
107  Kokkos::parallel_for("colIdsGlobalHost",
108  Kokkos::RangePolicy<Kokkos::HostSpace::execution_space>(
109  0, colIdsGlobalHost.extent(0)),
110  [=](const int i) {
111  colIdsGlobalHost(i) =
112  colMap->getGlobalElement(colIdsHost(i));
113  });
114 
115  auto colIdsDevice = Kokkos::create_mirror_view_and_copy(
116  typename Base::device_t(), colIdsGlobalHost);
117 
118  this->colIdsDevice_ = colIdsDevice;
119  this->offsDevice_ = inmatrix->getLocalRowPtrsDevice();
120 
121  if (this->nWeightsPerRow_ > 0) {
122 
123  this->rowWeightsDevice_ = typename Base::WeightsDeviceView(
124  "rowWeightsDevice_", inmatrix->getLocalNumRows(),
125  this->nWeightsPerRow_);
126 
127  this->numNzWeight_ = Kokkos::View<bool *, host_t>(
128  "numNzWeight_", this->nWeightsPerRow_);
129 
130  for (int i = 0; i < this->nWeightsPerRow_; ++i) {
131  this->numNzWeight_(i) = false;
132  }
133  }
134 }
135 
137 template <typename User, typename UserCoord>
138  template <typename Adapter>
140  const User &in, User *&out,
141  const PartitioningSolution<Adapter> &solution) const
142 {
143  // Get an import list (rows to be received)
144  size_t numNewRows;
145  ArrayRCP<gno_t> importList;
146  try{
147  numNewRows = Zoltan2::getImportList<Adapter,
149  (solution, this, importList);
150  }
152 
153  // Move the rows, creating a new matrix.
154  RCP<User> outPtr = this->doMigration(in, numNewRows,importList.getRawPtr());
155  out = const_cast<User *>(outPtr.get());
156  outPtr.release();
157 }
158 
160 template <typename User, typename UserCoord>
161  template <typename Adapter>
163  const User &in, RCP<User> &out,
164  const PartitioningSolution<Adapter> &solution) const
165 {
166  // Get an import list (rows to be received)
167  size_t numNewRows;
168  ArrayRCP<gno_t> importList;
169  try{
170  numNewRows = Zoltan2::getImportList<Adapter,
172  (solution, this, importList);
173  }
175 
176  // Move the rows, creating a new matrix.
177  out = this->doMigration(in, numNewRows, importList.getRawPtr());
178 }
179 
180 } //namespace Zoltan2
181 
182 #endif
Helper functions for Partitioning Problems.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
typename InputTraits< User >::scalar_t scalar_t
MatrixAdapter defines the adapter interface for matrices.
typename Kokkos::HostSpace::memory_space host_t
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tmatrix_t
typename node_t::device_type device_t
TpetraCrsMatrixAdapter(const RCP< const User > &inmatrix, int nWeightsPerRow=0)
Constructor.
default_part_t part_t
The data type to represent part numbers.
default_offset_t offset_t
The data type to represent offsets.
Provides access for Zoltan2 to Tpetra::CrsMatrix data.
typename node_t::device_type device_t
typename InputTraits< User >::part_t part_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...
Provides access for Zoltan2 to Tpetra::RowMatrix data.
typename InputTraits< User >::node_t node_t
A PartitioningSolution is a solution to a partitioning problem.
typename InputTraits< User >::gno_t gno_t
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
Traits for application input objects.
Defines the TpetraRowMatrixAdapter class.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and identifiers.
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
typename InputTraits< User >::offset_t offset_t
Defines the MatrixAdapter interface.
RCP< const User > getUserMatrix() const
Access to user&#39;s matrix.
typename InputTraits< User >::lno_t lno_t
default_scalar_t scalar_t
The data type for weights and coordinates.
This file defines the StridedData class.