| 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 |
#include <Isorropia_ConfigDefs.hpp> |
| 41 |
#include <Isorropia_Exception.hpp> |
| 42 |
#include <Isorropia_Epetra.hpp> |
| 43 |
#include <Isorropia_EpetraProber.hpp> |
| 44 |
|
| 45 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 46 |
|
| 47 |
#include <ispatest_utils.hpp> |
| 48 |
#include <ispatest_epetra_utils.hpp> |
| 49 |
|
| 50 |
#ifdef HAVE_MPI |
| 51 |
#include <mpi.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
#ifdef HAVE_EPETRA |
| 55 |
#include <Epetra_Comm.h> |
| 56 |
#include <Epetra_Map.h> |
| 57 |
#include <Epetra_CrsMatrix.h> |
| 58 |
#include <Epetra_CrsGraph.h> |
| 59 |
#include <Epetra_Vector.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifdef HAVE_EPETRAEXT |
| 63 |
#include <EpetraExt_MatrixMatrix.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifdef HAVE_MPI |
| 67 |
#include <Epetra_MpiComm.h> |
| 68 |
#else |
| 69 |
#include <Epetra_SerialComm.h> |
| 70 |
#endif |
| 71 |
|
| 72 |
|
| 73 |
#ifdef HAVE_EPETRA |
| 74 |
Epetra_CrsMatrix* create_epetra_test_matrix_1(int numProcs, |
| 75 |
int localProc, |
| 76 |
bool verbose) |
| 77 |
{ |
| 78 |
if (verbose) { |
| 79 |
std::cout << " creating Epetra_CrsMatrix with un-even distribution..." |
| 80 |
<< std::endl; |
| 81 |
} |
| 82 |
|
| 83 |
//create an Epetra_CrsMatrix with rows spread un-evenly over |
| 84 |
//processors. |
| 85 |
#ifdef HAVE_MPI |
| 86 |
Epetra_MpiComm comm(MPI_COMM_WORLD); |
| 87 |
#else |
| 88 |
Epetra_SerialComm comm; |
| 89 |
#endif |
| 90 |
|
| 91 |
int local_num_rows = 360; |
| 92 |
int nnz_per_row = local_num_rows/4+1; |
| 93 |
int global_num_rows = numProcs*local_num_rows; |
| 94 |
|
| 95 |
int mid_proc = numProcs/2; |
| 96 |
bool num_procs_even = numProcs%2==0 ? true : false; |
| 97 |
|
| 98 |
int adjustment = local_num_rows/2; |
| 99 |
|
| 100 |
//adjust local_num_rows so that it's not equal on all procs. |
| 101 |
if (localProc < mid_proc) { |
| 102 |
local_num_rows -= adjustment; |
| 103 |
} |
| 104 |
else { |
| 105 |
local_num_rows += adjustment; |
| 106 |
} |
| 107 |
|
| 108 |
//if numProcs is not an even number, undo the local_num_rows adjustment |
| 109 |
//on one proc so that the total will still be correct. |
| 110 |
if (localProc == numProcs-1) { |
| 111 |
if (num_procs_even == false) { |
| 112 |
local_num_rows -= adjustment; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
//now we're ready to create a row-map. |
| 117 |
Epetra_Map rowmap(global_num_rows, local_num_rows, 0, comm); |
| 118 |
|
| 119 |
//create a matrix |
| 120 |
Epetra_CrsMatrix* input_matrix = |
| 121 |
new Epetra_CrsMatrix(Copy, rowmap, nnz_per_row); |
| 122 |
|
| 123 |
int err = ispatest::fill_matrix(*input_matrix, nnz_per_row, verbose); |
| 124 |
if (err != 0) { |
| 125 |
delete input_matrix; |
| 126 |
return(0); |
| 127 |
} |
| 128 |
|
| 129 |
return(input_matrix); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
bool probing_test(Epetra_CrsMatrix & in_mat, bool build_list){ |
| 135 |
const Epetra_CrsGraph & graph=in_mat.Graph(); |
| 136 |
|
| 137 |
Teuchos::ParameterList main, zoltan; |
| 138 |
|
| 139 |
if(build_list){ |
| 140 |
// zoltan.set("DISTANCE","2"); |
| 141 |
main.set("ZOLTAN",zoltan); |
| 142 |
} |
| 143 |
|
| 144 |
Isorropia::Epetra::Prober prober(Teuchos::rcp<const Epetra_CrsGraph>(&graph,false),main); |
| 145 |
Epetra_CrsMatrix out_mat(Copy,graph); |
| 146 |
int rv=prober.probe(in_mat,out_mat); |
| 147 |
if(rv!=0) {printf("ERROR: probing failed\n");return false;} |
| 148 |
|
| 149 |
#ifdef HAVE_EPETRAEXT |
| 150 |
EpetraExt::MatrixMatrix::Add(in_mat,false,1,out_mat,-1); |
| 151 |
double nrm=out_mat.NormInf()/in_mat.NormInf(); |
| 152 |
if(!in_mat.Comm().MyPID()) |
| 153 |
printf("diff norm = %22.16e\n",nrm); |
| 154 |
if(nrm < 1e-12) return true; |
| 155 |
else return false; |
| 156 |
#endif |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
#endif |
| 170 |
|
| 171 |
int main(int argc, char** argv) { |
| 172 |
|
| 173 |
|
| 174 |
bool verbose = false; |
| 175 |
int numProcs = 1; |
| 176 |
int localProc = 0; |
| 177 |
|
| 178 |
#ifdef HAVE_MPI |
| 179 |
MPI_Init(&argc, &argv); |
| 180 |
MPI_Comm_rank(MPI_COMM_WORLD, &localProc); |
| 181 |
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); |
| 182 |
#endif |
| 183 |
|
| 184 |
try { |
| 185 |
verbose = ispatest::set_verbose(localProc, argc, argv); |
| 186 |
} |
| 187 |
catch(std::exception& exc) { |
| 188 |
std::cout << "err, setting verbosity: " << exc.what() << std::endl; |
| 189 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 190 |
#ifdef HAVE_MPI |
| 191 |
MPI_Finalize(); |
| 192 |
#endif /* HAVE_MPI */ |
| 193 |
return(-1); |
| 194 |
} |
| 195 |
|
| 196 |
// Only print on proc 0 |
| 197 |
if (localProc != 0) { |
| 198 |
verbose = false; |
| 199 |
} |
| 200 |
|
| 201 |
bool test_passed = true; |
| 202 |
#ifdef HAVE_EPETRA |
| 203 |
//TEST HERE |
| 204 |
Epetra_CrsMatrix* mat=create_epetra_test_matrix_1(numProcs,localProc,true); |
| 205 |
test_passed=probing_test(*mat,true); |
| 206 |
test_passed=test_passed && probing_test(*mat,false); |
| 207 |
|
| 208 |
// Cleanup |
| 209 |
delete mat; |
| 210 |
|
| 211 |
#else |
| 212 |
std::cout << "probing main: currently can only test " |
| 213 |
<< "rebalancing with Epetra enabled." << std::endl; |
| 214 |
test_passed = false; |
| 215 |
#endif |
| 216 |
|
| 217 |
if (test_passed && verbose) { |
| 218 |
std::cout << "PROBING main: tests passed."<<std::endl; |
| 219 |
} |
| 220 |
|
| 221 |
#ifdef HAVE_MPI |
| 222 |
MPI_Finalize(); |
| 223 |
#endif |
| 224 |
|
| 225 |
return(0); |
| 226 |
} |
| 227 |
|
| 228 |
|