Zoltan2  Version of the Day
PartitioningSolution.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 PartitioningSolution class.
47 //
48 // Normally a Solution is created by a Problem.
49 // We create a few Solutions in this unit test.
50 
51 // TODO test ::convertSolutionToImportList
52 
55 #include <Zoltan2_TestHelpers.hpp>
56 
59 
60 using Teuchos::ArrayRCP;
61 using Teuchos::Array;
62 using Teuchos::RCP;
63 using Teuchos::rcp;
64 using Teuchos::arcp;
65 
66 
68 
69 void makeArrays(int wdim, int *lens, partId_t **ids, scalar_t **sizes,
70  ArrayRCP<ArrayRCP<partId_t> > &idList, ArrayRCP<ArrayRCP<scalar_t> > &sizeList)
71 {
72  ArrayRCP<partId_t> *idArrays = new ArrayRCP<partId_t> [wdim];
73  ArrayRCP<scalar_t> *sizeArrays = new ArrayRCP<scalar_t> [wdim];
74 
75  for (int w=0; w < wdim; w++){
76  idArrays[w] = arcp(ids[w], 0, lens[w], false);
77  sizeArrays[w] = arcp(sizes[w], 0, lens[w], false);
78  }
79 
80  idList = arcp(idArrays, 0, wdim, true);
81  sizeList = arcp(sizeArrays, 0, wdim, true);
82 }
83 
84 int main(int argc, char *argv[])
85 {
86  Teuchos::GlobalMPISession session(&argc, &argv);
87  RCP<const Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
88  int nprocs = comm->getSize();
89  int rank = comm->getRank();
90  int fail=0, gfail=0;
91  double epsilon = 10e-6;
92 
94  // Arrays to hold part Ids and part Sizes for each weight dimension.
95 
96  int numIdsPerProc = 10;
97  int maxWeightDim = 3;
98  int maxNumPartSizes = nprocs;
99  int *lengths = new int [maxWeightDim];
100  partId_t **idLists = new partId_t * [maxWeightDim];
101  scalar_t **sizeLists = new scalar_t * [maxWeightDim];
102 
103  for (int w=0; w < maxWeightDim; w++){
104  idLists[w] = new partId_t [maxNumPartSizes];
105  sizeLists[w] = new scalar_t [maxNumPartSizes];
106  }
107 
109  // A default environment
110  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
111 
113  // A simple identifier map.
114 
115  gno_t *myGids = new gno_t [numIdsPerProc];
116  for (int i=0, x=rank*numIdsPerProc; i < numIdsPerProc; i++){
117  myGids[i] = x++;
118  }
119 
120  ArrayRCP<const gno_t> gidArray(myGids, 0, numIdsPerProc, true);
121 
122  RCP<const Zoltan2::IdentifierMap<user_t> > idMap =
123  rcp(new Zoltan2::IdentifierMap<user_t>(env, comm, gidArray));
124 
126  // TEST:
127  // One weight dimension, one part per proc.
128  // Some part sizes are 2 and some are 1.
129 
130  int numGlobalParts = nprocs;
131  int weightDim = 1;
132 
133  ArrayRCP<ArrayRCP<partId_t> > ids;
134  ArrayRCP<ArrayRCP<scalar_t> > sizes;
135 
136  memset(lengths, 0, sizeof(int) * maxWeightDim);
137 
138  lengths[0] = 1; // We give a size for 1 part.
139  idLists[0][0] = rank; // The part is my part.
140  sizeLists[0][0] = rank%2 + 1.0; // The size is 1.0 or 2.0
141 
142  makeArrays(1, lengths, idLists, sizeLists, ids, sizes);
143 
144  // Normalized part size for every part, for checking later on
145 
146  scalar_t *normalizedPartSizes = new scalar_t [numGlobalParts];
147  scalar_t sumSizes=0;
148  for (int i=0; i < numGlobalParts; i++){
149  normalizedPartSizes[i] = 1.0;
150  if (i % 2) normalizedPartSizes[i] = 2.0;
151  sumSizes += normalizedPartSizes[i];
152  }
153  for (int i=0; i < numGlobalParts; i++)
154  normalizedPartSizes[i] /= sumSizes;
155 
157  // Create a solution object with part size information, and check it.
158 
159  RCP<Zoltan2::PartitioningSolution<idInput_t> > solution;
160 
161  try{
162  solution = rcp(new Zoltan2::PartitioningSolution<idInput_t>(
163  env, // application environment info
164  comm, // problem communicator
165  idMap, // problem identifiers (global Ids, local Ids)
166  weightDim, // weight dimension
167  ids.view(0,weightDim), // part ids
168  sizes.view(0,weightDim))); // part sizes
169  }
170  catch (std::exception &e){
171  fail=1;
172  }
173 
174  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor call 1", 1);
175 
176  // Test the Solution queries that are used by algorithms
177 
178  if (solution->getTargetGlobalNumberOfParts() != size_t(numGlobalParts))
179  fail=2;
180 
181  if (!fail && solution->getLocalNumberOfParts() != 1)
182  fail=3;
183 
184  if (!fail && !solution->oneToOnePartDistribution())
185  fail=4;
186 
187  if (!fail && solution->getPartDistribution() != NULL)
188  fail=5;
189 
190  if (!fail && solution->getProcDistribution() != NULL)
191  fail=6;
192 
193  if (!fail &&
194  ((nprocs>1 && solution->criteriaHasUniformPartSizes(0)) ||
195  (nprocs==1 && !solution->criteriaHasUniformPartSizes(0))) )
196  fail=8;
197 
198  if (!fail){
199  for (int partId=0; !fail && partId < numGlobalParts; partId++){
200  scalar_t psize = solution->getCriteriaPartSize(0, partId);
201 
202  if ( psize < normalizedPartSizes[partId] - epsilon ||
203  psize > normalizedPartSizes[partId] + epsilon )
204  fail=9;
205  }
206  }
207 
208  delete [] normalizedPartSizes;
209 
210  gfail = globalFail(comm, fail);
211  if (gfail){
212  printFailureCode(comm, fail); // exits after printing "FAIL"
213  }
214 
215  // Test the Solution set method that is called by algorithms
216 
217  partId_t *partAssignments = new partId_t [numIdsPerProc];
218  for (int i=0; i < numIdsPerProc; i++){
219  partAssignments[i] = myGids[i] % numGlobalParts; // round robin
220  }
221  ArrayRCP<partId_t> partList = arcp(partAssignments, 0, numIdsPerProc);
222 
223  try{
224  solution->setParts(gidArray, partList, true);
225  }
226  catch (std::exception &e){
227  fail=10;
228  }
229 
230  gfail = globalFail(comm, fail);
231  if (gfail){
232  printFailureCode(comm, fail); // exits after printing "FAIL"
233  }
234 
235  // Test the Solution get methods that may be called by users
236  // or migration functions.
237 
238  if (solution->getLocalNumberOfIds() != size_t(numIdsPerProc))
239  fail = 11;
240 
241  if (!fail){
242  const gno_t *gids = solution->getIdList();
243  for (int i=0; !fail && i < numIdsPerProc; i++){
244  if (gids[i] != myGids[i])
245  fail = 12;
246  }
247  }
248 
249  if (!fail){
250  const partId_t *parts = solution->getPartList();
251  for (int i=0; !fail && i < numIdsPerProc; i++){
252  if (parts[i] != myGids[i] % numGlobalParts)
253  fail = 13;
254  }
255  }
256 
257  gfail = globalFail(comm, fail);
258  if (gfail){
259  printFailureCode(comm, fail); // exits after printing "FAIL"
260  }
261 
262  if (rank==0)
263  std::cout << "PASS" << std::endl;
264 
266  // TODO:
268  // Create a solution object without part size information, and check it.
270  // Test multiple weights.
272  // Test multiple parts per process.
274  // Specify a list of parts of size 0. (The rest should be uniform.)
275 
276  delete [] lengths;
277  for (int w=0; w < maxWeightDim; w++){
278  delete [] idLists[w];
279  delete [] sizeLists[w];
280  }
281  delete [] idLists;
282  delete [] sizeLists;
283 }
An IdentifierMap manages a global space of object identifiers.
int globalFail(const RCP< const Comm< int > > &comm, int fail)
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)
Defines the PartitioningSolution class.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > user_t
void makeArrays(int wdim, int *lens, partId_t **ids, scalar_t **sizes, ArrayRCP< ArrayRCP< partId_t > > &idList, ArrayRCP< ArrayRCP< scalar_t > > &sizeList)
common code used by tests
zoltan2_partId_t partId_t
int main(int argc, char *argv[])
list idList
Match up parameters to validators.
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.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
double scalar_t
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
Defines the BasicIdentifierInput class.