| 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 |
// This is an example of partitioning a hypergraph, while requesting |
| 42 |
// parts of unequal size. |
| 43 |
//-------------------------------------------------------------------- |
| 44 |
|
| 45 |
//Include Isorropia_Exception.hpp only because the helper functions at |
| 46 |
//the bottom of this file (which create the epetra objects) can |
| 47 |
//potentially throw exceptions. |
| 48 |
#include <Isorropia_Exception.hpp> |
| 49 |
|
| 50 |
//The Isorropia symbols being demonstrated are declared |
| 51 |
//in these headers: |
| 52 |
#include <Isorropia_Epetra.hpp> |
| 53 |
#include <Isorropia_EpetraCostDescriber.hpp> |
| 54 |
#include <Isorropia_EpetraRedistributor.hpp> |
| 55 |
#include <Isorropia_EpetraPartitioner.hpp> |
| 56 |
|
| 57 |
#ifdef HAVE_MPI |
| 58 |
#include <mpi.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef HAVE_EPETRA |
| 62 |
#ifdef HAVE_MPI |
| 63 |
#include <Epetra_MpiComm.h> |
| 64 |
#else |
| 65 |
#include <Epetra_SerialComm.h> |
| 66 |
#endif |
| 67 |
#include <Epetra_Map.h> |
| 68 |
#include <Epetra_Vector.h> |
| 69 |
#include <Epetra_CrsMatrix.h> |
| 70 |
#include <Epetra_LinearProblem.h> |
| 71 |
#endif |
| 72 |
|
| 73 |
#include "ispatest_lbeval_utils.hpp" |
| 74 |
|
| 75 |
//Declaration for helper-function that creates epetra rowmatrix objects. This |
| 76 |
//function is implemented at the bottom of this file. |
| 77 |
#ifdef HAVE_EPETRA |
| 78 |
Teuchos::RCP<const Epetra_RowMatrix> |
| 79 |
create_epetra_rowmatrix(int numProcs, |
| 80 |
int localProc, |
| 81 |
int local_n); |
| 82 |
#endif |
| 83 |
|
| 84 |
int main(int argc, char** argv) { |
| 85 |
#if defined(HAVE_MPI) && defined(HAVE_EPETRA) |
| 86 |
|
| 87 |
int numProcs = 1; |
| 88 |
int localProc = 0; |
| 89 |
|
| 90 |
//first, set up our MPI environment... |
| 91 |
MPI_Init(&argc, &argv); |
| 92 |
MPI_Comm_rank(MPI_COMM_WORLD, &localProc); |
| 93 |
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); |
| 94 |
|
| 95 |
int local_n = 1200; |
| 96 |
|
| 97 |
//Create a Epetra_RowMatrix object. |
| 98 |
|
| 99 |
Teuchos::RCP<const Epetra_RowMatrix> rowmatrix; |
| 100 |
try { |
| 101 |
rowmatrix = create_epetra_rowmatrix(numProcs, localProc, local_n); |
| 102 |
} |
| 103 |
catch(std::exception& exc) { |
| 104 |
std::cout << "vert_weights example: create_epetra_rowmatrix threw" |
| 105 |
<< " exception '" << exc.what() << "' on proc " |
| 106 |
<< localProc << std::endl; |
| 107 |
MPI_Finalize(); |
| 108 |
return(-1); |
| 109 |
} |
| 110 |
|
| 111 |
// Zoltan parameters |
| 112 |
|
| 113 |
Teuchos::ParameterList paramlist; |
| 114 |
|
| 115 |
paramlist.set("PARTITIONING METHOD", "HYPERGRAPH"); |
| 116 |
|
| 117 |
// Now create the partitioner. By default the partitioning occurs |
| 118 |
// in the constructor. Include a flag to omit the partitioning. |
| 119 |
|
| 120 |
Teuchos::RCP<Isorropia::Epetra::Partitioner> partitioner = |
| 121 |
Teuchos::rcp(new Isorropia::Epetra::Partitioner( |
| 122 |
rowmatrix, // object to partition |
| 123 |
paramlist, // parameters |
| 124 |
false)); // NO - do not do the partitioning in the constructor |
| 125 |
|
| 126 |
//Specify the proportion of rows to be assigned to each part. We'll |
| 127 |
//have the even-numbered parts be twice the size of the odd-numbered |
| 128 |
//parts. |
| 129 |
|
| 130 |
float *partSize = new float [numProcs]; |
| 131 |
int *partGlobalId = new int [numProcs]; |
| 132 |
|
| 133 |
for (int i=0; i < numProcs; i++){ |
| 134 |
partGlobalId[i] = i; |
| 135 |
partSize[i] = (i % 2) ? 1.0 : 2.0; |
| 136 |
} |
| 137 |
|
| 138 |
partitioner->setPartSizes(numProcs, partGlobalId, partSize); |
| 139 |
|
| 140 |
delete [] partGlobalId; |
| 141 |
|
| 142 |
// NOW perform the partitioning |
| 143 |
|
| 144 |
partitioner->partition(); |
| 145 |
|
| 146 |
//Next create a Redistributor object and use it to create a repartitioned |
| 147 |
//copy of the matrix. |
| 148 |
|
| 149 |
Isorropia::Epetra::Redistributor rd(partitioner); |
| 150 |
|
| 151 |
Teuchos::RCP<Epetra_CrsMatrix> bal_matrix; |
| 152 |
|
| 153 |
//Use a try-catch block because Isorropia will throw an exception |
| 154 |
//if it encounters an error. |
| 155 |
|
| 156 |
if (localProc == 0) { |
| 157 |
std::cout << " calling Isorropia::Epetra::Redistributor::redistribute..." |
| 158 |
<< std::endl; |
| 159 |
} |
| 160 |
|
| 161 |
try { |
| 162 |
bal_matrix = rd.redistribute(*rowmatrix); |
| 163 |
} |
| 164 |
catch(std::exception& exc) { |
| 165 |
std::cout << "linsys example: Isorropia::Epetra::Redistributor threw " |
| 166 |
<< "exception '" << exc.what() << "' on proc " |
| 167 |
<< localProc << std::endl; |
| 168 |
MPI_Finalize(); |
| 169 |
return(-1); |
| 170 |
} |
| 171 |
|
| 172 |
// How many rows does each process have? |
| 173 |
|
| 174 |
std::cout << "Partition " << localProc << ", Size " << partSize[localProc] << ", Number of rows " << bal_matrix->NumMyRows() << std::endl; |
| 175 |
|
| 176 |
|
| 177 |
delete [] partSize; |
| 178 |
|
| 179 |
MPI_Finalize(); |
| 180 |
|
| 181 |
#else |
| 182 |
std::cout << "part_sizes: must have both MPI and EPETRA. Make sure " |
| 183 |
<< "Trilinos is configured with --enable-mpi and --enable-epetra." |
| 184 |
<< std::endl; |
| 185 |
#endif |
| 186 |
|
| 187 |
return(0); |
| 188 |
} |
| 189 |
|
| 190 |
//Below is the implementation of the helper-function that creates the |
| 191 |
//epetra rowmatrix for use in the above example program. |
| 192 |
|
| 193 |
#if defined(HAVE_MPI) && defined(HAVE_EPETRA) |
| 194 |
|
| 195 |
Teuchos::RCP<const Epetra_RowMatrix> |
| 196 |
create_epetra_rowmatrix(int numProcs, |
| 197 |
int localProc, |
| 198 |
int local_n) |
| 199 |
{ |
| 200 |
if (localProc == 0) { |
| 201 |
std::cout << " creating Epetra_CrsMatrix with even row-distribution..." |
| 202 |
<< std::endl; |
| 203 |
} |
| 204 |
|
| 205 |
//create an Epetra_CrsMatrix with rows spread evenly over |
| 206 |
//processors. |
| 207 |
|
| 208 |
Epetra_MpiComm comm(MPI_COMM_WORLD); |
| 209 |
int global_num_rows = numProcs*local_n; |
| 210 |
|
| 211 |
Epetra_Map rowmap(global_num_rows, local_n, 0, comm); |
| 212 |
|
| 213 |
int nnz_per_row = 9; |
| 214 |
Teuchos::RCP<Epetra_CrsMatrix> matrix = |
| 215 |
Teuchos::rcp(new Epetra_CrsMatrix(Copy, rowmap, nnz_per_row)); |
| 216 |
|
| 217 |
// Add rows one-at-a-time |
| 218 |
double negOne = -1.0; |
| 219 |
double posTwo = 4.0; |
| 220 |
for (int i=0; i<local_n; i++) { |
| 221 |
int GlobalRow = matrix->GRID(i); |
| 222 |
int RowLess1 = GlobalRow - 1; |
| 223 |
int RowPlus1 = GlobalRow + 1; |
| 224 |
int RowLess2 = GlobalRow - 2; |
| 225 |
int RowPlus2 = GlobalRow + 2; |
| 226 |
int RowLess3 = GlobalRow - 3; |
| 227 |
int RowPlus3 = GlobalRow + 3; |
| 228 |
int RowLess4 = GlobalRow - 4; |
| 229 |
int RowPlus4 = GlobalRow + 4; |
| 230 |
|
| 231 |
if (RowLess4>=0) { |
| 232 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowLess4); |
| 233 |
} |
| 234 |
if (RowLess3>=0) { |
| 235 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowLess3); |
| 236 |
} |
| 237 |
if (RowLess2>=0) { |
| 238 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowLess2); |
| 239 |
} |
| 240 |
if (RowLess1>=0) { |
| 241 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowLess1); |
| 242 |
} |
| 243 |
if (RowPlus1<global_num_rows) { |
| 244 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowPlus1); |
| 245 |
} |
| 246 |
if (RowPlus2<global_num_rows) { |
| 247 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowPlus2); |
| 248 |
} |
| 249 |
if (RowPlus3<global_num_rows) { |
| 250 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowPlus3); |
| 251 |
} |
| 252 |
if (RowPlus4<global_num_rows) { |
| 253 |
matrix->InsertGlobalValues(GlobalRow, 1, &negOne, &RowPlus4); |
| 254 |
} |
| 255 |
|
| 256 |
matrix->InsertGlobalValues(GlobalRow, 1, &posTwo, &GlobalRow); |
| 257 |
} |
| 258 |
|
| 259 |
int err = matrix->FillComplete(); |
| 260 |
if (err != 0) { |
| 261 |
throw Isorropia::Exception("create_epetra_matrix: error in matrix.FillComplete()"); |
| 262 |
} |
| 263 |
|
| 264 |
return(matrix); |
| 265 |
} |
| 266 |
|
| 267 |
#endif //HAVE_MPI && HAVE_EPETRA |
| 268 |
|