1 | #pragma once |
---|
2 | |
---|
3 | #include <cassert> |
---|
4 | #include <cstddef> |
---|
5 | #include <atomic> |
---|
6 | #include <chrono> |
---|
7 | #include <fstream> |
---|
8 | #include <iostream> |
---|
9 | |
---|
10 | #include <unistd.h> |
---|
11 | #include <sys/sysinfo.h> |
---|
12 | |
---|
13 | // Barrier from |
---|
14 | class barrier_t { |
---|
15 | public: |
---|
16 | barrier_t(size_t total) |
---|
17 | : waiting(0) |
---|
18 | , total(total) |
---|
19 | {} |
---|
20 | |
---|
21 | void wait(unsigned) { |
---|
22 | size_t target = waiting++; |
---|
23 | target = (target - (target % total)) + total; |
---|
24 | while(waiting < target) |
---|
25 | asm volatile("pause"); |
---|
26 | |
---|
27 | assert(waiting < (1ul << 60)); |
---|
28 | } |
---|
29 | |
---|
30 | private: |
---|
31 | std::atomic<size_t> waiting; |
---|
32 | size_t total; |
---|
33 | }; |
---|
34 | |
---|
35 | class Random { |
---|
36 | private: |
---|
37 | unsigned int seed; |
---|
38 | public: |
---|
39 | Random(int seed) { |
---|
40 | this->seed = seed; |
---|
41 | } |
---|
42 | |
---|
43 | /** returns pseudorandom x satisfying 0 <= x < n. **/ |
---|
44 | unsigned int next() { |
---|
45 | seed ^= seed << 6; |
---|
46 | seed ^= seed >> 21; |
---|
47 | seed ^= seed << 7; |
---|
48 | return seed; |
---|
49 | } |
---|
50 | }; |
---|
51 | |
---|
52 | static inline long long rdtscl(void) { |
---|
53 | unsigned int lo, hi; |
---|
54 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); |
---|
55 | return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); |
---|
56 | } |
---|
57 | |
---|
58 | void affinity(int tid) { |
---|
59 | static int cpus = get_nprocs(); |
---|
60 | |
---|
61 | cpu_set_t mask; |
---|
62 | CPU_ZERO(&mask); |
---|
63 | int cpu = cpus - tid; // Set CPU affinity to tid, starting from the end |
---|
64 | CPU_SET(cpu, &mask); |
---|
65 | auto result = sched_setaffinity(0, sizeof(mask), &mask); |
---|
66 | if(result != 0) { |
---|
67 | std::cerr << "Affinity set failed with " << result<< ", wanted " << cpu << std::endl; |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | static const constexpr std::size_t cache_line_size = 64; |
---|
72 | void check_cache_line_size() { |
---|
73 | std::cout << "Checking cache line size" << std::endl; |
---|
74 | const std::string cache_file = "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"; |
---|
75 | |
---|
76 | std::ifstream ifs (cache_file, std::ifstream::in); |
---|
77 | |
---|
78 | if(!ifs.good()) { |
---|
79 | std::cerr << "Could not open file to check cache line size" << std::endl; |
---|
80 | std::cerr << "Looking for: " << cache_file << std::endl; |
---|
81 | std::exit(2); |
---|
82 | } |
---|
83 | |
---|
84 | size_t got; |
---|
85 | ifs >> got; |
---|
86 | |
---|
87 | ifs.close(); |
---|
88 | |
---|
89 | if(cache_line_size != got) { |
---|
90 | std::cerr << "Cache line has incorrect size : " << got << std::endl; |
---|
91 | std::exit(1); |
---|
92 | } |
---|
93 | |
---|
94 | std::cout << "Done" << std::endl; |
---|
95 | } |
---|
96 | |
---|
97 | using Clock = std::chrono::high_resolution_clock; |
---|
98 | using duration_t = std::chrono::duration<double>; |
---|
99 | using std::chrono::nanoseconds; |
---|
100 | |
---|
101 | template<typename Ratio, typename T> |
---|
102 | T duration_cast(T seconds) { |
---|
103 | return std::chrono::duration_cast<std::chrono::duration<T, Ratio>>(std::chrono::duration<T>(seconds)).count(); |
---|
104 | } |
---|