#include "mpi.h"
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
int rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// coefficient matrix
vector<vector<double>> C = {
{0.05, -0.06, -0.12, 0.14},
{0.04, -0.12, 0.08, 0.11},
{0.34, 0.08, -0.06, 0.14},
{0.11, 0.12, 0, -0.03}
};
// constant vector
vector<double> d = {-2.17, 1.4, -2.1, -0.8};
// initial approximation (vector of zeros)
vector<double> x = {0, 0, 0, 0};
// desired accuracy
double eps = 1e-6;
// iteration counter
int k = 0;
// get the number of rows per process
int rows_per_proc = 4 / size;
// scatter the coefficient matrix and constant vector
vector<double> local_C(rows_per_proc * 4);
vector<double> local_d(rows_per_proc);
MPI_Scatter(C.data(), rows_per_proc * 4, MPI_DOUBLE, local_C.data(), rows_per_proc * 4, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter(d.data(), rows_per_proc, MPI_DOUBLE, local_d.data(), rows_per_proc, MPI_DOUBLE, 0, MPI_COMM_WORLD);
// iteration process
while (true) {
// compute new approximation locally
vector<double> x_new(rows_per_proc);
for (int i = 0; i < rows_per_proc; i++) {
double s = 0;
for (int j = 0; j < 4; j++) {
s += local_C[i * 4 + j] * x[j];
}
x_new[i] = s + local_d[i];
}
// gather new approximation from all processes
vector<double> global_x(4);
MPI_Allgather(x_new.data(), rows_per_proc, MPI_DOUBLE, global_x.data(), rows_per_proc, MPI_DOUBLE, MPI_COMM_WORLD);
// check for accuracy on root process
if (rank == 0) {
double norm = 0;
for (int i = 0; i < 4; i++) {
norm += pow(global_x[i] - x[i], 2);
}
if (sqrt(norm) < eps) {
break;
}
}
// broadcast updated approximation to all processes
MPI_Bcast(global_x.data(), 4, MPI_DOUBLE, 0, MPI_COMM_WORLD);
x = global_x;
// increment counter on all processes
k++; }
// print solution and number of iterations on root process
if (rank == 0) {
cout << "Solution: ";
for (int i = 0; i < 4; i++) {
cout << x[i] << " ";}
cout << endl;
cout << "Number of iterations: " << k << endl; }
MPI_Finalize();
return 0;}
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}