-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory_write_read_rand_threads.cpp
65 lines (55 loc) · 1.99 KB
/
memory_write_read_rand_threads.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <benchmark/benchmark.h>
#include <mutex>
#include <atomic>
#include <algorithm>
#include <random>
#define REPEAT2(x) x x
#define REPEAT8(x) REPEAT2(x) REPEAT2(x) REPEAT2(x) REPEAT2(x)
#define REPEAT32(x) REPEAT8(x) REPEAT8(x) REPEAT8(x) REPEAT8(x)
#define REPEAT64(x) REPEAT32(x) REPEAT32(x)
#define REPEAT(x) REPEAT64(x) REPEAT64(x)
template <class Word>
static void BM_WriteRandThreaded(benchmark::State& state) {
std::vector<char> vec1(state.range(0));
void* const start = vec1.data();
Word* const pStart = static_cast<Word*>(start);
Word fill(0);
size_t size = vec1.size() / sizeof(Word);
std::vector<int> randPos(size);
for (int i = 0; i < randPos.size(); ++i) {
randPos[i] = i;
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(randPos.begin(), randPos.end(), g);
while (state.KeepRunning()) {
for (int pos = 0; pos < randPos.size();) {
REPEAT(*(pStart + randPos[pos++]) = fill;)
}
}
}
template <class Word>
static void BM_WriteSeqIndexThreaded(benchmark::State& state) {
std::vector<char> vec1(state.range(0));
void* const start = vec1.data();
Word* const pStart = static_cast<Word*>(start);
Word fill(0);
size_t size = vec1.size() / sizeof(Word);
std::vector<int> randPos(size);
for (int i = 0; i < randPos.size(); ++i) {
randPos[i] = i;
}
while (state.KeepRunning()) {
for (int pos = 0; pos < randPos.size();) {
REPEAT(*(pStart + randPos[pos++]) = fill;)
}
}
}
#define ARGS(N) ->RangeMultiplier(2)->Range(1024, 8*1024*1024)->Threads(N)->UseRealTime();
// Register the function as a benchmark
BENCHMARK_TEMPLATE(BM_WriteRandThreaded, long) ARGS(1);
BENCHMARK_TEMPLATE(BM_WriteRandThreaded, long) ARGS(2);
BENCHMARK_TEMPLATE(BM_WriteRandThreaded, long) ARGS(4);
BENCHMARK_TEMPLATE(BM_WriteSeqIndexThreaded, long) ARGS(1);
BENCHMARK_TEMPLATE(BM_WriteSeqIndexThreaded, long) ARGS(2);
BENCHMARK_TEMPLATE(BM_WriteSeqIndexThreaded, long) ARGS(4);