xref: /petsc/src/benchmarks/Index.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*3a40ed3dSBarry Smith static char vcid[] = "$Id: Index.c,v 1.16 1997/07/09 21:01:29 balay Exp bsmith $";
3c9a02da4SSatish Balay #endif
41f480b34SSatish Balay 
51f480b34SSatish Balay #include "petsc.h"
61f480b34SSatish Balay #include "sys.h"
71f480b34SSatish Balay 
877c4ece6SBarry Smith int BlastCache();
907b89f8fSSatish Balay int test1();
1007b89f8fSSatish Balay int test2();
1177c4ece6SBarry Smith 
121f480b34SSatish Balay int main( int argc, char **argv)
131f480b34SSatish Balay {
1477c4ece6SBarry Smith   PetscInitialize(&argc, &argv,0,0);
151f480b34SSatish Balay 
1677c4ece6SBarry Smith   test1();
1777c4ece6SBarry Smith   test2();
1877c4ece6SBarry Smith 
1977c4ece6SBarry Smith   PetscFinalize();
20*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
2177c4ece6SBarry Smith }
2277c4ece6SBarry Smith 
2377c4ece6SBarry Smith int test1()
2477c4ece6SBarry Smith {
2547794344SBarry Smith   PLogDouble  t1, t2;
2647794344SBarry Smith   double      value;
272758efb8SSatish Balay   int         i, ierr,*z,*zi, intval;
2877c4ece6SBarry Smith   Scalar      *x, *y;
2977c4ece6SBarry Smith   PetscRandom r;
3077c4ece6SBarry Smith 
31029af93fSBarry Smith   ierr = PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
3277c4ece6SBarry Smith   x    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(x);
3377c4ece6SBarry Smith   y    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(y);
3477c4ece6SBarry Smith 
3577c4ece6SBarry Smith   z     = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(z);
3677c4ece6SBarry Smith   zi    = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(zi);
3777c4ece6SBarry Smith 
3877c4ece6SBarry Smith 
391f480b34SSatish Balay 
401f480b34SSatish Balay   /* Take care of paging effects */
411f480b34SSatish Balay   t1 = PetscGetTime();
421f480b34SSatish Balay 
431f480b34SSatish Balay    /* Form the random set of integers */
4477c4ece6SBarry Smith   for (i=0; i<2000; i++) {
4577c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
4677c4ece6SBarry Smith     intval = (int)(value*20000.0);
47c9a02da4SSatish Balay     z[i]   = intval;
481f480b34SSatish Balay   }
491f480b34SSatish Balay 
5077c4ece6SBarry Smith   for (i=0; i<2000; i++) {
5177c4ece6SBarry Smith     ierr    = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
5277c4ece6SBarry Smith     intval  = (int)(value*20000.0);
53ba8edd79SBarry Smith     zi[i]   = intval;
5477c4ece6SBarry Smith   }
5577c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
5677c4ece6SBarry Smith 
5777c4ece6SBarry Smith   BlastCache();
581f480b34SSatish Balay 
591f480b34SSatish Balay   t1 = PetscGetTime();
6077c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
611f480b34SSatish Balay   t2 = PetscGetTime();
6277c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
631f480b34SSatish Balay 
6477c4ece6SBarry Smith   BlastCache();
651f480b34SSatish Balay 
661f480b34SSatish Balay   t1 = PetscGetTime();
67608f96ebSSatish Balay   for (i=0; i<500; i+=4) {
68608f96ebSSatish Balay     x[i]   = y[z[i]];
69608f96ebSSatish Balay     x[1+i] = y[z[1+i]];
70608f96ebSSatish Balay     x[2+i] = y[z[2+i]];
71608f96ebSSatish Balay     x[3+i] = y[z[3+i]];
72608f96ebSSatish Balay   }
73608f96ebSSatish Balay   t2 = PetscGetTime();
74608f96ebSSatish Balay   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 4",(t2-t1)/2000.0);
75608f96ebSSatish Balay 
76608f96ebSSatish Balay   BlastCache();
77608f96ebSSatish Balay 
78608f96ebSSatish Balay   t1 = PetscGetTime();
7977c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[z[i]]; }
801f480b34SSatish Balay   t2 = PetscGetTime();
8177c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
8277c4ece6SBarry Smith 
8377c4ece6SBarry Smith   BlastCache();
841f480b34SSatish Balay 
851f480b34SSatish Balay   t1 = PetscGetTime();
86608f96ebSSatish Balay   for (i=0; i<1000; i+=2) {  x[i] = y[z[i]];  x[1+i] = y[z[1+i]]; }
87608f96ebSSatish Balay   t2 = PetscGetTime();
88608f96ebSSatish Balay   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 2",(t2-t1)/2000.0);
89608f96ebSSatish Balay 
90608f96ebSSatish Balay   BlastCache();
91608f96ebSSatish Balay 
92608f96ebSSatish Balay   t1 = PetscGetTime();
9377c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
941f480b34SSatish Balay   t2 = PetscGetTime();
9577c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
961f480b34SSatish Balay 
9777c4ece6SBarry Smith   BlastCache();
9877c4ece6SBarry Smith 
9977c4ece6SBarry Smith   t1 = PetscGetTime();
10077c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[zi[i]]; }
10177c4ece6SBarry Smith   t2 = PetscGetTime();
10277c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
10377c4ece6SBarry Smith 
104608f96ebSSatish Balay   PetscMemcpy(x,y,10);
105608f96ebSSatish Balay   PetscMemcpy(z,zi,10);
10677c4ece6SBarry Smith   PetscFree(z);
10777c4ece6SBarry Smith   PetscFree(zi);
10877c4ece6SBarry Smith   PetscFree(x);
10977c4ece6SBarry Smith   PetscFree(y);
11077c4ece6SBarry Smith   PetscRandomDestroy(r);
111*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
11277c4ece6SBarry Smith }
11377c4ece6SBarry Smith 
11477c4ece6SBarry Smith int test2()
11577c4ece6SBarry Smith {
11647794344SBarry Smith   PLogDouble   t1, t2;
11747794344SBarry Smith   double       value;
11877c4ece6SBarry Smith   int          i, ierr,z[2000],zi[20000], intval, tmp;
11977c4ece6SBarry Smith   Scalar       x[20000], y[20000];
12077c4ece6SBarry Smith   PetscRandom  r;
12177c4ece6SBarry Smith 
122029af93fSBarry Smith   ierr = PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
12377c4ece6SBarry Smith 
12477c4ece6SBarry Smith   /* Take care of paging effects */
12577c4ece6SBarry Smith   t1 = PetscGetTime();
12677c4ece6SBarry Smith 
12777c4ece6SBarry Smith   for (i=0; i<2000; i++) {
12877c4ece6SBarry Smith     zi[i] = i;
12977c4ece6SBarry Smith     z[i]  = i;
13077c4ece6SBarry Smith   }
13177c4ece6SBarry Smith 
13277c4ece6SBarry Smith   for (i=0; i<20000; i++) {
13377c4ece6SBarry Smith     x[i] = i;
13477c4ece6SBarry Smith     y[i]  = i;
13577c4ece6SBarry Smith   }
13677c4ece6SBarry Smith 
13777c4ece6SBarry Smith    /* Form the random set of integers */
13877c4ece6SBarry Smith   for (i=0; i<2000; i++) {
13977c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
14077c4ece6SBarry Smith     intval = (int)(value*20000.0);
14177c4ece6SBarry Smith     tmp    = z[i];
14277c4ece6SBarry Smith     z[i]   = z[intval];
14377c4ece6SBarry Smith     z[intval] = tmp;
14477c4ece6SBarry Smith   }
14577c4ece6SBarry Smith 
14677c4ece6SBarry Smith   for (i=0; i<2000; i++) {
14777c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
14877c4ece6SBarry Smith     intval = (int)(value*20000.0);
14977c4ece6SBarry Smith     tmp    = zi[i];
15077c4ece6SBarry Smith     zi[i]  = zi[intval];
15177c4ece6SBarry Smith     zi[intval] = tmp;
15277c4ece6SBarry Smith   }
15377c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
15477c4ece6SBarry Smith 
15577c4ece6SBarry Smith   /* BlastCache();  */
15677c4ece6SBarry Smith 
15777c4ece6SBarry Smith   t1 = PetscGetTime();
15877c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
15977c4ece6SBarry Smith   t2 = PetscGetTime();
16077c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
16177c4ece6SBarry Smith 
16277c4ece6SBarry Smith   /* BlastCache();  */
16377c4ece6SBarry Smith 
16477c4ece6SBarry Smith   t1 = PetscGetTime();
16577c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[i] = x[z[i]]; }
16677c4ece6SBarry Smith   t2 = PetscGetTime();
16777c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
16877c4ece6SBarry Smith 
16977c4ece6SBarry Smith   /* BlastCache(); */
17077c4ece6SBarry Smith 
17177c4ece6SBarry Smith   t1 = PetscGetTime();
17277c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
17377c4ece6SBarry Smith   t2 = PetscGetTime();
17477c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
17577c4ece6SBarry Smith 
17677c4ece6SBarry Smith   /* BlastCache(); */
17777c4ece6SBarry Smith 
17877c4ece6SBarry Smith   t1 = PetscGetTime();
17977c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[z[i]] = x[zi[i]]; }
18077c4ece6SBarry Smith   t2 = PetscGetTime();
18177c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
18277c4ece6SBarry Smith 
18377c4ece6SBarry Smith 
18477c4ece6SBarry Smith   PetscRandomDestroy(r);
185*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
18677c4ece6SBarry Smith }
18777c4ece6SBarry Smith 
18877c4ece6SBarry Smith int BlastCache()
18977c4ece6SBarry Smith {
19077c4ece6SBarry Smith   int    i,n = 1000000;
19177c4ece6SBarry Smith   Scalar *x,*y,*z,*a, *b;
19277c4ece6SBarry Smith 
19307b89f8fSSatish Balay   x = (Scalar *) PetscMalloc(5*n*sizeof(Scalar)); CHKPTRA(x);
19477c4ece6SBarry Smith   y = x + n;
19577c4ece6SBarry Smith   z = y + n;
19677c4ece6SBarry Smith   a = z + n;
19777c4ece6SBarry Smith   b = a + n;
19877c4ece6SBarry Smith 
19977c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
200ba8edd79SBarry Smith     a[i] = (Scalar) i;
201ba8edd79SBarry Smith     y[i] = (Scalar) i;
202ba8edd79SBarry Smith     z[i] = (Scalar) i;
203ba8edd79SBarry Smith     b[i] = (Scalar) i;
204ba8edd79SBarry Smith     x[i] = (Scalar) i;
205ba8edd79SBarry Smith   }
206ba8edd79SBarry Smith 
207ba8edd79SBarry Smith   for ( i=0; i<n; i++ ) {
20877c4ece6SBarry Smith     a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
20977c4ece6SBarry Smith   }
21077c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
21177c4ece6SBarry Smith     b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
21277c4ece6SBarry Smith   }
21377c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
21477c4ece6SBarry Smith     z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
21577c4ece6SBarry Smith   }
21677c4ece6SBarry Smith   PetscFree(x);
217*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
2181f480b34SSatish Balay }
219