-
Notifications
You must be signed in to change notification settings - Fork 12
/
loop.c
103 lines (80 loc) · 1.39 KB
/
loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define N 128
int a[N][N], b[N][N], c[N][N], d[N][N];
void
init_arrays()
{
int i, j;
for (i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
a[i][j] = i + j;
b[i][j] = i + j;
c[i][j] = i + j;
d[i][j] = i + j;
}
}
}
#ifdef LOOP_DIST
void
foo(void)
{
int i, j;
for (i = 1; i < N; i++) {
for (j = 1; j < N; j ++) {
a[i][j] = b[i][j] + c[i][j];
}
for (j = 1; j < N; j ++) {
d[i][j] = a[i - 1][j - 1] * 2;
}
}
}
#elif LOOP_DIST_COMPLETE
void
foo(void)
{
int i, j;
for (i = 1; i < N; i++) {
for (j = 1; j < N; j ++) {
a[i][j] = b[i][j] + c[i][j];
}
}
for (i = 1; i < N; i++) {
for (j = 1; j < N; j ++) {
d[i][j] = a[i - 1][j - 1] * 2;
}
}
}
#else
void
foo(void)
{
int i, j;
for (i = 1; i < N; i++) {
for (j = 1; j < N; j ++) {
a[i][j] = b[i][j] + c[i][j];
d[i][j] = a[i - 1][j - 1] * 2;
}
}
}
#endif
uint64_t
trace_cpu_time_now(void)
{
uint32_t a, d;
asm volatile ("rdtsc":"=a" (a), "=d" (d));
return (uint64_t)a + ((uint64_t)d << (uint64_t)32);
}
int
main(void)
{
init_arrays();
uint64_t start = trace_cpu_time_now();
asm volatile("" ::: "memory");
foo();
asm volatile("" ::: "memory");
uint64_t end = trace_cpu_time_now();
printf("CPU cycles: %ld\n", end - start);
return 0;
}