-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential.c
More file actions
60 lines (51 loc) · 1.37 KB
/
sequential.c
File metadata and controls
60 lines (51 loc) · 1.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
#include "benchmarks.h"
#define REPEATS 5
double measure_sequential(int size_bytes)
{
int elements = size_bytes / sizeof(int);
int *A = malloc(size_bytes);
if (!A)
return -1;
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++)
{
sum += A[i];
}
}
double end = get_time();
free(A);
double total_accesses = (double)elements * REPEATS;
double total_time_ns = (end - start) * 1e9;
return total_time_ns / total_accesses;
}
void run_sequential_test(FILE *fp, int specific_size, int iterations)
{
printf("\nSEQUENTIAL ACCESS TEST\n");
if (specific_size == -1)
{
printf("Block Size (KB)\tAccess Time (ns)\n");
printf("---------------------------------\n");
fprintf(fp, "Sequential,BlockSize_KB,AccessTime_ns\n");
for (int size_kb = 1; size_kb <= 8192; size_kb *= 2)
{
double result = measure_sequential(size_kb * KB);
printf("%8d\t\t%.4f\n", size_kb, result);
fprintf(fp, "Sequential,%d,%.4f\n", size_kb, result);
}
}
else
{
int size_kb = specific_size;
for (int rep = 1; rep <= iterations; rep++)
{
double result = measure_sequential(size_kb * KB);
fprintf(fp, "%d,%.4f\n", rep, result);
printf("Run %d: %.4f ns\n", rep, result);
}
}
}