-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbandwidth.c
More file actions
72 lines (59 loc) · 1.51 KB
/
bandwidth.c
File metadata and controls
72 lines (59 loc) · 1.51 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
#include "benchmarks.h"
#include <string.h>
#define REPEATS 5
#define INNER_REPEATS 10000
double measure_bandwidth(int block_size)
{
char *src = malloc(block_size);
char *dst = malloc(block_size);
if (!src || !dst)
return -1;
for (int i = 0; i < block_size; i++)
{
src[i] = (char)(i % 256);
}
double start = get_time();
for (int r = 0; r < REPEATS; r++)
{
for (int i = 0; i < INNER_REPEATS; i++)
{
memcpy(dst, src, block_size);
}
}
double end = get_time();
free(src);
free(dst);
double total_bytes = (double)block_size * INNER_REPEATS * REPEATS;
double total_MB = total_bytes / (1024.0 * 1024.0);
double total_time = end - start;
double bandwidth = total_MB / total_time;
return bandwidth;
}
void run_bandwidth_test(FILE *fp, int specific_size, int iterations)
{
printf("\nBANDWIDTH TEST\n");
if (specific_size == -1)
{
printf("Block Size (KB)\tBandwidth (MB/s)\n");
printf("----------------------------------\n");
for (int size_kb = 1; size_kb <= 8192; size_kb *= 2)
{
double bw = measure_bandwidth(size_kb * KB);
if (bw > 0)
printf("%8d\t\t%.2f\n", size_kb, bw);
else
printf("%8d\t\tError\n", size_kb);
fprintf(fp, "Bandwidth,%d,%.2f\n", size_kb, bw);
}
}
else
{
int size_kb = specific_size;
for (int rep = 1; rep <= iterations; rep++)
{
double bw = measure_bandwidth(size_kb * KB);
fprintf(fp, "%d,%.4f\n", rep, bw);
printf("Run %d: %.4f MB/s\n", rep, bw);
}
}
}