Debian Codegraph @

packages/isorropia/test/mtx/test_unsym.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 // Read in a matrix market file. Use isorropia to do graph
41 // partitioning. Compute the graph metrics both before and after partitioning.
42 //
43 // This tests Isorropia::Epetra::Partitioner followed by
44 // redistribution with a Isorropia::Epetra::Redistributor.
45 //
46 // For graph partitioning:
47 // The GRAPH_SYMMETRIZE option is assumed to be set to default "TRANSPOSE".
48 // This test has to be modified for other options.
49 //
50 // If run with --f={filename} a matrix market file other than west0067.mtx
51 // will be processed.
52 //
53
54 #include <Isorropia_ConfigDefs.hpp>
55 #include <Isorropia_Epetra.hpp>
56 #include <Isorropia_EpetraPartitioner.hpp>
57 #include <Isorropia_EpetraRedistributor.hpp>
58 #include <Isorropia_EpetraCostDescriber.hpp>
59
60 #include <ispatest_lbeval_utils.hpp>
61 #include <ispatest_epetra_utils.hpp>
62
63 #ifdef HAVE_EPETRA
64 #ifdef HAVE_MPI
65 #include <Epetra_MpiComm.h>
66 #else
67 #include <Epetra_SerialComm.h>
68 #endif
69 #include <Epetra_Map.h>
70 #include <Epetra_CrsMatrix.h>
71 #include <Epetra_Vector.h>
72 #ifdef HAVE_EPETRAEXT
73 #include <EpetraExt_CrsMatrixIn.h>
74 #endif
75 #endif
76
77 #include <Teuchos_CommandLineProcessor.hpp>
78
79 int main(int argc, char** argv)
80 {
81
82 int rc=0, fail = 0;
83 #ifdef HAVE_EPETRAEXT
84 int localProc = 0;
85
86 int numProcs;
87 #ifdef HAVE_MPI
88 MPI_Init(&argc, &argv);
89 MPI_Comm_rank(MPI_COMM_WORLD, &localProc);
90 MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
91 const Epetra_MpiComm Comm(MPI_COMM_WORLD);
92 #else
93 const Epetra_SerialComm Comm;
94 numProcs = 1;
95 #endif
96
97 Teuchos::CommandLineProcessor clp(false,true);
98
99 // --f=fileName provides a different matrix market file for input
100 // --run-all will continue to run all tests even if there is a failure
101
102 std::string *inputFile = new std::string("west0067.mtx");
103 bool runAll = false;
104
105 clp.setOption( "f", inputFile,
106 "Name of input matrix market file");
107 clp.setOption( "run-all", "abort", &runAll,
108 "Don't abort if one test fails, run all of them.");
109
110 Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return =
111 clp.parse(argc,argv);
112
113 if( parse_return == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED)
114 {
115 #ifdef HAVE_MPI
116 MPI_Finalize();
117 #endif
118 return 0;
119 }
120 if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL )
121 {
122 #ifdef HAVE_MPI
123 MPI_Finalize();
124 #endif
125 return 1;
126 }
127
128 const char *fname = inputFile->c_str();
129
130 // Read in the matrix market file and distribute its rows across the
131 // processes.
132 //
133 // This reader uses the default Epetra_Map for number of rows for the
134 // RowMap() and for the RangeMap(). For non-square matrices it uses
135 // the default Epetra_Map for the number of columns for the DomainMap(),
136 // otherwise it uses the RowMap().
137 //
138 // The maps can be specified with other versions of MMFtoCrsMatrix().
139
140 Epetra_CrsMatrix *matrixPtr;
141 rc = EpetraExt::MatrixMarketFileToCrsMatrix(fname, Comm, matrixPtr);
142 if (rc < 0)
143 {
144 if (localProc==0) std::cerr << "error reading input file"<< std::cout;
145 return(1);
146 }
147
148 bool square = (matrixPtr->NumGlobalRows() == matrixPtr->NumGlobalCols());
149
150 // Run some partitioning tests
151 // Test graph partitioning with square unsymmetric matrices
152
153 Teuchos::RCP<Epetra_CrsMatrix> testm = Teuchos::rcp(matrixPtr);
154 Teuchos::RCP<const Epetra_RowMatrix> rm = testm;
155 Isorropia::Epetra::CostDescriber costs;
156
157 double myShareBefore = 1.0 / numProcs;
158 double myShare = myShareBefore;
159
160 double balance1, balance2, cutn1, cutn2, cutl1, cutl2;
161 double cutWgt1, cutWgt2;
162 int numCuts1, numCuts2;
163 rc = ispatest::compute_graph_metrics(*rm, costs, myShare,
164 balance1, numCuts1, cutWgt1, cutn1, cutl1);
165
166 if (square)
167 {
168 Teuchos::ParameterList params;
169 params.set("partitioning method", "graph");
170 Isorropia::Epetra::Partitioner iso_part (rm, params);
171
172 Teuchos::RCP<Isorropia::Epetra::Partitioner> partitioner =
173 Teuchos::rcpFromRef (iso_part);
174 Isorropia::Epetra::Redistributor redist(partitioner);
175
176 Teuchos::RCP<Epetra_CrsMatrix> bal_matrix;
177 try
178 {
179 bal_matrix = redist.redistribute(*testm);
180 }
181 catch (std::exception& exc)
182 {
183 std::cout << "Isorropia exception " << exc.what() << std::endl;
184 return 1;
185 }
186 fail = 0; // Bad way to test, If Zoltan did not crash then success !!
187 // But all metrics could get worse from the initial metrics
188 // Leaving metrics testing to zdrive
189 rc = ispatest::compute_graph_metrics(*bal_matrix, costs, myShare,
190 balance2, numCuts2, cutWgt2, cutn2, cutl2);
191 /*if (balance1 >= balance2)
192 {
193 fail = 0;
194 }
195 else
196 {
197 fail = 1;
198 }*/
199 if (localProc == 0)
200 {
201 cout << "CutN before and after :" << cutn1 << " " << cutn2 << endl;
202 cout << "CutL before and after :" << cutl1 << " " << cutl2 << endl;
203 cout << "Balance before and after :" << balance1 << " " << balance2
204 << endl;
205 cout << "NumCuts before and after :" << numCuts1 << " " << numCuts2
206 << endl;
207 }
208 }
209 else
210 {
211 // We do not support this case now , TODO : Add it when we support
212 // GRAPH_SYMMETRIZE as bipartite graph
213 if (localProc == 0)
214 {
215 std::cout << "Test not run because i/p is not square" << std::endl;
216 }
217 fail = 0;
218 }
219
220
221 #else
222 fail = 0;
223 if (localProc == 0)
224 {
225 std::cout << "Test not run because it requires EPETRA_EXT" << std::endl;
226 }
227 #endif
228
229 #ifdef HAVE_MPI
230 MPI_Finalize();
231 #endif
232
233 return fail;
234 }