1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "mpi.h"
using namespace std;
const double SMALL = 1.0E-30; // Used to stop divide-by-zero
const double PZERO = 1.0E-10; // Interpretation of "zero" in output
using vec = vector<double>;
using veci = vector<int>;
using matrix = vector<vec>;
// Function prototypes
bool GaussElimination( matrix A, vec &X );
ostream &operator << ( ostream &strm, const vec &V );
matrix readData();
matrix randomData( int N );
// MPI stuff
int tag = 99;
MPI_Status stat;
int myrank, size;
veci procRow; // Processor owning a given row
veci localRow; // Number of row on its own processor
//======================================
int main( int argc, char * argv[] )
{
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &myrank );
MPI_Comm_size( MPI_COMM_WORLD, &size );
const int NTIMES = 1000; // Repeat count
int N = 128; // Generated size
bool random = false;
unsigned seed = 42;
// unsigned seed = time( 0 );
srand( seed );
matrix A;
vec X;
bool ok;
// Set up matrix and RHS
if ( random ) A = randomData( N ); // Randomly generated
else A = readData(); // Read from (e.g.) file
// Solve (time multiple runs)
clock_t start = clock();
for ( int times = 1; times <= NTIMES; times++ ) ok = GaussElimination( A, X );
clock_t finish = clock();
if ( myrank == 0 )
{
cout << "Time: " << (double)( finish - start ) / CLOCKS_PER_SEC << "\n\n";
if ( ok ) cout << "Solution: " << X << '\n';
else cout << "Unable to solve\n";
}
MPI_Finalize();
}
//======================================
bool GaussElimination( matrix A, vec &X )
//-------------------------------------------------------------------
// Solve linear system by Gaussian elimination
// Note: A is the AUGMENTED matrix (last column is actually B); it has N rows and N+1 columns
// Note: Copy of A because first argument is passed by value
// Note: Each processor only has SOME of the rows
//-------------------------------------------------------------------
{
int N = A[0].size() - 1;
vec row( N + 1 );
X = vec( N );
int proc; // Processor in charge of pivot row
bool doPivoting = true; // Slower, but more reliable
struct ptype{ double Amax; int r; };
//**********************
// Forward elimination *
//**********************
for ( int i = 0; i < N; i++ )
{
proc = procRow[i];
//************************************************************************
// Partial pivoting: find row r below i with largest element in column i *
//************************************************************************
if ( doPivoting && i < N - 1 )
{
// Each processor updates its own maxA
ptype p{ 0.0, i }, pmax;
for ( int k = i; k < N; k++ )
{
if ( myrank == procRow[k] )
{
double val = abs( A[localRow[k]][i] );
if ( val > p.Amax ) p = ptype{ val, k };
}
}
// Global reduction to get the row to swap with
MPI_Allreduce( &p, &pmax, 1, MPI_DOUBLE_INT, MPI_MAXLOC, MPI_COMM_WORLD );
int r = pmax.r;
// Swap (tails of) ith and rth rows
if ( r != i )
{
int remote = procRow[r];
if ( myrank == proc && myrank == remote ) // Swap rows on same processor
{
int ii = localRow[i];
int rr = localRow[r];
for ( int j = i; j <= N; j++ ) swap( A[ii][j], A[rr][j] );
}
else if ( myrank == proc )
{
int ii = localRow[i];
MPI_Sendrecv_replace( A[ii].data() + i, N + 1 - i, MPI_DOUBLE, remote, tag, remote, tag, MPI_COMM_WORLD, &stat );
}
else if ( myrank == remote )
{
int rr = localRow[r];
MPI_Sendrecv_replace( A[rr].data() + i, N + 1 - i, MPI_DOUBLE, proc , tag, proc , tag, MPI_COMM_WORLD, &stat );
}
}
}
//**************************
// Reduction of lower rows *
//**************************
// Send tail of pivot row to other processors
if ( myrank == proc ) row = A[localRow[i]];
MPI_Bcast( row.data() + i, N + 1 - i, MPI_DOUBLE, proc, MPI_COMM_WORLD );
if ( abs( row[i] ) < SMALL ) return false; // Singular matrix
for ( int r = i + 1; r < N; r++ )
{
if ( myrank == procRow[r] )
{
int rr = localRow[r];
double multiple = A[rr][i] / row[i];
for ( int j = i; j < N + 1; j++ ) A[rr][j] -= multiple * row[j];
}
}
}
//***********************
// Backward elimination *
//***********************
for ( int i = N - 1; i >= 0; i-- )
{
proc = procRow[i];
if ( myrank == proc )
{
int ii = localRow[i];
X[i] = A[ii][N] / A[ii][i]; // Solution for the pivot row (also a multiplier)
}
MPI_Bcast( &X[i], 1, MPI_DOUBLE, proc, MPI_COMM_WORLD );
for ( int r = i - 1; r >= 0; r-- )
{
if ( myrank == procRow[r] )
{
int rr = localRow[r];
A[rr][N] -= A[rr][i] * X[i];
A[rr][i] = 0.0;
}
}
}
return true;
}
//======================================
ostream &operator << ( ostream &strm, const vec &V )
{
for ( auto x : V ) strm << setw( 12 ) << ( abs( x ) < PZERO ? 0.0 : x ) << ' ';
return strm;
}
//======================================
matrix readData()
{
// ifstream in( "in.txt" );
stringstream in( "4"
" 1 2 3 4 1"
"-2 5 5 7 2"
" 1 9 10 3 3"
" 2 2 4 3 4" );
int N;
if ( !myrank ) in >> N;
MPI_Bcast( &N, 1, MPI_INT, 0, MPI_COMM_WORLD );
procRow = veci( N );
localRow = veci( N );
matrix A;
vec row( N + 1 );
for ( int i = 0; i < N; i++ )
{
procRow [i] = i % size;
localRow[i] = i / size;
int proc = procRow[i];
if ( myrank == 0 )
{
for ( int j = 0; j < N + 1; j++ ) in >> row[j];
if ( myrank == proc ) A.push_back( row );
else MPI_Send( row.data(), N + 1, MPI_DOUBLE, proc, tag, MPI_COMM_WORLD );
}
else if ( myrank == proc )
{
MPI_Recv( row.data(), N + 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &stat );
A.push_back( row );
}
}
return A;
}
//======================================
matrix randomData( int N )
{
procRow = veci( N );
localRow = veci( N );
matrix A;
vec row( N + 1 );
for ( int i = 0; i < N; i++ )
{
procRow [i] = i % size;
localRow[i] = i / size;
int proc = procRow[i];
if ( myrank == 0 )
{
for ( int j = 0; j < N + 1; j++ ) row[j] = rand() % 10;
if ( myrank == proc ) A.push_back( row );
else MPI_Send( row.data(), N + 1, MPI_DOUBLE, proc, tag, MPI_COMM_WORLD );
}
else if ( myrank == proc )
{
MPI_Recv( row.data(), N + 1, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &stat );
A.push_back( row );
}
}
return A;
}
//======================================
|