-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcycmm.c
More file actions
157 lines (142 loc) · 3.37 KB
/
Copy pathfcycmm.c
File metadata and controls
157 lines (142 loc) · 3.37 KB
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
152
153
154
155
156
/*
* fcycmm.c - Compute time used by function f
* Modified version of fcyc.c specifically for use with mm.c.
* In fcyc_full, hardcodes the arguments passed to the test function.
*/
#include <stdlib.h>
#include <sys/times.h>
#include <stdio.h>
#include "mm.h"
#include "clock.h"
#include "fcycmm.h"
/* defined in mm.c */
extern array ga, gb, gc;
void reset(array, int);
static double *values = NULL;
int samplecount = 0;
#define KEEP_VALS 1
#define KEEP_SAMPLES 1
#if KEEP_SAMPLES
double *samples = NULL;
#endif
/* Start new sampling process */
static void init_sampler(int k, int maxsamples)
{
if (values)
free(values);
values = calloc(k, sizeof(double));
#if KEEP_SAMPLES
if (samples)
free(samples);
/* Allocate extra for wraparound analysis */
samples = calloc(maxsamples+k, sizeof(double));
#endif
samplecount = 0;
}
/* Add new sample. */
void add_sample(double val, int k)
{
int pos = 0;
if (samplecount < k) {
pos = samplecount;
values[pos] = val;
} else if (val < values[k-1]) {
pos = k-1;
values[pos] = val;
}
#if KEEP_SAMPLES
samples[samplecount] = val;
#endif
samplecount++;
/* Insertion sort */
while (pos > 0 && values[pos-1] > values[pos]) {
double temp = values[pos-1];
values[pos-1] = values[pos];
values[pos] = temp;
pos--;
}
}
/* Get current minimum */
double get_min()
{
return values[0];
}
/* What is relative error for kth smallest sample */
double err(int k)
{
if (samplecount < k)
return 1000.0;
return (values[k-1] - values[0])/values[0];
}
/* Have k minimum measurements converged within epsilon? */
int has_converged(int k_arg, double epsilon_arg, int maxsamples)
{
if ((samplecount >= k_arg) &&
((1 + epsilon_arg)*values[0] >= values[k_arg-1]))
return samplecount;
if ((samplecount >= maxsamples))
return -1;
return 0;
}
/* Code to clear cache */
/* Pentium III has 512K L2 cache, which is 128K ints */
#define ASIZE (1 << 17)
/* Cache block size is 32 bytes */
#define STRIDE 8
static int stuff[ASIZE];
static int sink;
static void clear()
{
int x = sink;
int i;
for (i = 0; i < ASIZE; i += STRIDE)
x += stuff[i];
sink = x;
}
double fcyc_full(test_funct f, int n, int clear_cache,
int k, double epsilon, int maxsamples, int compensate)
{
double result;
init_sampler(k, maxsamples);
if (compensate) {
do {
double cyc;
if (clear_cache)
clear();
reset(gc, n); /* hardcoded reset result array to zero */
start_comp_counter();
f(ga, gb, gc, n); /* note hardcoded arguments */
cyc = get_comp_counter();
add_sample(cyc, k);
} while (!has_converged(k, epsilon, maxsamples) && samplecount < maxsamples);
} else {
do {
double cyc;
if (clear_cache)
clear();
reset(gc, n); /* hardcoded reset result array to zero */
start_counter();
f(ga, gb, gc, n); /* note hardcoded arguments */
cyc = get_counter();
add_sample(cyc, k);
} while (!has_converged(k, epsilon, maxsamples) && samplecount < maxsamples);
}
#ifdef DEBUG
{
int i;
printf(" %d smallest values: [", k);
for (i = 0; i < k; i++)
printf("%.0f%s", values[i], i==k-1 ? "]\n" : ", ");
}
#endif
result = values[0];
#if !KEEP_VALS
free(values);
values = NULL;
#endif
return result;
}
double fcyc(test_funct f, int n, int clear_cache)
{
return fcyc_full(f, n, clear_cache, 3, 0.01, 20, 0);
}