forked from gongyiling/cpp_lecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_bandwidth.cpp
42 lines (38 loc) · 897 Bytes
/
memory_bandwidth.cpp
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
#include <iostream>
#include <vector>
#include <list>
#include <chrono>
#include <sched.h>
#define CACHELINE_SIZE 64
int test(int M, const std::vector<int>& c)
{
int sum = 0;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < M; i++)
{
for (size_t j = 0; j < c.size(); j+= CACHELINE_SIZE / sizeof(int))
{
sum += c[j];
}
}
auto end = std::chrono::high_resolution_clock::now();
std::cout << "elapsed milliseconds: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << std::endl;
return sum;
}
int main()
{
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
sched_setaffinity(0, sizeof(set), &set);
const int N = 1024 * 1024 * 16;
std::vector<int> va;
for (int i = 0; i < N; i++)
{
va.push_back(rand());
}
const int M = 100;
int sum = test(M, va);
std::cout << sum << std::endl;
return 0;
}