Actual source code: Index.c
petsc-3.4.2 2013-07-02

      2: #include <petscsys.h>
      3: #include <petsctime.h>

      6: extern int BlastCache(void);
      7: extern int test1(void);
      8: extern int test2(void);

     12: int main(int argc,char **argv)
     13: {

     16:   PetscInitialize(&argc,&argv,0,0);

     18:   test1();
     19:   test2();

     21:   PetscFinalize();
     22:   return(0);
     23: }

     27: int test1(void)
     28: {
     29:   PetscLogDouble t1,t2;
     30:   double         value;
     31:   int            i,ierr,*z,*zi,intval;
     32:   PetscScalar    *x,*y;
     33:   PetscRandom    r;

     35:   PetscRandomCreate(PETSC_COMM_SELF,&r);
     36:   PetscRandomSetFromOptions(r);
     37:   PetscMalloc(20000*sizeof(PetscScalar),&x);
     38:   PetscMalloc(20000*sizeof(PetscScalar),&y);

     40:   PetscMalloc(2000*sizeof(int),&z);
     41:   PetscMalloc(2000*sizeof(int),&zi);

     45:   /* Take care of paging effects */
     46:   PetscTime(&t1);

     48:   /* Form the random set of integers */
     49:   for (i=0; i<2000; i++) {
     50:     PetscRandomGetValue(r,&value);
     51:     intval = (int)(value*20000.0);
     52:     z[i]   = intval;
     53:   }

     55:   for (i=0; i<2000; i++) {
     56:     PetscRandomGetValue(r,&value);
     57:     intval = (int)(value*20000.0);
     58:     zi[i]  = intval;
     59:   }
     60:   /* fprintf(stdout,"Done setup\n"); */

     62:   BlastCache();

     64:   PetscTime(&t1);
     65:   for (i=0; i<2000; i++) x[i] = y[i];
     66:   PetscTime(&t2);
     67:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);

     69:   BlastCache();

     71:   PetscTime(&t1);
     72:   for (i=0; i<500; i+=4) {
     73:     x[i]   = y[z[i]];
     74:     x[1+i] = y[z[1+i]];
     75:     x[2+i] = y[z[2+i]];
     76:     x[3+i] = y[z[3+i]];
     77:   }
     78:   PetscTime(&t2);
     79:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[idx[i]] - unroll 4",(t2-t1)/2000.0);

     81:   BlastCache();

     83:   PetscTime(&t1);CHKERRQ(ierr)
     84:   for (i=0; i<2000; i++) x[i] = y[z[i]];
     85:   PetscTime(&t2);
     86:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);

     88:   BlastCache();

     90:   PetscTime(&t1);
     91:   for (i=0; i<1000; i+=2) {  x[i] = y[z[i]];  x[1+i] = y[z[1+i]]; }
     92:   PetscTime(&t2);
     93:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[idx[i]] - unroll 2",(t2-t1)/2000.0);

     95:   BlastCache();

     97:   PetscTime(&t1);
     98:   for (i=0; i<2000; i++) x[z[i]] = y[i];
     99:   PetscTime(&t2);
    100:   fprintf(stdout,"%-27s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);

    102:   BlastCache();

    104:   PetscTime(&t1);
    105:   for (i=0; i<2000; i++) x[z[i]] = y[zi[i]];
    106:   PetscTime(&t2);
    107:   fprintf(stdout,"%-27s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);

    109:   PetscMemcpy(x,y,10);
    110:   PetscMemcpy(z,zi,10);
    111:   PetscFree(z);
    112:   PetscFree(zi);
    113:   PetscFree(x);
    114:   PetscFree(y);
    115:   PetscRandomDestroy(&r);
    116:   return(0);
    117: }

    121: int test2(void)
    122: {
    123:   PetscLogDouble t1,t2;
    124:   double         value;
    125:   int            i,ierr,z[20000],zi[20000],intval,tmp;
    126:   PetscScalar    x[20000],y[20000];
    127:   PetscRandom    r;

    129:   PetscRandomCreate(PETSC_COMM_SELF,&r);
    130:   PetscRandomSetFromOptions(r);

    132:   /* Take care of paging effects */
    133:   PetscTime(&t1);

    135:   for (i=0; i<20000; i++) {
    136:     x[i]  = i;
    137:     y[i]  = i;
    138:     z[i]  = i;
    139:     zi[i] = i;
    140:   }

    142:   /* Form the random set of integers */
    143:   for (i=0; i<20000; i++) {
    144:     PetscRandomGetValue(r,&value);
    145:     intval    = (int)(value*20000.0);
    146:     tmp       = z[i];
    147:     z[i]      = z[intval];
    148:     z[intval] = tmp;
    149:   }

    151:   for (i=0; i<20000; i++) {
    152:     PetscRandomGetValue(r,&value);
    153:     intval     = (int)(value*20000.0);
    154:     tmp        = zi[i];
    155:     zi[i]      = zi[intval];
    156:     zi[intval] = tmp;
    157:   }
    158:   /* fprintf(stdout,"Done setup\n"); */

    160:   /* BlastCache(); */

    162:   PetscTime(&t1);
    163:   for (i=0; i<2000; i++) x[i] = y[i];
    164:   PetscTime(&t2);
    165:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);

    167:   /* BlastCache(); */

    169:   PetscTime(&t1);
    170:   for (i=0; i<2000; i++) y[i] = x[z[i]];
    171:   PetscTime(&t2);
    172:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);

    174:   /* BlastCache(); */

    176:   PetscTime(&t1);
    177:   for (i=0; i<2000; i++) x[z[i]] = y[i];
    178:   PetscTime(&t2);
    179:   fprintf(stdout,"%-27s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);

    181:   /* BlastCache(); */

    183:   PetscTime(&t1);
    184:   for (i=0; i<2000; i++) y[z[i]] = x[zi[i]];
    185:   PetscTime(&t2);
    186:   fprintf(stdout,"%-27s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);

    189:   PetscRandomDestroy(&r);
    190:   return(0);
    191: }

    195: int BlastCache(void)
    196: {
    197:   int         i,ierr,n = 1000000;
    198:   PetscScalar *x,*y,*z,*a,*b;

    200:   PetscMalloc(5*n*sizeof(PetscScalar),&x);
    201:   y    = x + n;
    202:   z    = y + n;
    203:   a    = z + n;
    204:   b    = a + n;

    206:   for (i=0; i<n; i++) {
    207:     a[i] = (PetscScalar) i;
    208:     y[i] = (PetscScalar) i;
    209:     z[i] = (PetscScalar) i;
    210:     b[i] = (PetscScalar) i;
    211:     x[i] = (PetscScalar) i;
    212:   }

    214:   for (i=0; i<n; i++) a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
    215:   for (i=0; i<n; i++) b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
    216:   for (i=0; i<n; i++) z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
    217:   PetscFree(x);
    218:   return(0);
    219: }