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