Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <mpi.h>
- #include <math.h>
- #define PI 3.14159265358979311600
- #define LOOPS 10000
- double losuj()
- {
- return ( (double)rand() / (double)RAND_MAX );
- }
- int main(int argc, char *argv[])
- {
- double x,y;
- double pi;
- int myid,numproc;
- long int kolo = 0.0;
- long int nSum, i;
- double czas1;
- double czas2;
- srand(time(NULL));
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &myid);
- MPI_Comm_size(MPI_COMM_WORLD, &numproc);
- czas1 = MPI_Wtime();
- for( i = 0; i < LOOPS/numproc; i++ )
- {
- x = ( (double)rand() / (double)RAND_MAX );
- y = ( (double)rand() / (double)RAND_MAX );
- if( x * x + y * y < 1.0)
- kolo++;
- MPI_Allreduce(&kolo, &nSum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
- MPI_Barrier(MPI_COMM_WORLD);
- pi = ( (double)nSum * 4.0 ) / ( (double)LOOPS );
- }
- czas2 = time(NULL);
- if(myid == 0)
- {
- printf("Czas potrzebny na obliczenie: %lf\n", czas2 - czas1);
- printf("Dana liczba pi: %1.20f\n", PI);
- printf("obliczona wartość pi: %1.20f dla %ld/%ld trafień do koła z wszystkich losowań\n", pi, nSum, LOOPS);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment