Zoltan2  Version of the Day
rcb_C.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 
53 #include <Zoltan2_InputTraits.hpp>
54 #include <vector>
55 #include <cstdlib>
56 
57 using namespace std;
58 using std::vector;
59 
64 // Zoltan2 is templated. What data types will we use for
65 // scalars (coordinate values and weights), for local ids, and
66 // for global ids?
67 //
68 // If Zoltan2 was compiled with explicit instantiation, we will
69 // use the library's data types. These macros are defined
70 // in Zoltan2_config.h.
71 
72 #ifdef HAVE_ZOLTAN2_INST_FLOAT_INT_LONG
73 typedef float scalar_t;
74 typedef int localId_t;
75 typedef long globalId_t;
76 #else
77  #ifdef HAVE_ZOLTAN2_INST_DOUBLE_INT_LONG
78  typedef double scalar_t;
79  typedef int localId_t;
80  typedef long globalId_t;
81  #else
82  #ifdef HAVE_ZOLTAN2_INST_FLOAT_INT_INT
83  typedef float scalar_t;
84  typedef int localId_t;
85  typedef int globalId_t;
86  #else
87  #ifdef HAVE_ZOLTAN2_INST_DOUBLE_INT_INT
88  typedef double scalar_t;
89  typedef int localId_t;
90  typedef int globalId_t;
91  #else
92  typedef float scalar_t;
93  typedef int localId_t;
94  typedef int globalId_t;
95  #endif
96  #endif
97  #endif
98 #endif
99 
100 
101 int main(int argc, char *argv[])
102 {
103 #ifdef HAVE_ZOLTAN2_MPI
104  MPI_Init(&argc, &argv);
105  int rank, nprocs;
106  MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
107  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
108 #else
109  int rank=0, nprocs=1;
110 #endif
111 
112  // TODO explain
114 
115  // TODO explain
117 
119  // Create input data.
120 
121  size_t localCount = 40;
122  int dim = 3;
123 
124  scalar_t *coords = new scalar_t [dim * localCount];
125 
126  scalar_t *x = coords;
127  scalar_t *y = x + localCount;
128  scalar_t *z = y + localCount;
129 
130  // Create coordinates that range from 0 to 10.0
131 
132  srand(rank);
133  scalar_t scalingFactor = 10.0 / RAND_MAX;
134 
135  for (size_t i=0; i < localCount*dim; i++){
136  coords[i] = scalar_t(rand()) * scalingFactor;
137  }
138 
139  // Create global ids for the coordinates.
140 
141  globalId_t *globalIds = new globalId_t [localCount];
142  globalId_t offset = rank * localCount;
143 
144  for (size_t i=0; i < localCount; i++)
145  globalIds[i] = offset++;
146 
148  // Create parameters for an RCB problem
149 
150  double tolerance = 1.1;
151 
152  if (rank == 0)
153  std::cout << "Imbalance tolerance is " << tolerance << std::endl;
154 
155  Teuchos::ParameterList params("test params");
156  params.set("debug_level", "basic_status");
157  params.set("debug_procs", "0");
158  params.set("error_check_level", "debug_mode_assertions");
159 
160  params.set("compute_metrics", "true");
161  params.set("algorithm", "rcb");
162  params.set("imbalance_tolerance", tolerance );
163  params.set("num_global_parts", nprocs);
164 
165  params.set("bisection_num_test_cuts", 1);
166 
169  // A simple problem with no weights.
172 
173  // Create a Zoltan2 input adapter for this geometry. TODO explain
174 
175  inputAdapter_t ia1(localCount, globalIds, x, y, z, 1, 1, 1);
176 
177  // Create a Zoltan2 partitioning problem
178 
179 #ifdef HAVE_ZOLTAN2_MPI
182  MPI_COMM_WORLD);
183 #else
186 #endif
187 
188  // Solve the problem
189 
190  problem1->solve();
191 
192  // Check the solution.
193 
194  if (rank == 0)
195  problem1->printMetrics(cout);
196 
197  if (rank == 0){
198  scalar_t imb = problem1->getImbalance();
199  if (imb <= tolerance)
200  std::cout << "pass: " << imb << std::endl;
201  else
202  std::cout << "fail: " << imb << std::endl;
203  std::cout << std::endl;
204  }
205 
208  // Try a problem with weights (1 dimension)
211 
212  scalar_t *weights = new scalar_t [localCount];
213  for (size_t i=0; i < localCount; i++){
214  weights[i] = 1.0 + scalar_t(rank) / scalar_t(nprocs);
215  }
216 
217  // Create a Zoltan2 input adapter that includes weights.
218 
219  vector<const scalar_t *>coordVec(2);
220  vector<int> coordStrides(2);
221 
222  coordVec[0] = x; coordStrides[0] = 1;
223  coordVec[1] = y; coordStrides[1] = 1;
224 
225  vector<const scalar_t *>weightVec(1);
226  vector<int> weightStrides(1);
227 
228  weightVec[0] = weights; weightStrides[0] = 1;
229 
230  inputAdapter_t ia2(
231  localCount, globalIds,
232  coordVec, coordStrides,
233  weightVec, weightStrides);
234 
235  // Create a Zoltan2 partitioning problem
236 
237 #ifdef HAVE_ZOLTAN2_MPI
240  MPI_COMM_WORLD);
241 #else
244 #endif
245 
246  // Solve the problem
247 
248  problem2->solve();
249 
250  // Check the solution.
251 
252  if (rank == 0)
253  problem2->printMetrics(cout);
254 
255  if (rank == 0){
256  scalar_t imb = problem2->getImbalance();
257  if (imb <= tolerance)
258  std::cout << "pass: " << imb << std::endl;
259  else
260  std::cout << "fail: " << imb << std::endl;
261  std::cout << std::endl;
262  }
263 
264  if (localCount > 0){
265  delete [] weights;
266  weights = NULL;
267  }
268 
271  // Try a problem with multiple weights.
274 
275  // Add to the parameters the multicriteria objective.
276 
277  params.set("partitioning_objective", "multicriteria_minimize_total_weight");
278 
279  // Create the new weights.
280 
281  weights = new scalar_t [localCount*3];
282  srand(rank);
283 
284  for (size_t i=0; i < localCount*3; i+=3){
285  weights[i] = 1.0 + rank / nprocs; // weight dimension 1
286  weights[i+1] = rank<nprocs/2 ? 1 : 2; // weight dimension 2
287  weights[i+2] = rand()/RAND_MAX +.5; // weight dimension 3
288  }
289 
290  // Create a Zoltan2 input adapter with these weights.
291 
292  weightVec.resize(3);
293  weightStrides.resize(3);
294 
295  weightVec[0] = weights; weightStrides[0] = 3;
296  weightVec[1] = weights+1; weightStrides[1] = 3;
297  weightVec[2] = weights+2; weightStrides[2] = 3;
298 
299  inputAdapter_t ia3(
300  localCount, globalIds,
301  coordVec, coordStrides,
302  weightVec, weightStrides);
303 
304  // Create a Zoltan2 partitioning problem.
305 
306 #ifdef HAVE_ZOLTAN2_MPI
309  MPI_COMM_WORLD);
310 #else
313 #endif
314 
315  // Solve the problem
316 
317  problem3->solve();
318 
319  // Check the solution.
320 
321  if (rank == 0)
322  problem3->printMetrics(cout);
323 
324  if (rank == 0){
325  scalar_t imb = problem3->getImbalance();
326  if (imb <= tolerance)
327  std::cout << "pass: " << imb << std::endl;
328  else
329  std::cout << "fail: " << imb << std::endl;
330  std::cout << std::endl;
331  }
332 
334  // Try the other multicriteria objectives.
335 
336  bool dataHasChanged = false; // default is true
337 
338  params.set("partitioning_objective", "multicriteria_minimize_maximum_weight");
339  problem3->resetParameters(&params);
340  problem3->solve(dataHasChanged);
341  if (rank == 0){
342  problem3->printMetrics(cout);
343  scalar_t imb = problem3->getImbalance();
344  if (imb <= tolerance)
345  std::cout << "pass: " << imb << std::endl;
346  else
347  std::cout << "fail: " << imb << std::endl;
348  std::cout << std::endl;
349  }
350 
351  params.set("partitioning_objective", "multicriteria_balance_total_maximum");
352  problem3->resetParameters(&params);
353  problem3->solve(dataHasChanged);
354  if (rank == 0){
355  problem3->printMetrics(cout);
356  scalar_t imb = problem3->getImbalance();
357  if (imb <= tolerance)
358  std::cout << "pass: " << imb << std::endl;
359  else
360  std::cout << "fail: " << imb << std::endl;
361  std::cout << std::endl;
362  }
363 
364  if (localCount > 0){
365  delete [] weights;
366  weights = NULL;
367  }
368 
371  // Using part sizes, ask for some parts to be empty.
374 
375  // Change the number of parts to twice the number of processes to
376  // ensure that we have more than one global part.
377 
378  params.set("num_global_parts", nprocs*2);
379 
380  // Using the initial problem that did not have any weights, reset
381  // parameter list, and give it some part sizes.
382 
383  problem1->resetParameters(&params);
384 
385  zoltan2_partId_t *partIds = new zoltan2_partId_t [2];
386  scalar_t *partSizes = new scalar_t [2];
387 
388  partIds[0] = rank*2; partSizes[0] = 0;
389  partIds[1] = rank*2+1; partSizes[1] = 1;
390 
391  problem1->setPartSizes(2, partIds, partSizes);
392 
393  // Solve the problem. The argument "dataHasChanged" indicates
394  // that we have not changed the input data, which allows the problem
395  // so skip some work when re-solving.
396 
397  dataHasChanged = false;
398 
399  problem1->solve(dataHasChanged);
400 
401  // Obtain the solution
402 
404  problem1->getSolution();
405 
406  // Check it. Part sizes should all be odd.
407 
408  const zoltan2_partId_t *partAssignments = solution4.getPartList();
409 
410  int numInEmptyParts = 0;
411  for (size_t i=0; i < localCount; i++){
412  if (partAssignments[i] % 2 == 0)
413  numInEmptyParts++;
414  }
415 
416  if (rank == 0)
417  std::cout << "Request that " << nprocs << " parts be empty." <<std::endl;
418 
419  // Check the solution.
420 
421  if (rank == 0)
422  problem1->printMetrics(cout);
423 
424  if (rank == 0){
425  scalar_t imb = problem1->getImbalance();
426  if (imb <= tolerance)
427  std::cout << "pass: " << imb << std::endl;
428  else
429  std::cout << "fail: " << imb << std::endl;
430  std::cout << std::endl;
431  }
432 
433  delete [] partIds;
434  partIds = NULL;
435  delete [] partSizes;
436  partSizes = NULL;
437 
438  if (coords)
439  delete [] coords;
440 
441  if (globalIds)
442  delete [] globalIds;
443 
444  delete problem1;
445  delete problem2;
446  delete problem3;
447 
448 #ifdef HAVE_ZOLTAN2_MPI
449  MPI_Finalize();
450 #endif
451 
452  if (rank == 0)
453  std::cout << "PASS" << std::endl;
454 }
455 
int localId_t
Definition: block.cpp:91
A simple class that can be the User template argument for an InputAdapter.
int globalId_t
Definition: block.cpp:92
Defines the BasicCoordinateInput class.
Defines the PartitioningSolution class.
void resetParameters(ParameterList *params)
Reset the list of parameters.
int main(int argc, char *argv[])
int zoltan2_partId_t
a data type for part IDs
const zoltan2_partId_t * getPartList() const
Returns the part list corresponding to the global ID list.
A PartitioningSolution is a solution to a partitioning problem.
Traits for application input objects.
void printMetrics(ostream &os) const
Print the array of metrics.
double scalar_t
const PartitioningSolution< Adapter > & getSolution()
Get the solution to the problem.
PartitioningProblem sets up partitioning problems for the user.
void setPartSizes(int len, partId_t *partIds, scalar_t *partSizes, bool makeCopy=true)
Set or reset relative sizes for the partitions that Zoltan2 will create.
Defines the PartitioningProblem class.
BasicCoordinateInput represents geometric coordinates that are supplied by the user as pointers to st...
void solve(bool updateInputData=true)
Direct the problem to create a solution.
Zoltan2::BasicIdentifierInput< myTypes_t > inputAdapter_t
const scalar_t getImbalance(int dim=0) const
Returns the imbalance of the solution.