xref: /petsc/src/benchmarks/Index.c (revision e1311b9049e89cb3452dcd306fde571f4b440ff2)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: Index.c,v 1.19 1998/03/24 00:15:09 balay Exp balay $";
3 #endif
4 
5 #include "petsc.h"
6 #include "sys.h"
7 
8 int BlastCache(void);
9 int test1(void);
10 int test2(void);
11 
12 int main( int argc, char **argv)
13 {
14   int ierr;
15 
16   PetscInitialize(&argc, &argv,0,0);
17 
18   ierr = test1(); CHKERRA(ierr);
19   ierr = test2(); CHKERRA(ierr);
20 
21   PetscFinalize();
22   PetscFunctionReturn(0);
23 }
24 
25 int test1(void)
26 {
27   PLogDouble  t1, t2;
28   double      value;
29   int         i, ierr,*z,*zi, intval;
30   Scalar      *x, *y;
31   PetscRandom r;
32 
33   ierr = PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
34   x    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(x);
35   y    = (Scalar *)PetscMalloc(20000*sizeof(Scalar)); CHKPTRA(y);
36 
37   z     = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(z);
38   zi    = (int *)PetscMalloc(2000*sizeof(int)); CHKPTRA(zi);
39 
40 
41 
42   /* Take care of paging effects */
43   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
44 
45    /* Form the random set of integers */
46   for (i=0; i<2000; i++) {
47     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
48     intval = (int)(value*20000.0);
49     z[i]   = intval;
50   }
51 
52   for (i=0; i<2000; i++) {
53     ierr    = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
54     intval  = (int)(value*20000.0);
55     zi[i]   = intval;
56   }
57   fprintf(stderr,"Done setup\n");
58 
59   ierr = BlastCache(); CHKERRQ(ierr);
60 
61   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
62   for (i=0; i<2000; i++) {  x[i] = y[i]; }
63   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
64   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
65 
66   ierr = BlastCache(); CHKERRQ(ierr);
67 
68   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
69   for (i=0; i<500; i+=4) {
70     x[i]   = y[z[i]];
71     x[1+i] = y[z[1+i]];
72     x[2+i] = y[z[2+i]];
73     x[3+i] = y[z[3+i]];
74   }
75   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
76   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 4",(t2-t1)/2000.0);
77 
78   ierr = BlastCache(); CHKERRQ(ierr);
79 
80   ierr = PetscGetTime(&t1); CHKERRQ(ierr)
81   for (i=0; i<2000; i++) {  x[i] = y[z[i]]; }
82   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
83   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
84 
85   ierr = BlastCache(); CHKERRQ(ierr);
86 
87   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
88   for (i=0; i<1000; i+=2) {  x[i] = y[z[i]];  x[1+i] = y[z[1+i]]; }
89   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
90   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]] - unroll 2",(t2-t1)/2000.0);
91 
92   ierr = BlastCache(); CHKERRQ(ierr);
93 
94   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
95   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
96   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
97   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
98 
99   ierr = BlastCache(); CHKERRQ(ierr);
100 
101   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
102   for (i=0; i<2000; i++) {  x[z[i]] = y[zi[i]]; }
103   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
104   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
105 
106   PetscMemcpy(x,y,10);
107   PetscMemcpy(z,zi,10);
108   PetscFree(z);
109   PetscFree(zi);
110   PetscFree(x);
111   PetscFree(y);
112   PetscRandomDestroy(r);
113   PetscFunctionReturn(0);
114 }
115 
116 int test2(void)
117 {
118   PLogDouble   t1, t2;
119   double       value;
120   int          i, ierr,z[20000],zi[20000], intval, tmp;
121   Scalar       x[20000], y[20000];
122   PetscRandom  r;
123 
124   ierr = PetscRandomCreate(PETSC_COMM_SELF,RANDOM_DEFAULT,&r); CHKERRQ(ierr);
125 
126   /* Take care of paging effects */
127   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
128 
129   for (i=0; i<20000; i++) {
130     x[i]  = i;
131     y[i]  = i;
132     z[i]  = i;
133     zi[i] = i;
134   }
135 
136    /* Form the random set of integers */
137   for (i=0; i<20000; i++) {
138     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
139     intval = (int)(value*20000.0);
140     tmp    = z[i];
141     z[i]   = z[intval];
142     z[intval] = tmp;
143   }
144 
145   for (i=0; i<20000; i++) {
146     ierr   = PetscRandomGetValue(r, &value); CHKERRQ(ierr);
147     intval = (int)(value*20000.0);
148     tmp    = zi[i];
149     zi[i]  = zi[intval];
150     zi[intval] = tmp;
151   }
152   fprintf(stderr,"Done setup\n");
153 
154   /* ierr = BlastCache();  CHKERRQ(ierr); */
155 
156   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
157   for (i=0; i<2000; i++) {  x[i] = y[i]; }
158   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
159   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[i]",(t2-t1)/2000.0);
160 
161   /* ierr = BlastCache();  CHKERRQ(ierr); */
162 
163   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
164   for (i=0; i<2000; i++) {  y[i] = x[z[i]]; }
165   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
166   fprintf(stderr,"%-19s : %e sec\n","x[i] = y[idx[i]]",(t2-t1)/2000.0);
167 
168   /* ierr = BlastCache();  CHKERRQ(ierr); */
169 
170   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
171   for (i=0; i<2000; i++) {  x[z[i]] = y[i]; }
172   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
173   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[i]",(t2-t1)/2000.0);
174 
175   /* ierr = BlastCache();  CHKERRQ(ierr); */
176 
177   ierr = PetscGetTime(&t1); CHKERRQ(ierr);
178   for (i=0; i<2000; i++) {  y[z[i]] = x[zi[i]]; }
179   ierr = PetscGetTime(&t2); CHKERRQ(ierr);
180   fprintf(stderr,"%-19s : %e sec\n","x[z[i]] = y[zi[i]]",(t2-t1)/2000.0);
181 
182 
183   PetscRandomDestroy(r);
184   PetscFunctionReturn(0);
185 }
186 
187 int BlastCache(void)
188 {
189   int    i,n = 1000000;
190   Scalar *x,*y,*z,*a, *b;
191 
192   x = (Scalar *) PetscMalloc(5*n*sizeof(Scalar)); CHKPTRA(x);
193   y = x + n;
194   z = y + n;
195   a = z + n;
196   b = a + n;
197 
198   for ( i=0; i<n; i++ ) {
199     a[i] = (Scalar) i;
200     y[i] = (Scalar) i;
201     z[i] = (Scalar) i;
202     b[i] = (Scalar) i;
203     x[i] = (Scalar) i;
204   }
205 
206   for ( i=0; i<n; i++ ) {
207     a[i] = 3.0*x[i] + 2.0*y[i] + 3.3*z[i] - 25.*b[i];
208   }
209   for ( i=0; i<n; i++ ) {
210     b[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
211   }
212   for ( i=0; i<n; i++ ) {
213     z[i] = 3.0*x[i] + 2.0*y[i] + 3.3*a[i] - 25.*b[i];
214   }
215   PetscFree(x);
216   PetscFunctionReturn(0);
217 }
218