xref: /petsc/src/benchmarks/Index.c (revision 608f96eb4f80b150d49ed2fa0fafd4acdd2a6f3a)
1c9a02da4SSatish Balay #ifndef lint
2*608f96ebSSatish Balay static char vcid[] = "$Id: Index.c,v 1.9 1996/04/22 20:44:28 balay Exp balay $";
3c9a02da4SSatish Balay #endif
41f480b34SSatish Balay 
51f480b34SSatish Balay #include "stdio.h"
61f480b34SSatish Balay #include "petsc.h"
71f480b34SSatish Balay #include "sys.h"
81f480b34SSatish Balay 
977c4ece6SBarry Smith int BlastCache();
1077c4ece6SBarry Smith 
111f480b34SSatish Balay int main( int argc, char **argv)
121f480b34SSatish Balay {
1377c4ece6SBarry Smith   PetscInitialize(&argc, &argv,0,0);
141f480b34SSatish Balay 
1577c4ece6SBarry Smith   test1();
1677c4ece6SBarry Smith   test2();
1777c4ece6SBarry Smith 
1877c4ece6SBarry Smith   PetscFinalize();
1977c4ece6SBarry Smith   return 0;
2077c4ece6SBarry Smith }
2177c4ece6SBarry Smith 
2277c4ece6SBarry Smith int test1()
2377c4ece6SBarry Smith {
2477c4ece6SBarry Smith   double    t1, t2, value;
2577c4ece6SBarry Smith   int       i, ierr,*z,*zi, intval, tmp;
2677c4ece6SBarry Smith   Scalar    *x, *y;
2777c4ece6SBarry Smith   PetscRandom  r;
2877c4ece6SBarry Smith 
2977c4ece6SBarry Smith   ierr = PetscRandomCreate(MPI_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
3077c4ece6SBarry Smith   x    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(x);
3177c4ece6SBarry Smith   y    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(y);
3277c4ece6SBarry Smith 
3377c4ece6SBarry Smith   z     = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(z);
3477c4ece6SBarry Smith   zi    = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(zi);
3577c4ece6SBarry Smith 
3677c4ece6SBarry Smith 
371f480b34SSatish Balay 
381f480b34SSatish Balay   /* Take care of paging effects */
391f480b34SSatish Balay   t1 = PetscGetTime();
401f480b34SSatish Balay 
411f480b34SSatish Balay    /* Form the random set of integers */
4277c4ece6SBarry Smith   for (i=0; i<2000; i++) {
4377c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
4477c4ece6SBarry Smith     intval = (int)(value*20000.0);
45c9a02da4SSatish Balay     z[i]   = intval;
461f480b34SSatish Balay   }
471f480b34SSatish Balay 
4877c4ece6SBarry Smith   for (i=0; i<2000; i++) {
4977c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
5077c4ece6SBarry Smith     intval = (int)(value*20000.0);
51c9a02da4SSatish Balay     z[i]   = intval;
5277c4ece6SBarry Smith   }
5377c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
5477c4ece6SBarry Smith 
5577c4ece6SBarry Smith   BlastCache();
561f480b34SSatish Balay 
571f480b34SSatish Balay   t1 = PetscGetTime();
5877c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
591f480b34SSatish Balay   t2 = PetscGetTime();
6077c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
611f480b34SSatish Balay 
6277c4ece6SBarry Smith   BlastCache();
631f480b34SSatish Balay 
641f480b34SSatish Balay   t1 = PetscGetTime();
65*608f96ebSSatish Balay   for (i=0; i<500; i+=4) {
66*608f96ebSSatish Balay     x[i]   = y[z[i]];
67*608f96ebSSatish Balay     x[1+i] = y[z[1+i]];
68*608f96ebSSatish Balay     x[2+i] = y[z[2+i]];
69*608f96ebSSatish Balay     x[3+i] = y[z[3+i]];
70*608f96ebSSatish Balay   }
71*608f96ebSSatish Balay   t2 = PetscGetTime();
72*608f96ebSSatish Balay   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 4",(t2-t1)/2000.0);
73*608f96ebSSatish Balay 
74*608f96ebSSatish Balay   BlastCache();
75*608f96ebSSatish Balay 
76*608f96ebSSatish Balay   t1 = PetscGetTime();
7777c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[z[i]]; }
781f480b34SSatish Balay   t2 = PetscGetTime();
7977c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
8077c4ece6SBarry Smith 
8177c4ece6SBarry Smith   BlastCache();
821f480b34SSatish Balay 
831f480b34SSatish Balay   t1 = PetscGetTime();
84*608f96ebSSatish Balay   for (i=0; i<1000; i+=2) {  x[i] = y[z[i]];  x[1+i] = y[z[1+i]]; }
85*608f96ebSSatish Balay   t2 = PetscGetTime();
86*608f96ebSSatish Balay   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 2",(t2-t1)/2000.0);
87*608f96ebSSatish Balay 
88*608f96ebSSatish Balay   BlastCache();
89*608f96ebSSatish Balay 
90*608f96ebSSatish Balay   t1 = PetscGetTime();
9177c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
921f480b34SSatish Balay   t2 = PetscGetTime();
9377c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
941f480b34SSatish Balay 
9577c4ece6SBarry Smith   BlastCache();
9677c4ece6SBarry Smith 
9777c4ece6SBarry Smith   t1 = PetscGetTime();
9877c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[zi[i]]; }
9977c4ece6SBarry Smith   t2 = PetscGetTime();
10077c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
10177c4ece6SBarry Smith 
102*608f96ebSSatish Balay   PetscMemcpy(x,y,10);
103*608f96ebSSatish Balay   PetscMemcpy(z,zi,10);
10477c4ece6SBarry Smith   PetscFree(z);
10577c4ece6SBarry Smith   PetscFree(zi);
10677c4ece6SBarry Smith   PetscFree(x);
10777c4ece6SBarry Smith   PetscFree(y);
10877c4ece6SBarry Smith   PetscRandomDestroy(r);
10977c4ece6SBarry Smith   return 0;
11077c4ece6SBarry Smith }
11177c4ece6SBarry Smith 
11277c4ece6SBarry Smith int test2()
11377c4ece6SBarry Smith {
11477c4ece6SBarry Smith   double    t1, t2, value;
11577c4ece6SBarry Smith   int       i, ierr,z[2000],zi[20000], intval, tmp;
11677c4ece6SBarry Smith   Scalar    x[20000], y[20000];
11777c4ece6SBarry Smith   PetscRandom  r;
11877c4ece6SBarry Smith 
11977c4ece6SBarry Smith   ierr = PetscRandomCreate(MPI_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
12077c4ece6SBarry Smith 
12177c4ece6SBarry Smith   /* Take care of paging effects */
12277c4ece6SBarry Smith   t1 = PetscGetTime();
12377c4ece6SBarry Smith 
12477c4ece6SBarry Smith   for (i=0; i<2000; i++) {
12577c4ece6SBarry Smith     zi[i] = i;
12677c4ece6SBarry Smith     z[i]  = i;
12777c4ece6SBarry Smith   }
12877c4ece6SBarry Smith 
12977c4ece6SBarry Smith   for (i=0; i<20000; i++) {
13077c4ece6SBarry Smith     x[i] = i;
13177c4ece6SBarry Smith     y[i]  = i;
13277c4ece6SBarry Smith   }
13377c4ece6SBarry Smith 
13477c4ece6SBarry Smith    /* Form the random set of integers */
13577c4ece6SBarry Smith   for (i=0; i<2000; i++) {
13677c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
13777c4ece6SBarry Smith     intval = (int)(value*20000.0);
13877c4ece6SBarry Smith     tmp    = z[i];
13977c4ece6SBarry Smith     z[i]   = z[intval];
14077c4ece6SBarry Smith     z[intval] = tmp;
14177c4ece6SBarry Smith   }
14277c4ece6SBarry Smith 
14377c4ece6SBarry Smith   for (i=0; i<2000; i++) {
14477c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
14577c4ece6SBarry Smith     intval = (int)(value*20000.0);
14677c4ece6SBarry Smith     tmp    = zi[i];
14777c4ece6SBarry Smith     zi[i]  = zi[intval];
14877c4ece6SBarry Smith     zi[intval] = tmp;
14977c4ece6SBarry Smith   }
15077c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
15177c4ece6SBarry Smith 
15277c4ece6SBarry Smith   /* BlastCache();  */
15377c4ece6SBarry Smith 
15477c4ece6SBarry Smith   t1 = PetscGetTime();
15577c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
15677c4ece6SBarry Smith   t2 = PetscGetTime();
15777c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
15877c4ece6SBarry Smith 
15977c4ece6SBarry Smith   /* BlastCache();  */
16077c4ece6SBarry Smith 
16177c4ece6SBarry Smith   t1 = PetscGetTime();
16277c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[i] = x[z[i]]; }
16377c4ece6SBarry Smith   t2 = PetscGetTime();
16477c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
16577c4ece6SBarry Smith 
16677c4ece6SBarry Smith   /* BlastCache(); */
16777c4ece6SBarry Smith 
16877c4ece6SBarry Smith   t1 = PetscGetTime();
16977c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
17077c4ece6SBarry Smith   t2 = PetscGetTime();
17177c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
17277c4ece6SBarry Smith 
17377c4ece6SBarry Smith   /* BlastCache(); */
17477c4ece6SBarry Smith 
17577c4ece6SBarry Smith   t1 = PetscGetTime();
17677c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[z[i]] = x[zi[i]]; }
17777c4ece6SBarry Smith   t2 = PetscGetTime();
17877c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
17977c4ece6SBarry Smith 
18077c4ece6SBarry Smith 
18177c4ece6SBarry Smith   PetscRandomDestroy(r);
18277c4ece6SBarry Smith   return 0;
18377c4ece6SBarry Smith }
18477c4ece6SBarry Smith 
18577c4ece6SBarry Smith int BlastCache()
18677c4ece6SBarry Smith {
18777c4ece6SBarry Smith   int    i,n = 1000000;
18877c4ece6SBarry Smith   Scalar *x,*y,*z,*a, *b;
18977c4ece6SBarry Smith 
19077c4ece6SBarry Smith   x = (Scalar *) PetscMalloc(5*n); CHKPTRA(x);
19177c4ece6SBarry Smith   y = x + n;
19277c4ece6SBarry Smith   z = y + n;
19377c4ece6SBarry Smith   a = z + n;
19477c4ece6SBarry Smith   b = a + n;
19577c4ece6SBarry Smith 
19677c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
19777c4ece6SBarry Smith     a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
19877c4ece6SBarry Smith   }
19977c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
20077c4ece6SBarry Smith     b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
20177c4ece6SBarry Smith   }
20277c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
20377c4ece6SBarry Smith     z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
20477c4ece6SBarry Smith   }
20577c4ece6SBarry Smith   PetscFree(x);
2061f480b34SSatish Balay   return 0;
2071f480b34SSatish Balay }
208