-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-memory.cpp
More file actions
45 lines (37 loc) · 1.17 KB
/
02-memory.cpp
File metadata and controls
45 lines (37 loc) · 1.17 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
// Timing memory operations and calculating bandwidth
// $ g++ -std=c++11 -O3 02-memory.cpp && ./a.out -n 400000000 -repeat 1 -skip 1
// $ cat /proc/cpuinfo
// $ cat /proc/meminfo
// $ htop
// $ getconf -a | grep CACHE
// ADVANCED $ valgrind --tool=cachegrind ./a.out
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
int main(int argc, char** argv) {
Timer t;
long n = read_option<long>("-n", argc, argv);
long repeat = read_option<long>("-repeat", argc, argv, "1");
long skip = read_option<long>("-skip", argc, argv, "1");
t.tic();
double* x = (double*) malloc(n * sizeof(double));
printf("time to malloc = %f s\n", t.toc());
t.tic();
for (long i = 0; i < n; i += skip) x[i] = i;
printf("time to initialize = %f s\n", t.toc());
t.tic();
for (long k = 0; k < repeat; k++) {
for (long i = 0; i < n; i += skip) {
x[i] = 2 * i;
}
}
printf("time to write = %f s\n", t.toc());
printf("bandwidth = %f GB/s\n", n * repeat / skip * sizeof(double) / 1e9 / t.toc());
double sum = 0;
for (long i = 0; i < n; i += skip) sum += x[i];
printf("sum = %f\n", sum);
t.tic();
free(x);
printf("time to free = %f s\n", t.toc());
return 0;
}