integral2_mpi.c 4.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// calculate integral using client-server model
// compile:  mpicc integral2_mpi.c -lm
// usage: mpirun -n process# ./a.out trial#

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <mpi.h>


#define CHUNK_SIZE 1000

// w is weight function
double w(double x, double y)
{
  return exp( - ( x*x + y*y ) );
}

//f is the function to be integrated
double f(double x, double y)
{

   return M_PI *  ( x*x + y*y ) ;

}


void generator(double * x, double * y)
{
   double xt, yt, ratio, tmp ;
   double delta = 0.5 ;


   tmp = (double)rand() / (double)RAND_MAX  ;
   xt = (*x) + delta * (2 * tmp - 1) ; 

   tmp = (double)rand() / (double)RAND_MAX  ;
   yt = (*y) + delta * (2 * tmp - 1) ; 

   ratio = w(xt, yt ) / w(*x, *y) ;
   tmp = (double)rand() / (double)RAND_MAX  ;

   if(ratio > tmp )
   {

      *x = xt ;
      *y = yt ;
   }


}


int main(int argc, char* argv[])
{
   double  x, y ;

   double sum ;
   double total_sum ;
   double integral ;
   int trials, trials_per_p;
   int i, j ;
   int pnumber, myrank;
   int x_array_tag = 1111 ;
   int y_array_tag = 2222 ;
   int demand_tag = 3333 ;
   int all_done_tag = 4444 ;
   int sum_tag = 5555 ;


   MPI_Init(&argc,&argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
   MPI_Comm_size(MPI_COMM_WORLD, &pnumber);




   x = 0.0 ;
   y = 0.0 ;


   trials = atoi(argv[1]);


   if(myrank == (pnumber - 1 ))
   {

      int sent = 0 ;
      int demander ;
      int destination = 0;
      double * x_array ;
      double * y_array ;
      int flag=0;
      MPI_Status status;
      MPI_Request request;
      x_array = malloc( trials * sizeof(double)) ;
      y_array = malloc( trials * sizeof(double)) ;

      int generated = 0 ;

      for(i=0 ;  i < trials ; i++)
      {
         generator(&x, &y);
         x_array[generated] = x ;
         y_array[generated] = y ;
         generated++ ;
      }


      while(sent < trials)
      {
     
         MPI_Recv(&demander, 1, MPI_INT, MPI_ANY_SOURCE,  demand_tag, MPI_COMM_WORLD, &status) ;

            MPI_Send(&x_array[sent  ], CHUNK_SIZE , MPI_DOUBLE, demander, x_array_tag, MPI_COMM_WORLD);
            MPI_Send(&y_array[sent  ], CHUNK_SIZE , MPI_DOUBLE, demander, y_array_tag, MPI_COMM_WORLD);
            sent += CHUNK_SIZE ;

      }




      for (i=0 ; i < (pnumber-2) ; i++)
      {
         MPI_Recv(&demander, 1, MPI_INT, MPI_ANY_SOURCE,  demand_tag, MPI_COMM_WORLD, &status) ;

      }

      double all_done = 1.0 ;
     


      MPI_Send(&all_done, 1 , MPI_DOUBLE, 0, all_done_tag, MPI_COMM_WORLD);


   }


   if(myrank == 0)
   {
      int all_done = 0;
      double sum = 0.0 ;
      double temp;
      MPI_Status status;
      MPI_Request request;

      while(1)
      {
         MPI_Recv(&temp, 1, MPI_DOUBLE, MPI_ANY_SOURCE,  MPI_ANY_TAG , MPI_COMM_WORLD, &status) ;
         if(status.MPI_SOURCE == (pnumber - 1))
         {
            printf("all done received\n");
            break ;
         }
         sum += temp ;
      }

      printf("%f\n", sum / trials);
      MPI_Finalize();
   }



   
   if((myrank != 0) && (myrank != pnumber-1) )
   {

      MPI_Status status;
      MPI_Request request;

      int flag;
      double * local_x_array ;
      double * local_y_array ;

      local_x_array = malloc( CHUNK_SIZE * sizeof(double)) ;
      local_y_array = malloc( CHUNK_SIZE * sizeof(double)) ;

      while(1)
      {
         MPI_Send(&myrank, 1 , MPI_INT, (pnumber-1), demand_tag, MPI_COMM_WORLD);
         MPI_Recv(local_x_array, CHUNK_SIZE , MPI_DOUBLE, (pnumber - 1 ),  x_array_tag, MPI_COMM_WORLD, &status) ;
         MPI_Recv(local_y_array, CHUNK_SIZE , MPI_DOUBLE, (pnumber - 1 ),  y_array_tag, MPI_COMM_WORLD, &status) ;

     
            for(i=0 ; i < CHUNK_SIZE ; i++)
            {
               sum += f(local_x_array[i], local_y_array[i]) ;

            }
         MPI_Send(&sum, 1 , MPI_DOUBLE, 0, sum_tag, MPI_COMM_WORLD);
         sum = 0.0 ;

       }

         

   }


   return 0;

}