forked from xxyzz/ostep-hw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
72 lines (63 loc) · 2.42 KB
/
mem.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
// Simple routine to return absolute time (in seconds).
double Time_GetSeconds() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) ((double)t.tv_sec + (double)t.tv_usec / 1e6);
}
// Program that allocates an array of ints of certain size,
// and then proceeds to update each int in a loop, forever.
int main(int argc, char *argv[]) {
for(int i = 1; i < 9; i++) {
long long int size = (long long int) i;
long long int size_in_bytes = size * 1024 * 1024 * 1024;
printf("allocating %lld bytes (%.2f MB)\n",
size_in_bytes, size_in_bytes / (1024 * 1024.0));
// the big memory allocation happens here
int *x = malloc(size_in_bytes);
if (x == NULL) {
fprintf(stderr, "memory allocation failed\n");
exit(1);
}
long long int num_ints = size_in_bytes / sizeof(int);
printf(" number of integers in array: %lld\n", num_ints);
// now the main loop: each time through, touch each integer
// (and increment its value by 1).
int k = 0;
double time_since_last_print = 2.0;
double t = Time_GetSeconds();
int loop_count = 0;
double bandwidthSum = 0;
while (1) {
x[k++] += 1; // main work of loop done here.
// if we've gone through the whole loop, reset a bunch of stuff
// and then (perhaps) print out some statistics.
if (k == num_ints) {
double delta_time = Time_GetSeconds() - t;
time_since_last_print += delta_time;
double bandwidth = size_in_bytes / (1024.0*1024.0*delta_time);
bandwidthSum += bandwidth;
if (time_since_last_print >= 0.2) { // only print every .2 seconds
printf("loop %d in %.2f ms (bandwidth: %.2f MB/s)\n",
loop_count, 1000 * delta_time, bandwidth);
time_since_last_print = 0;
}
if (loop_count > 9) {
break;
} else {
k = 0;
t = Time_GetSeconds();
loop_count++;
}
}
}
free(x);
printf("Average bandwidth: %.2f\n\n", bandwidthSum / loop_count);
}
return 0;
}