xref: /petsc/src/benchmarks/streams/MPIVersion.c (revision bcab024551cbaaaa7f49e357634a3584f91cb6d5)
1 
2 # include <stdio.h>
3 # include <math.h>
4 # include <limits.h>
5 # include <float.h>
6 
7 /*
8 * Program: Stream
9 * Programmer: Joe R. Zagar
10 * Revision: 4.0-BETA, October 24, 1995
11 * Original code developed by John D. McCalpin
12 *
13 * This program measures memory transfer rates in MB/s for simple
14 * computational kernels coded in C.  These numbers reveal the quality
15 * of code generation for simple uncacheable kernels as well as showing
16 * the cost of floating-point operations relative to memory accesses.
17 *
18 * INSTRUCTIONS:
19 *
20 *       1) Stream requires a good bit of memory to run.  Adjust the
21 *          value of 'N' (below) to give a 'timing calibration' of
22 *          at least 20 clock-ticks.  This will provide rate estimates
23 *          that should be good to about 5% precision.
24 */
25 
26 # define N      2000000
27 # define NTIMES 50
28 # define OFFSET 0
29 
30 /*
31 *      3) Compile the code with full optimization.  Many compilers
32 *         generate unreasonably bad code before the optimizer tightens
33 *         things up.  If the results are unreasonably good, on the
34 *         other hand, the optimizer might be too smart for me!
35 *
36 *         Try compiling with:
37 *               cc -O stream_d.c second.c -o stream_d -lm
38 *
39 *         This is known to work on Cray, SGI, IBM, and Sun machines.
40 *
41 *
42 *      4) Mail the results to mccalpin@cs.virginia.edu
43 *         Be sure to include:
44 *              a) computer hardware model number and software revision
45 *              b) the compiler flags
46 *              c) all of the output from the test case.
47 * Thanks!
48 *
49 */
50 
51 # define HLINE "-------------------------------------------------------------\n"
52 
53 # ifndef MIN
54 # define MIN(x,y) ((x)<(y) ? (x) : (y))
55 # endif
56 # ifndef MAX
57 # define MAX(x,y) ((x)>(y) ? (x) : (y))
58 # endif
59 
60 static double a[N+OFFSET],
61               b[N+OFFSET],
62               c[N+OFFSET];
63 /*double *a,*b,*c;*/
64 
65 static double mintime[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};
66 
67 static const char *label[4] = {"Copy:      ", "Scale:     ", "Add:       ", "Triad:     "};
68 
69 static double bytes[4] = {
70   2 * sizeof(double) * N,
71   2 * sizeof(double) * N,
72   3 * sizeof(double) * N,
73   3 * sizeof(double) * N
74 };
75 
76 #include <petscsys.h>
77 
78 int main(int argc,char **args)
79 {
80   int          quantum, checktick(void);
81   register int j, k;
82   double       scalar, t, times[4][NTIMES],irate[4],rate[4];
83   int          rank,size,resultlen;
84   char         hostname[MPI_MAX_PROCESSOR_NAME];
85   MPI_Status   status;
86 
87   MPI_Init(&argc,&args);
88   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
89   MPI_Comm_size(MPI_COMM_WORLD,&size);
90   if (!rank) printf("Number of MPI processes %d ",size);
91 
92   for (j=0; j<MPI_MAX_PROCESSOR_NAME; j++) {
93     hostname[j] = 0;
94   }
95   MPI_Get_processor_name(hostname,&resultlen);
96   if (!rank) {
97     printf("Processor names  %s ",hostname);
98     for (j=1; j<size; j++) {
99       MPI_Recv(hostname,MPI_MAX_PROCESSOR_NAME,MPI_CHAR,j,0,MPI_COMM_WORLD,&status);
100       printf("%s ",hostname);
101     }
102     printf("\n");
103  } else {
104    MPI_Send(hostname,MPI_MAX_PROCESSOR_NAME,MPI_CHAR,0,0,MPI_COMM_WORLD);
105  }
106  MPI_Barrier(MPI_COMM_WORLD);
107 
108   /* --- SETUP --- determine precision and check timing --- */
109 
110   if (!rank) {
111     /*printf(HLINE);
112     printf("Array size = %d, Offset = %d\n" , N, OFFSET);
113     printf("Total memory required = %.1f MB.\n", (3 * N * BytesPerWord) / 1048576.0);
114     printf("Each test is run %d times, but only\n", NTIMES);
115     printf("the *best* time for each is used.\n");
116     printf(HLINE); */
117   }
118 
119   /* Get initial value for system clock. */
120 
121   /*  a = malloc(N*sizeof(double));
122   b = malloc(N*sizeof(double));
123   c = malloc(N*sizeof(double));*/
124   for (j=0; j<N; j++) {
125     a[j] = 1.0;
126     b[j] = 2.0;
127     c[j] = 0.0;
128   }
129 
130   if (!rank) {
131     if  ((quantum = checktick()) >= 1) ; /* printf("Your clock granularity/precision appears to be %d microseconds.\n", quantum); */
132     else ; /* printf("Your clock granularity appears to be less than one microsecond.\n");*/
133   }
134 
135   t = MPI_Wtime();
136   for (j = 0; j < N; j++) a[j] = 2.0E0 * a[j];
137   t = 1.0E6 * (MPI_Wtime() - t);
138 
139   if (!rank) {
140     /*  printf("Each test below will take on the order of %d microseconds.\n", (int) t);
141     printf("   (= %d clock ticks)\n", (int) (t/quantum));
142     printf("Increase the size of the arrays if this shows that\n");
143     printf("you are not getting at least 20 clock ticks per test.\n");
144     printf(HLINE);*/
145   }
146 
147 
148   /*   --- MAIN LOOP --- repeat test cases NTIMES times --- */
149 
150   scalar = 3.0;
151   for (k=0; k<NTIMES; k++)
152   {
153     MPI_Barrier(MPI_COMM_WORLD);
154     times[0][k] = MPI_Wtime();
155     /* should all these barriers be pulled outside of the time call? */
156     MPI_Barrier(MPI_COMM_WORLD);
157     for (j=0; j<N; j++) c[j] = a[j];
158     MPI_Barrier(MPI_COMM_WORLD);
159     times[0][k] = MPI_Wtime() - times[0][k];
160 
161     times[1][k] = MPI_Wtime();
162     MPI_Barrier(MPI_COMM_WORLD);
163     for (j=0; j<N; j++) b[j] = scalar*c[j];
164     MPI_Barrier(MPI_COMM_WORLD);
165     times[1][k] = MPI_Wtime() - times[1][k];
166 
167     times[2][k] = MPI_Wtime();
168     MPI_Barrier(MPI_COMM_WORLD);
169     for (j=0; j<N; j++) c[j] = a[j]+b[j];
170     MPI_Barrier(MPI_COMM_WORLD);
171     times[2][k] = MPI_Wtime() - times[2][k];
172 
173     times[3][k] = MPI_Wtime();
174     MPI_Barrier(MPI_COMM_WORLD);
175     for (j=0; j<N; j++) a[j] = b[j]+scalar*c[j];
176     MPI_Barrier(MPI_COMM_WORLD);
177     times[3][k] = MPI_Wtime() - times[3][k];
178   }
179 
180   /*   --- SUMMARY --- */
181 
182   for (k=0; k<NTIMES; k++)
183     for (j=0; j<4; j++) mintime[j] = MIN(mintime[j], times[j][k]);
184 
185   for (j=0; j<4; j++) irate[j] = 1.0E-06 * bytes[j]/mintime[j];
186   MPI_Reduce(irate,rate,4,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
187 
188   if (!rank) {
189     printf("%s  %11.4f   Rate (MB/s) \n", label[3],rate[3]);
190     /* for (j=0; j<4; j++) printf("%s%11.4f\n", label[j],rate[j]);*/
191   }
192   MPI_Finalize();
193   return 0;
194 }
195 
196 # define        M        20
197 
198 int checktick(void)
199 {
200   int    i, minDelta, Delta;
201   double t1, t2, timesfound[M];
202 
203 /*  Collect a sequence of M unique time values from the system. */
204 
205   for (i = 0; i < M; i++) {
206     t1 = MPI_Wtime();
207     while (((t2=MPI_Wtime()) - t1) < 1.0E-6) ;
208     timesfound[i] = t1 = t2;
209   }
210 
211 /*
212 * Determine the minimum difference between these M values.
213 * This result will be our estimate (in microseconds) for the
214 * clock granularity.
215 */
216 
217   minDelta = 1000000;
218   for (i = 1; i < M; i++) {
219     Delta    = (int)(1.0E6 * (timesfound[i]-timesfound[i-1]));
220     minDelta = MIN(minDelta, MAX(Delta,0));
221   }
222 
223   return(minDelta);
224 }
225 
226