proteus  1.8.1
C/C++/Fortran libraries
flopTest.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <strings.h>
5 #include <mach/mach_time.h>
6 /*#include <Accelerate/Accelerate.h>*/
7 
8 //****************************************************
9 #pragma mark -
10 #pragma mark * local ( static ) function prototypes *
11 //----------------------------------------------------
12 
13 static double CurrentTime(void);
14 
15 //****************************************************
16 #pragma mark -
17 #pragma mark * exported function implementations *
18 //----------------------------------------------------
19 
20 int main(int argc, char* argv[])
21 {
22  const int vlen=atoi(argv[1]),stride=1;
23  register int i,m,ntimes;
24  register double a=atof(argv[2]),b=atof(argv[3]),c=atof(argv[4]),xi,yi;
25  double *x,*y,*z,*u,*v,*w,start,stop,diff;
26  ntimes =0;
27  while (ntimes< 25)
28  {
29  ntimes+=1;
30  if(argc < 5)
31  {
32  printf("usage: zerotest vlen da db dc");
33  exit(1);
34  }
35  start = CurrentTime();
36  x = malloc(sizeof(double)*vlen);
37  y = malloc(sizeof(double)*vlen);
38  z = malloc(sizeof(double)*vlen);
39  u = malloc(sizeof(double)*vlen);
40  v = malloc(sizeof(double)*vlen);
41  w = malloc(sizeof(double)*vlen);
42  stop = CurrentTime();
43  printf("%10.2e s %10.2e seconds per double; malloc\n",(stop-start),(stop-start)/(3.0*vlen));
44 
45  start = CurrentTime();
46  memset(x,0,vlen*sizeof(double));
47  memset(y,0,vlen*sizeof(double));
48  memset(z,0,vlen*sizeof(double));
49  stop = CurrentTime();
50  printf("%10.2e s %10.2e seconds per double; memset\n",(stop-start),(stop-start)/(3.0*vlen));
51 
52  start = CurrentTime();
53  for (i=0;i<vlen;i++)
54  {
55  u[i]=c;
56  v[i]=b;
57  w[i]=a;
58  }
59  stop = CurrentTime();
60  printf("%10.2e s %10.2e seconds per double; x[i]=c \n",(stop-start),(stop-start)/(3.0*vlen));
61 
62  start = CurrentTime();
63  for (i=0;i<vlen;i++)
64  {
65  y[i]=x[i];
66  }
67  stop = CurrentTime();
68  printf("%10.2e s %10.2e seconds per double; y[i]=x[i]\n",(stop-start),(stop-start)/(float)(vlen));
69 
70  start = CurrentTime();
71  cblas_dcopy(vlen,x,stride,y,stride);
72  stop = CurrentTime();
73  printf("%10.2e s %10.2e seconds per double; dcopy y[i]=x[i]\n",(stop-start),(stop-start)/(float)(vlen));
74 
75  start = CurrentTime();
76  for (i=0;i<vlen;i++)
77  {
78  x[i]*=a;
79  }
80  stop = CurrentTime();
81  printf("%10.2e s %10.2f MFLOPS; y[i]=a*x[i]\n",(stop-start),((stop-start)/(float)(vlen))/1.0e6);
82 
83  start = CurrentTime();
84  cblas_dscal(vlen,a,x,stride);
85  stop = CurrentTime();
86  printf("%10.2e s %10.2f MFLOPS; dscal y[i]=a*x[i]\n",(stop-start),((stop-start)/(float)(vlen))/1.0e6);
87 
88  start = CurrentTime();
89  for(i=0;i<vlen;i++)
90  y[i] += a*x[i];
91  stop = CurrentTime();
92  printf("%10.2e s %10.2f MFLOPS; y[i] += a*x[i]\n",(stop-start),(vlen*2/(stop-start))/1.0e6);
93 
94  start = CurrentTime();
95  cblas_daxpy(vlen,a,x,stride,y,stride);
96  stop = CurrentTime();
97  printf("%10.2e s %10.2f MFLOPS; daxpy y[i] += a*x[i]\n",(stop-start),(vlen*2/(stop-start))/1.0e6);
98 
99  for(i=0;i<vlen;i++)
100  y[i] += a*x[i];
101  stop = CurrentTime();
102  printf("%10.2e s %10.2f MFLOPS; y[i] += a*x[i]\n",(stop-start),(vlen*2/(stop-start))/1.0e6);
103 
104  start = CurrentTime();
105  for(i=0;i<vlen;i++)
106  y[i] = c + a*x[i];
107  stop = CurrentTime();
108  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i]\n",(stop-start),(vlen*2/(stop-start))/1.0e6);
109 
110  start = CurrentTime();
111  for(i=0;i<vlen;i++)
112  y[i] = c + a*x[i] + b*y[i];
113  stop = CurrentTime();
114  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*y[i]\n",(stop-start),(vlen*4/(stop-start))/1.0e6);
115 
116  start = CurrentTime();
117  for(i=0;i<vlen;i++)
118  y[i] = c + a*x[i] + b*y[i]+c*x[i]*y[i];
119  stop = CurrentTime();
120  printf("%10.2e s %10.2f MFLOPS y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i]\n",(stop-start),(vlen*7/(stop-start))/1.0e6);
121 
122  start = CurrentTime();
123  for(i=0;i<vlen;i++)
124  y[i] = c + a*x[i] + b*y[i]+c*x[i]*y[i] + a*x[i]*x[i];
125  stop = CurrentTime();
126  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i]\n",(stop-start),(vlen*10/(stop-start))/1.0e6);
127 
128  start = CurrentTime();
129  for(i=0;i<vlen;i++)
130  y[i] = c + a*x[i] + b*y[i]+c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i];
131  stop = CurrentTime();
132  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i]\n",(stop-start),(vlen*13/(stop-start))/1.0e6);
133 
134  start = CurrentTime();
135  for (i=0;i<vlen;i++)
136  y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i] + c*x[i]*x[i]*x[i];
137  stop = CurrentTime();
138  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i] + c*x[i]*x[i]*x[i]\n",(stop-start),(vlen*17/(stop-start))/1.0e6);
139 
140  start = CurrentTime();
141  for (i=0;i<vlen;i++)
142  y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i] + c*x[i]*x[i]*x[i] + a*x[i]*y[i]*y[i] + b*x[i]*z[i]*x[i];
143  stop = CurrentTime();
144  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*y[i] + c*x[i]*y[i] + a*x[i]*x[i] + b*y[i]*y[i] + c*x[i]*x[i]*x[i] + a*x[i]*y[i]*y[i] + b*x[i]*z[i]*x[i]\n",(stop-start),(vlen*25/(stop-start))/1.0e6);
145 
146  /* 3 vectors */
147 
148  start = CurrentTime();
149  for(i=0;i<vlen;i++)
150  y[i] = c + a*x[i] + b*z[i];
151  stop = CurrentTime();
152  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*z[i]\n",(stop-start),(vlen*4/(stop-start))/1.0e6);
153 
154  start = CurrentTime();
155  for(i=0;i<vlen;i++)
156  y[i] = c + a*x[i] + b*z[i]+c*x[i]*z[i];
157  stop = CurrentTime();
158  printf("%10.2e s %10.2f MFLOPS y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i]\n",(stop-start),(vlen*7/(stop-start))/1.0e6);
159 
160  start = CurrentTime();
161  for(i=0;i<vlen;i++)
162  y[i] = c + a*x[i] + b*z[i]+c*x[i]*z[i] + a*x[i]*x[i];
163  stop = CurrentTime();
164  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i]\n",(stop-start),(vlen*10/(stop-start))/1.0e6);
165 
166  start = CurrentTime();
167  for(i=0;i<vlen;i++)
168  y[i] = c + a*x[i] + b*z[i]+c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i];
169  stop = CurrentTime();
170  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i]\n",(stop-start),(vlen*13/(stop-start))/1.0e6);
171 
172  start = CurrentTime();
173  for (i=0;i<vlen;i++)
174  y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i] + c*x[i]*x[i]*x[i];
175  stop = CurrentTime();
176  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i] + c*x[i]*x[i]*x[i]\n",(stop-start),(vlen*17/(stop-start))/1.0e6);
177 
178  start = CurrentTime();
179  for (i=0;i<vlen;i++)
180  y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i] + c*x[i]*x[i]*x[i] + a*x[i]*z[i]*z[i] + b*x[i]*z[i]*x[i];
181  stop = CurrentTime();
182  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*x[i] + b*z[i] + c*x[i]*z[i] + a*x[i]*x[i] + b*z[i]*z[i] + c*x[i]*x[i]*x[i] + a*x[i]*z[i]*z[i] + b*x[i]*z[i]*x[i]\n",(stop-start),(vlen*25/(stop-start))/1.0e6);
183 
184 
185  /* 3 vectors */
186  start = CurrentTime();
187  for(i=0;i<vlen;i++)
188  y[i] = c + a*u[i] + b*z[i]+c*x[i]*z[i];
189  stop = CurrentTime();
190  printf("%10.2e s %10.2f MFLOPS y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i]\n",(stop-start),(vlen*7/(stop-start))/1.0e6);
191 
192  start = CurrentTime();
193  for(i=0;i<vlen;i++)
194  y[i] = c + a*u[i] + b*z[i]+c*x[i]*z[i] + a*u[i]*x[i];
195  stop = CurrentTime();
196  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i]\n",(stop-start),(vlen*10/(stop-start))/1.0e6);
197 
198  start = CurrentTime();
199  for(i=0;i<vlen;i++)
200  y[i] = c + a*u[i] + b*z[i]+c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i];
201  stop = CurrentTime();
202  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i]\n",(stop-start),(vlen*13/(stop-start))/1.0e6);
203 
204  start = CurrentTime();
205  for (i=0;i<vlen;i++)
206  y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i] + c*u[i]*x[i]*u[i];
207  stop = CurrentTime();
208  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i] + c*u[i]*x[i]*u[i]\n",(stop-start),(vlen*17/(stop-start))/1.0e6);
209 
210  start = CurrentTime();
211  for (i=0;i<vlen;i++)
212  y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i] + c*u[i]*x[i]*u[i] + a*x[i]*z[i]*z[i] + b*u[i]*z[i]*x[i];
213  stop = CurrentTime();
214  printf("%10.2e s %10.2f MFLOPS; y[i] = c + a*u[i] + b*z[i] + c*x[i]*z[i] + a*u[i]*x[i] + b*z[i]*z[i] + c*u[i]*x[i]*u[i] + a*x[i]*z[i]*z[i] + b*u[i]*z[i]*x[i]\n",(stop-start),(vlen*25/(stop-start))/1.0e6);
215 
216 
217  start = CurrentTime();
218  free(x);
219  free(y);
220  free(z);
221  free(u);
222  free(v);
223  free(w);
224  stop = CurrentTime();
225  printf("%10.2e s %10.2e seconds per double; free; \n",(stop-start),(stop-start)/(6.0*vlen));
226 }
227 }
228 
229 //****************************************************
230 #pragma mark -
231 #pragma mark * local ( static ) function implementations *
232 //----------------------------------------------------
233 
234 
235 //
236 // Returns the current time in seconds
237 //
238 //cek took from apple
239 static double CurrentTime(void)
240 {
241  static double scale = 0.0;
242 
243  if (0.0 == scale) {
244  mach_timebase_info_data_t info;
245  mach_timebase_info(&info);
246  scale = info.numer / info.denom * 1e-9;
247  }
248 
249  return mach_absolute_time() * scale;
250 } // CurrentTime
w
#define w(x)
Definition: jf.h:22
v
Double v
Definition: Headers.h:95
z
Double * z
Definition: Headers.h:49
main
int main(int argc, char *argv[])
Definition: flopTest.c:20
u
Double u
Definition: Headers.h:89
c
Double c
Definition: Headers.h:54