Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
block.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 
17 
24 int main(int argc, char *argv[]) {
25 #ifdef HAVE_ZOLTAN2_MPI
26  MPI_Init(&argc, &argv);
27  int rank, nprocs;
28  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
29  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
30 #else
31  int rank=0, nprocs=1;
32 #endif
33 
34  // For convenience, we'll use the Tpetra defaults for local/global ID types
35  // Users can substitute their preferred local/global ID types
36  typedef Tpetra::Map<> Map_t;
37  typedef Map_t::local_ordinal_type localId_t;
38  typedef Map_t::global_ordinal_type globalId_t;
39  typedef Tpetra::Details::DefaultTypes::scalar_type scalar_t;
40 
42  // Generate some input data.
43 
44  int localCount = 40*(rank+1);
45  int totalCount = 20*nprocs*(nprocs+1);
46  int targetCount = totalCount / nprocs;
47  globalId_t *globalIds = new globalId_t[localCount];
48 
49  if (rank==0) {
50  for (int i=0, num=40; i < nprocs ; i++, num+=40) {
51  std::cout << "Rank " << i << " generates " << num << " ids." << std::endl;
52  }
53  }
54 
55  globalId_t offset = 0;
56  for (int i=1; i <= rank; i++) {
57  offset += 40*i;
58  }
59 
60  for (int i=0; i < localCount; i++) {
61  globalIds[i] = offset++;
62  }
63 
65  // Create a Zoltan2 input adapter with no weights
66 
67  // TODO explain
69 
70  // TODO explain
71  typedef Zoltan2::BasicIdentifierAdapter<myTypes> inputAdapter_t;
72 
73  std::vector<const scalar_t *> noWeights;
74  std::vector<int> noStrides;
75 
76  inputAdapter_t ia(localCount, globalIds, noWeights, noStrides);
77 
79  // Create parameters for a Block problem
80 
81  Teuchos::ParameterList params("test params");
82  params.set("debug_level", "basic_status");
83  params.set("debug_procs", "0");
84  params.set("error_check_level", "debug_mode_assertions");
85 
86  params.set("algorithm", "block");
87  params.set("imbalance_tolerance", 1.1);
88  params.set("num_global_parts", nprocs);
89 
91  // Create a Zoltan2 partitioning problem
92 
95 
97  // Solve the problem - do the partitioning
98 
99  problem->solve();
100 
102  // Check and print the solution.
103  // Count number of IDs assigned to each part; compare to targetCount
104 
105  const globalId_t *ids = NULL;
106  ia.getIDsView(ids);
107  std::vector<int> partCounts(nprocs, 0), globalPartCounts(nprocs, 0);
108 
109  for (size_t i = 0; i < ia.getLocalNumIDs(); i++) {
110  int pp = problem->getSolution().getPartListView()[i];
111  std::cout << rank << " LID " << i << " GID " << ids[i]
112  << " PART " << pp << std::endl;
113  partCounts[pp]++;
114  }
115 
116 #ifdef HAVE_ZOLTAN2_MPI
117  MPI_Allreduce(&(partCounts[0]), &(globalPartCounts[0]), nprocs,
118  MPI_INT, MPI_SUM, MPI_COMM_WORLD);
119 #else
120  for (int i = 0; i < nprocs; i++) globalPartCounts[i] = partCounts[i];
121 #endif
122 
123  if (rank == 0) {
124  int ierr = 0;
125  for (int i = 0; i < nprocs; i++) {
126  if (globalPartCounts[i] != targetCount) {
127  std::cout << "FAIL: part " << i << " has " << globalPartCounts[i]
128  << " != " << targetCount << "; " << ++ierr << " errors"
129  << std::endl;
130  }
131  }
132  if (ierr == 0) {
133  std::cout << "PASS" << std::endl;
134  }
135  }
136 
137  delete [] globalIds;
138  delete problem;
139 #ifdef HAVE_ZOLTAN2_MPI
140  MPI_Finalize();
141 #endif
142 }
A simple class that can be the User template argument for an InputAdapter.
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 BasicIdentifierAdapter 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.