| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Trilinos: An Object-Oriented Solver Framework |
| 4 |
// |
| 5 |
// Copyright 2001-2024 NTESS and the Trilinos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
//-------------------------------------------------------------------- |
| 11 |
//This file is a self-contained example of creating an Epetra_CrsGraph |
| 12 |
//and Epetra_CrsMatrix object, and using Isorropia through the EpetraExt |
| 13 |
//transform interface to rebalance them. |
| 14 |
// NOTE: This is a modified version of the matrix_1.cpp example in Isorropia. |
| 15 |
//-------------------------------------------------------------------- |
| 16 |
|
| 17 |
#include <Isorropia_ConfigDefs.hpp> |
| 18 |
|
| 19 |
#ifdef HAVE_MPI |
| 20 |
#include <mpi.h> |
| 21 |
#include <Epetra_MpiComm.h> |
| 22 |
#else |
| 23 |
#include <Epetra_SerialComm.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <Epetra_Map.h> |
| 27 |
#include <Epetra_CrsMatrix.h> |
| 28 |
#include <Epetra_MultiVector.h> |
| 29 |
#include <Epetra_LinearProblem.h> |
| 30 |
#include <EpetraExt_Isorropia_CrsGraph.h> |
| 31 |
#include <EpetraExt_LPTrans_From_GraphTrans.h> |
| 32 |
#include <Isorropia_Exception.hpp> |
| 33 |
|
| 34 |
#include <Teuchos_ParameterList.hpp> |
| 35 |
#include <Teuchos_RCP.hpp> |
| 36 |
|
| 37 |
//Declaration for helper-function that creates epetra objects. These |
| 38 |
//functions are implemented at the bottom of this file. |
| 39 |
Teuchos::RCP<Epetra_CrsMatrix> |
| 40 |
create_epetra_matrix(int numProcs, int localProc); |
| 41 |
|
| 42 |
|
| 43 |
int main(int argc, char** argv) { |
| 44 |
#if defined(HAVE_MPI) && defined(HAVE_EPETRA) |
| 45 |
|
| 46 |
int p, numProcs = 1; |
| 47 |
int localProc = 0; |
| 48 |
|
| 49 |
//first, set up our MPI environment... |
| 50 |
MPI_Init(&argc, &argv); |
| 51 |
MPI_Comm_rank(MPI_COMM_WORLD, &localProc); |
| 52 |
MPI_Comm_size(MPI_COMM_WORLD, &numProcs); |
| 53 |
|
| 54 |
//Now create a balanced copy of the matrix using the EpetraExt |
| 55 |
//transform interface to Isorropia. |
| 56 |
//NOTE: By default, Isorropia will use Zoltan for the |
| 57 |
//repartitioning, if Isorropia was configured with Zoltan support. |
| 58 |
//(i.e., --enable-isorropia-zoltan flag to configure, plus Zoltan include |
| 59 |
//paths and library directives) |
| 60 |
//If Isorropia was not configured with Zoltan support, then a simple |
| 61 |
//built-in linear partitioner will be used to make sure the number |
| 62 |
//of nonzeros on each processor is equal or close to equal. |
| 63 |
|
| 64 |
Teuchos::ParameterList paramlist; |
| 65 |
|
| 66 |
#ifdef HAVE_ISORROPIA_ZOLTAN |
| 67 |
|
| 68 |
// If Zoltan is available, we'll specify that the Zoltan package use |
| 69 |
// graph-partitioning for the partitioning operation and specifically |
| 70 |
// 1D hypergraph partitioning, by creating a parameter sublist named |
| 71 |
// "Zoltan" and setting the appropriate values. |
| 72 |
// (See Zoltan documentation for other valid parameters...) |
| 73 |
|
| 74 |
Teuchos::ParameterList& sublist = paramlist.sublist("Zoltan"); |
| 75 |
sublist.set("LB_METHOD", "HYPERGRAPH"); |
| 76 |
|
| 77 |
#else |
| 78 |
//If Zoltan is not available, we don't need to set any parameters. |
| 79 |
#endif |
| 80 |
|
| 81 |
// Create the object to generate the balanced graph. |
| 82 |
Teuchos::RCP<EpetraExt::Isorropia_CrsGraph> Trans = |
| 83 |
Teuchos::rcp( new EpetraExt::Isorropia_CrsGraph( paramlist ) ); |
| 84 |
|
| 85 |
// Create a linear problem transform interface. |
| 86 |
Teuchos::RCP<EpetraExt::LinearProblem_GraphTrans> LPTrans = |
| 87 |
Teuchos::rcp( new EpetraExt::LinearProblem_GraphTrans( |
| 88 |
*(dynamic_cast<EpetraExt::StructuralSameTypeTransform<Epetra_CrsGraph>*>(Trans.get())) ) ); |
| 89 |
|
| 90 |
// Create an Epetra_CrsMatrix object. |
| 91 |
Teuchos::RCP<Epetra_CrsMatrix> crsmatrix; |
| 92 |
try { |
| 93 |
crsmatrix = create_epetra_matrix(numProcs, localProc); |
| 94 |
} |
| 95 |
catch(std::exception& e) { |
| 96 |
std::cout << "ERROR: create_epetra_matrix threw exception '" |
| 97 |
<< e.what() << "' on proc " << localProc << std::endl; |
| 98 |
MPI_Finalize(); |
| 99 |
return(-1); |
| 100 |
} |
| 101 |
|
| 102 |
// Create Epetra_MultiVectors for the lhs and rhs of the linear problem. |
| 103 |
Epetra_MultiVector lhs(crsmatrix->Map(), 1); |
| 104 |
Epetra_MultiVector rhs(crsmatrix->Map(), 1); |
| 105 |
rhs.PutScalar( 1.0 ); |
| 106 |
|
| 107 |
// Create the linear problem with the original partitioning. |
| 108 |
Epetra_LinearProblem problem( &*crsmatrix, &lhs, &rhs ); |
| 109 |
|
| 110 |
// Create the new linear problem and perform the balanced partitioning. |
| 111 |
// NOTE: The balanced linear system will be in tProblem after fwd() is called. |
| 112 |
// It is not necessary for the RCP to manage the transformed problem. |
| 113 |
Teuchos::RCP<Epetra_LinearProblem> tProblem = Teuchos::rcp( &((*LPTrans)( problem )), false ); |
| 114 |
LPTrans->fwd(); |
| 115 |
|
| 116 |
int graphrows1 = crsmatrix->NumMyRows(); |
| 117 |
int bal_graph_rows = tProblem->GetMatrix()->NumMyRows(); |
| 118 |
int graphnnz1 = crsmatrix->NumMyNonzeros(); |
| 119 |
int bal_graph_nnz = tProblem->GetMatrix()->NumMyNonzeros(); |
| 120 |
|
| 121 |
for(p=0; p<numProcs; ++p) { |
| 122 |
MPI_Barrier(MPI_COMM_WORLD); |
| 123 |
|
| 124 |
if (p != localProc) continue; |
| 125 |
|
| 126 |
std::cout << "proc " << p << ": input matrix local rows: " << graphrows1 |
| 127 |
<< ", local NNZ: " << graphnnz1 << std::endl; |
| 128 |
std::cout << "proc " << p << ": balanced matrix local rows: " |
| 129 |
<< bal_graph_rows << ", local NNZ: " << bal_graph_nnz << std::endl; |
| 130 |
} |
| 131 |
|
| 132 |
MPI_Finalize(); |
| 133 |
|
| 134 |
#else |
| 135 |
std::cout << "ERROR: This MUST be and MPI build with Epetra enabled!" << std::endl; |
| 136 |
#endif |
| 137 |
|
| 138 |
return(0); |
| 139 |
} |
| 140 |
|
| 141 |
//Below are implementations of the helper-functions that create the |
| 142 |
//poorly-balanced epetra objects for use in the above example program. |
| 143 |
|
| 144 |
#if defined(HAVE_MPI) && defined(HAVE_EPETRA) |
| 145 |
|
| 146 |
Teuchos::RCP<Epetra_CrsMatrix> |
| 147 |
create_epetra_matrix(int numProcs, int localProc) |
| 148 |
{ |
| 149 |
if (localProc == 0) { |
| 150 |
std::cout << " creating Epetra_CrsMatrix with un-even distribution..." |
| 151 |
<< std::endl; |
| 152 |
} |
| 153 |
|
| 154 |
//create an Epetra_CrsMatrix with rows spread un-evenly over |
| 155 |
//processors. |
| 156 |
Epetra_MpiComm comm(MPI_COMM_WORLD); |
| 157 |
int local_num_rows = 200; |
| 158 |
int nnz_per_row = local_num_rows/4+1; |
| 159 |
int global_num_rows = numProcs*local_num_rows; |
| 160 |
|
| 161 |
int mid_proc = numProcs/2; |
| 162 |
bool num_procs_even = numProcs%2==0 ? true : false; |
| 163 |
|
| 164 |
int adjustment = local_num_rows/2; |
| 165 |
|
| 166 |
//adjust local_num_rows so that it's not equal on all procs. |
| 167 |
if (localProc < mid_proc) { |
| 168 |
local_num_rows -= adjustment; |
| 169 |
} |
| 170 |
else { |
| 171 |
local_num_rows += adjustment; |
| 172 |
} |
| 173 |
|
| 174 |
//if numProcs is not an even number, undo the local_num_rows adjustment |
| 175 |
//on one proc so that the total will still be correct. |
| 176 |
if (localProc == numProcs-1) { |
| 177 |
if (num_procs_even == false) { |
| 178 |
local_num_rows -= adjustment; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
//now we're ready to create a row-map. |
| 183 |
Epetra_Map rowmap(global_num_rows, local_num_rows, 0, comm); |
| 184 |
|
| 185 |
//create a matrix |
| 186 |
Teuchos::RCP<Epetra_CrsMatrix> matrix = |
| 187 |
Teuchos::rcp(new Epetra_CrsMatrix(Copy, rowmap, nnz_per_row)); |
| 188 |
|
| 189 |
std::vector<int> indices(nnz_per_row); |
| 190 |
std::vector<double> coefs(nnz_per_row); |
| 191 |
|
| 192 |
int err = 0; |
| 193 |
|
| 194 |
for(int i=0; i<local_num_rows; ++i) { |
| 195 |
int global_row = rowmap.GID(i); |
| 196 |
int first_col = global_row - nnz_per_row/2; |
| 197 |
|
| 198 |
if (first_col < 0) { |
| 199 |
first_col = 0; |
| 200 |
} |
| 201 |
else if (first_col > (global_num_rows - nnz_per_row)) { |
| 202 |
first_col = global_num_rows - nnz_per_row; |
| 203 |
} |
| 204 |
|
| 205 |
for(int j=0; j<nnz_per_row; ++j) { |
| 206 |
indices[j] = first_col + j; |
| 207 |
coefs[j] = 1.0; |
| 208 |
} |
| 209 |
|
| 210 |
int err = matrix->InsertGlobalValues(global_row, nnz_per_row, |
| 211 |
&coefs[0], &indices[0]); |
| 212 |
if (err < 0) { |
| 213 |
err = matrix->ReplaceGlobalValues(global_row, nnz_per_row, |
| 214 |
&coefs[0], &indices[0]); |
| 215 |
if (err < 0) { |
| 216 |
throw Isorropia::Exception("create_epetra_matrix: error inserting matrix values."); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
err = matrix->FillComplete(); |
| 222 |
if (err != 0) { |
| 223 |
throw Isorropia::Exception("create_epetra_matrix: error in matrix.FillComplete()"); |
| 224 |
} |
| 225 |
|
| 226 |
return(matrix); |
| 227 |
} |
| 228 |
|
| 229 |
#endif //HAVE_MPI && HAVE_EPETRA |
| 230 |
|