xref: /petsc/src/benchmarks/Index.c (revision 77c4ece699e97450631aa6fc5b0ef04ff52df029)
11f480b34SSatish Balay 
21f480b34SSatish Balay #include "stdio.h"
31f480b34SSatish Balay #include "petsc.h"
41f480b34SSatish Balay #include "sys.h"
51f480b34SSatish Balay 
6*77c4ece6SBarry Smith int BlastCache();
7*77c4ece6SBarry Smith 
81f480b34SSatish Balay int main( int argc, char **argv)
91f480b34SSatish Balay {
10*77c4ece6SBarry Smith   PetscInitialize(&argc, &argv,0,0);
111f480b34SSatish Balay 
12*77c4ece6SBarry Smith   test1();
13*77c4ece6SBarry Smith   test2();
14*77c4ece6SBarry Smith 
15*77c4ece6SBarry Smith   PetscFinalize();
16*77c4ece6SBarry Smith   return 0;
17*77c4ece6SBarry Smith }
18*77c4ece6SBarry Smith 
19*77c4ece6SBarry Smith int test1()
20*77c4ece6SBarry Smith {
21*77c4ece6SBarry Smith   double    t1, t2, value;
22*77c4ece6SBarry Smith   int       i, ierr,*z,*zi, intval, tmp;
23*77c4ece6SBarry Smith   Scalar    *x, *y;
24*77c4ece6SBarry Smith   PetscRandom  r;
25*77c4ece6SBarry Smith 
26*77c4ece6SBarry Smith   ierr = PetscRandomCreate(MPI_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
27*77c4ece6SBarry Smith   x    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(x);
28*77c4ece6SBarry Smith   y    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(y);
29*77c4ece6SBarry Smith 
30*77c4ece6SBarry Smith   z     = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(z);
31*77c4ece6SBarry Smith   zi    = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(zi);
32*77c4ece6SBarry Smith 
33*77c4ece6SBarry Smith 
341f480b34SSatish Balay 
351f480b34SSatish Balay   /* Take care of paging effects */
361f480b34SSatish Balay   t1 = PetscGetTime();
371f480b34SSatish Balay 
38*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
39*77c4ece6SBarry Smith     zi[i] = i;
402e7ac647SSatish Balay     z[i]  = i;
411f480b34SSatish Balay   }
421f480b34SSatish Balay 
43*77c4ece6SBarry Smith   for (i=0; i<20000; i++) {
44*77c4ece6SBarry Smith     x[i] = i;
45*77c4ece6SBarry Smith     y[i]  = i;
46*77c4ece6SBarry Smith   }
47*77c4ece6SBarry Smith 
481f480b34SSatish Balay    /* Form the random set of integers */
49*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
50*77c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
51*77c4ece6SBarry Smith     intval = (int)(value*20000.0);
521f480b34SSatish Balay     tmp    = z[i];
531f480b34SSatish Balay     z[i]   = z[intval];
541f480b34SSatish Balay     z[intval] = tmp;
551f480b34SSatish Balay   }
561f480b34SSatish Balay 
57*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
58*77c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
59*77c4ece6SBarry Smith     intval = (int)(value*20000.0);
60*77c4ece6SBarry Smith     tmp    = zi[i];
61*77c4ece6SBarry Smith     zi[i]  = zi[intval];
62*77c4ece6SBarry Smith     zi[intval] = tmp;
63*77c4ece6SBarry Smith   }
64*77c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
65*77c4ece6SBarry Smith 
66*77c4ece6SBarry Smith   BlastCache();
671f480b34SSatish Balay 
681f480b34SSatish Balay   t1 = PetscGetTime();
69*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
701f480b34SSatish Balay   t2 = PetscGetTime();
71*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
721f480b34SSatish Balay 
73*77c4ece6SBarry Smith   BlastCache();
741f480b34SSatish Balay 
751f480b34SSatish Balay   t1 = PetscGetTime();
76*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[z[i]]; }
771f480b34SSatish Balay   t2 = PetscGetTime();
78*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
79*77c4ece6SBarry Smith 
80*77c4ece6SBarry Smith   BlastCache();
811f480b34SSatish Balay 
821f480b34SSatish Balay   t1 = PetscGetTime();
83*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
841f480b34SSatish Balay   t2 = PetscGetTime();
85*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
861f480b34SSatish Balay 
87*77c4ece6SBarry Smith   BlastCache();
88*77c4ece6SBarry Smith 
89*77c4ece6SBarry Smith   t1 = PetscGetTime();
90*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[zi[i]]; }
91*77c4ece6SBarry Smith   t2 = PetscGetTime();
92*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
93*77c4ece6SBarry Smith 
94*77c4ece6SBarry Smith   PetscFree(z);
95*77c4ece6SBarry Smith   PetscFree(zi);
96*77c4ece6SBarry Smith   PetscFree(x);
97*77c4ece6SBarry Smith   PetscFree(y);
98*77c4ece6SBarry Smith   PetscRandomDestroy(r);
99*77c4ece6SBarry Smith   return 0;
100*77c4ece6SBarry Smith }
101*77c4ece6SBarry Smith 
102*77c4ece6SBarry Smith int test2()
103*77c4ece6SBarry Smith {
104*77c4ece6SBarry Smith   double    t1, t2, value;
105*77c4ece6SBarry Smith   int       i, ierr,z[2000],zi[20000], intval, tmp;
106*77c4ece6SBarry Smith   Scalar    x[20000], y[20000];
107*77c4ece6SBarry Smith   PetscRandom  r;
108*77c4ece6SBarry Smith 
109*77c4ece6SBarry Smith   ierr = PetscRandomCreate(MPI_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
110*77c4ece6SBarry Smith 
111*77c4ece6SBarry Smith   /* Take care of paging effects */
112*77c4ece6SBarry Smith   t1 = PetscGetTime();
113*77c4ece6SBarry Smith 
114*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
115*77c4ece6SBarry Smith     zi[i] = i;
116*77c4ece6SBarry Smith     z[i]  = i;
117*77c4ece6SBarry Smith   }
118*77c4ece6SBarry Smith 
119*77c4ece6SBarry Smith   for (i=0; i<20000; i++) {
120*77c4ece6SBarry Smith     x[i] = i;
121*77c4ece6SBarry Smith     y[i]  = i;
122*77c4ece6SBarry Smith   }
123*77c4ece6SBarry Smith 
124*77c4ece6SBarry Smith    /* Form the random set of integers */
125*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
126*77c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
127*77c4ece6SBarry Smith     intval = (int)(value*20000.0);
128*77c4ece6SBarry Smith     tmp    = z[i];
129*77c4ece6SBarry Smith     z[i]   = z[intval];
130*77c4ece6SBarry Smith     z[intval] = tmp;
131*77c4ece6SBarry Smith   }
132*77c4ece6SBarry Smith 
133*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {
134*77c4ece6SBarry Smith     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
135*77c4ece6SBarry Smith     intval = (int)(value*20000.0);
136*77c4ece6SBarry Smith     tmp    = zi[i];
137*77c4ece6SBarry Smith     zi[i]  = zi[intval];
138*77c4ece6SBarry Smith     zi[intval] = tmp;
139*77c4ece6SBarry Smith   }
140*77c4ece6SBarry Smith   fprintf(stderr,"Done setup\n");
141*77c4ece6SBarry Smith 
142*77c4ece6SBarry Smith   /* BlastCache();  */
143*77c4ece6SBarry Smith 
144*77c4ece6SBarry Smith   t1 = PetscGetTime();
145*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[i] = y[i]; }
146*77c4ece6SBarry Smith   t2 = PetscGetTime();
147*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
148*77c4ece6SBarry Smith 
149*77c4ece6SBarry Smith   /* BlastCache();  */
150*77c4ece6SBarry Smith 
151*77c4ece6SBarry Smith   t1 = PetscGetTime();
152*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[i] = x[z[i]]; }
153*77c4ece6SBarry Smith   t2 = PetscGetTime();
154*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
155*77c4ece6SBarry Smith 
156*77c4ece6SBarry Smith   /* BlastCache(); */
157*77c4ece6SBarry Smith 
158*77c4ece6SBarry Smith   t1 = PetscGetTime();
159*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
160*77c4ece6SBarry Smith   t2 = PetscGetTime();
161*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
162*77c4ece6SBarry Smith 
163*77c4ece6SBarry Smith   /* BlastCache(); */
164*77c4ece6SBarry Smith 
165*77c4ece6SBarry Smith   t1 = PetscGetTime();
166*77c4ece6SBarry Smith   for (i=0; i<2000; i++) {  y[z[i]] = x[zi[i]]; }
167*77c4ece6SBarry Smith   t2 = PetscGetTime();
168*77c4ece6SBarry Smith   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
169*77c4ece6SBarry Smith 
170*77c4ece6SBarry Smith 
171*77c4ece6SBarry Smith   PetscRandomDestroy(r);
172*77c4ece6SBarry Smith   return 0;
173*77c4ece6SBarry Smith }
174*77c4ece6SBarry Smith 
175*77c4ece6SBarry Smith int BlastCache()
176*77c4ece6SBarry Smith {
177*77c4ece6SBarry Smith   int    i,n = 1000000;
178*77c4ece6SBarry Smith   Scalar *x,*y,*z,*a, *b;
179*77c4ece6SBarry Smith 
180*77c4ece6SBarry Smith   x = (Scalar *) PetscMalloc(5*n); CHKPTRA(x);
181*77c4ece6SBarry Smith   y = x + n;
182*77c4ece6SBarry Smith   z = y + n;
183*77c4ece6SBarry Smith   a = z + n;
184*77c4ece6SBarry Smith   b = a + n;
185*77c4ece6SBarry Smith 
186*77c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
187*77c4ece6SBarry Smith     a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
188*77c4ece6SBarry Smith   }
189*77c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
190*77c4ece6SBarry Smith     b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
191*77c4ece6SBarry Smith   }
192*77c4ece6SBarry Smith   for ( i=0; i<n; i++ ) {
193*77c4ece6SBarry Smith     z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
194*77c4ece6SBarry Smith   }
195*77c4ece6SBarry Smith   PetscFree(x);
1961f480b34SSatish Balay   return 0;
1971f480b34SSatish Balay }
198