| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "BelosEpetraUtils.h" |
| 11 |
#include "BelosEpetraAdapter.hpp" |
| 12 |
#include "Teuchos_Workspace.hpp" |
| 13 |
#include "Epetra_CrsMatrix.h" |
| 14 |
#include "Epetra_MultiVector.h" |
| 15 |
#include "Epetra_Map.h" |
| 16 |
#include "Epetra_Import.h" |
| 17 |
#ifdef EPETRA_MPI |
| 18 |
#include "Epetra_MpiComm.h" |
| 19 |
#include "BelosGlobalComm.hpp" |
| 20 |
#else |
| 21 |
#include "Epetra_SerialComm.h" |
| 22 |
#endif |
| 23 |
#include "Epetra_Map.h" |
| 24 |
|
| 25 |
#ifdef HAVE_BELOS_TRIUTILS |
| 26 |
#include "Trilinos_Util.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
// RWH TODO FIXME: Trilinos_Util_distrib_msr_matrix available only if 32 bit GIDs available. |
| 30 |
#ifndef EPETRA_NO_32BIT_GLOBAL_INDICES |
| 31 |
|
| 32 |
namespace Belos { |
| 33 |
|
| 34 |
namespace Util { |
| 35 |
|
| 36 |
#ifdef HAVE_BELOS_TRIUTILS |
| 37 |
|
| 38 |
int createEpetraProblem( std::string &filename, |
| 39 |
RCP<Epetra_Map> *rowMap, |
| 40 |
RCP<Epetra_CrsMatrix> *A, |
| 41 |
RCP<Epetra_MultiVector> *B, |
| 42 |
RCP<Epetra_MultiVector> *X, |
| 43 |
int *MyPID |
| 44 |
) |
| 45 |
{ |
| 46 |
int one = 1; |
| 47 |
return Belos::Util::createEpetraProblem( filename, rowMap, A, B, X, MyPID, one ); |
| 48 |
} |
| 49 |
|
| 50 |
int createEpetraProblem( std::string &filename, |
| 51 |
RCP<Epetra_Map> *rowMap, |
| 52 |
RCP<Epetra_CrsMatrix> *A, |
| 53 |
RCP<Epetra_MultiVector> *B, |
| 54 |
RCP<Epetra_MultiVector> *X, |
| 55 |
int *MyPID_out, |
| 56 |
int &numRHS |
| 57 |
) |
| 58 |
{ |
| 59 |
// |
| 60 |
int &MyPID = *MyPID_out; |
| 61 |
// |
| 62 |
int i; |
| 63 |
int n_nonzeros, N_update; |
| 64 |
int *bindx=0, *update=0, *col_inds=0; |
| 65 |
double *val=0, *row_vals=0; |
| 66 |
double *xguess=0, *b=0, *xexact=0; |
| 67 |
|
| 68 |
RCP<Epetra_Comm> epetraComm; |
| 69 |
#ifdef EPETRA_MPI |
| 70 |
epetraComm = rcp(new Epetra_MpiComm( Belos::get_global_comm() ) ); |
| 71 |
#else |
| 72 |
epetraComm = rcp(new Epetra_SerialComm()); |
| 73 |
#endif |
| 74 |
|
| 75 |
MyPID = epetraComm->MyPID(); |
| 76 |
// |
| 77 |
// ********************************************************************** |
| 78 |
// ******************Set up the problem to be solved********************* |
| 79 |
// ********************************************************************** |
| 80 |
// |
| 81 |
int NumGlobalElements; // total # of rows in matrix |
| 82 |
// |
| 83 |
// *****Read in matrix from HB file****** |
| 84 |
// |
| 85 |
Trilinos_Util_read_hb(const_cast<char *>(filename.c_str()), MyPID, &NumGlobalElements, &n_nonzeros, |
| 86 |
&val, &bindx, &xguess, &b, &xexact); |
| 87 |
// |
| 88 |
// *****Distribute data among processors***** |
| 89 |
// |
| 90 |
Trilinos_Util_distrib_msr_matrix(*epetraComm, &NumGlobalElements, &n_nonzeros, &N_update, |
| 91 |
&update, &val, &bindx, &xguess, &b, &xexact); |
| 92 |
// |
| 93 |
// *****Construct the matrix***** |
| 94 |
// |
| 95 |
int NumMyElements = N_update; // # local rows of matrix on processor |
| 96 |
// |
| 97 |
// Create an integer std::vector NumNz that is used to build the Petra Matrix. |
| 98 |
// NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation |
| 99 |
// on this processor |
| 100 |
// |
| 101 |
int * NumNz = new int[NumMyElements]; |
| 102 |
for (i=0; i<NumMyElements; i++) { |
| 103 |
NumNz[i] = bindx[i+1] - bindx[i] + 1; |
| 104 |
} |
| 105 |
// |
| 106 |
RCP<Epetra_Map> epetraMap = rcp(new Epetra_Map(NumGlobalElements, NumMyElements, update, 0, *epetraComm)); |
| 107 |
Teuchos::set_extra_data( epetraComm, "Map::Comm", Teuchos::inOutArg(epetraMap) ); |
| 108 |
if(rowMap) *rowMap = epetraMap; |
| 109 |
// |
| 110 |
// Create a Epetra_Matrix |
| 111 |
// |
| 112 |
*A = rcp(new Epetra_CrsMatrix(BELOSEPETRACOPY, *epetraMap, NumNz)); |
| 113 |
Teuchos::set_extra_data( epetraMap, "Operator::Map", Teuchos::ptr(A) ); |
| 114 |
// |
| 115 |
// Add rows one-at-a-time |
| 116 |
// |
| 117 |
int NumEntries; |
| 118 |
for (i=0; i<NumMyElements; i++) { |
| 119 |
row_vals = val + bindx[i]; |
| 120 |
col_inds = bindx + bindx[i]; |
| 121 |
NumEntries = bindx[i+1] - bindx[i]; |
| 122 |
#ifndef NDEBUG |
| 123 |
int info = |
| 124 |
#endif |
| 125 |
(*A)->InsertGlobalValues(update[i], NumEntries, row_vals, col_inds); |
| 126 |
assert( info == 0 ); |
| 127 |
#ifndef NDEBUG |
| 128 |
info = |
| 129 |
#endif |
| 130 |
(*A)->InsertGlobalValues(update[i], 1, val+i, update+i); |
| 131 |
assert( info == 0 ); |
| 132 |
} |
| 133 |
// |
| 134 |
// Finish up |
| 135 |
// |
| 136 |
#ifndef NDEBUG |
| 137 |
int info = |
| 138 |
#endif |
| 139 |
(*A)->FillComplete(); |
| 140 |
assert( info == 0 ); |
| 141 |
#ifndef NDEBUG |
| 142 |
info = |
| 143 |
#endif |
| 144 |
(*A)->OptimizeStorage(); |
| 145 |
assert( info == 0 ); |
| 146 |
(*A)->SetTracebackMode(1); // Shutdown Epetra Warning tracebacks |
| 147 |
// |
| 148 |
// Construct the right-hand side and solution multivectors. |
| 149 |
// |
| 150 |
if (B) { |
| 151 |
if (b != NULL) { |
| 152 |
*B = rcp(new Epetra_MultiVector(BELOSEPETRACOPY, *epetraMap, b, NumMyElements, 1 )); |
| 153 |
Teuchos::set_extra_data( epetraMap, "B::Map", Teuchos::ptr(B) ); |
| 154 |
numRHS = 1; |
| 155 |
} |
| 156 |
else { |
| 157 |
*B = rcp (new Epetra_MultiVector (*epetraMap, numRHS)); |
| 158 |
Teuchos::set_extra_data( epetraMap, "B::Map", Teuchos::ptr(B) ); |
| 159 |
(*B)->Random (); |
| 160 |
} |
| 161 |
} |
| 162 |
if (X) { |
| 163 |
*X = rcp (new Epetra_MultiVector (*epetraMap, numRHS)); |
| 164 |
Teuchos::set_extra_data( epetraMap, "X::Map", Teuchos::ptr(X) ); |
| 165 |
(*X)->PutScalar (0.0); |
| 166 |
} |
| 167 |
// |
| 168 |
// Create workspace |
| 169 |
// |
| 170 |
Teuchos::set_default_workspace_store( |
| 171 |
Teuchos::rcp(new Teuchos::WorkspaceStoreInitializeable(static_cast<size_t>(2e+6))) |
| 172 |
); |
| 173 |
// |
| 174 |
// Free up memory |
| 175 |
// |
| 176 |
delete [] NumNz; |
| 177 |
free(update); |
| 178 |
free(val); |
| 179 |
free(bindx); |
| 180 |
if (xexact) free(xexact); |
| 181 |
if (xguess) free(xguess); |
| 182 |
if (b) free(b); |
| 183 |
|
| 184 |
return (0); |
| 185 |
} |
| 186 |
|
| 187 |
#endif |
| 188 |
|
| 189 |
int rebalanceEpetraProblem( RCP<Epetra_Map> &Map, |
| 190 |
RCP<Epetra_CrsMatrix> &A, |
| 191 |
RCP<Epetra_MultiVector> &B, |
| 192 |
RCP<Epetra_MultiVector> &X, |
| 193 |
Epetra_Comm &Comm |
| 194 |
) |
| 195 |
{ |
| 196 |
// Rebalance linear system across multiple processors. |
| 197 |
if ( Comm.NumProc() > 1 ) { |
| 198 |
RCP<Epetra_Map> newMap = rcp( new Epetra_Map( Map->NumGlobalElements(), Map->IndexBase(), Comm ) ); |
| 199 |
RCP<Epetra_Import> newImport = rcp( new Epetra_Import( *newMap, *Map ) ); |
| 200 |
|
| 201 |
// Create rebalanced versions of the linear system. |
| 202 |
RCP<Epetra_CrsMatrix> newA = rcp( new Epetra_CrsMatrix( BELOSEPETRACOPY, *newMap, 0 ) ); |
| 203 |
newA->Import( *A, *newImport, Insert ); |
| 204 |
newA->FillComplete(); |
| 205 |
RCP<Epetra_MultiVector> newB = rcp( new Epetra_MultiVector( *newMap, B->NumVectors() ) ); |
| 206 |
newB->Import( *B, *newImport, Insert ); |
| 207 |
RCP<Epetra_MultiVector> newX = rcp( new Epetra_MultiVector( *newMap, X->NumVectors() ) ); |
| 208 |
newX->Import( *X, *newImport, Insert ); |
| 209 |
|
| 210 |
// Set the pointers to the new rebalance linear system. |
| 211 |
A = newA; |
| 212 |
B = newB; |
| 213 |
X = newX; |
| 214 |
Map = newMap; |
| 215 |
} |
| 216 |
|
| 217 |
return (0); |
| 218 |
} |
| 219 |
|
| 220 |
} // namespace Util |
| 221 |
|
| 222 |
namespace Test { |
| 223 |
|
| 224 |
MPISession::MPISession (Teuchos::Ptr<int> argc, Teuchos::Ptr<char**> argv) |
| 225 |
{ |
| 226 |
#ifdef EPETRA_MPI |
| 227 |
MPI_Init (argc.getRawPtr(), argv.getRawPtr()); |
| 228 |
#endif // EPETRA_MPI |
| 229 |
} |
| 230 |
|
| 231 |
MPISession::~MPISession () |
| 232 |
{ |
| 233 |
#ifdef EPETRA_MPI |
| 234 |
MPI_Finalize (); |
| 235 |
#endif // EPETRA_MPI |
| 236 |
} |
| 237 |
|
| 238 |
Teuchos::RCP<const Epetra_Comm> |
| 239 |
MPISession::getComm () |
| 240 |
{ |
| 241 |
using Teuchos::rcp; |
| 242 |
|
| 243 |
if (comm_.is_null()) { |
| 244 |
#ifdef EPETRA_MPI |
| 245 |
comm_ = rcp (new Epetra_MpiComm (Belos::get_global_comm())); |
| 246 |
#else |
| 247 |
comm_ = rcp (new Epetra_SerialComm); |
| 248 |
#endif // EPETRA_MPI |
| 249 |
} |
| 250 |
return comm_; |
| 251 |
} |
| 252 |
|
| 253 |
} // namespace Test |
| 254 |
|
| 255 |
} // namespace Belos |
| 256 |
|
| 257 |
#endif // EPETRA_NO_32BIT_GLOBAL_INDICES |