-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstride.c
More file actions
75 lines (65 loc) · 1.82 KB
/
stride.c
File metadata and controls
75 lines (65 loc) · 1.82 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
#include "benchmarks.h"
#define REPEATS 5
static double measure_stride(int array_size_bytes, int stride_bytes)
{
int elements = array_size_bytes / sizeof(int);
int stride_elems = stride_bytes / (int)sizeof(int);
if (stride_elems <= 0)
stride_elems = 1;
int *A = malloc(array_size_bytes);
if (!A)
return -1.0;
for (int i = 0; i < elements; i++)
{
A[i] = i;
}
volatile long long sum = 0;
double start = get_time();
for (int r = 0; r < REPEATS; r++)
{
for (int i = 0; i < elements; i += stride_elems)
{
sum += A[i];
}
}
double end = get_time();
free(A);
double total_accesses = 0.0;
for (int r = 0; r < REPEATS; r++)
{
total_accesses += (elements + stride_elems - 1) / stride_elems;
}
double total_time_ns = (end - start) * 1e9;
return total_time_ns / total_accesses;
}
void run_stride_test(FILE *fp, int specific_stride, int iterations)
{
printf("\nSTRIDE ACCESS TEST\n");
int array_size_bytes = 8 * MB;
if (specific_stride == -1)
{
printf("Stride (bytes)\tAccess Time (ns)\n");
printf("---------------------------------\n");
fprintf(fp, "Stride,StrideBytes,AccessTime_ns\n");
int strides_bytes[] = {4, 8, 16, 32, 64, 128,
256, 512, 1024, 2048, 4096, 8192};
int num_strides = sizeof(strides_bytes) / sizeof(strides_bytes[0]);
for (int i = 0; i < num_strides; i++)
{
int stride = strides_bytes[i];
double t = measure_stride(array_size_bytes, stride);
printf("%8d\t\t%.4f\n", stride, t);
fprintf(fp, "Stride,%d,%.4f\n", stride, t);
}
}
else
{
int stride = specific_stride;
for (int rep = 1; rep <= iterations; rep++)
{
double t = measure_stride(array_size_bytes, stride);
fprintf(fp, "%d,%.4f\n", rep, t);
printf("Run %d: %.4f ns\n", rep, t);
}
}
}