Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6f366754f2e0b45922da51b4844a021888d38ce5
[simgrid.git] / examples / smpi / NAS / ep.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5 #include "smpi/mpi.h"
6 #include "nas_common.h"
7 #include "simgrid/instr.h" //TRACE_
8
9 char class;
10 int nprocs;
11
12 #define TRUE 1
13 #define FALSE 0
14
15 int main(int argc, char **argv) {
16   double dum[3] = {1.,1.,1.};
17   double x1, x2, sx, sy, tm, an, tt, gc;
18   double Mops;
19   double epsilon=1.0E-8, a = 1220703125., s=271828183.;
20   double t1, t2, t3, t4;
21   double sx_verify_value, sy_verify_value, sx_err, sy_err;
22
23   int    m;
24   int    mk=16;
25   int    nk = (int)(pow(2,mk)),
26          nq=10, 
27          np, node, no_nodes, i, ik, kk, l, k, nit, no_large_nodes, np_add, k_offset;
28   int    verified;
29   char   size[500]; // mind the size of the string to represent a big number
30
31   double *x = (double *) malloc (2*nk*sizeof(double));
32   double *q = (double *) malloc (nq*sizeof(double));
33
34   MPI_Init( &argc, &argv );
35   MPI_Comm_size( MPI_COMM_WORLD, &no_nodes);
36   MPI_Comm_rank( MPI_COMM_WORLD, &node);
37
38   TRACE_smpi_set_category ("start");
39
40   get_info(argc, argv, &nprocs, &class);
41   check_info(EP, nprocs, class);
42
43   if      (class == 'S')
44     { m = 24; }
45   else if (class == 'W')
46     { m = 25; }
47   else if (class == 'A')
48     { m = 28; }
49   else if (class == 'B')
50     { m = 30; }
51   else if (class == 'C')
52     { m = 32; }
53   else if (class == 'D')
54     { m = 36; }
55   else if (class == 'E')
56     { m = 40; }
57   else {
58     printf("EP: Internal error: invalid class type %c\n", class);
59     exit(1);
60   }
61
62   int mm = m -mk;
63   int nn = (int)(pow(2,mm));
64
65   int root = 0;
66   if (node == root ) {
67     /* Because the size of the problem is too large to store in a 32-bit integer for some classes, we put it into a
68      * string (for printing). Have to strip off the decimal point put in there by the floating point print statement
69      * (internal file) */
70     fprintf(stdout," NAS Parallel Benchmarks 3.2 -- EP Benchmark");
71     snprintf(size,500,"%lu",(unsigned long)pow(2,m+1));
72     fprintf(stdout," Number of random numbers generated: %s\n",size);
73     fprintf(stdout," Number of active processes: %d\n",no_nodes);
74   }
75   verified = FALSE;
76
77   /* Compute the number of "batches" of random number pairs generated per processor. Adjust if the number of processors
78    * does not evenly divide the total number  */
79   np = nn / no_nodes;
80   no_large_nodes = nn % no_nodes;
81   if (node < no_large_nodes)
82     np_add = 1;
83   else
84     np_add = 0;
85   np = np + np_add;
86
87   if (np == 0) {
88     fprintf(stdout,"Too many nodes: %d  %d",no_nodes,nn);
89     MPI_Abort(MPI_COMM_WORLD,1);
90     exit(0);
91   }
92
93   /* Call the random number generator functions and initialize the x-array to reduce the effects of paging the timings.
94    Also, call all mathematical functions that are used. Make sure initializations cannot be eliminated as dead code. */
95   vranlc(0, dum[0], dum[1], &(dum[2]));
96
97   dum[0] = randlc(&(dum[1]),&(dum[2]));
98   for (i=0;i<2*nk;i++) {
99     x[i] = -1e99;
100   }
101   Mops = log(sqrt(abs(1)));
102
103   /* Synchronize before placing time stamp */
104   MPI_Barrier( MPI_COMM_WORLD );
105
106   TRACE_smpi_set_category ("ep");
107
108   timer_clear(1);
109   timer_clear(2);
110   timer_clear(3);
111   timer_start(1);
112
113   t1 = a;
114   t1 = vranlc(0, t1, a, x);
115
116   /* Compute AN = A ^ (2 * NK) (mod 2^46). */
117   t1 = a;
118   for (i=1; i <= mk+1; i++) {
119     t2 = randlc(&t1, &t1);
120   }
121   an = t1;
122   tt = s;
123   gc = 0;
124   tt = 0.;
125   sx = 0.;
126   sy = 0.;
127   for (i=0; i < nq ; i++) {
128     q[i] = 0.;
129   }
130
131 /* Each instance of this loop may be performed independently. We compute the k offsets separately to take into account
132  * the fact that some nodes have more numbers to generate than others */
133   if (np_add == 1)
134     k_offset = node * np -1;
135   else
136     k_offset = no_large_nodes*(np+1) + (node-no_large_nodes)*np -1;
137
138   int stop = FALSE;
139   for(k = 1; k <= np; k++) {// SMPI_SAMPLE_LOCAL(0.25 * np, 0.03) {
140     stop = FALSE;
141     kk = k_offset + k ;
142     t1 = s;
143     t2 = an;
144
145     //       Find starting seed t1 for this kk.
146     for (i=1;i<=100 && !stop;i++) {
147       ik = kk / 2;
148       if (2 * ik != kk)  {
149         t3 = randlc(&t1, &t2);
150       }
151       if (ik==0)
152         stop = TRUE;
153       else {
154         t3 = randlc(&t2, &t2);
155         kk = ik;
156       }
157     }
158     //       Compute uniform pseudorandom numbers.
159
160     timer_start(3);
161
162     t1 = vranlc(2 * nk, t1, a, x);
163
164     timer_stop(3);
165
166     /* Compute Gaussian deviates by acceptance-rejection method and tally counts in concentric square annuli.
167      * This loop is not vectorizable. */
168     timer_start(2);
169
170     for(i=1; i<=nk;i++) {
171       x1 = 2. * x[2*i-2] -1.0;
172       x2 = 2. * x[2*i-1] - 1.0;
173       t1 = x1*x1 + x2*x2;
174       if (t1 <= 1.) {
175         t2   = sqrt(-2. * log(t1) / t1);
176         t3   = (x1 * t2);
177         t4   = (x2 * t2);
178         l    = (int)(fabs(t3) > fabs(t4) ? fabs(t3) : fabs(t4));
179         q[l] = q[l] + 1.;
180         sx   = sx + t3;
181         sy   = sy + t4;
182       }
183     }
184     timer_stop(2);
185   }
186
187   TRACE_smpi_set_category ("finalize");
188
189   MPI_Allreduce(&sx, x, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
190   sx = x[0];
191   MPI_Allreduce(&sy, x, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
192   sy = x[0];
193   MPI_Allreduce(q, x, nq, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
194
195   for(i = 0; i < nq; i++) {
196     q[i] = x[i];
197   }
198   for(i = 0; i < nq; i++) {
199     gc += q[i];
200   }
201
202   timer_stop(1);
203   tm = timer_read(1);
204   MPI_Allreduce(&tm, x, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
205   tm = x[0];
206
207   free(x);
208
209   if(node == root) {
210     nit = 0;
211     verified = TRUE;
212
213     if(m == 24) {
214       sx_verify_value = -3.247834652034740E3;
215       sy_verify_value = -6.958407078382297E3;
216     } else if(m == 25) {
217       sx_verify_value = -2.863319731645753E3;
218       sy_verify_value = -6.320053679109499E3;
219     } else if(m == 28) {
220       sx_verify_value = -4.295875165629892E3;
221       sy_verify_value = -1.580732573678431E4;
222     } else if(m == 30) {
223       sx_verify_value =  4.033815542441498E4;
224       sy_verify_value = -2.660669192809235E4;
225     } else if(m == 32) {
226       sx_verify_value =  4.764367927995374E4;
227       sy_verify_value = -8.084072988043731E4;
228     } else if(m == 36) {
229       sx_verify_value =  1.982481200946593E5;
230       sy_verify_value = -1.020596636361769E5;
231     } else {
232       verified = FALSE;
233     }
234
235     if(verified) {
236       sx_err = fabs((sx - sx_verify_value)/sx_verify_value);
237       sy_err = fabs((sy - sy_verify_value)/sy_verify_value);
238       verified = ((sx_err < epsilon) && (sy_err < epsilon));
239     }
240
241     Mops = (pow(2.0, m+1))/tm/1000;
242
243     fprintf(stdout,"EP Benchmark Results:\n");
244     fprintf(stdout,"CPU Time=%d\n",(int) tm);
245     fprintf(stdout,"N = 2^%d\n",m);
246     fprintf(stdout,"No. Gaussain Pairs =%d\n",(int) gc);
247     fprintf(stdout,"Sum = %f %ld\n",sx,(long) sy);
248     fprintf(stdout,"Count:");
249     for(i = 0; i < nq; i++) {
250       fprintf(stdout,"%d\t %ld\n",i,(long) q[i]);
251     }
252     c_print_results("EP", class, m+1, 0, 0, nit, nprocs, no_nodes, tm, Mops, "Random number generated",verified);
253
254     fprintf(stdout,"Total time:     %f\n",(timer_read(1)/1000));
255     fprintf(stdout,"Gaussian pairs: %f\n",(timer_read(2)/1000));
256     fprintf(stdout,"Random numbers: %f\n",(timer_read(3)/1000));
257   }
258
259   free(q);
260
261   MPI_Finalize();
262   return 0;
263 }