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