forked from hpc12/tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembench.c
78 lines (70 loc) · 2.11 KB
/
membench.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
//////////////////////////////////////////////////////////////////////
// $Id: membench.c,v 1.2 1998/02/01 23:13:27 dmartin Exp $
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <sys/times.h>
#include <sys/types.h>
#define SAMPLE 10
#define CACHE_MIN (1024)
#define CACHE_MAX (1024*1024*4)
#define TIME_DIF_TO_NS(s,f) \
((f.tv_sec-s.tv_sec)*1000000000.0 + (f.tv_nsec-s.tv_nsec))
int x[CACHE_MAX];
int main()
{
int register i, index, stride, limit, temp;
double sample_ns, sample_sec, sec, ns;
struct timespec start,finish;
int steps, tsteps, csize;
for (csize = CACHE_MIN; csize <= CACHE_MAX; csize *= 2){
for (stride = 1; stride <= csize/2; stride *= 2){
sec = 0;
ns = 0.0;
limit = csize-stride+1;
steps = 0;
do {
clock_gettime(CLOCK_REALTIME,&start);
for (i = SAMPLE*stride; i != 0; i--)
for (index = 0; index < limit; index += stride)
x[index]++;
clock_gettime(CLOCK_REALTIME,&finish);
sample_ns = TIME_DIF_TO_NS(start,finish);
sample_sec = sample_ns/1000000000.0;
steps++;
sec += sample_sec;
ns += sample_ns;
} while (sec < 1.0);
tsteps=0;
do {
clock_gettime(CLOCK_REALTIME,&start);
for (i = SAMPLE*stride; i != 0; i--)
for (index = 0; index < limit; index += stride)
temp += index;
clock_gettime(CLOCK_REALTIME,&finish);
sample_ns = TIME_DIF_TO_NS(start,finish);
sample_sec = sample_ns/1000000000.0;
tsteps++;
sec -= sample_sec;
ns -= sample_ns;
} while (tsteps < steps);
printf("Size: %7ld Stride: %7ld read+write: %14.0f ns\n",
csize*sizeof(int),
stride*sizeof(int),
(double) (sec*1000000000.0)/(steps*SAMPLE*stride*((limit-1.0)/stride+1.0)));
}
printf ("\n");
}
//::w exit (0);
return 0;
}
//////////////////////////////////////////////////////////////////////
// $Log: membench.c,v $
// Revision 1.2 1998/02/01 23:13:27 dmartin
// added exit(0)
//
// Revision 1.1 1998/01/19 00:47:39 dmartin
// Initial revision
//
//////////////////////////////////////////////////////////////////////