[6991] | 1 | // This is an alternative parallel version of quadrature_sequential.c |
---|
| 2 | // |
---|
| 3 | // It differs from quadratur_parallel.c by the way work is distributed. |
---|
| 4 | // This one uses a 'round robin' strategy. |
---|
| 5 | // |
---|
| 6 | // To compile: mpicc quadrature_parallel.c -lm |
---|
| 7 | // |
---|
| 8 | // This code also exhibits linear speedup. See listed output at the |
---|
| 9 | // bottom of this file. |
---|
| 10 | // |
---|
| 11 | // Ole Nielsen, SUT 2003 |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | #include <math.h> |
---|
| 15 | #include <stdio.h> |
---|
| 16 | #include <time.h> |
---|
| 17 | #include <mpi.h> |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | //Define the circle arc to be integrated. |
---|
| 21 | long double fct(long double x) { |
---|
| 22 | return sqrt(1.0-x*x); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | int main(argc,argv) |
---|
| 27 | int argc; |
---|
| 28 | char *argv[]; |
---|
| 29 | { |
---|
| 30 | int N, i, lo, hi; |
---|
| 31 | long double result=0.0, my_result=0.0; |
---|
| 32 | long double reference = 3.1415926535897932; //Reference value, 17 digits |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | double t0; |
---|
| 36 | int p; // Myid |
---|
| 37 | int P; // Number of processors |
---|
| 38 | |
---|
| 39 | int namelen, tag=0, j, k; |
---|
| 40 | char processor_name[MPI_MAX_PROCESSOR_NAME]; |
---|
| 41 | int *C; |
---|
| 42 | |
---|
| 43 | MPI_Status status; |
---|
| 44 | |
---|
| 45 | MPI_Init(&argc, &argv); |
---|
| 46 | MPI_Comm_rank(MPI_COMM_WORLD, &p); |
---|
| 47 | MPI_Comm_size(MPI_COMM_WORLD, &P); |
---|
| 48 | MPI_Get_processor_name(processor_name, &namelen); |
---|
| 49 | |
---|
| 50 | printf("This is process %d of %d running on %s\n", |
---|
| 51 | p, P, processor_name); |
---|
| 52 | |
---|
| 53 | N = 100000000; // Resolution |
---|
| 54 | //N = 29; // Resolution |
---|
| 55 | |
---|
| 56 | if (p == 0) { |
---|
| 57 | printf("Resolution = %d\n", N); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // Begin computation |
---|
| 61 | t0 = MPI_Wtime(); |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | for (k=0; k<15; k++) { |
---|
| 65 | //Each process p handles own stride of points |
---|
| 66 | my_result = 0.0; |
---|
| 67 | for (i=p; i<N; i+=P) { |
---|
| 68 | my_result += fct( (i+0.5)/N ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | my_result = 4*my_result/N; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | printf("P%d: Partial result = %.16Lf\n", p, my_result); |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | // Communication phase |
---|
| 78 | if (p == 0) { |
---|
| 79 | |
---|
| 80 | //Receive results from others |
---|
| 81 | result = my_result; |
---|
| 82 | for (j=1; j<P; j++) { |
---|
| 83 | printf("P%d: receiving from P%d\n", p, j); |
---|
| 84 | MPI_Recv(&my_result, 1, MPI_LONG_DOUBLE, j, tag, |
---|
| 85 | MPI_COMM_WORLD, &status); |
---|
| 86 | result += my_result; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | printf("Error = %.2e\n", fabs(result-reference)); |
---|
| 90 | printf("Time = %.2f sec\n", MPI_Wtime() - t0); |
---|
| 91 | |
---|
| 92 | } else { |
---|
| 93 | // send my_result to intended proc 0 |
---|
| 94 | MPI_Send(&my_result, 1, MPI_LONG_DOUBLE, 0, tag, |
---|
| 95 | MPI_COMM_WORLD); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | MPI_Finalize(); |
---|
| 99 | return 0; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | //Observed output, Resolution = 100000000 |
---|
| 103 | //P = 1 |
---|
| 104 | //Error = 3.44e-13 |
---|
| 105 | //Time = 13.52 sec |
---|
| 106 | // |
---|
| 107 | //P = 2 |
---|
| 108 | //Error = 3.44e-13 |
---|
| 109 | //Time = 6.76 sec |
---|
| 110 | // |
---|
| 111 | //P = 4 |
---|
| 112 | //Error = 3.44e-13 |
---|
| 113 | //Time = 3.39 sec |
---|
| 114 | // |
---|
| 115 | //P = 8 |
---|
| 116 | //Error = 3.44e-13 |
---|
| 117 | //Time = 1.69 sec |
---|
| 118 | // |
---|
| 119 | // |
---|
| 120 | //SPEEDUP & EFFICIENCY |
---|
| 121 | //t = array([13.52, 6.76, 3.39, 1.69]) |
---|
| 122 | // |
---|
| 123 | //t[0]/t |
---|
| 124 | // |
---|
| 125 | //array([ 1. , 2. , 3.98820059, 8. ]) |
---|
| 126 | // |
---|
| 127 | //p = [1, 2, 4, 8] |
---|
| 128 | // |
---|
| 129 | //t[0]/t/p |
---|
| 130 | //array([ 1. , 1. , 0.99705015, 1. ]) |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|