Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mpi.h>
- #include <string>
- #include <unistd.h>
- #include <stdlib.h>
- #include <cmath>
- using namespace std;
- double f(double x)
- {
- return sqrt(1 - x*x);
- }
- int main(int argc, char** argt)
- {
- srand (time(NULL));
- MPI_Init(&argc, &argt);
- int rank, size;
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &size);
- int n = 0;
- double poczatek_calkowania = -1.0, koniec_calkowania = 1.0;
- if(rank == 0)
- {
- cout << "Podaj liczbe przdzialow calkowania n = ";
- cin >> n;
- }
- MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
- double delta = (koniec_calkowania - poczatek_calkowania) / n;
- double suma = 0.0;
- for(int i=rank; i<n; i+=size)
- {
- int przedzial = i;
- double x = poczatek_calkowania + przedzial*delta;
- suma += delta*f(x);
- }
- double calkowanie = 0.0;
- MPI_Reduce(&suma, &calkowanie, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
- if(rank == 0)
- {
- double pi = 2*calkowanie;
- cout << endl << "Pi = " << pi << endl;
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement