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