| 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=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 */ | 
|---|
| 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_0() | 
|---|
| 30 | { | 
|---|
| 31 | T_Type s=0; | 
|---|
| 32 | int k; | 
|---|
| 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[k]; | 
|---|
| 35 | N_OP += SZ; | 
|---|
| 36 | return; | 
|---|
| 37 | } | 
|---|
| 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 | } | 
|---|
| 47 |  | 
|---|
| 48 | void fop_1() | 
|---|
| 49 | { | 
|---|
| 50 | T_Type s=0; | 
|---|
| 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 | { | 
|---|
| 63 | T_Type s=0; | 
|---|
| 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 |  | 
|---|
| 74 | void fop_3() | 
|---|
| 75 | { | 
|---|
| 76 | T_Type s=0; | 
|---|
| 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 |  | 
|---|
| 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; | 
|---|
| 95 | N_OP += 70*SZ;  /* le facteur 70 est approximatif */ | 
|---|
| 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; | 
|---|
| 108 | N_OP += 100*SZ;  /* le facteur 100 est approximatif */ | 
|---|
| 109 | } | 
|---|
| 110 | return; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 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; | 
|---|
| 122 | int nloop = 1000; | 
|---|
| 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"); | 
|---|
| 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"); | 
|---|
| 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"); | 
|---|
| 136 | printf("  Op=2 Double Loop: z[k] = Somme_i(x[k]*y[i]+x[i]*y[k]) \n"); | 
|---|
| 137 | printf("    Op=2 2x(*,+)=4 op /  tour de boucle \n"); | 
|---|
| 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"); | 
|---|
| 139 | printf("    Op=3  7 op (*,+) /  tour de boucle \n"); | 
|---|
| 140 | printf("  Op=4 Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+y[k]*cos(x[i])) \n"); | 
|---|
| 141 | printf("    Op=4   ~ 70 op float /  tour de boucle \n"); | 
|---|
| 142 | printf("  Op=5 Double Loop: z[k] = Somme_i(x[k]*sin(y[i])+log(fabs(x[i])+0.1)+y[k]) \n"); | 
|---|
| 143 | printf("    Op=5   ~ 100 op float /  tour de boucle \n"); | 
|---|
| 144 | printf("    Test 4,5 Size=2000 par defaut  \n"); | 
|---|
| 145 | return 1; | 
|---|
| 146 | } | 
|---|
| 147 | OPE = atoi(arg[1]); | 
|---|
| 148 | SZ = 20000; | 
|---|
| 149 | if (OPE == 0) SZ = 1000000; | 
|---|
| 150 | if (OPE >= 4) SZ = 2000; | 
|---|
| 151 | if (narg > 2)  SZ = atoi(arg[2]); | 
|---|
| 152 | nloop = 100; | 
|---|
| 153 | if (narg > 3)  nloop = atoi(arg[3]); | 
|---|
| 154 | ckprt = 0; | 
|---|
| 155 | if (narg > 4)  ckprt = atoi(arg[4]); | 
|---|
| 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); | 
|---|
| 165 | z[i] = (T_Type)(0); | 
|---|
| 166 | } | 
|---|
| 167 | PrtTim("--Fin malloc+init x,y,z "); | 
|---|
| 168 | N_OP = 0; | 
|---|
| 169 | if (OPE == 5)  fop_5(); | 
|---|
| 170 | else if (OPE == 4)  fop_4(); | 
|---|
| 171 | else if (OPE == 3)  fop_3(); | 
|---|
| 172 | else if (OPE == 2)  fop_2(); | 
|---|
| 173 | else if (OPE == 1)  fop_1(); | 
|---|
| 174 | else if (OPE == 30)  for(i=0; i<nloop; i++) fop_30(); | 
|---|
| 175 | else for(i=0; i<nloop; i++) fop_0(); | 
|---|
| 176 | PrtTim("---Fin OpeDoubleBoucle "); | 
|---|
| 177 | mflops = N_OP/ GetPartialCPUTime()*1.e-6; | 
|---|
| 178 | printf("-> Nb Operations= %g  MFLOPS= %g \n",N_OP,mflops); | 
|---|
| 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"); | 
|---|
| 191 | printf(":::::: FIN cpupower N_OP= %g MFLOPS= %g ::::::: \n", N_OP,mflops); | 
|---|
| 192 |  | 
|---|
| 193 | free(x); | 
|---|
| 194 | free(y); | 
|---|
| 195 | free(z); | 
|---|
| 196 | return 0; | 
|---|
| 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 | } | 
|---|