Blame view
lab1/pi_mpi.c
977 Bytes
6a98a5afa lab1 |
1 |
#include <stdio.h> |
adfef3110 add n |
2 |
#include <stdlib.h> |
6a98a5afa lab1 |
3 |
#include <string.h> |
adfef3110 add n |
4 |
|
f0c55387c fix mpi makefile |
5 |
#include <mpi.h> |
adfef3110 add n |
6 |
#define N 1000000000 |
6a98a5afa lab1 |
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
int main (int argc, char** argv) { int rank; int size; long long int n; long long int i; double l_sum,total_sum, x, h; MPI_Init(NULL, NULL); MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); n=N; if(rank==0) { if(argc==2) { |
29b151012 MAJ after first lab |
28 |
n = atol(argv[1]); |
6a98a5afa lab1 |
29 30 31 32 33 34 35 |
} printf("MPI version with process = %d ", size); printf("Number of intervals: %lld ", n); } |
29b151012 MAJ after first lab |
36 |
MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD); |
6a98a5afa lab1 |
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
h = 1.0/n; l_sum = 0.0; for (i = rank; i < n; i += size) { x = (i + 0.5)*h; l_sum += 4.0/(1.0 + x*x); } l_sum *= h; MPI_Reduce(&l_sum,&total_sum,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); if(rank==0) { |
adfef3110 add n |
55 56 |
printf("Pi=%0.20g ", total_sum); |
6a98a5afa lab1 |
57 58 59 60 61 62 |
} MPI_Finalize(); return 0; } |