-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathp2.c
152 lines (138 loc) · 2.86 KB
/
p2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#define _GNU_SOURCE
#define _MULTI_THREADED
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <getopt.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <float.h>
#include <string.h>
#include <assert.h>
#include <sched.h>
#include <stdint.h>
#include <inttypes.h>
#ifdef USEHUGE
#include <sys/mman.h>
#endif
#ifdef USENUMA
#include <numa.h>
#endif
int cacheLineSize = 64; /* bytes per cacheline */
double increaseArray = 0.925;
int pageSize = 4096;
int perCacheLine;
int cacheLinesPerPage;
double begin,end;
/* crude wall clock time keeping, returns a double of seconds */
double
second ()
{
struct timeval tp;
struct timezone tzp;
gettimeofday (&tp, &tzp);
return ((double) tp.tv_sec + (double) tp.tv_usec * 1.e-6);
}
void
swap (int64_t * a, int64_t x, int64_t y)
{
int64_t t;
t = a[x];
a[x] = a[y];
a[y] = t;
}
/* choose between l and h, inclusive of both. */
uint64_t
choose (uint64_t l, uint64_t h)
{
uint64_t range, smallr, ret;
range = h - l;
assert (l <= h);
smallr = range / perCacheLine;
ret = (l + (uint64_t) (drand48 () * smallr) * perCacheLine);
assert (ret <= h);
if (l < h)
return ret;
return h;
}
int
followAr (int64_t * a, int64_t size, int repeat, int64_t hops)
{
int64_t p = 0;
int64_t *b;
int i;
int64_t base;
for (i = 0; i < repeat; i++)
{
base=0;
while (base<size) {
b=&a[base]; // start pointer chasing at begin of page
p=b[0];
// printf("%p\n",(void *)&p[b]);
while (p)
{
p = b[p];
// printf("%p\n",(void *)&p[b]);
}
base=base+hops*cacheLineSize; // fix 4k / 8 bytes = 512
}
}
return (a[0]);
}
initAr(int64_t *a,int64_t size)
{
while (base<size)
{
max=MIN(hops,size-base);
b=&a[base];
for (i = 0; i < cacheLinePerPage; i = i + perCacheLine) //fix
{
b[i] = i + perCacheLine; /* assign each int the index of the next int */
}
b[i-cacheLineSize]=0;
base=base+pageSize/sizeof(int64_t);
}
}
shuffleAr(int64_t *a, int64_t size)
{
srand48 ((long int) getpid ());
base=0;
while (base<size)
{
b=&a[base];
// leave the first hop alone, it loads the page and cacheline and we don't want to exit
for (i = 0; i < cacheLinesPerPage; i = i + perCacheLine) //fix
{
c = choose (i, cacheLinesPerPage - perCacheLine);
x = b[i];
y = b[c];
swap (b,i,c);
swap (b,x,y);
}
base=base+pageSize/sizeof(int64_t);
}
base=0;
}
int
main (int argc, char *argv[])
{
int64_t *a,*b;
int64_t *aa = NULL;
int64_t x, y;
int64_t i, c, max =0;
int64_t size, len = 0;
int64_t base,hops;
double start,end;
size = maxmem / sizeof (int64_t); // number of int64s.
if (posix_memalign((void **)&a, getpagesize(), sizeof(int64_t) * maxmem) != 0) {
printf ("Memory allocation failed\n");
exit(-1);
}
initAr(a,size);
shuffler(a,size);
start = second ();
followAr (a, size, scale, hops);
end = second();
printf ("took %f seconds\n",end-start);
}