Zoltan2
 All Namespaces Files Functions Variables Pages
kokkosBlock.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 
50 #include <Kokkos_Core.hpp>
51 #include <Teuchos_DefaultComm.hpp>
52 #include <Zoltan2_BasicKokkosIdentifierAdapter.hpp>
53 #include <Zoltan2_PartitioningProblem.hpp>
54 #include <Zoltan2_PartitioningSolution.hpp>
55 
56 using Teuchos::Comm;
57 using Teuchos::RCP;
58 
65 int main(int narg, char *arg[]) {
66  Tpetra::ScopeGuard tscope(&narg, &arg);
67  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
68 
69  int rank = comm->getRank();
70  int nprocs = comm->getSize();
71 
72  // For convenience, we'll use the Tpetra defaults for local/global ID types
73  // Users can substitute their preferred local/global ID types
74  typedef Tpetra::Map<> Map_t;
75  typedef Map_t::local_ordinal_type localId_t;
76  typedef Map_t::global_ordinal_type globalId_t;
77  typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
78  typedef Tpetra::Map<>::node_type node_t;
79 
81  // Generate some input data.
82 
83  int localCount = 40 * (rank + 1);
84  int totalCount = 20 * nprocs * (nprocs + 1);
85  int targetCount = totalCount / nprocs;
86 
87  Kokkos::View<globalId_t*, typename node_t::device_type>
88  globalIds(Kokkos::ViewAllocateWithoutInitializing("globalIds"), localCount);
89  auto host_globalIds = Kokkos::create_mirror_view(globalIds);
90 
91  if (rank == 0) {
92  for (int i = 0, num = 40; i < nprocs ; i++, num += 40) {
93  std::cout << "Rank " << i << " generates " << num << " ids." << std::endl;
94  }
95  }
96 
97  globalId_t offset = 0;
98  for (int i = 1; i <= rank; i++) {
99  offset += 40 * i;
100  }
101 
102  for (int i = 0; i < localCount; i++) {
103  host_globalIds(i) = offset++;
104  }
105  Kokkos::deep_copy(globalIds, host_globalIds);
106 
108  // Create a Zoltan2 input adapter with no weights
109 
110  typedef Zoltan2::BasicUserTypes<scalar_t, localId_t, globalId_t> myTypes;
111  typedef Zoltan2::BasicKokkosIdentifierAdapter<myTypes> inputAdapter_t;
112 
113  const int nWeights = 1;
114  Kokkos::View<scalar_t **, typename node_t::device_type>
115  weights("weights", localCount, nWeights);
116  for (int index = 0; index < localCount; index++) {
117  weights(index, 0) = 1; // Error check relies on uniform weights
118  }
119 
120  inputAdapter_t ia(globalIds, weights);
121 
123  // Create parameters for a Block problem
124 
125  Teuchos::ParameterList params("test params");
126  params.set("debug_level", "basic_status");
127  params.set("debug_procs", "0");
128  params.set("error_check_level", "debug_mode_assertions");
129 
130  params.set("algorithm", "block");
131  params.set("imbalance_tolerance", 1.1);
132  params.set("num_global_parts", nprocs);
133 
135  // Create a Zoltan2 partitioning problem
136 
137  Zoltan2::PartitioningProblem<inputAdapter_t> *problem =
138  new Zoltan2::PartitioningProblem<inputAdapter_t>(&ia, &params);
139 
141  // Solve the problem - do the partitioning
142 
143  problem->solve();
144 
146  // Check and print the solution.
147  // Count number of IDs assigned to each part; compare to targetCount
148 
149  Kokkos::View<const globalId_t *, typename node_t::device_type> ids;
150  ia.getIDsKokkosView(ids);
151 
152  auto host_ids = Kokkos::create_mirror_view(ids);
153  Kokkos::deep_copy(host_ids, ids);
154 
155  Kokkos::View<int*, Kokkos::HostSpace> partCounts("partCounts", nprocs);
156 
157  Kokkos::View<int*, Kokkos::HostSpace>
158  globalPartCounts("globalPartCounts", nprocs);
159 
160  for (size_t i = 0; i < ia.getLocalNumIDs(); i++) {
161  int pp = problem->getSolution().getPartListView()[i];
162  std::cout << rank << " LID " << i << " GID " << host_ids(i)
163  << " PART " << pp << std::endl;
164  partCounts(pp)++;
165  }
166 
167  Teuchos::reduceAll<int, int>(*comm, Teuchos::REDUCE_SUM, nprocs,
168  partCounts.data(), globalPartCounts.data());
169 
170  if (rank == 0) {
171  int ierr = 0;
172  for (int i = 0; i < nprocs; i++) {
173  if (globalPartCounts(i) != targetCount) {
174  std::cout << "FAIL: part " << i << " has " << globalPartCounts(i)
175  << " != " << targetCount << "; " << ++ierr << " errors"
176  << std::endl;
177  }
178  }
179  if (ierr == 0) {
180  std::cout << "PASS" << std::endl;
181  }
182  }
183 
184  delete problem;
185 }
186 
int main(int argc, char *argv[])
Definition: block.cpp:60