Debian Codegraph @

packages/isorropia/test/geometric/test_geometric.cpp

raw
1 //@HEADER
2 //************************************************************************
3 //
4 // Isorropia: Partitioning and Load Balancing Package
5 // Copyright (2006) Sandia Corporation
6 //
7 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 //license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 //************************************************************************
38 //@HEADER
39
40 //
41 // Read in the file "simple.coords" and perform geometric partitioning
42 // using different coordinate weights and different Zoltan methods.
43 //
44 // --f={filename} will read a different coordinate file
45 // --v will print out the partitioning (small coordinate files only)
46 //
47 // The input file should be a text file containing 1, 2 or 3-dimensional
48 // coordinates, one per line, with white space separating coordinate values.
49 //
50
51 #include <Isorropia_ConfigDefs.hpp>
52 #include <Isorropia_Epetra.hpp>
53 #include <Isorropia_EpetraPartitioner.hpp>
54 #include <Isorropia_EpetraRedistributor.hpp>
55
56 #define PRINTLIMIT 100
57
58 #ifdef HAVE_EPETRA
59 #ifdef HAVE_MPI
60 #include <Epetra_MpiComm.h>
61 #else
62 #include <Epetra_SerialComm.h>
63 #endif
64 #include <Epetra_MultiVector.h>
65 #include <Epetra_Import.h>
66 #include <Epetra_BlockMap.h>
67
68 #include <Teuchos_CommandLineProcessor.hpp>
69 #include <Teuchos_RCP.hpp>
70
71 #include <string>
72
73 #include "ispatest_lbeval_utils.hpp"
74 #include "ispatest_epetra_utils.hpp"
75
76 #ifdef _MSC_VER
77 # include <winprocess.h>
78 #endif
79
80 static int localProc = 0;
81 static int numProcs = 1;
82 static int run_test(Teuchos::RCP<const Epetra_MultiVector> &coords,
83 Teuchos::RCP<const Epetra_MultiVector> &weights,
84 const Teuchos::ParameterList &params);
85 static int run_test(Teuchos::RCP<const Epetra_MultiVector> &coords,
86 const Teuchos::ParameterList &params);
87
88 int main(int argc, char** argv) {
89
90 int fail = 0, dim=0;
91
92 #ifdef HAVE_MPI
93 MPI_Init(&argc, &argv);
94 MPI_Comm_rank(MPI_COMM_WORLD, &localProc);
95 MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
96 const Epetra_MpiComm Comm(MPI_COMM_WORLD);
97 #else
98 const Epetra_SerialComm Comm;
99 #endif
100
101 // =============================================================
102 // get command line options
103 // =============================================================
104
105 Teuchos::CommandLineProcessor clp(false,true);
106
107 std::string *inputFile = new std::string("simple.coords");
108 bool verbose = false;
109
110 clp.setOption( "f", inputFile, "Name of coordinate input file");
111
112 clp.setOption( "v", "q", &verbose,
113 "Display coordinates and weights before and after partitioning.");
114
115 Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return =
116 clp.parse(argc,argv);
117
118 if( parse_return == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED){
119 #ifdef HAVE_MPI
120 MPI_Finalize();
121 #endif
122 return 0;
123 }
124 if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) {
125 #ifdef HAVE_MPI
126 MPI_Finalize();
127 #endif
128 return 1;
129 }
130
131 // =============================================================
132 // Open file of coordinates and distribute them across processes
133 // so they are unbalanced.
134 // =============================================================
135
136 Epetra_MultiVector *mv = ispatest::file2multivector(Comm, *inputFile);
137
138 if (!mv || ((dim = mv->NumVectors()) < 1)){
139 if (localProc == 0)
140 std::cerr << "Invalid input file " << *inputFile << std::endl;
141 exit(1);
142 }
143
144 if (localProc == 0){
145 std::cerr << "Found input file " << *inputFile << ", " ;
146 std::cerr << dim << " dimensional coordinates" << std::endl;
147 }
148
149 delete inputFile;
150
151 int base = mv->Map().IndexBase();
152 int globalSize = mv->GlobalLength();
153 int myShare = 0;
154 int n = numProcs - 1;
155
156 if (n){
157 if (localProc < n){
158 int oneShare = globalSize / n;
159 int leftOver = globalSize - (n * oneShare);
160 myShare = oneShare + ((localProc < leftOver) ? 1 : 0);
161 }
162 }
163 else{
164 myShare = globalSize;
165 }
166
167 Epetra_BlockMap unbalancedMap(globalSize, myShare, 1, base, mv->Map().Comm());
168 Epetra_Import importer(unbalancedMap, mv->Map());
169 Epetra_MultiVector umv(unbalancedMap, dim);
170 umv.Import(*mv, importer, Insert);
171
172 delete mv;
173
174 Teuchos::RCP<const Epetra_MultiVector> coords =
175 Teuchos::rcp(new const Epetra_MultiVector(umv));
176
177 // =============================================================
178 // Create some different coordinate weight vectors
179 // =============================================================
180
181 Epetra_MultiVector *unitWgts = ispatest::makeWeights(coords->Map(), &ispatest::unitWeights);
182 Epetra_MultiVector *veeWgts = ispatest::makeWeights(coords->Map(), &ispatest::veeWeights);
183 Epetra_MultiVector *altWgts = ispatest::makeWeights(coords->Map(), &ispatest::alternateWeights);
184
185 Teuchos::RCP<const Epetra_MultiVector> unit_weights_rcp = Teuchos::rcp(unitWgts);
186 Teuchos::RCP<const Epetra_MultiVector> vee_weights_rcp = Teuchos::rcp(veeWgts);
187 Teuchos::RCP<const Epetra_MultiVector> alt_weights_rcp = Teuchos::rcp(altWgts);
188
189 if (localProc == 0){
190 std::cerr << "Unit weights: Each object has weight 1.0" << std::endl;
191 std::cerr << "V weights: Low and high GIDs have high weights, center GIDs have low weights" << std::endl;
192 std::cerr << "Alternate weights: Objects on even rank processes have one weight, on odd another weight" << std::endl;
193 std::cerr << std::endl;
194 }
195
196 // ======================================================================
197 // Create a parameter list for Zoltan, and one for internal partitioning
198 // ======================================================================
199
200 Teuchos::ParameterList internalParams;
201
202 internalParams.set("PARTITIONING_METHOD", "SIMPLE_LINEAR");
203
204
205 Teuchos::ParameterList zoltanParams;
206
207 Teuchos::ParameterList sublist = zoltanParams.sublist("ZOLTAN");
208
209 //sublist.set("DEBUG_LEVEL", "1"); // Zoltan will print out parameters
210 //sublist.set("DEBUG_LEVEL", "5"); // proc 0 will trace Zoltan calls
211 //sublist.set("DEBUG_MEMORY", "2"); // Zoltan will trace alloc & free
212
213 // =============================================================
214 // Run some tests
215 // =============================================================
216 zoltanParams.set("PARTITIONING METHOD", "RCB");
217
218 if (localProc == 0){
219 std::cerr << "RCB - unit weights" << std::endl;
220 }
221
222 fail = run_test(coords, unit_weights_rcp, zoltanParams);
223
224 if (fail) goto failure;
225
226 if (localProc == 0){
227 std::cerr << "PASS" << std::endl << std::endl;
228 }
229 // *************************************************************
230
231 if (localProc == 0){
232 std::cerr << "HSFC - V weights" << std::endl;
233 }
234
235
236 zoltanParams.set("PARTITIONING METHOD", "HSFC");
237 fail = run_test(coords, vee_weights_rcp, zoltanParams);
238
239 if (fail) goto failure;
240
241 if (localProc == 0){
242 std::cerr << "PASS" << std::endl << std::endl;
243 }
244
245 // *************************************************************
246
247 if (localProc == 0){
248 std::cerr << "RIB - alternate weights" << std::endl;
249 }
250 zoltanParams.set("PARTITIONING METHOD", "RIB");
251
252 fail = run_test(coords, alt_weights_rcp, zoltanParams);
253
254 if (fail) goto failure;
255
256 if (localProc == 0){
257 std::cerr << "PASS" << std::endl << std::endl;
258 }
259
260 // *************************************************************
261
262 if (localProc == 0){
263 std::cerr << "RIB - no weights supplied" << std::endl;
264 }
265 zoltanParams.set("PARTITIONING METHOD", "RIB");
266
267 fail = run_test(coords, zoltanParams);
268
269 if (fail) goto failure;
270
271 if (localProc == 0){
272 std::cerr << "PASS" << std::endl << std::endl;
273 }
274
275 // *************************************************************
276
277 goto done;
278
279
280 failure:
281
282 if (localProc == 0){
283 std::cerr << "FAIL: test failed" << std::endl;
284 }
285
286 done:
287
288 #ifdef HAVE_MPI
289 MPI_Finalize();
290 #endif
291
292 return fail;
293 }
294
295 static int check_test(Teuchos::RCP<const Epetra_MultiVector> &coords,
296 Teuchos::RCP<const Epetra_MultiVector> &weights,
297 Teuchos::RCP<Isorropia::Epetra::Partitioner> &part)
298 {
299 // Create a Redistributor based on the partitioning
300
301 Isorropia::Epetra::Redistributor rd(part);
302
303 // Redistribute the coordinates
304
305 Teuchos::RCP<Epetra_MultiVector> new_coords = rd.redistribute(*coords);
306
307 // Redistribute the weights
308
309 Teuchos::RCP<Epetra_MultiVector> new_weights = rd.redistribute(*weights);
310
311 // =============================================================
312 // Compute balance both before and after partitioning
313 // =============================================================
314
315 double min1, min2, max1, max2, avg1, avg2;
316 double goal = 1.0 / (double)numProcs;
317
318 const Epetra_Vector * &before = (*weights)(0);
319 Epetra_Vector * &after = (*new_weights)(0);
320
321 ispatest::compute_balance(*before, goal, min1, max1, avg1);
322
323 if (localProc == 0){
324 std::cerr << "Balance before partitioning: min " ;
325 std::cerr << min1 << " max " << max1 << " average " << avg1 << std::endl;
326 }
327
328 ispatest::compute_balance(*after, goal, min2, max2, avg2);
329
330 if (localProc == 0){
331 std::cerr << "Balance after partitioning: min ";
332 std::cerr << min2 << " max " << max2 << " average " << avg2 << std::endl;
333 }
334
335 return (avg2 > avg1);
336 }
337 static int run_test(Teuchos::RCP<const Epetra_MultiVector> &coords,
338 Teuchos::RCP<const Epetra_MultiVector> &weights,
339 const Teuchos::ParameterList &params)
340 {
341
342 Teuchos::RCP<Isorropia::Epetra::Partitioner> part =
343 Teuchos::rcp(new Isorropia::Epetra::Partitioner(coords, weights, params));
344
345 return check_test(coords, weights, part);
346 }
347
348 static int run_test(Teuchos::RCP<const Epetra_MultiVector> &coords,
349 const Teuchos::ParameterList &params)
350 {
351 Teuchos::RCP<Isorropia::Epetra::Partitioner> part =
352 Teuchos::rcp(new Isorropia::Epetra::Partitioner(coords, params));
353
354 // both Zoltan and Isorropia partitioners will use unit weights,
355 // so we create unit weights to compute balances
356
357 Epetra_MultiVector tmp(coords->Map(), 1);
358 tmp.PutScalar(1.0);
359
360 Teuchos::RCP<const Epetra_MultiVector> unitweights =
361 Teuchos::rcp(new const Epetra_MultiVector(tmp));
362
363 return check_test(coords, unitweights, part);
364 }
365
366 #endif // HAVE_EPETRA