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 // ***********************************************************************
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_TPETRACRSMATRIXADAPTER_HPP_
51 #define _ZOLTAN2_TPETRACRSMATRIXADAPTER_HPP_
52 
55 #include <Zoltan2_StridedData.hpp>
56 #include <Zoltan2_InputTraits.hpp>
58 
59 #include <Tpetra_CrsMatrix.hpp>
60 
61 #include <vector>
62 
63 namespace Zoltan2 {
64 
66 
85 template <typename User, typename UserCoord = User>
86  class TpetraCrsMatrixAdapter : public TpetraRowMatrixAdapter<User,UserCoord> {
87 
88 public:
89 #ifndef DOXYGEN_SHOULD_SKIP_THIS
90  using scalar_t = typename InputTraits<User>::scalar_t;
91  using lno_t = typename InputTraits<User>::lno_t;
92  using gno_t = typename InputTraits<User>::gno_t;
93  using part_t = typename InputTraits<User>::part_t;
94  using node_t = typename InputTraits<User>::node_t;
95  using offset_t = typename InputTraits<User>::offset_t;
96  using tmatrix_t = Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t>;
97  using device_t = typename node_t::device_type;
98  using host_t = typename Kokkos::HostSpace::memory_space;
99  using user_t = User;
100  using userCoord_t = UserCoord;
101 
102  using Base = MatrixAdapter<User, UserCoord>;
103  using RowMatrix = TpetraRowMatrixAdapter<User, UserCoord>;
104  #endif
105 
111  TpetraCrsMatrixAdapter(const RCP<const User> &inmatrix,
112  int nWeightsPerRow=0);
113 
116  RCP<const User> getUserMatrix() const { return this->matrix_; }
117 
118  template <typename Adapter>
119  void applyPartitioningSolution(const User &in, User *&out,
120  const PartitioningSolution<Adapter> &solution) const;
121 
122  template <typename Adapter>
123  void applyPartitioningSolution(const User &in, RCP<User> &out,
124  const PartitioningSolution<Adapter> &solution) const;
125 };
126 
128 // Definitions
130 
131  template <typename User, typename UserCoord>
133  const RCP<const User> &inmatrix, int nWeightsPerRow):
134  RowMatrix(nWeightsPerRow, inmatrix) {
135 
136  auto colIdsHost = inmatrix->getLocalIndicesHost();
137 
138  auto colIdsGlobalHost =
139  typename Base::IdsHostView("colIdsGlobalHost", colIdsHost.extent(0));
140  auto colMap = inmatrix->getColMap();
141 
142  // Convert to global IDs using Tpetra::Map
143  Kokkos::parallel_for("colIdsGlobalHost",
144  Kokkos::RangePolicy<Kokkos::HostSpace::execution_space>(
145  0, colIdsGlobalHost.extent(0)),
146  [=](const int i) {
147  colIdsGlobalHost(i) =
148  colMap->getGlobalElement(colIdsHost(i));
149  });
150 
151  auto colIdsDevice = Kokkos::create_mirror_view_and_copy(
152  typename Base::device_t(), colIdsGlobalHost);
153 
154  this->colIdsDevice_ = colIdsDevice;
155  this->offsDevice_ = inmatrix->getLocalRowPtrsDevice();
156 
157  if (this->nWeightsPerRow_ > 0) {
158 
159  this->rowWeightsDevice_ = typename Base::WeightsDeviceView(
160  "rowWeightsDevice_", inmatrix->getLocalNumRows(),
161  this->nWeightsPerRow_);
162 
163  this->numNzWeight_ = Kokkos::View<bool *, host_t>(
164  "numNzWeight_", this->nWeightsPerRow_);
165 
166  for (int i = 0; i < this->nWeightsPerRow_; ++i) {
167  this->numNzWeight_(i) = false;
168  }
169  }
170 }
171 
173 template <typename User, typename UserCoord>
174  template <typename Adapter>
176  const User &in, User *&out,
177  const PartitioningSolution<Adapter> &solution) const
178 {
179  // Get an import list (rows to be received)
180  size_t numNewRows;
181  ArrayRCP<gno_t> importList;
182  try{
183  numNewRows = Zoltan2::getImportList<Adapter,
185  (solution, this, importList);
186  }
188 
189  // Move the rows, creating a new matrix.
190  RCP<User> outPtr = this->doMigration(in, numNewRows,importList.getRawPtr());
191  out = const_cast<User *>(outPtr.get());
192  outPtr.release();
193 }
194 
196 template <typename User, typename UserCoord>
197  template <typename Adapter>
199  const User &in, RCP<User> &out,
200  const PartitioningSolution<Adapter> &solution) const
201 {
202  // Get an import list (rows to be received)
203  size_t numNewRows;
204  ArrayRCP<gno_t> importList;
205  try{
206  numNewRows = Zoltan2::getImportList<Adapter,
208  (solution, this, importList);
209  }
211 
212  // Move the rows, creating a new matrix.
213  out = this->doMigration(in, numNewRows, importList.getRawPtr());
214 }
215 
216 } //namespace Zoltan2
217 
218 #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.