Debian Codegraph @

packages/isorropia/test/mtx/test_simple_methods.cpp

raw
1 /*
2 //@HEADER
3 //************************************************************************
4 //
5 // Isorropia: Partitioning and Load Balancing Package
6 // Copyright (2006) Sandia Corporation
7 //
8 //Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 //license for use of this work by or on behalf of the U.S. Government.
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 //************************************************************************
39 //@HEADER
40 */
41
42
43 #include <cstring>
44 #include <cstdio>
45 #include <iostream>
46 #include <fstream>
47
48 #include <Epetra_ConfigDefs.h>
49 #include <Epetra_Time.h>
50 #include <Epetra_Import.h>
51 #include <Epetra_Map.h>
52 #include <Epetra_LocalMap.h>
53 #include <Epetra_CrsGraph.h>
54 #include <Epetra_CrsMatrix.h>
55 #include <Epetra_Vector.h>
56 #include <Epetra_MultiVector.h>
57 #include <EpetraExt_MatrixMatrix.h>
58 #include <EpetraExt_BlockMapIn.h>
59 #include <EpetraExt_CrsMatrixIn.h>
60 #include <EpetraExt_RowMatrixOut.h>
61
62
63 #define SUCCESS 0
64 #define FAIL 1
65
66 #ifdef HAVE_MPI
67 #include <mpi.h>
68 #endif
69
70 #ifdef EPETRA_MPI
71 #include <Epetra_MpiComm.h>
72 #else
73 #include <Epetra_SerialComm.h>
74 #endif
75
76 #include <Teuchos_ParameterList.hpp>
77
78 #include <Isorropia_EpetraPartitioner.hpp>
79 #include <ispatest_utils.hpp>
80 #include <ispatest_epetra_utils.hpp>
81
82 ///////////////////////////////////////
83 // Answers
84 ///////////////////////////////////////
85 int blockAnswer2[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
86 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
87 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
88 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
89 int blockAnswer4[100] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
90 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
91 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
92 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
93 int cyclicAnswer2[100] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,
94 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,
95 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,
96 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
97 int cyclicAnswer4[100] = {0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,
98 1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,
99 2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,
100 3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3};
101 ///////////////////////////////////////
102
103
104
105 int getMatrixDim(char *mmfilename, Epetra_Comm &comm,
106 int *N, int *M, int *nnz);
107
108 int read_matrixmarket_linear(const char *, Epetra_Comm &, Epetra_CrsMatrix *&);
109
110 bool testPartitioningMVRCPInput(Teuchos::RCP<const Epetra_MultiVector> mv, const char *method, int worldsize, int myrank);
111 bool testPartitioningMVPtrInput(const Epetra_MultiVector *mv, const char *method, int worldsize, int myrank);
112
113 bool testPartitioningMatrixRCPInput(Teuchos::RCP<const Epetra_RowMatrix> mat, const char *method, int worldsize, int myrank);
114 bool testPartitioningMatrixPtrInput(const Epetra_CrsMatrix *mat, const char *method, int worldsize, int myrank);
115
116 bool testPartitioningMapRCPInput(Teuchos::RCP<const Epetra_BlockMap> map, const char *method, int worldsize, int myrank);
117 bool testPartitioningMapPtrInput(const Epetra_BlockMap *map, const char *method, int worldsize, int myrank);
118
119 bool checkPartition1(const Isorropia::Epetra::Partitioner &partitioner);
120 bool checkPartition2(const Isorropia::Epetra::Partitioner &partitioner, const Epetra_BlockMap &map, const char* method);
121 bool checkPartition4(const Isorropia::Epetra::Partitioner &partitioner, const Epetra_BlockMap &map, const char* method);
122
123
124 ////////////////////////////////////////////////////////////////////////////////
125 // Testing simple methods with Multivector
126 ////////////////////////////////////////////////////////////////////////////////
127 int main(int argc, char** argv)
128 {
129
130 #ifdef EPETRA_MPI
131 MPI_Init(&argc,&argv);
132 Epetra_MpiComm Comm(MPI_COMM_WORLD);
133 #else
134 Epetra_SerialComm Comm;
135 #endif
136
137 Epetra_CrsMatrix *A;
138
139 int worldsize = Comm.NumProc();
140 int myrank = Comm.MyPID();
141 bool success = true;
142
143 /////////////////////////////////////////////////////////////
144 /// Read and distribute matrices
145 /////////////////////////////////////////////////////////////
146 read_matrixmarket_linear("arrow100.mtx",Comm,A);
147 /////////////////////////////////////////////////////////////
148
149 /////////////////////////////////////////////////////////////
150 // Test partitioning with matrix ptr input
151 /////////////////////////////////////////////////////////////
152 success = success && testPartitioningMatrixPtrInput(A,"BLOCK",worldsize,myrank);
153 success = testPartitioningMatrixPtrInput(A,"CYCLIC",worldsize,myrank) && success; // test lhs of && so it is run
154 /////////////////////////////////////////////////////////////
155
156 /////////////////////////////////////////////////////////////
157 // Test partitioning with matrix rcp input
158 /////////////////////////////////////////////////////////////
159 Teuchos::RCP<const Epetra_RowMatrix> rowmatrix = Teuchos::rcp(A);
160
161 success = testPartitioningMatrixRCPInput(rowmatrix,"BLOCK",worldsize,myrank) && success;
162 success = testPartitioningMatrixRCPInput(rowmatrix,"CYCLIC",worldsize,myrank) && success; // test lhs of && so it is run
163 /////////////////////////////////////////////////////////////
164
165 /////////////////////////////////////////////////////////////
166 // Test partitioning with multivector ptr input
167 /////////////////////////////////////////////////////////////
168 const Epetra_MultiVector *mvPtr = new Epetra_MultiVector(A->RowMap(),1);
169
170 success = testPartitioningMVPtrInput(mvPtr,"BLOCK",worldsize,myrank) && success;
171 success = testPartitioningMVPtrInput(mvPtr,"CYCLIC",worldsize,myrank) && success;
172 /////////////////////////////////////////////////////////////
173
174 /////////////////////////////////////////////////////////////
175 // Test partitioning with multivector rcp input
176 /////////////////////////////////////////////////////////////
177 Teuchos::RCP<const Epetra_MultiVector> mv = Teuchos::rcp(mvPtr);
178
179 success = testPartitioningMVRCPInput(mv,"BLOCK",worldsize,myrank) && success;
180 success = testPartitioningMVRCPInput(mv,"CYCLIC",worldsize,myrank) && success;
181 /////////////////////////////////////////////////////////////
182
183 /////////////////////////////////////////////////////////////
184 // Test partitioning with BlockMap ptr input
185 /////////////////////////////////////////////////////////////
186 const Epetra_BlockMap *mapPtr = &(A->RowMap());
187
188 success = testPartitioningMapPtrInput(mapPtr,"BLOCK",worldsize,myrank) && success;
189 success = testPartitioningMapPtrInput(mapPtr,"CYCLIC",worldsize,myrank) && success;
190 /////////////////////////////////////////////////////////////
191
192 /////////////////////////////////////////////////////////////
193 // Test partitioning with BlockMap rcp input
194 /////////////////////////////////////////////////////////////
195 Teuchos::RCP<const Epetra_BlockMap> map = Teuchos::rcp(mapPtr,false);
196
197 success = testPartitioningMapRCPInput(map,"BLOCK",worldsize,myrank) && success;
198 success = testPartitioningMapRCPInput(map,"CYCLIC",worldsize,myrank) && success;
199 /////////////////////////////////////////////////////////////
200
201 #ifdef EPETRA_MPI
202 MPI_Finalize();
203 #endif
204
205 if(!success)
206 {
207 return FAIL;
208 }
209
210 return(SUCCESS);
211 }
212 ////////////////////////////////////////////////////////////////////////////
213
214 ////////////////////////////////////////////////////////////////////////////
215 ////////////////////////////////////////////////////////////////////////////
216 bool testPartitioningMVRCPInput(Teuchos::RCP<const Epetra_MultiVector> mv, const char *method, int worldsize, int myrank)
217 {
218 bool success=true;
219
220 /////////////////////////////////////////////////////////////
221 // Partition
222 /////////////////////////////////////////////////////////////
223 Teuchos::ParameterList paramlist;
224 paramlist.set("IMBALANCE TOL","1.03");
225
226 if(strcmp(method,"BLOCK")==0)
227 {
228 paramlist.set("PARTITIONING_METHOD", "BLOCK");
229 }
230 else if(strcmp(method,"CYCLIC")==0)
231 {
232 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
233 }
234 else
235 {
236 if(myrank==0)
237 {
238 std::cout << "Error in testPartitionMVInput() with method argument " << method << "." << std::endl
239 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
240 }
241 return(false);
242 }
243
244 Isorropia::Epetra::Partitioner partitioner(mv, paramlist, false);
245 partitioner.partition();
246 /////////////////////////////////////////////////////////////
247
248 /////////////////////////////////////////////////////////////
249 /// Check partition
250 /////////////////////////////////////////////////////////////
251 if (worldsize==1)
252 {
253 success = checkPartition1(partitioner);
254 }
255 else if (worldsize==2)
256 {
257 success = checkPartition2(partitioner,mv->Map(),method);
258 }
259 else if (worldsize==4)
260 {
261 success = checkPartition4(partitioner,mv->Map(),method);
262 }
263 else
264 {
265 if(myrank==0)
266 {
267 std::cout << "WARNING for testPartitionMVInput() with number of processors = " << worldsize << std::endl
268 << " Number of processes not supported .... (by default) returning success" << std::endl;
269 }
270 }
271
272 if(myrank==0)
273 {
274 if (success == false)
275 {
276 std::cout << "Failure in testPartitionMVInput(), method argument " << method
277 << ", numProcs = " << worldsize << std::endl;
278 }
279 else
280 {
281 std::cout << "Success in testPartitionMVInput(), method argument " << method
282 << ", numProcs = " << worldsize << std::endl;
283 }
284 }
285 /////////////////////////////////////////////////////////////
286
287 return success;
288 }
289 ////////////////////////////////////////////////////////////////////////////
290
291 ////////////////////////////////////////////////////////////////////////////
292 ////////////////////////////////////////////////////////////////////////////
293 bool testPartitioningMVPtrInput(const Epetra_MultiVector *mv, const char *method, int worldsize, int myrank)
294 {
295 bool success=true;
296
297 /////////////////////////////////////////////////////////////
298 // Partition
299 /////////////////////////////////////////////////////////////
300 Teuchos::ParameterList paramlist;
301 paramlist.set("IMBALANCE TOL","1.03");
302
303 if(strcmp(method,"BLOCK")==0)
304 {
305 paramlist.set("PARTITIONING_METHOD", "BLOCK");
306 }
307 else if(strcmp(method,"CYCLIC")==0)
308 {
309 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
310 }
311 else
312 {
313 if(myrank==0)
314 {
315 std::cout << "Error in testPartitionMVInput() with method argument " << method << "." << std::endl
316 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
317 }
318 return(false);
319 }
320
321 Isorropia::Epetra::Partitioner partitioner(mv, paramlist, false);
322 partitioner.partition();
323 /////////////////////////////////////////////////////////////
324
325 /////////////////////////////////////////////////////////////
326 /// Check partition
327 /////////////////////////////////////////////////////////////
328 if (worldsize==1)
329 {
330 success = checkPartition1(partitioner);
331 }
332 else if (worldsize==2)
333 {
334 success = checkPartition2(partitioner,mv->Map(),method);
335 }
336 else if (worldsize==4)
337 {
338 success = checkPartition4(partitioner,mv->Map(),method);
339 }
340 else
341 {
342 if(myrank==0)
343 {
344 std::cout << "WARNING for testPartitionMVInput() with number of processors = " << worldsize << std::endl
345 << " Number of processes not supported .... (by default) returning success" << std::endl;
346 }
347 }
348
349 if(myrank==0)
350 {
351 if (success == false)
352 {
353 std::cout << "Failure in testPartitionMVInput(), method argument " << method
354 << ", numProcs = " << worldsize << std::endl;
355 }
356 else
357 {
358 std::cout << "Success in testPartitionMVInput(), method argument " << method
359 << ", numProcs = " << worldsize << std::endl;
360 }
361 }
362 /////////////////////////////////////////////////////////////
363
364 return success;
365 }
366 ////////////////////////////////////////////////////////////////////////////
367
368 ////////////////////////////////////////////////////////////////////////////
369 ////////////////////////////////////////////////////////////////////////////
370 bool testPartitioningMatrixRCPInput(Teuchos::RCP<const Epetra_RowMatrix> mat, const char *method, int worldsize, int myrank)
371 {
372 bool success=true;
373
374 /////////////////////////////////////////////////////////////
375 // Partition
376 /////////////////////////////////////////////////////////////
377 Teuchos::ParameterList paramlist;
378 paramlist.set("IMBALANCE TOL","1.03");
379
380 if(strcmp(method,"BLOCK")==0)
381 {
382 paramlist.set("PARTITIONING_METHOD", "BLOCK");
383 }
384 else if(strcmp(method,"CYCLIC")==0)
385 {
386 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
387 }
388 else
389 {
390 if(myrank==0)
391 {
392 std::cout << "Error in testPartitionMatrixInput() with method argument " << method << "." << std::endl
393 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
394 }
395 return(false);
396 }
397
398 Isorropia::Epetra::Partitioner partitioner(mat, paramlist, false);
399 partitioner.partition();
400 /////////////////////////////////////////////////////////////
401
402 /////////////////////////////////////////////////////////////
403 /// Check partition
404 /////////////////////////////////////////////////////////////
405 if (worldsize==1)
406 {
407 success = checkPartition1(partitioner);
408 }
409 else if (worldsize==2)
410 {
411 success = checkPartition2(partitioner,mat->Map(),method);
412 }
413 else if (worldsize==4)
414 {
415 success = checkPartition4(partitioner,mat->Map(),method);
416 }
417 else
418 {
419 if(myrank==0)
420 {
421 std::cout << "WARNING for testPartitionMatrixInput() with number of processors = " << worldsize << std::endl
422 << " Number of processes not supported .... (by default) returning success" << std::endl;
423 }
424 }
425
426 if(myrank==0)
427 {
428 if (success == false)
429 {
430 std::cout << "Failure in testPartitionMatrixInput(), method argument " << method
431 << ", numProcs = " << worldsize << std::endl;
432 }
433 else
434 {
435 std::cout << "Success in testPartitionMatrixInput(), method argument " << method
436 << ", numProcs = " << worldsize << std::endl;
437 }
438 }
439 /////////////////////////////////////////////////////////////
440
441 return success;
442 }
443 ////////////////////////////////////////////////////////////////////////////
444
445 ////////////////////////////////////////////////////////////////////////////
446 ////////////////////////////////////////////////////////////////////////////
447 bool testPartitioningMatrixPtrInput(const Epetra_CrsMatrix *mat, const char *method, int worldsize, int myrank)
448 {
449 bool success=true;
450
451 /////////////////////////////////////////////////////////////
452 // Partition
453 /////////////////////////////////////////////////////////////
454 Teuchos::ParameterList paramlist;
455 paramlist.set("IMBALANCE TOL","1.03");
456
457 if(strcmp(method,"BLOCK")==0)
458 {
459 paramlist.set("PARTITIONING_METHOD", "BLOCK");
460 }
461 else if(strcmp(method,"CYCLIC")==0)
462 {
463 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
464 }
465 else
466 {
467 if(myrank==0)
468 {
469 std::cout << "Error in testPartitionMatrixInput() with method argument " << method << "." << std::endl
470 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
471 }
472 return(false);
473 }
474
475 Isorropia::Epetra::Partitioner partitioner(mat, paramlist, false);
476 partitioner.partition();
477 /////////////////////////////////////////////////////////////
478
479 /////////////////////////////////////////////////////////////
480 /// Check partition
481 /////////////////////////////////////////////////////////////
482 if (worldsize==1)
483 {
484 success = checkPartition1(partitioner);
485 }
486 else if (worldsize==2)
487 {
488 success = checkPartition2(partitioner,mat->Map(),method);
489 }
490 else if (worldsize==4)
491 {
492 success = checkPartition4(partitioner,mat->Map(),method);
493 }
494 else
495 {
496 if(myrank==0)
497 {
498 std::cout << "WARNING for testPartitionMatrixInput() with number of processors = " << worldsize << std::endl
499 << " Number of processes not supported .... (by default) returning success" << std::endl;
500 }
501 }
502
503 if(myrank==0)
504 {
505 if (success == false)
506 {
507 std::cout << "Failure in testPartitionMatrixInput(), method argument " << method
508 << ", numProcs = " << worldsize << std::endl;
509 }
510 else
511 {
512 std::cout << "Success in testPartitionMatrixInput(), method argument " << method
513 << ", numProcs = " << worldsize << std::endl;
514 }
515 }
516 /////////////////////////////////////////////////////////////
517
518 return success;
519 }
520 ////////////////////////////////////////////////////////////////////////////
521
522 ////////////////////////////////////////////////////////////////////////////
523 ////////////////////////////////////////////////////////////////////////////
524 bool testPartitioningMapRCPInput(Teuchos::RCP<const Epetra_BlockMap> map, const char *method, int worldsize, int myrank)
525 {
526 bool success=true;
527
528 /////////////////////////////////////////////////////////////
529 // Partition
530 /////////////////////////////////////////////////////////////
531 Teuchos::ParameterList paramlist;
532 paramlist.set("IMBALANCE TOL","1.03");
533
534 if(strcmp(method,"BLOCK")==0)
535 {
536 paramlist.set("PARTITIONING_METHOD", "BLOCK");
537 }
538 else if(strcmp(method,"CYCLIC")==0)
539 {
540 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
541 }
542 else
543 {
544 if(myrank==0)
545 {
546 std::cout << "Error in testPartitionMapInput() with method argument " << method << "." << std::endl
547 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
548 }
549 return(false);
550 }
551
552 Isorropia::Epetra::Partitioner partitioner(map, paramlist, false);
553 partitioner.partition();
554 /////////////////////////////////////////////////////////////
555
556 /////////////////////////////////////////////////////////////
557 /// Check partition
558 /////////////////////////////////////////////////////////////
559 if (worldsize==1)
560 {
561 success = checkPartition1(partitioner);
562 }
563 else if (worldsize==2)
564 {
565 success = checkPartition2(partitioner,*map, method);
566 }
567 else if (worldsize==4)
568 {
569 success = checkPartition4(partitioner,*map, method);
570 }
571 else
572 {
573 if(myrank==0)
574 {
575 std::cout << "WARNING for testPartitionMapInput() with number of processors = " << worldsize << std::endl
576 << " Number of processes not supported .... (by default) returning success" << std::endl;
577 }
578 }
579
580 if(myrank==0)
581 {
582 if (success == false)
583 {
584 std::cout << "Failure in testPartitionMapInput(), method argument " << method
585 << ", numProcs = " << worldsize << std::endl;
586 }
587 else
588 {
589 std::cout << "Success in testPartitionMapInput(), method argument " << method
590 << ", numProcs = " << worldsize << std::endl;
591 }
592 }
593 /////////////////////////////////////////////////////////////
594
595 return success;
596 }
597 ////////////////////////////////////////////////////////////////////////////
598
599 ////////////////////////////////////////////////////////////////////////////
600 ////////////////////////////////////////////////////////////////////////////
601 bool testPartitioningMapPtrInput(const Epetra_BlockMap *map, const char *method, int worldsize, int myrank)
602 {
603 bool success=true;
604
605 /////////////////////////////////////////////////////////////
606 // Partition
607 /////////////////////////////////////////////////////////////
608 Teuchos::ParameterList paramlist;
609 paramlist.set("IMBALANCE TOL","1.03");
610
611 if(strcmp(method,"BLOCK")==0)
612 {
613 paramlist.set("PARTITIONING_METHOD", "BLOCK");
614 }
615 else if(strcmp(method,"CYCLIC")==0)
616 {
617 paramlist.set("PARTITIONING_METHOD", "CYCLIC");
618 }
619 else
620 {
621 if(myrank==0)
622 {
623 std::cout << "Error in testPartitionMapInput() with method argument " << method << "." << std::endl
624 << " Method must be 'BLOCK' or 'CYCLIC'" << std::endl;
625 }
626 return(false);
627 }
628
629 Isorropia::Epetra::Partitioner partitioner(map, paramlist, false);
630 partitioner.partition();
631 /////////////////////////////////////////////////////////////
632
633 /////////////////////////////////////////////////////////////
634 /// Check partition
635 /////////////////////////////////////////////////////////////
636 if (worldsize==1)
637 {
638 success = checkPartition1(partitioner);
639 }
640 else if (worldsize==2)
641 {
642 success = checkPartition2(partitioner,*map, method);
643 }
644 else if (worldsize==4)
645 {
646 success = checkPartition4(partitioner,*map, method);
647 }
648 else
649 {
650 if(myrank==0)
651 {
652 std::cout << "WARNING for testPartitionMapInput() with number of processors = " << worldsize << std::endl
653 << " Number of processes not supported .... (by default) returning success" << std::endl;
654 }
655 }
656
657 if(myrank==0)
658 {
659 if (success == false)
660 {
661 std::cout << "Failure in testPartitionMapInput(), method argument " << method
662 << ", numProcs = " << worldsize << std::endl;
663 }
664 else
665 {
666 std::cout << "Success in testPartitionMapInput(), method argument " << method
667 << ", numProcs = " << worldsize << std::endl;
668 }
669 }
670 /////////////////////////////////////////////////////////////
671
672 return success;
673 }
674 ////////////////////////////////////////////////////////////////////////////
675
676 ////////////////////////////////////////////////////////////////////////////
677 ////////////////////////////////////////////////////////////////////////////
678 bool checkPartition1(const Isorropia::Epetra::Partitioner &partitioner)
679 {
680 int numRows = 100;
681
682 for(int i=0;i<numRows;i++)
683 {
684 if(partitioner[i] !=0) // For worldsize=1, everything should be assigned to part 0
685 {
686 return false;
687 }
688 }
689 return true;
690 }
691 ////////////////////////////////////////////////////////////////////////////
692
693 ////////////////////////////////////////////////////////////////////////////////
694 ////////////////////////////////////////////////////////////////////////////////
695 bool checkPartition2(const Isorropia::Epetra::Partitioner &partitioner,
696 const Epetra_BlockMap &map, const char *method)
697 {
698 int success = 1;
699 int numRows = map.NumMyElements();
700
701 for(int i=0;i<numRows;i++)
702 {
703 if ( strcmp(method,"BLOCK")==0 && partitioner[i] != blockAnswer2[map.GID(i)])
704 {
705 success = 0;
706 break;
707 }
708 else if ( strcmp(method,"CYCLIC")==0 && partitioner[i] != cyclicAnswer2[map.GID(i)])
709 {
710 success = 0;
711 break;
712 }
713 }
714
715 #ifdef EPETRA_MPI
716 int returnFlag;
717 MPI_Allreduce(&success,&returnFlag,1,MPI_INT,MPI_LAND,MPI_COMM_WORLD);
718
719 if(returnFlag==0)
720 {
721 return false;
722 }
723 #endif
724
725 return true;
726 }
727 ////////////////////////////////////////////////////////////////////////////
728
729 ////////////////////////////////////////////////////////////////////////////
730 ////////////////////////////////////////////////////////////////////////////
731 bool checkPartition4(const Isorropia::Epetra::Partitioner &partitioner,
732 const Epetra_BlockMap &map, const char *method)
733 {
734 int success = 1;
735 int numRows = map.NumMyElements();
736
737 for(int i=0;i<numRows;i++)
738 {
739 if ( strcmp(method,"BLOCK")==0 && partitioner[i] != blockAnswer4[map.GID(i)])
740 {
741 success = 0;
742 break;
743 }
744 else if ( strcmp(method,"CYCLIC")==0 && partitioner[i] != cyclicAnswer4[map.GID(i)])
745 {
746 success = 0;
747 break;
748 }
749 }
750
751 #ifdef EPETRA_MPI
752 int returnFlag;
753 MPI_Allreduce(&success,&returnFlag,1,MPI_INT,MPI_LAND,MPI_COMM_WORLD);
754
755 if(returnFlag==0)
756 {
757 return false;
758 }
759 #endif
760
761 return true;
762 }
763 ////////////////////////////////////////////////////////////////////////////
764
765 ////////////////////////////////////////////////////////////////////////////
766 ////////////////////////////////////////////////////////////////////////////
767 int read_matrixmarket_linear(const char *infilename, Epetra_Comm &comm,
768 Epetra_CrsMatrix *&A)
769 {
770
771 char mmfilename[256];
772 strcpy(mmfilename,infilename);
773
774 Epetra_Map *rowMap;
775
776 // Construct a Map that puts approximately the same number of
777 // equations on each processor. Row-based decomposition.
778 // Peek into MatrixMarket file to get Matrix Dimensions
779 int nrows, ncols, nnz;
780 if (!getMatrixDim(mmfilename, comm, &nrows, &ncols, &nnz))
781 {
782 return 0;
783 }
784
785 rowMap = new Epetra_Map(nrows, 0, comm);
786
787 // Read MatrixMarket file into matrix A. 1D row partitioning
788 //rowMap, colMap, rangeMap, domainMap
789 EpetraExt::MatrixMarketFileToCrsMatrix(mmfilename, *rowMap, A, false, false);
790
791 return 0;
792 }
793 ////////////////////////////////////////////////////////////////////////////
794
795 ////////////////////////////////////////////////////////////////////////////
796 int getMatrixDim(char *mmfilename, Epetra_Comm &comm,
797 int *N, // Output: Number of rows.
798 int *M, // Output: Number of columns.
799 int *nnz // Output: Number of nonzeros.
800 )
801 {
802 // Peek into the MatrixMarket file to get the matrix dimensions.
803 // Proc 0 does the peeking and broadcasts to other procs.
804 int tmp[3]={0,0,0};
805 int aok = 1;
806
807 if (comm.MyPID() == 0) {
808
809 std::ifstream mm(mmfilename);
810
811 if (!mm) {
812 cout << "getMatrixDim: Cannot open MatrixMarket " << mmfilename << endl;
813 *N = *M = -1;
814 aok = 0;
815 }
816 else {
817 // Skip comments in MatrixMarket header.
818 char c = mm.peek();
819 char line[100];
820 while (c == '%') {
821 mm.getline(line, 100);
822 c = mm.peek();
823 continue;
824 }
825 // Get N, M and nnz from MatrixMarket file.
826 mm >> tmp[0] >> tmp[1] >> tmp[2];
827 mm.close();
828 }
829 }
830 comm.Broadcast(tmp, 3, 0);
831 *N = tmp[0];
832 *M = tmp[1];
833 *nnz = tmp[2];
834 if (*N < 1 || *M < 1 || *N != *M) {
835 if (comm.MyPID()==0) {
836 cout << "getMatrixDim: Invalid input in matrix " << mmfilename << endl;
837 cout << "getMatrixDim: N=" << *N << " M=" << *M
838 << " nnz=" << *nnz << endl;
839 }
840 aok = 0;
841 }
842
843 return aok;
844 }
845 ////////////////////////////////////////////////////////////////////////////
846
847
848
849