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