xref: /petsc/src/benchmarks/PetscMalloc.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
1 #include <petscsys.h>
2 #include <petsctime.h>
3 
4 int main(int argc,char **argv)
5 {
6   PetscLogDouble x,y;
7   double         value;
8   void           *arr[1000],*dummy;
9   int            i,rand1[1000],rand2[1000];
10   PetscRandom    r;
11   PetscBool      flg;
12 
13   PetscCall(PetscInitialize(&argc,&argv,0,0));
14   PetscCall(PetscRandomCreate(PETSC_COMM_SELF,&r));
15   PetscCall(PetscRandomSetFromOptions(r));
16   for (i=0; i<1000; i++) {
17     PetscCall(PetscRandomGetValue(r,&value));
18     rand1[i] = (int)(value* 144327);
19     PetscCall(PetscRandomGetValue(r,&value));
20     rand2[i] = (int)(value* 144327);
21   }
22 
23   /* Take care of paging effects */
24   PetscCall(PetscMalloc1(100,&dummy));
25   PetscCall(PetscFree(dummy));
26   PetscCall(PetscTime(&x));
27 
28   /* Do all mallocs */
29   for (i=0; i< 1000; i++) {
30     PetscCall(PetscMalloc1(rand1[i],&arr[i]));
31   }
32 
33   PetscCall(PetscTime(&x));
34 
35   /* Do some frees */
36   for (i=0; i< 1000; i+=2) {
37     PetscCall(PetscFree(arr[i]));
38   }
39 
40   /* Do some mallocs */
41   for (i=0; i< 1000; i+=2) {
42     PetscCall(PetscMalloc1(rand2[i],&arr[i]));
43   }
44   PetscCall(PetscTime(&y));
45 
46   for (i=0; i< 1000; i++) {
47     PetscCall(PetscFree(arr[i]));
48   }
49 
50   fprintf(stdout,"%-15s : %e sec, with options : ","PetscMalloc",(y-x)/500.0);
51   PetscCall(PetscOptionsHasName(NULL,"-malloc",&flg));
52   if (flg) fprintf(stdout,"-malloc ");
53   fprintf(stdout,"\n");
54 
55   PetscCall(PetscRandomDestroy(&r));
56   PetscCall(PetscFinalize());
57   return 0;
58 }
59