Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #include "mpi.h"
- int main(int argc, char* argv[])
- {
- int points = 100000;
- int rank;
- int nproc;
- //coord pct
- double x,y;
- int i;
- //nr de pct din interiorul cercului pt fiecare proces
- int count = 0;
- double z;
- double pi;
- //nr de puncte din interiorul cercului in total(din toate procesele)
- int insideCirclePoints;
- //nr de puncte generate in total(din toate procesele)
- int totalPoints;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &nproc);
- if(rank != 0)
- {
- srand48(time(NULL));
- for (i=0; i < points; ++i)
- {
- x = (double)drand48();
- y = (double)drand48();
- //punct in cerc?
- z = ((x*x)+(y*y));
- if (z<=1)
- {
- count++;
- }
- }
- }
- MPI_Reduce(&count, &insideCirclePoints, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
- MPI_Reduce(&points, &totalPoints, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
- totalPoints -= points;
- if (rank == 0)
- {
- pi = ((double)insideCirclePoints/(double)totalPoints)*4.0;
- printf("pi = %f\n", pi);
- }
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment