Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <mpi.h>
- #include <time.h>
- double calka(double x)
- {
- return 4.0/ (1 + x * x );
- }
- int main(int argc, char *argv[])
- {
- int id, numproc;
- int i,n, root = 0;
- double pi,h,suma,x;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &id);
- MPI_Comm_size(MPI_COMM_WORLD, &numproc);
- if(id == root)
- {
- printf("Okresl liczba przedzialow: ");
- }
- scanf("%d", &n);
- MPI_Bcast( &n, 1, MPI_INT, root, MPI_COMM_WORLD);
- h = 1.0/(double)n;
- suma = 0.0;
- for ( i = id; i < n; i+=numproc)
- {
- x = h * ((double)i + 0.5);
- suma += 4.0/(1 + x * x);
- }
- MPI_Reduce( &suma, &pi, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);
- if ( id == root )
- {
- pi = h * pi;
- printf("pi = %.20f\n", pi);
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment