Zoltan2
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 
60 
61 using Teuchos::ArrayRCP;
62 using Teuchos::Array;
63 using Teuchos::RCP;
64 using Teuchos::rcp;
65 using Teuchos::arcp;
66 
67 
68 
69 void makeArrays(int wdim, int *lens, zzpart_t **ids, zscalar_t **sizes,
70  ArrayRCP<ArrayRCP<zzpart_t> > &idList, ArrayRCP<ArrayRCP<zscalar_t> > &sizeList)
71 {
72  ArrayRCP<zzpart_t> *idArrays = new ArrayRCP<zzpart_t> [wdim];
73  ArrayRCP<zscalar_t> *sizeArrays = new ArrayRCP<zscalar_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
95 
96  int numIdsPerProc = 10;
97  int maxNumWeights = 3;
98  int maxNumPartSizes = nprocs;
99  int *lengths = new int [maxNumWeights];
100  zzpart_t **idLists = new zzpart_t * [maxNumWeights];
101  zscalar_t **sizeLists = new zscalar_t * [maxNumWeights];
102 
103  for (int w=0; w < maxNumWeights; w++){
104  idLists[w] = new zzpart_t [maxNumPartSizes];
105  sizeLists[w] = new zscalar_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  zgno_t *myGids = new zgno_t [numIdsPerProc];
116  for (int i=0, x=rank*numIdsPerProc; i < numIdsPerProc; i++)
117  myGids[i] = x++;
118 
120  // TEST:
121  // One weight, one part per proc.
122  // Some part sizes are 2 and some are 1.
123 
124  int numGlobalParts = nprocs;
125  int nWeights = 1;
126 
127  ArrayRCP<ArrayRCP<zzpart_t> > ids;
128  ArrayRCP<ArrayRCP<zscalar_t> > sizes;
129 
130  memset(lengths, 0, sizeof(int) * maxNumWeights);
131 
132  lengths[0] = 1; // We give a size for 1 part.
133  idLists[0][0] = rank; // The part is my part.
134  sizeLists[0][0] = rank%2 + 1.0; // The size is 1.0 or 2.0
135 
136  makeArrays(1, lengths, idLists, sizeLists, ids, sizes);
137 
138  // Normalized part size for every part, for checking later on
139 
140  zscalar_t *normalizedPartSizes = new zscalar_t [numGlobalParts];
141  zscalar_t sumSizes=0;
142  for (int i=0; i < numGlobalParts; i++){
143  normalizedPartSizes[i] = 1.0;
144  if (i % 2) normalizedPartSizes[i] = 2.0;
145  sumSizes += normalizedPartSizes[i];
146  }
147  for (int i=0; i < numGlobalParts; i++)
148  normalizedPartSizes[i] /= sumSizes;
149 
151  // Create a solution object with part size information, and check it.
152 
153  RCP<Zoltan2::PartitioningSolution<idInput_t> > solution;
154 
155  try{
156  solution = rcp(new Zoltan2::PartitioningSolution<idInput_t>(
157  env, // application environment info
158  comm, // problem communicator
159  nWeights, // number of weights
160  ids.view(0,nWeights), // part ids
161  sizes.view(0,nWeights))); // part sizes
162  }
163  catch (std::exception &e){
164  fail=1;
165  }
166 
167  TEST_FAIL_AND_EXIT(*comm, fail==0, "constructor call 1", 1);
168 
169  // Test the Solution queries that are used by algorithms
170 
171  if (solution->getTargetGlobalNumberOfParts() != size_t(numGlobalParts))
172  fail=2;
173 
174  if (!fail && solution->getLocalNumberOfParts() != 1)
175  fail=3;
176 
177  if (!fail && !solution->oneToOnePartDistribution())
178  fail=4;
179 
180  if (!fail && solution->getPartDistribution() != NULL)
181  fail=5;
182 
183  if (!fail && solution->getProcDistribution() != NULL)
184  fail=6;
185 
186  if (!fail &&
187  ((nprocs>1 && solution->criteriaHasUniformPartSizes(0)) ||
188  (nprocs==1 && !solution->criteriaHasUniformPartSizes(0))) )
189  fail=8;
190 
191  if (!fail){
192  for (int partId=0; !fail && partId < numGlobalParts; partId++){
193  zscalar_t psize = solution->getCriteriaPartSize(0, partId);
194 
195  if ( psize < normalizedPartSizes[partId] - epsilon ||
196  psize > normalizedPartSizes[partId] + epsilon )
197  fail=9;
198  }
199  }
200 
201  delete [] normalizedPartSizes;
202 
203  gfail = globalFail(comm, fail);
204  if (gfail){
205  printFailureCode(comm, fail); // exits after printing "FAIL"
206  }
207 
208  // Test the Solution set method that is called by algorithms
209 
210  zzpart_t *partAssignments = new zzpart_t [numIdsPerProc];
211  for (int i=0; i < numIdsPerProc; i++){
212  partAssignments[i] = myGids[i] % numGlobalParts; // round robin
213  }
214  ArrayRCP<zzpart_t> partList = arcp(partAssignments, 0, numIdsPerProc);
215 
216  try{
217  solution->setParts(partList);
218  }
219  catch (std::exception &e){
220  fail=10;
221  }
222 
223  gfail = globalFail(comm, fail);
224  if (gfail){
225  printFailureCode(comm, fail); // exits after printing "FAIL"
226  }
227 
228  // Test the Solution get methods that may be called by users
229  // or migration functions.
230 
231  if (!fail){
232  const zzpart_t *parts = solution->getPartListView();
233  for (int i=0; !fail && i < numIdsPerProc; i++){
234  if (parts[i] != zzpart_t(myGids[i] % numGlobalParts))
235  fail = 13;
236  }
237  }
238 
239  gfail = globalFail(comm, fail);
240  if (gfail){
241  printFailureCode(comm, fail); // exits after printing "FAIL"
242  }
243 
244  if (rank==0)
245  std::cout << "PASS" << std::endl;
246 
248  // TODO:
250  // Create a solution object without part size information, and check it.
252  // Test multiple weights.
254  // Test multiple parts per process.
256  // Specify a list of parts of size 0. (The rest should be uniform.)
257 
258  delete [] lengths;
259  for (int w=0; w < maxNumWeights; w++){
260  delete [] idLists[w];
261  delete [] sizeLists[w];
262  }
263  delete [] idLists;
264  delete [] sizeLists;
265  delete [] myGids;
266 }
int globalFail(const RCP< const Comm< int > > &comm, int fail)
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)
Defines the PartitioningSolution class.
int main(int argc, char *argv[])
idInput_t::part_t zzpart_t
common code used by tests
This class represents a collection of global Identifiers and their associated weights, if any.
list idList
Match up parameters to validators.
void makeArrays(int wdim, int *lens, zzpart_t **ids, zscalar_t **sizes, ArrayRCP< ArrayRCP< zzpart_t > > &idList, ArrayRCP< ArrayRCP< zscalar_t > > &sizeList)
A PartitioningSolution is a solution to a partitioning problem.
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.
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
Zoltan2::BasicIdentifierAdapter< zzuser_t > idInput_t
#define epsilon
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > zzuser_t