xref: /petsc/src/benchmarks/Index.c (revision c9a02da457bccbf9bf3c4b23c5054e06257e2198)
1*c9a02da4SSatish Balay #ifndef lint
2*c9a02da4SSatish Balay static char vcid[] = "$Id: PetscTime.c,v 1.6 1996/03/19 21:30:13 bsmith Exp $";
3*c9a02da4SSatish 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);
45*c9a02da4SSatish 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);
51*c9a02da4SSatish 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();
6577c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[z[i]]; }
661f480b34SSatish Balay   t2 = PetscGetTime();
6777c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
6877c4ece6SBarry Smith 
6977c4ece6SBarry Smith   BlastCache();
701f480b34SSatish Balay 
711f480b34SSatish Balay   t1 = PetscGetTime();
7277c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
731f480b34SSatish Balay   t2 = PetscGetTime();
7477c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
751f480b34SSatish Balay 
7677c4ece6SBarry Smith   BlastCache();
7777c4ece6SBarry Smith 
7877c4ece6SBarry Smith   t1 = PetscGetTime();
7977c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[zi[i]]; }
8077c4ece6SBarry Smith   t2 = PetscGetTime();
8177c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
8277c4ece6SBarry Smith 
8377c4ece6SBarry Smith   PetscFree(z);
8477c4ece6SBarry Smith   PetscFree(zi);
8577c4ece6SBarry Smith   PetscFree(x);
8677c4ece6SBarry Smith   PetscFree(y);
8777c4ece6SBarry Smith   PetscRandomDestroy(r);
8877c4ece6SBarry Smith   return 0;
8977c4ece6SBarry Smith }
9077c4ece6SBarry Smith 
9177c4ece6SBarry Smith int test2()
9277c4ece6SBarry Smith {
9377c4ece6SBarry Smith   double    t1, t2, value;
9477c4ece6SBarry Smith   int       i, ierr,z[2000],zi[20000], intval, tmp;
9577c4ece6SBarry Smith   Scalar    x[20000], y[20000];
9677c4ece6SBarry Smith   PetscRandom  r;
9777c4ece6SBarry Smith 
9877c4ece6SBarry Smith   ierr = PetscRandomCreate(MPI_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
9977c4ece6SBarry Smith 
10077c4ece6SBarry Smith   /* Take care of paging effects */
10177c4ece6SBarry Smith   t1 = PetscGetTime();
10277c4ece6SBarry Smith 
10377c4ece6SBarry Smith   for (i=0; i<2000; i++) {
10477c4ece6SBarry Smith     zi[i] = i;
10577c4ece6SBarry Smith     z[i]  = i;
10677c4ece6SBarry Smith   }
10777c4ece6SBarry Smith 
10877c4ece6SBarry Smith   for (i=0; i<20000; i++) {
10977c4ece6SBarry Smith     x[i] = i;
11077c4ece6SBarry Smith     y[i]  = i;
11177c4ece6SBarry Smith   }
11277c4ece6SBarry Smith 
11377c4ece6SBarry Smith    /* Form the random set of integers */
11477c4ece6SBarry Smith   for (i=0; i<2000; i++) {
11577c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
11677c4ece6SBarry Smith     intval = (int)(value*20000.0);
11777c4ece6SBarry Smith     tmp    = z[i];
11877c4ece6SBarry Smith     z[i]   = z[intval];
11977c4ece6SBarry Smith     z[intval] = tmp;
12077c4ece6SBarry Smith   }
12177c4ece6SBarry Smith 
12277c4ece6SBarry Smith   for (i=0; i<2000; i++) {
12377c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
12477c4ece6SBarry Smith     intval = (int)(value*20000.0);
12577c4ece6SBarry Smith     tmp    = zi[i];
12677c4ece6SBarry Smith     zi[i]  = zi[intval];
12777c4ece6SBarry Smith     zi[intval] = tmp;
12877c4ece6SBarry Smith   }
12977c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
13077c4ece6SBarry Smith 
13177c4ece6SBarry Smith   /* BlastCache();  */
13277c4ece6SBarry Smith 
13377c4ece6SBarry Smith   t1 = PetscGetTime();
13477c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
13577c4ece6SBarry Smith   t2 = PetscGetTime();
13677c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
13777c4ece6SBarry Smith 
13877c4ece6SBarry Smith   /* BlastCache();  */
13977c4ece6SBarry Smith 
14077c4ece6SBarry Smith   t1 = PetscGetTime();
14177c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[i] = x[z[i]]; }
14277c4ece6SBarry Smith   t2 = PetscGetTime();
14377c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
14477c4ece6SBarry Smith 
14577c4ece6SBarry Smith   /* BlastCache(); */
14677c4ece6SBarry Smith 
14777c4ece6SBarry Smith   t1 = PetscGetTime();
14877c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
14977c4ece6SBarry Smith   t2 = PetscGetTime();
15077c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
15177c4ece6SBarry Smith 
15277c4ece6SBarry Smith   /* BlastCache(); */
15377c4ece6SBarry Smith 
15477c4ece6SBarry Smith   t1 = PetscGetTime();
15577c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[z[i]] = x[zi[i]]; }
15677c4ece6SBarry Smith   t2 = PetscGetTime();
15777c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
15877c4ece6SBarry Smith 
15977c4ece6SBarry Smith 
16077c4ece6SBarry Smith   PetscRandomDestroy(r);
16177c4ece6SBarry Smith   return 0;
16277c4ece6SBarry Smith }
16377c4ece6SBarry Smith 
16477c4ece6SBarry Smith int BlastCache()
16577c4ece6SBarry Smith {
16677c4ece6SBarry Smith   int    i,n = 1000000;
16777c4ece6SBarry Smith   Scalar *x,*y,*z,*a, *b;
16877c4ece6SBarry Smith 
16977c4ece6SBarry Smith   x = (Scalar *) PetscMalloc(5*n); CHKPTRA(x);
17077c4ece6SBarry Smith   y = x + n;
17177c4ece6SBarry Smith   z = y + n;
17277c4ece6SBarry Smith   a = z + n;
17377c4ece6SBarry Smith   b = a + n;
17477c4ece6SBarry Smith 
17577c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
17677c4ece6SBarry Smith     a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
17777c4ece6SBarry Smith   }
17877c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
17977c4ece6SBarry Smith     b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
18077c4ece6SBarry Smith   }
18177c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
18277c4ece6SBarry Smith     z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
18377c4ece6SBarry Smith   }
18477c4ece6SBarry Smith   PetscFree(x);
1851f480b34SSatish Balay   return 0;
1861f480b34SSatish Balay }
187