Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
kokkosBlock.cpp
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 #include <Kokkos_Core.hpp>
15 #include <Teuchos_DefaultComm.hpp>
19 
20 using Teuchos::Comm;
21 using Teuchos::RCP;
22 
29 int main(int narg, char *arg[]) {
30  Tpetra::ScopeGuard tscope(&narg, &arg);
31  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
32 
33  int rank = comm->getRank();
34  int nprocs = comm->getSize();
35 
36  // For convenience, we'll use the Tpetra defaults for local/global ID types
37  // Users can substitute their preferred local/global ID types
38  typedef Tpetra::Map<> Map_t;
39  typedef Map_t::local_ordinal_type localId_t;
40  typedef Map_t::global_ordinal_type globalId_t;
41  typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
42  typedef Tpetra::Map<>::node_type node_t;
43 
45  // Generate some input data.
46 
47  int localCount = 40 * (rank + 1);
48  int totalCount = 20 * nprocs * (nprocs + 1);
49  int targetCount = totalCount / nprocs;
50 
51  Kokkos::View<globalId_t*, typename node_t::device_type>
52  globalIds(Kokkos::ViewAllocateWithoutInitializing("globalIds"), localCount);
53  auto host_globalIds = Kokkos::create_mirror_view(globalIds);
54 
55  if (rank == 0) {
56  for (int i = 0, num = 40; i < nprocs ; i++, num += 40) {
57  std::cout << "Rank " << i << " generates " << num << " ids." << std::endl;
58  }
59  }
60 
61  globalId_t offset = 0;
62  for (int i = 1; i <= rank; i++) {
63  offset += 40 * i;
64  }
65 
66  for (int i = 0; i < localCount; i++) {
67  host_globalIds(i) = offset++;
68  }
69  Kokkos::deep_copy(globalIds, host_globalIds);
70 
72  // Create a Zoltan2 input adapter with no weights
73 
76 
77  const int nWeights = 1;
78  Kokkos::View<scalar_t **, typename node_t::device_type>
79  weights("weights", localCount, nWeights);
80  auto host_weights = Kokkos::create_mirror_view(weights);
81 
82  for (int index = 0; index < localCount; index++) {
83  host_weights(index, 0) = 1; // Error check relies on uniform weights
84  }
85 
86  Kokkos::deep_copy(weights, host_weights);
87 
88  inputAdapter_t ia(globalIds, weights);
89 
91  // Create parameters for a Block problem
92 
93  Teuchos::ParameterList params("test params");
94  params.set("debug_level", "basic_status");
95  params.set("debug_procs", "0");
96  params.set("error_check_level", "debug_mode_assertions");
97 
98  params.set("algorithm", "block");
99  params.set("imbalance_tolerance", 1.1);
100  params.set("num_global_parts", nprocs);
101 
103  // Create a Zoltan2 partitioning problem
104 
107 
109  // Solve the problem - do the partitioning
110 
111  problem->solve();
112 
114  // Check and print the solution.
115  // Count number of IDs assigned to each part; compare to targetCount
116 
117  Kokkos::View<const globalId_t *, typename node_t::device_type> ids;
118  ia.getIDsKokkosView(ids);
119 
120  auto host_ids = Kokkos::create_mirror_view(ids);
121  Kokkos::deep_copy(host_ids, ids);
122 
123  Kokkos::View<int*, Kokkos::HostSpace> partCounts("partCounts", nprocs);
124 
125  Kokkos::View<int*, Kokkos::HostSpace>
126  globalPartCounts("globalPartCounts", nprocs);
127 
128  for (size_t i = 0; i < ia.getLocalNumIDs(); i++) {
129  int pp = problem->getSolution().getPartListView()[i];
130  std::cout << rank << " LID " << i << " GID " << host_ids(i)
131  << " PART " << pp << std::endl;
132  partCounts(pp)++;
133  }
134 
135  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, nprocs,
136  partCounts.data(), globalPartCounts.data());
137 
138  if (rank == 0) {
139  int ierr = 0;
140  for (int i = 0; i < nprocs; i++) {
141  if (globalPartCounts(i) != targetCount) {
142  std::cout << "FAIL: part " << i << " has " << globalPartCounts(i)
143  << " != " << targetCount << "; " << ++ierr << " errors"
144  << std::endl;
145  }
146  }
147  if (ierr == 0) {
148  std::cout << "PASS" << std::endl;
149  }
150  }
151 
152  delete problem;
153 }
154 
static ArrayRCP< ArrayRCP< zscalar_t > > weights
A simple class that can be the User template argument for an InputAdapter.
typename Zoltan2::InputTraits< ztcrsmatrix_t >::node_t node_t
int main(int narg, char **arg)
Definition: coloring1.cpp:164
Defines the PartitioningSolution class.
This class represents a collection of global Identifiers and their associated weights, if any.
Defines the BasicKokkosIdentifierAdapter class.
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
Defines the PartitioningProblem class.
void solve(bool updateInputData=true)
Direct the problem to create a solution.