1// Some work exercising the cache simulator
2// with a simple call graph
3
4#include "../callgrind.h"
5
6#include <stdio.h>
7#include <stdlib.h>
8
9#define SIZE 100000
10
11double *a, *b, *c;
12
13void init()
14{
15   int i;
16   for(i = 0; i< SIZE; i++) a[i] = b[i] = 1.0;
17}
18
19void do_add()
20{
21   int i;
22   for(i = 0; i< SIZE; i++) {
23	a[i] += 1.0;
24	c[i] = a[i] + b[i];
25   }
26}
27
28double do_sum()
29{
30   int i;
31   double sum=0.0;
32
33   do_add();
34   for(i = 0; i< SIZE; i++) sum += c[i];
35
36   return sum;
37}
38
39double do_some_work(int iter)
40{
41   double sum=0.0;
42
43   if (iter > 0) sum += do_some_work(iter-1);
44   do_add();
45   sum += do_sum();
46
47   return sum;
48}
49
50int main(void)
51{
52   double res;
53
54   a = (double*) malloc(SIZE * sizeof(double));
55   b = (double*) malloc(SIZE * sizeof(double));
56   c = (double*) malloc(SIZE * sizeof(double));
57
58   CALLGRIND_ZERO_STATS;
59   init();
60   res = do_some_work(1);
61   CALLGRIND_DUMP_STATS;
62
63   printf("Sum: %.0f\n", res);
64   return RUNNING_ON_VALGRIND;
65}
66
67