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 */
|
---|
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 ou fop_2 */
|
---|
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;
|
---|
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;
|
---|
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;
|
---|
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 | /* Fonctions de timing (TCPU) - voir en fin de fichier */
|
---|
69 | void InitTim(void);
|
---|
70 | void PrtTim(const char * Comm);
|
---|
71 | double GetPartialCPUTime();
|
---|
72 | double GetTotalCPUTime();
|
---|
73 |
|
---|
74 | int main(int narg, char* arg[])
|
---|
75 | {
|
---|
76 | int maxnprt = 1000;
|
---|
77 | int i,nprt;
|
---|
78 | double mflops;
|
---|
79 |
|
---|
80 | /* SophyaInit(); */
|
---|
81 | InitTim(); /* Initializing the CPU timer */
|
---|
82 |
|
---|
83 | if (narg < 2) {
|
---|
84 | printf("--- Programme cpupower: (Puissance de calcul) ---- \n");
|
---|
85 | printf(" Usage cpupower Op=1/2/3 [Size=20000] [CkPrt=0] \n");
|
---|
86 | return 1;
|
---|
87 | }
|
---|
88 | OPE = atoi(arg[1]);
|
---|
89 | SZ = 20000;
|
---|
90 | if (narg > 2) SZ = atoi(arg[2]);
|
---|
91 | ckprt = 0;
|
---|
92 | if (narg > 3) ckprt = atoi(arg[3]);
|
---|
93 |
|
---|
94 | printf("::::::: cpupower: OPE=%d SZ= %d ::::::: \n", OPE,SZ);
|
---|
95 | x = malloc(SZ*sizeof(T_Type));
|
---|
96 | y = malloc(SZ*sizeof(T_Type));
|
---|
97 | z = malloc(SZ*sizeof(T_Type));
|
---|
98 |
|
---|
99 | for(i=0; i<SZ; i++) {
|
---|
100 | x[i] = (T_Type)((random()%10000)-5000);
|
---|
101 | y[i] = (T_Type)((random()%10000)-5000);
|
---|
102 | y[i] = (T_Type)(0);
|
---|
103 | }
|
---|
104 | PrtTim("--Fin malloc+init x,y,z ");
|
---|
105 | if (OPE == 3) fop_3();
|
---|
106 | else if (OPE == 2) fop_2();
|
---|
107 | else fop_1();
|
---|
108 | PrtTim("---Fin OpeDoubleBoucle ");
|
---|
109 | mflops = N_OP/ GetPartialCPUTime()*1.e-6;
|
---|
110 | printf("-> Nb Operations= %g MFLOPS= %g \n",N_OP,mflops);
|
---|
111 | if (ckprt > 0) {
|
---|
112 | printf(" CheckPrint - ckprt= %d maxnprt= %d \n", ckprt, maxnprt);
|
---|
113 | nprt = 0;
|
---|
114 | for(i=0; i<SZ; i+= ckprt) {
|
---|
115 | printf("i=%d x[i]=%g y[i]=%g z[i]=%g \n", i, (double)x[i],
|
---|
116 | (double)y[i], (double)z[i]);
|
---|
117 | nprt++;
|
---|
118 | if (nprt > 1000) break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | PrtTim("----Fin cpupower");
|
---|
123 | printf(":::::: FIN cpupower N_OP= %g MFLOPS= %g ::::::: \n", N_OP,mflops);
|
---|
124 |
|
---|
125 | free(x);
|
---|
126 | free(y);
|
---|
127 | free(z);
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | static clock_t CPUT0, CPUT;
|
---|
133 | static time_t ELT0, ELT;
|
---|
134 | static double _totcpu, _partialcpu;
|
---|
135 |
|
---|
136 | void InitTim(void)
|
---|
137 | {
|
---|
138 | CPUT0 = CPUT = clock();
|
---|
139 | ELT0 = ELT = time(NULL);
|
---|
140 | _totcpu = _partialcpu = 0.;
|
---|
141 | return;
|
---|
142 | }
|
---|
143 | double GetPartialCPUTime() { return _partialcpu; }
|
---|
144 | double GetTotalCPUTime() { return _totcpu; }
|
---|
145 |
|
---|
146 | /* Nouvelle-Fonction */
|
---|
147 | void PrtTim(const char * Comm)
|
---|
148 | {
|
---|
149 | float tcal,tcalt;
|
---|
150 | clock_t cput;
|
---|
151 | time_t elt;
|
---|
152 | int etm,etmt;
|
---|
153 |
|
---|
154 | elt = time(NULL); cput = clock();
|
---|
155 | tcalt = ( (float)(cput) - (float)(CPUT0) ) / (float)(CLOCKS_PER_SEC);
|
---|
156 | tcal = ( (float)(cput) - (float)(CPUT) ) / (float)(CLOCKS_PER_SEC);
|
---|
157 | _totcpu = tcalt;
|
---|
158 | _partialcpu = tcal;
|
---|
159 |
|
---|
160 | etm = elt - ELT;
|
---|
161 | etmt = elt - ELT0;
|
---|
162 | printf("%s CPUTime: Total= %g (Partial= %g) Sec. \n",
|
---|
163 | Comm, tcalt, tcal);
|
---|
164 | printf("ElapsedTime(hh:mm:ss): Total= %02d:%02d:%02d ",
|
---|
165 | etmt/3600, (etmt%3600)/60, etmt%60);
|
---|
166 | printf(" (Partial= %02d:%02d:%02d)\n",
|
---|
167 | etm/3600, (etm%3600)/60, etm%60);
|
---|
168 |
|
---|
169 | ELT = elt;
|
---|
170 | CPUT = cput;
|
---|
171 | return;
|
---|
172 | }
|
---|