Zoltan2
Metric.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 // Test the following:
47 // EvaluatePartition class
48 // MetricValues class
49 // Metric related namespace methods
50 
51 
53 #include <Zoltan2_TestHelpers.hpp>
55 #include <stdlib.h>
56 #include <vector>
57 
58 
59 using Teuchos::ArrayRCP;
60 using Teuchos::Array;
61 using Teuchos::RCP;
62 using Teuchos::rcp;
63 using Teuchos::arcp;
64 
65 using namespace std;
66 using std::endl;
67 using std::cout;
68 
69 
70 void doTest(RCP<const Comm<int> > comm, int numLocalObj,
71  int nWeights, int numLocalParts, bool givePartSizes);
72 
73 int main(int argc, char *argv[])
74 {
75  Teuchos::GlobalMPISession session(&argc, &argv);
76  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
77  int rank = comm->getRank();
78 
79  doTest(comm, 10, 0, -1, false);
80  doTest(comm, 10, 0, 1, false);
81  doTest(comm, 10, 0, 1, true);
82  doTest(comm, 10, 1, 1, false);
83  doTest(comm, 10, 1, 1, true);
84  doTest(comm, 10, 2, 1, false);
85  doTest(comm, 10, 2, 1, true);
86  doTest(comm, 10, 1, 2, true);
87  doTest(comm, 10, 1, 2, false);
88  doTest(comm, 10, 1, -1, false);
89  doTest(comm, 10, 1, -1, true);
90  doTest(comm, 10, 2, -1, false);
91 
92  if (rank==0)
93  cout << "PASS" << endl;
94 }
95 
96 // Assumes numLocalObj is the same on every process.
97 
98 void doTest(RCP<const Comm<int> > comm, int numLocalObj,
99  int nWeights, int numLocalParts, bool givePartSizes)
100 {
103  typedef Zoltan2::EvaluatePartition<idInput_t> quality_t;
104  typedef idInput_t::part_t part_t;
105 
106  int rank = comm->getRank();
107  int nprocs = comm->getSize();
108  int fail=0;
109  srand(rank+1);
110  bool testEmptyParts = (numLocalParts < 1);
111  int numGlobalParts = 0;
112 
113  if (testEmptyParts){
114  numGlobalParts = nprocs / 2;
115  if (numGlobalParts >= 1)
116  numLocalParts = (rank < numGlobalParts ? 1 : 0);
117  else{
118  numLocalParts = 1;
119  testEmptyParts = false;
120  }
121  }
122  else{
123  numGlobalParts = nprocs * numLocalParts;
124  }
125 
126  if (rank == 0){
127  cout << endl;
128  cout << "Test: number of weights " << nWeights;
129  cout << ", desired number of parts " << numGlobalParts;
130  if (givePartSizes)
131  cout << ", with differing part sizes." << endl;
132  else
133  cout << ", with uniform part sizes." << endl;
134  cout << "Number of procs " << nprocs;
135  cout << ", each with " << numLocalObj << " objects, part = rank." << endl;
136  }
137 
138  // An environment. This is usually created by the problem.
139 
140  Teuchos::ParameterList pl("test list");
141  pl.set("num_local_parts", numLocalParts);
142 
143  RCP<const Zoltan2::Environment> env =
144  rcp(new Zoltan2::Environment(pl, comm));
145 
146  // A simple identifier map. Usually created by the model.
147 
148  zgno_t *myGids = new zgno_t [numLocalObj];
149  for (int i=0, x=rank*numLocalObj; i < numLocalObj; i++, x++){
150  myGids[i] = x;
151  }
152 
153  // Part sizes. Usually supplied by the user to the Problem.
154  // Then the problem supplies them to the Solution.
155 
156  int partSizeDim = (givePartSizes ? (nWeights ? nWeights : 1) : 0);
157  ArrayRCP<ArrayRCP<part_t> > ids(partSizeDim);
158  ArrayRCP<ArrayRCP<zscalar_t> > sizes(partSizeDim);
159 
160  if (givePartSizes && numLocalParts > 0){
161  part_t *myParts = new part_t [numLocalParts];
162  myParts[0] = rank * numLocalParts;
163  for (int i=1; i < numLocalParts; i++)
164  myParts[i] = myParts[i-1] + 1;
165  ArrayRCP<part_t> partNums(myParts, 0, numLocalParts, true);
166 
167  zscalar_t sizeFactor = nprocs/2 - rank;
168  if (sizeFactor < 0) sizeFactor *= -1;
169  sizeFactor += 1;
170 
171  for (int dim=0; dim < partSizeDim; dim++){
172  zscalar_t *psizes = new zscalar_t [numLocalParts];
173  for (int i=0; i < numLocalParts; i++)
174  psizes[i] = sizeFactor;
175  sizes[dim] = arcp(psizes, 0, numLocalParts, true);
176 
177  ids[dim] = partNums;
178  }
179  }
180 
181  // An input adapter with random weights. Created by the user.
182 
183  std::vector<const zscalar_t *> weights;
184  std::vector<int> strides; // default to 1
185 
186  int len = numLocalObj*nWeights;
187  ArrayRCP<zscalar_t> wgtBuf;
188  zscalar_t *wgts = NULL;
189 
190  if (len > 0){
191  wgts = new zscalar_t [len];
192  wgtBuf = arcp(wgts, 0, len, true);
193  for (int i=0; i < len; i++)
194  wgts[i] = (zscalar_t(rand()) / zscalar_t(RAND_MAX)) + 1.0;
195  }
196 
197  for (int i=0; i < nWeights; i++, wgts+=numLocalObj)
198  weights.push_back(wgts);
199 
200  idInput_t *ia = NULL;
201 
202  try{
203  ia = new idInput_t(numLocalObj, myGids, weights, strides);
204  }
205  catch (std::exception &e){
206  fail=1;
207  }
208 
209  TEST_FAIL_AND_EXIT(*comm, fail==0, "create adapter", 1);
210 
211  // A solution (usually created by a problem)
212 
213  RCP<Zoltan2::PartitioningSolution<idInput_t> > solution;
214 
215  try{
216  if (givePartSizes)
217  solution = rcp(new Zoltan2::PartitioningSolution<idInput_t>(
218  env, comm, nWeights,
219  ids.view(0,partSizeDim), sizes.view(0,partSizeDim)));
220  else
221  solution = rcp(new Zoltan2::PartitioningSolution<idInput_t>(
222  env, comm, nWeights));
223  }
224  catch (std::exception &e){
225  fail=1;
226  }
227 
228  TEST_FAIL_AND_EXIT(*comm, fail==0, "create solution", 1);
229 
230  // Part assignment for my objects: The algorithm usually calls this.
231 
232  part_t *partNum = new part_t [numLocalObj];
233  ArrayRCP<part_t> partAssignment(partNum, 0, numLocalObj, true);
234  for (int i=0; i < numLocalObj; i++)
235  partNum[i] = rank;
236 
237  solution->setParts(partAssignment);
238 
239  // create metric object (also usually created by a problem)
240 
241  RCP<quality_t> metricObject;
242 
243  try{
244  metricObject = rcp(new quality_t(ia, &pl, comm, solution.getRawPtr()));
245  }
246  catch (std::exception &e){
247  fail=1;
248  }
249 
250  TEST_FAIL_AND_EXIT(*comm, fail==0, "compute metrics", 1);
251 
252 
253  if (rank==0){
254  ;
255  try{
256  zscalar_t imb = metricObject->getObjectCountImbalance();
257  cout << "Object imbalance: " << imb << endl;
258  }
259  catch (std::exception &e){
260  fail=1;
261  }
262  }
263 
264  TEST_FAIL_AND_EXIT(*comm, fail==0, "getObjectCountImbalance", 1);
265 
266  if (rank==0 && nWeights > 0){
267  try{
268  for (int i=0; i < nWeights; i++){
269  zscalar_t imb = metricObject->getWeightImbalance(i);
270  cout << "Weight " << i << " imbalance: " << imb << endl;
271  }
272  }
273  catch (std::exception &e){
274  fail=10;
275  }
276  if (!fail && nWeights > 1){
277  try{
278  zscalar_t imb = metricObject->getNormedImbalance();
279  cout << "Normed weight imbalance: " << imb << endl;
280  }
281  catch (std::exception &e){
282  fail=11;
283  }
284  }
285  }
286 
287  TEST_FAIL_AND_EXIT(*comm, fail==0, "get imbalances", 1);
288 
289  if (rank==0){
290  try{
291  metricObject->printMetrics(cout);
292  }
293  catch (std::exception &e){
294  fail=1;
295  }
296  }
297 
298  TEST_FAIL_AND_EXIT(*comm, fail==0, "print metrics", 1);
299  delete ia;
300 }
void doTest(RCP< const Comm< int > > comm, int numLocalObj, int nWeights, int numLocalParts, bool givePartSizes)
Definition: Metric.cpp:98
double zscalar_t
A simple class that can be the User template argument for an InputAdapter.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
static ArrayRCP< ArrayRCP< zscalar_t > > weights
common code used by tests
int main(int argc, char *argv[])
Definition: Metric.cpp:73
SparseMatrixAdapter_t::part_t part_t
This class represents a collection of global Identifiers and their associated weights, if any.
A PartitioningSolution is a solution to a partitioning problem.
Defines the EvaluatePartition class.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static const std::string fail
int zgno_t
Defines the BasicIdentifierAdapter class.
Zoltan2::BasicIdentifierAdapter< zzuser_t > idInput_t
A class that computes and returns quality metrics.