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 | #include <x86intrin.h> |
---|
14 | |
---|
15 | // Barrier from |
---|
16 | class barrier_t { |
---|
17 | public: |
---|
18 | barrier_t(size_t total) |
---|
19 | : waiting(0) |
---|
20 | , total(total) |
---|
21 | {} |
---|
22 | |
---|
23 | void wait(unsigned) { |
---|
24 | size_t target = waiting++; |
---|
25 | target = (target - (target % total)) + total; |
---|
26 | while(waiting < target) |
---|
27 | asm volatile("pause"); |
---|
28 | |
---|
29 | assert(waiting < (1ul << 60)); |
---|
30 | } |
---|
31 | |
---|
32 | private: |
---|
33 | std::atomic<size_t> waiting; |
---|
34 | size_t total; |
---|
35 | }; |
---|
36 | |
---|
37 | class Random { |
---|
38 | private: |
---|
39 | unsigned int seed; |
---|
40 | public: |
---|
41 | Random(int seed) { |
---|
42 | this->seed = seed; |
---|
43 | } |
---|
44 | |
---|
45 | /** returns pseudorandom x satisfying 0 <= x < n. **/ |
---|
46 | unsigned int next() { |
---|
47 | seed ^= seed << 6; |
---|
48 | seed ^= seed >> 21; |
---|
49 | seed ^= seed << 7; |
---|
50 | return seed; |
---|
51 | } |
---|
52 | }; |
---|
53 | |
---|
54 | static inline long long rdtscl(void) { |
---|
55 | unsigned int lo, hi; |
---|
56 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); |
---|
57 | return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); |
---|
58 | } |
---|
59 | |
---|
60 | static inline void affinity(int tid) { |
---|
61 | static int cpus = get_nprocs(); |
---|
62 | |
---|
63 | cpu_set_t mask; |
---|
64 | CPU_ZERO(&mask); |
---|
65 | int cpu = cpus - tid; // Set CPU affinity to tid, starting from the end |
---|
66 | CPU_SET(cpu, &mask); |
---|
67 | auto result = sched_setaffinity(0, sizeof(mask), &mask); |
---|
68 | if(result != 0) { |
---|
69 | std::cerr << "Affinity set failed with " << result<< ", wanted " << cpu << std::endl; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | static const constexpr std::size_t cache_line_size = 64; |
---|
74 | static inline void check_cache_line_size() { |
---|
75 | std::cout << "Checking cache line size" << std::endl; |
---|
76 | const std::string cache_file = "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"; |
---|
77 | |
---|
78 | std::ifstream ifs (cache_file, std::ifstream::in); |
---|
79 | |
---|
80 | if(!ifs.good()) { |
---|
81 | std::cerr << "Could not open file to check cache line size" << std::endl; |
---|
82 | std::cerr << "Looking for: " << cache_file << std::endl; |
---|
83 | std::exit(2); |
---|
84 | } |
---|
85 | |
---|
86 | size_t got; |
---|
87 | ifs >> got; |
---|
88 | |
---|
89 | ifs.close(); |
---|
90 | |
---|
91 | if(cache_line_size != got) { |
---|
92 | std::cerr << "Cache line has incorrect size : " << got << std::endl; |
---|
93 | std::exit(1); |
---|
94 | } |
---|
95 | |
---|
96 | std::cout << "Done" << std::endl; |
---|
97 | } |
---|
98 | |
---|
99 | using Clock = std::chrono::high_resolution_clock; |
---|
100 | using duration_t = std::chrono::duration<double>; |
---|
101 | using std::chrono::nanoseconds; |
---|
102 | |
---|
103 | template<typename Ratio, typename T> |
---|
104 | T duration_cast(T seconds) { |
---|
105 | return std::chrono::duration_cast<std::chrono::duration<T, Ratio>>(std::chrono::duration<T>(seconds)).count(); |
---|
106 | } |
---|
107 | |
---|
108 | static inline unsigned rand_bit(unsigned rnum, size_t mask) { |
---|
109 | unsigned bit = mask ? rnum % __builtin_popcountl(mask) : 0; |
---|
110 | #if !defined(__BMI2__) |
---|
111 | uint64_t v = mask; // Input value to find position with rank r. |
---|
112 | unsigned int r = bit + 1;// Input: bit's desired rank [1-64]. |
---|
113 | unsigned int s; // Output: Resulting position of bit with rank r [1-64] |
---|
114 | uint64_t a, b, c, d; // Intermediate temporaries for bit count. |
---|
115 | unsigned int t; // Bit count temporary. |
---|
116 | |
---|
117 | // Do a normal parallel bit count for a 64-bit integer, |
---|
118 | // but store all intermediate steps. |
---|
119 | a = v - ((v >> 1) & ~0UL/3); |
---|
120 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
---|
121 | c = (b + (b >> 4)) & ~0UL/0x11; |
---|
122 | d = (c + (c >> 8)) & ~0UL/0x101; |
---|
123 | |
---|
124 | |
---|
125 | t = (d >> 32) + (d >> 48); |
---|
126 | // Now do branchless select! |
---|
127 | s = 64; |
---|
128 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
---|
129 | t = (d >> (s - 16)) & 0xff; |
---|
130 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
---|
131 | t = (c >> (s - 8)) & 0xf; |
---|
132 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
---|
133 | t = (b >> (s - 4)) & 0x7; |
---|
134 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
---|
135 | t = (a >> (s - 2)) & 0x3; |
---|
136 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
---|
137 | t = (v >> (s - 1)) & 0x1; |
---|
138 | s -= ((t - r) & 256) >> 8; |
---|
139 | return s - 1; |
---|
140 | #else |
---|
141 | uint64_t picked = _pdep_u64(1ul << bit, mask); |
---|
142 | return picked ? __builtin_ctzl(picked) : 0; |
---|
143 | #endif |
---|
144 | } |
---|