Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include "mpi.h"
- #include <iostream>
- using namespace std;
- float integral(const float a, const int n, const float h)
- {
- float result = 0.0;
- const float local_h = h / 2.0;
- for (auto i = 0; i < n; i++)
- {
- const auto aij = a + i * h;
- result += (aij + local_h) * (aij + local_h) * h;
- }
- return result;
- }
- void main(int argc, char* argv[])
- {
- int p,id;
- MPI_Status status;
- float result;
- const float a = 1;
- const float b = 4;
- const auto n = 100000;
- const auto dest = 0;
- const auto tag = 0;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &id);
- MPI_Comm_size(MPI_COMM_WORLD, &p);
- const auto h = (b - a) / n;
- const auto num = n / p;
- const auto range = (b - a) / p;
- const auto x = a + id * range;
- result = integral(x, num, h);
- printf("Rank %d, sum = %f\n", id, result);
- if (id == 0)
- {
- auto loacl_result = result;
- for (auto i = 1; i < p; i++)
- {
- const auto source = i;
- MPI_Recv(&result, 1, MPI_REAL, source, tag,
- MPI_COMM_WORLD, &status);
- loacl_result += result;
- }
- printf("Total sum = %f\n", loacl_result);
- }
- else
- MPI_Send(&result, 1, MPI_REAL, dest, tag,
- MPI_COMM_WORLD);
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment