1 | /* #include "machdefs.h" */
|
---|
2 |
|
---|
3 | #include <stdlib.h>
|
---|
4 | #include <stdio.h>
|
---|
5 | #include <math.h>
|
---|
6 | #include <string.h>
|
---|
7 | #include <time.h>
|
---|
8 |
|
---|
9 |
|
---|
10 | /* --------------------------------------------------- */
|
---|
11 | /* --- Petit programme d'estimation de la puissance CPU */
|
---|
12 | /* Compilation: csh> cc -O3 -o cpupower cpupower.c -lm */
|
---|
13 | /* R. Ansari - LAL Mai 2004 */
|
---|
14 | /* --------------------------------------------------- */
|
---|
15 |
|
---|
16 | /* Choix de type d'operations float double int ... */
|
---|
17 | #define T_Type double
|
---|
18 |
|
---|
19 | static int SZ; /* Taille de tableau */
|
---|
20 | static double N_OP; /* Nb operations */
|
---|
21 | static int OPE; /* Choix Operation fop_1/2/3/4 */
|
---|
22 | static int ckprt; /* > 0 : Print check apres calcul */
|
---|
23 |
|
---|
24 | /* Tableaux X,Y,Z */
|
---|
25 | static T_Type * x;
|
---|
26 | static T_Type * y;
|
---|
27 | static T_Type * z;
|
---|
28 |
|
---|
29 | void fop_1()
|
---|
30 | {
|
---|
31 | T_Type s=0;
|
---|
32 | int i,j,k;
|
---|
33 | printf("--- fop_1: Double Loop: z[k] = Somme_i(x[k]*y[i]) --- \n");
|
---|
34 | for(k=0; k<SZ; k++) {
|
---|
35 | for(i=0; i<SZ; i++) s += x[k]*y[i];
|
---|
36 | z[k] = s;
|
---|
37 | N_OP += 2*SZ;
|
---|
38 | }
|
---|
39 | return;
|
---|
40 | }
|
---|
41 |
|
---|
42 | void fop_2()
|
---|
43 | {
|
---|
44 | T_Type s=0;
|
---|
45 | int i,j,k;
|
---|
46 | printf("--- fop_2: Double Loop: z[k] = Somme_i(x[k]*y[i]+x[i]*y[k]) --- \n");
|
---|
47 | for(k=0; k<SZ; k++) {
|
---|
48 | for(i=0; i<SZ; i++) s += x[k]*y[i]+x[i]*y[k];
|
---|
49 | z[k] = s;
|
---|
50 | N_OP += 4*SZ;
|
---|
51 | }
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void fop_3()
|
---|
56 | {
|
---|
57 | T_Type s=0;
|
---|
58 | int i,j,k;
|
---|
59 | printf("--- fop_3: Double Loop: z[k] = Somme_i(x[k]*y[i]+x[i]*y[k]-0.85*(y[k]+x[k])) --- \n");
|
---|
60 | for(k=0; k<SZ; k++) {
|
---|
61 | for(i=0; i<SZ; i++) s += x[k]*y[i]+x[i]*y[k]-0.85*(y[k]+x[k]);
|
---|
62 | z[k] = s;
|
---|
63 | N_OP += 7*SZ;
|
---|
64 | }
|
---|
65 | return;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void fop_4()
|
---|
69 | {
|
---|
70 | T_Type s=0;
|
---|
71 | int i,j,k;
|
---|
72 | printf("--- fop_4: Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+y[k]*cos(x[i])) --- \n");
|
---|
73 | for(k=0; k<SZ; k++) {
|
---|
74 | for(i=0; i<SZ; i++) s += x[k]*sin(y[i])+y[k]*cos(x[i]);
|
---|
75 | z[k] = s;
|
---|
76 | N_OP += 70*SZ; // le facteur 70 est approximatif
|
---|
77 | }
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | void fop_5()
|
---|
82 | {
|
---|
83 | T_Type s=0;
|
---|
84 | int i,j,k;
|
---|
85 | printf("--- fop_5: Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+log(fabs(x[i])+0.1)+y[k]) --- \n");
|
---|
86 | for(k=0; k<SZ; k++) {
|
---|
87 | for(i=0; i<SZ; i++) s += x[k]*sin(y[i])+log(fabs(x[i])+0.1)+y[k];
|
---|
88 | z[k] = s;
|
---|
89 | N_OP += 100*SZ; // le facteur 100 est approximatif
|
---|
90 | }
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /* Fonctions de timing (TCPU) - voir en fin de fichier */
|
---|
95 | void InitTim(void);
|
---|
96 | void PrtTim(const char * Comm);
|
---|
97 | double GetPartialCPUTime();
|
---|
98 | double GetTotalCPUTime();
|
---|
99 |
|
---|
100 | int main(int narg, char* arg[])
|
---|
101 | {
|
---|
102 | int maxnprt = 1000;
|
---|
103 | int i,nprt;
|
---|
104 | double mflops;
|
---|
105 |
|
---|
106 | /* SophyaInit(); */
|
---|
107 | InitTim(); /* Initializing the CPU timer */
|
---|
108 |
|
---|
109 | if (narg < 2) {
|
---|
110 | printf("--- Programme cpupower: (Puissance de calcul) ---- \n");
|
---|
111 | printf(" Usage cpupower Op=1/2/3 [Size=20000] [CkPrt=0] \n");
|
---|
112 | printf(" Op=1 Double Loop: z[k] = Somme_i(x[k]*y[i]) \n");
|
---|
113 | printf(" Op=1 *,+=2 op / tour de boucle \n");
|
---|
114 | printf(" Op=2 Double Loop: z[k] = Somme_i(x[k]*y[i]+x[i]*y[k]) \n");
|
---|
115 | printf(" Op=2 2x(*,+)=4 op / tour de boucle \n");
|
---|
116 | printf(" Op=3 Double Loop: z[k] = Somme_i(x[k]*y[i]+x[i]*y[k]-0.85*(y[k]+x[k])) \n");
|
---|
117 | printf(" Op=3 7 op (*,+) / tour de boucle \n");
|
---|
118 | printf(" Op=4 Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+y[k]*cos(x[i])) \n");
|
---|
119 | printf(" Op=4 ~ 70 op float / tour de boucle \n");
|
---|
120 | printf(" Op=5 Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+log(fabs(x[i])+0.1)+y[k]) \n");
|
---|
121 | printf(" Op=5 ~ 100 op float / tour de boucle \n");
|
---|
122 | return 1;
|
---|
123 | }
|
---|
124 | OPE = atoi(arg[1]);
|
---|
125 | SZ = 20000;
|
---|
126 | if (narg > 2) SZ = atoi(arg[2]);
|
---|
127 | ckprt = 0;
|
---|
128 | if (narg > 3) ckprt = atoi(arg[3]);
|
---|
129 |
|
---|
130 | printf("::::::: cpupower: OPE=%d SZ= %d ::::::: \n", OPE,SZ);
|
---|
131 | x = malloc(SZ*sizeof(T_Type));
|
---|
132 | y = malloc(SZ*sizeof(T_Type));
|
---|
133 | z = malloc(SZ*sizeof(T_Type));
|
---|
134 |
|
---|
135 | for(i=0; i<SZ; i++) {
|
---|
136 | x[i] = (T_Type)((random()%10000)-5000);
|
---|
137 | y[i] = (T_Type)((random()%10000)-5000);
|
---|
138 | y[i] = (T_Type)(0);
|
---|
139 | }
|
---|
140 | PrtTim("--Fin malloc+init x,y,z ");
|
---|
141 | if (OPE == 5) fop_5();
|
---|
142 | else if (OPE == 4) fop_4();
|
---|
143 | else if (OPE == 3) fop_3();
|
---|
144 | else if (OPE == 2) fop_2();
|
---|
145 | else fop_1();
|
---|
146 | PrtTim("---Fin OpeDoubleBoucle ");
|
---|
147 | mflops = N_OP/ GetPartialCPUTime()*1.e-6;
|
---|
148 | printf("-> Nb Operations= %g MFLOPS= %g \n",N_OP,mflops);
|
---|
149 | if (ckprt > 0) {
|
---|
150 | printf(" CheckPrint - ckprt= %d maxnprt= %d \n", ckprt, maxnprt);
|
---|
151 | nprt = 0;
|
---|
152 | for(i=0; i<SZ; i+= ckprt) {
|
---|
153 | printf("i=%d x[i]=%g y[i]=%g z[i]=%g \n", i, (double)x[i],
|
---|
154 | (double)y[i], (double)z[i]);
|
---|
155 | nprt++;
|
---|
156 | if (nprt > 1000) break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | PrtTim("----Fin cpupower");
|
---|
161 | printf(":::::: FIN cpupower N_OP= %g MFLOPS= %g ::::::: \n", N_OP,mflops);
|
---|
162 |
|
---|
163 | free(x);
|
---|
164 | free(y);
|
---|
165 | free(z);
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | static clock_t CPUT0, CPUT;
|
---|
171 | static time_t ELT0, ELT;
|
---|
172 | static double _totcpu, _partialcpu;
|
---|
173 |
|
---|
174 | void InitTim(void)
|
---|
175 | {
|
---|
176 | CPUT0 = CPUT = clock();
|
---|
177 | ELT0 = ELT = time(NULL);
|
---|
178 | _totcpu = _partialcpu = 0.;
|
---|
179 | return;
|
---|
180 | }
|
---|
181 | double GetPartialCPUTime() { return _partialcpu; }
|
---|
182 | double GetTotalCPUTime() { return _totcpu; }
|
---|
183 |
|
---|
184 | /* Nouvelle-Fonction */
|
---|
185 | void PrtTim(const char * Comm)
|
---|
186 | {
|
---|
187 | float tcal,tcalt;
|
---|
188 | clock_t cput;
|
---|
189 | time_t elt;
|
---|
190 | int etm,etmt;
|
---|
191 |
|
---|
192 | elt = time(NULL); cput = clock();
|
---|
193 | tcalt = ( (float)(cput) - (float)(CPUT0) ) / (float)(CLOCKS_PER_SEC);
|
---|
194 | tcal = ( (float)(cput) - (float)(CPUT) ) / (float)(CLOCKS_PER_SEC);
|
---|
195 | _totcpu = tcalt;
|
---|
196 | _partialcpu = tcal;
|
---|
197 |
|
---|
198 | etm = elt - ELT;
|
---|
199 | etmt = elt - ELT0;
|
---|
200 | printf("%s CPUTime: Total= %g (Partial= %g) Sec. \n",
|
---|
201 | Comm, tcalt, tcal);
|
---|
202 | printf("ElapsedTime(hh:mm:ss): Total= %02d:%02d:%02d ",
|
---|
203 | etmt/3600, (etmt%3600)/60, etmt%60);
|
---|
204 | printf(" (Partial= %02d:%02d:%02d)\n",
|
---|
205 | etm/3600, (etm%3600)/60, etm%60);
|
---|
206 |
|
---|
207 | ELT = elt;
|
---|
208 | CPUT = cput;
|
---|
209 | return;
|
---|
210 | }
|
---|