Actual source code: Index.c

petsc-3.9.4 2018-09-11
Report Typos and Errors

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


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

 10: int main(int argc,char **argv)
 11: {

 14:   PetscInitialize(&argc,&argv,0,0);if (ierr) return ierr;
 15:   test1();
 16:   test2();
 17:   PetscFinalize();
 18:   return ierr;
 19: }

 21: int test1(void)
 22: {
 23:   PetscLogDouble t1,t2;
 24:   double         value;
 25:   int            i,ierr,*z,*zi,intval;
 26:   PetscScalar    *x,*y;
 27:   PetscRandom    r;

 29:   PetscRandomCreate(PETSC_COMM_SELF,&r);
 30:   PetscRandomSetFromOptions(r);
 31:   PetscMalloc1(20000,&x);
 32:   PetscMalloc1(20000,&y);

 34:   PetscMalloc1(2000,&z);
 35:   PetscMalloc1(2000,&zi);

 37:   /* Take care of paging effects */
 38:   PetscTime(&t1);

 40:   /* Form the random set of integers */
 41:   for (i=0; i<2000; i++) {
 42:     PetscRandomGetValue(r,&value);
 43:     intval = (int)(value*20000.0);
 44:     z[i]   = intval;
 45:   }

 47:   for (i=0; i<2000; i++) {
 48:     PetscRandomGetValue(r,&value);
 49:     intval = (int)(value*20000.0);
 50:     zi[i]  = intval;
 51:   }
 52:   /* fprintf(stdout,"Done setup\n"); */

 54:   BlastCache();

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

 61:   BlastCache();

 63:   PetscTime(&t1);
 64:   for (i=0; i<500; i+=4) {
 65:     x[i]   = y[z[i]];
 66:     x[1+i] = y[z[1+i]];
 67:     x[2+i] = y[z[2+i]];
 68:     x[3+i] = y[z[3+i]];
 69:   }
 70:   PetscTime(&t2);
 71:   fprintf(stdout,"%-27s : %e sec\n","x[i] = y[idx[i]] - unroll 4",(t2-t1)/2000.0);

 73:   BlastCache();

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

 80:   BlastCache();

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

 87:   BlastCache();

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

 94:   BlastCache();

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

101:   PetscMemcpy(x,y,10);
102:   PetscMemcpy(z,zi,10);
103:   PetscFree(z);
104:   PetscFree(zi);
105:   PetscFree(x);
106:   PetscFree(y);
107:   PetscRandomDestroy(&r);
108:   return(0);
109: }

111: int test2(void)
112: {
113:   PetscLogDouble t1,t2;
114:   double         value;
115:   int            i,ierr,z[20000],zi[20000],intval,tmp;
116:   PetscScalar    x[20000],y[20000];
117:   PetscRandom    r;

119:   PetscRandomCreate(PETSC_COMM_SELF,&r);
120:   PetscRandomSetFromOptions(r);

122:   /* Take care of paging effects */
123:   PetscTime(&t1);

125:   for (i=0; i<20000; i++) {
126:     x[i]  = i;
127:     y[i]  = i;
128:     z[i]  = i;
129:     zi[i] = i;
130:   }

132:   /* Form the random set of integers */
133:   for (i=0; i<20000; i++) {
134:     PetscRandomGetValue(r,&value);
135:     intval    = (int)(value*20000.0);
136:     tmp       = z[i];
137:     z[i]      = z[intval];
138:     z[intval] = tmp;
139:   }

141:   for (i=0; i<20000; i++) {
142:     PetscRandomGetValue(r,&value);
143:     intval     = (int)(value*20000.0);
144:     tmp        = zi[i];
145:     zi[i]      = zi[intval];
146:     zi[intval] = tmp;
147:   }
148:   /* fprintf(stdout,"Done setup\n"); */

150:   /* BlastCache(); */

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

157:   /* BlastCache(); */

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

164:   /* BlastCache(); */

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

171:   /* BlastCache(); */

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


179:   PetscRandomDestroy(&r);
180:   return(0);
181: }

183: int BlastCache(void)
184: {
185:   int         i,ierr,n = 1000000;
186:   PetscScalar *x,*y,*z,*a,*b;

188:   PetscMalloc1(5*n,&x);
189:   y    = x + n;
190:   z    = y + n;
191:   a    = z + n;
192:   b    = a + n;

194:   for (i=0; i<n; i++) {
195:     a[i] = (PetscScalar) i;
196:     y[i] = (PetscScalar) i;
197:     z[i] = (PetscScalar) i;
198:     b[i] = (PetscScalar) i;
199:     x[i] = (PetscScalar) i;
200:   }

202:   for (i=0; i<n; i++) a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
203:   for (i=0; i<n; i++) b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
204:   for (i=0; i<n; i++) z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
205:   PetscFree(x);
206:   return(0);
207: }