Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TpetraRowMatrixInput.cpp
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 //
46 // Basic testing of Zoltan2::TpetraRowMatrixAdapter
47 
53 #include <Zoltan2_InputTraits.hpp>
54 #include <Zoltan2_TestHelpers.hpp>
57 
58 #include <Teuchos_Comm.hpp>
59 #include <Teuchos_CommHelpers.hpp>
60 #include <Teuchos_DefaultComm.hpp>
61 #include <Teuchos_RCP.hpp>
62 #include <cstdlib>
63 #include <stdexcept>
64 
65 using Teuchos::Comm;
66 using Teuchos::Comm;
67 using Teuchos::RCP;
68 using Teuchos::rcp;
69 using Teuchos::rcp_const_cast;
70 using Teuchos::rcp_dynamic_cast;
71 
72 using ztcrsmatrix_t = Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t, znode_t>;
73 using ztrowmatrix_t = Tpetra::RowMatrix<zscalar_t, zlno_t, zgno_t, znode_t>;
75 using device_t = typename node_t::device_type;
78 using execspace_t =
79  typename rowAdapter_t::ConstWeightsHostView1D::execution_space;
80 
82 
83 template<typename offset_t>
84 void printMatrix(RCP<const Comm<int> > &comm, zlno_t nrows,
85  const zgno_t *rowIds, const offset_t *offsets, const zgno_t *colIds) {
86  int rank = comm->getRank();
87  int nprocs = comm->getSize();
88  comm->barrier();
89  for (int p=0; p < nprocs; p++){
90  if (p == rank){
91  std::cout << rank << ":" << std::endl;
92  for (zlno_t i=0; i < nrows; i++){
93  std::cout << " row " << rowIds[i] << ": ";
94  for (offset_t j=offsets[i]; j < offsets[i+1]; j++){
95  std::cout << colIds[j] << " ";
96  }
97  std::cout << std::endl;
98  }
99  std::cout.flush();
100  }
101  comm->barrier();
102  }
103  comm->barrier();
104 }
105 
107 
108 template <typename adapter_t, typename matrix_t>
109 void TestMatrixIds(adapter_t &ia, matrix_t &matrix) {
110 
111  using idsHost_t = typename adapter_t::ConstIdsHostView;
112  using offsetsHost_t = typename adapter_t::ConstOffsetsHostView;
113  using localInds_t =
114  typename adapter_t::user_t::nonconst_local_inds_host_view_type;
115  using localVals_t =
116  typename adapter_t::user_t::nonconst_values_host_view_type;
117 
118 
119  const auto nrows = matrix.getLocalNumRows();
120  const auto ncols = matrix.getLocalNumEntries();
121  const auto maxNumEntries = matrix.getLocalMaxNumRowEntries();
122 
123  typename adapter_t::Base::ConstIdsHostView colIdsHost_("colIdsHost_", ncols);
124  typename adapter_t::Base::ConstOffsetsHostView offsHost_("offsHost_",
125  nrows + 1);
126 
127  localInds_t localColInds("localColInds", maxNumEntries);
128  localVals_t localVals("localVals", maxNumEntries);
129 
130  for (size_t r = 0; r < nrows; r++) {
131  size_t numEntries = 0;
132  matrix.getLocalRowCopy(r, localColInds, localVals, numEntries);;
133 
134  offsHost_(r + 1) = offsHost_(r) + numEntries;
135  for (size_t e = offsHost_(r), i = 0; e < offsHost_(r + 1); e++) {
136  colIdsHost_(e) = matrix.getColMap()->getGlobalElement(localColInds(i++));
137  }
138  }
139 
140  idsHost_t rowIdsHost;
141  ia.getRowIDsHostView(rowIdsHost);
142 
143  const auto matrixIDS = matrix.getRowMap()->getLocalElementList();
144 
145  Z2_TEST_COMPARE_ARRAYS(matrixIDS, rowIdsHost);
146 
147  idsHost_t colIdsHost;
148  offsetsHost_t offsetsHost;
149  ia.getCRSHostView(offsetsHost, colIdsHost);
150 
151  Z2_TEST_COMPARE_ARRAYS(colIdsHost_, colIdsHost);
152  Z2_TEST_COMPARE_ARRAYS(offsHost_, offsetsHost);
153 }
154 
155 template <typename adapter_t, typename matrix_t>
156 void verifyInputAdapter(adapter_t &ia, matrix_t &matrix) {
157  using idsDevice_t = typename adapter_t::ConstIdsDeviceView;
158  using idsHost_t = typename adapter_t::ConstIdsHostView;
159  using weightsDevice_t = typename adapter_t::WeightsDeviceView1D;
160  using weightsHost_t = typename adapter_t::WeightsHostView1D;
161 
162  const auto nrows = ia.getLocalNumIDs();
163 
164  Z2_TEST_EQUALITY(ia.getLocalNumRows(), matrix.getLocalNumRows());
165  Z2_TEST_EQUALITY(ia.getLocalNumColumns(), matrix.getLocalNumCols());
166  Z2_TEST_EQUALITY(ia.getLocalNumEntries(), matrix.getLocalNumEntries());
167 
171 
172  idsDevice_t rowIdsDevice;
173  ia.getRowIDsDeviceView(rowIdsDevice);
174  idsHost_t rowIdsHost;
175  ia.getRowIDsHostView(rowIdsHost);
176 
177  Z2_TEST_DEVICE_HOST_VIEWS(rowIdsDevice, rowIdsHost);
178 
182  Z2_TEST_THROW(ia.setRowWeightsDevice(
183  typename adapter_t::WeightsDeviceView1D{}, 50),
184  std::runtime_error);
185 
186  weightsDevice_t wgts0("wgts0", nrows);
187  Kokkos::parallel_for(
188  nrows, KOKKOS_LAMBDA(const int idx) { wgts0(idx) = idx * 2; });
189 
190  Z2_TEST_NOTHROW(ia.setRowWeightsDevice(wgts0, 0));
191 
192  // Don't reuse the same View, since we don't copy the values,
193  // we just assign the View (increase use count)
194  weightsDevice_t wgts1("wgts1", nrows);
195  Kokkos::parallel_for(
196  nrows, KOKKOS_LAMBDA(const int idx) { wgts1(idx) = idx * 3; });
197 
198  Z2_TEST_NOTHROW(ia.setRowWeightsDevice(wgts1, 1));
199 
203  {
204  weightsDevice_t weightsDevice;
205  Z2_TEST_NOTHROW(ia.getRowWeightsDeviceView(weightsDevice, 0));
206 
207  weightsHost_t weightsHost;
208  Z2_TEST_NOTHROW(ia.getRowWeightsHostView(weightsHost, 0));
209 
210  Z2_TEST_DEVICE_HOST_VIEWS(weightsDevice, weightsHost);
211 
212  Z2_TEST_DEVICE_HOST_VIEWS(wgts0, weightsHost);
213  }
214  {
215  weightsDevice_t weightsDevice;
216  Z2_TEST_NOTHROW(ia.getRowWeightsDeviceView(weightsDevice, 1));
217 
218  weightsHost_t weightsHost;
219  Z2_TEST_NOTHROW(ia.getRowWeightsHostView(weightsHost, 1));
220 
221  Z2_TEST_DEVICE_HOST_VIEWS(weightsDevice, weightsHost);
222 
223  Z2_TEST_DEVICE_HOST_VIEWS(wgts1, weightsHost);
224  }
225  {
226  weightsDevice_t wgtsDevice;
227  Z2_TEST_THROW(ia.getRowWeightsDeviceView(wgtsDevice, 2),
228  std::runtime_error);
229 
230  weightsHost_t wgtsHost;
231  Z2_TEST_THROW(ia.getRowWeightsHostView(wgtsHost, 2), std::runtime_error);
232  }
233 
234  TestMatrixIds(ia, matrix);
235 }
236 
238 
239 int main(int narg, char *arg[]) {
241  using rowPart_t = rowAdapter_t::part_t;
242 
243  Tpetra::ScopeGuard tscope(&narg, &arg);
244  const auto comm = Tpetra::getDefaultComm();
245 
246  try {
247  Teuchos::ParameterList params;
248  params.set("input file", "simple");
249  params.set("file type", "Chaco");
250 
251  auto uinput = rcp(new UserInputForTests(params, comm));
252 
253  // Input crs matrix and row matrix cast from it.
254  const auto crsMatrix = uinput->getUITpetraCrsMatrix();
255  const auto rowMatrix = rcp_dynamic_cast<ztrowmatrix_t>(crsMatrix);
256 
257  const auto nrows = rowMatrix->getLocalNumRows();
258 
259  // To test migration in the input adapter we need a Solution object.
260  const auto env = rcp(new Zoltan2::Environment(comm));
261 
262  const int nWeights = 2;
263 
265  // User object is Tpetra::RowMatrix
267 
268  PrintFromRoot("Input adapter for Tpetra::RowMatrix");
269 
270  // Graph Adapters use crsGraph, original TpetraInput uses trM (=rowMatrix)
271  auto tpetraRowMatrixInput = rcp(new rowAdapter_t(rowMatrix, nWeights));
272 
273  verifyInputAdapter(*tpetraRowMatrixInput, *rowMatrix);
274 
275  rowPart_t *p = new rowPart_t[nrows];
276  memset(p, 0, sizeof(rowPart_t) * nrows);
277  ArrayRCP<rowPart_t> solnParts(p, 0, nrows, true);
278 
279  rowSoln_t solution(env, comm, nWeights);
280  solution.setParts(solnParts);
281 
282  ztrowmatrix_t *mMigrate = NULL;
283  tpetraRowMatrixInput->applyPartitioningSolution(*rowMatrix, mMigrate,
284  solution);
285  const auto newM = rcp(mMigrate);
286  auto cnewM = rcp_const_cast<const ztrowmatrix_t>(newM);
287  auto newInput = rcp(new rowAdapter_t(cnewM, nWeights));
288 
289  PrintFromRoot("Input adapter for Tpetra::RowMatrix migrated to proc 0");
290 
291  verifyInputAdapter(*newInput, *newM);
292 
293  } catch (std::exception &e) {
294  std::cout << e.what() << std::endl;
295  return EXIT_FAILURE;
296  }
297 
298  PrintFromRoot("PASS");
299 
300 }
void verifyInputAdapter(adapter_t &ia, matrix_t &matrix)
void PrintFromRoot(const std::string &message)
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > ztcrsmatrix_t
typename crsAdapter_t::ConstWeightsHostView1D::execution_space execspace_t
typename node_t::device_type device_t
Tpetra::RowMatrix< zscalar_t, zlno_t, zgno_t, znode_t > ztrowmatrix_t
Provides access for Zoltan2 to Tpetra::CrsMatrix data.
typename Zoltan2::InputTraits< ztcrsmatrix_t >::node_t node_t
int main(int narg, char **arg)
Definition: coloring1.cpp:199
common code used by tests
typename InputTraits< User >::part_t part_t
#define Z2_TEST_COMPARE_ARRAYS(val1, val2)
Provides access for Zoltan2 to Tpetra::RowMatrix data.
A PartitioningSolution is a solution to a partitioning problem.
#define Z2_TEST_DEVICE_HOST_VIEWS(deviceView, hostView)
#define Z2_TEST_EQUALITY(val1, val2)
Traits for application input objects.
Defines the TpetraRowMatrixAdapter class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Tpetra::Map::local_ordinal_type zlno_t
default_node_t node_t
The Kokkos node type. This is only meaningful for users of Tpetra objects.
#define Z2_TEST_THROW(code, ExceptType)
void TestMatrixIds(adapter_t &ia, matrix_t &matrix)
Tpetra::Map::global_ordinal_type zgno_t
#define Z2_TEST_NOTHROW(code)
void printMatrix(RCP< const Comm< int > > &comm, zlno_t nrows, const zgno_t *rowIds, const offset_t *offsets, const zgno_t *colIds)
Zoltan2::TpetraRowGraphAdapter< ztrowgraph_t > rowAdapter_t
Provides access for Zoltan2 to Tpetra::RowGraph data.