[b2a37b0] | 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 | |
---|
[9421f3d8] | 13 | #include <x86intrin.h> |
---|
| 14 | |
---|
[b2a37b0] | 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 | |
---|
[a82a8f4] | 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 | constexpr uint64_t extendedEuclidY(uint64_t a, uint64_t b); |
---|
| 55 | constexpr uint64_t extendedEuclidX(uint64_t a, uint64_t b){ |
---|
| 56 | return (b==0) ? 1 : extendedEuclidY(b, a - b * (a / b)); |
---|
| 57 | } |
---|
| 58 | constexpr uint64_t extendedEuclidY(uint64_t a, uint64_t b){ |
---|
| 59 | return (b==0) ? 0 : extendedEuclidX(b, a - b * (a / b)) - (a / b) * extendedEuclidY(b, a - b * (a / b)); |
---|
| 60 | } |
---|
| 61 | |
---|
[b2a37b0] | 62 | class Random { |
---|
| 63 | private: |
---|
[a82a8f4] | 64 | uint64_t x; |
---|
| 65 | |
---|
| 66 | static constexpr const uint64_t M = 1ul << 48ul; |
---|
| 67 | static constexpr const uint64_t A = 25214903917; |
---|
| 68 | static constexpr const uint64_t C = 11; |
---|
| 69 | static constexpr const uint64_t D = 16; |
---|
| 70 | |
---|
| 71 | public: |
---|
| 72 | static constexpr const uint64_t m = M; |
---|
| 73 | static constexpr const uint64_t a = A; |
---|
| 74 | static constexpr const uint64_t c = C; |
---|
| 75 | static constexpr const uint64_t d = D; |
---|
| 76 | static constexpr const uint64_t ai = extendedEuclidX(A, M); |
---|
[b2a37b0] | 77 | public: |
---|
[c0587193] | 78 | Random(unsigned int seed) { |
---|
[a82a8f4] | 79 | this->x = seed * a; |
---|
[b2a37b0] | 80 | } |
---|
| 81 | |
---|
| 82 | /** returns pseudorandom x satisfying 0 <= x < n. **/ |
---|
| 83 | unsigned int next() { |
---|
[a82a8f4] | 84 | //nextx = (a * x + c) % m; |
---|
| 85 | x = (A * x + C) & (M - 1); |
---|
| 86 | return x >> D; |
---|
| 87 | } |
---|
| 88 | unsigned int prev() { |
---|
| 89 | //prevx = (ainverse * (x - c)) mod m |
---|
| 90 | unsigned int r = x >> D; |
---|
| 91 | x = ai * (x - C) & (M - 1); |
---|
| 92 | return r; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void set_raw_state(uint64_t _x) { |
---|
| 96 | this->x = _x; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | uint64_t get_raw_state() { |
---|
| 100 | return this->x; |
---|
| 101 | } |
---|
[b2a37b0] | 102 | }; |
---|
| 103 | |
---|
| 104 | static inline long long rdtscl(void) { |
---|
| 105 | unsigned int lo, hi; |
---|
| 106 | __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); |
---|
| 107 | return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 ); |
---|
| 108 | } |
---|
| 109 | |
---|
[807a632] | 110 | static inline void affinity(int tid) { |
---|
[b2a37b0] | 111 | static int cpus = get_nprocs(); |
---|
| 112 | |
---|
| 113 | cpu_set_t mask; |
---|
| 114 | CPU_ZERO(&mask); |
---|
| 115 | int cpu = cpus - tid; // Set CPU affinity to tid, starting from the end |
---|
| 116 | CPU_SET(cpu, &mask); |
---|
| 117 | auto result = sched_setaffinity(0, sizeof(mask), &mask); |
---|
| 118 | if(result != 0) { |
---|
| 119 | std::cerr << "Affinity set failed with " << result<< ", wanted " << cpu << std::endl; |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | static const constexpr std::size_t cache_line_size = 64; |
---|
[807a632] | 124 | static inline void check_cache_line_size() { |
---|
[b2a37b0] | 125 | std::cout << "Checking cache line size" << std::endl; |
---|
| 126 | const std::string cache_file = "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"; |
---|
| 127 | |
---|
| 128 | std::ifstream ifs (cache_file, std::ifstream::in); |
---|
| 129 | |
---|
| 130 | if(!ifs.good()) { |
---|
| 131 | std::cerr << "Could not open file to check cache line size" << std::endl; |
---|
| 132 | std::cerr << "Looking for: " << cache_file << std::endl; |
---|
| 133 | std::exit(2); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | size_t got; |
---|
| 137 | ifs >> got; |
---|
| 138 | |
---|
| 139 | ifs.close(); |
---|
| 140 | |
---|
| 141 | if(cache_line_size != got) { |
---|
| 142 | std::cerr << "Cache line has incorrect size : " << got << std::endl; |
---|
| 143 | std::exit(1); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | std::cout << "Done" << std::endl; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | using Clock = std::chrono::high_resolution_clock; |
---|
| 150 | using duration_t = std::chrono::duration<double>; |
---|
| 151 | using std::chrono::nanoseconds; |
---|
| 152 | |
---|
| 153 | template<typename Ratio, typename T> |
---|
| 154 | T duration_cast(T seconds) { |
---|
| 155 | return std::chrono::duration_cast<std::chrono::duration<T, Ratio>>(std::chrono::duration<T>(seconds)).count(); |
---|
[9421f3d8] | 156 | } |
---|
| 157 | |
---|
[47a541d] | 158 | static inline unsigned rand_bit(unsigned rnum, size_t mask) __attribute__((artificial)); |
---|
[807a632] | 159 | static inline unsigned rand_bit(unsigned rnum, size_t mask) { |
---|
| 160 | unsigned bit = mask ? rnum % __builtin_popcountl(mask) : 0; |
---|
| 161 | #if !defined(__BMI2__) |
---|
[9421f3d8] | 162 | uint64_t v = mask; // Input value to find position with rank r. |
---|
[807a632] | 163 | unsigned int r = bit + 1;// Input: bit's desired rank [1-64]. |
---|
[9421f3d8] | 164 | unsigned int s; // Output: Resulting position of bit with rank r [1-64] |
---|
| 165 | uint64_t a, b, c, d; // Intermediate temporaries for bit count. |
---|
| 166 | unsigned int t; // Bit count temporary. |
---|
| 167 | |
---|
| 168 | // Do a normal parallel bit count for a 64-bit integer, |
---|
| 169 | // but store all intermediate steps. |
---|
| 170 | a = v - ((v >> 1) & ~0UL/3); |
---|
| 171 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
---|
| 172 | c = (b + (b >> 4)) & ~0UL/0x11; |
---|
| 173 | d = (c + (c >> 8)) & ~0UL/0x101; |
---|
| 174 | |
---|
| 175 | |
---|
| 176 | t = (d >> 32) + (d >> 48); |
---|
| 177 | // Now do branchless select! |
---|
| 178 | s = 64; |
---|
| 179 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
---|
| 180 | t = (d >> (s - 16)) & 0xff; |
---|
| 181 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
---|
| 182 | t = (c >> (s - 8)) & 0xf; |
---|
| 183 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
---|
| 184 | t = (b >> (s - 4)) & 0x7; |
---|
| 185 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
---|
| 186 | t = (a >> (s - 2)) & 0x3; |
---|
| 187 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
---|
| 188 | t = (v >> (s - 1)) & 0x1; |
---|
| 189 | s -= ((t - r) & 256) >> 8; |
---|
[807a632] | 190 | return s - 1; |
---|
| 191 | #else |
---|
| 192 | uint64_t picked = _pdep_u64(1ul << bit, mask); |
---|
| 193 | return picked ? __builtin_ctzl(picked) : 0; |
---|
| 194 | #endif |
---|
[33e62f1b] | 195 | } |
---|
| 196 | |
---|
| 197 | struct spinlock_t { |
---|
| 198 | std::atomic_bool ll = { false }; |
---|
| 199 | |
---|
| 200 | inline void lock() { |
---|
| 201 | while( __builtin_expect(ll.exchange(true),false) ) { |
---|
| 202 | while(ll.load(std::memory_order_relaxed)) |
---|
| 203 | asm volatile("pause"); |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | inline bool try_lock() { |
---|
| 208 | return false == ll.exchange(true); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | inline void unlock() { |
---|
| 212 | ll.store(false, std::memory_order_release); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | inline explicit operator bool() { |
---|
| 216 | return ll.load(std::memory_order_relaxed); |
---|
| 217 | } |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | static inline bool bts(std::atomic_size_t & target, size_t bit ) { |
---|
| 221 | //* |
---|
| 222 | int result = 0; |
---|
| 223 | asm volatile( |
---|
| 224 | "LOCK btsq %[bit], %[target]\n\t" |
---|
| 225 | :"=@ccc" (result) |
---|
| 226 | : [target] "m" (target), [bit] "r" (bit) |
---|
| 227 | ); |
---|
| 228 | return result != 0; |
---|
| 229 | /*/ |
---|
| 230 | size_t mask = 1ul << bit; |
---|
| 231 | size_t ret = target.fetch_or(mask, std::memory_order_relaxed); |
---|
| 232 | return (ret & mask) != 0; |
---|
| 233 | //*/ |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static inline bool btr(std::atomic_size_t & target, size_t bit ) { |
---|
| 237 | //* |
---|
| 238 | int result = 0; |
---|
| 239 | asm volatile( |
---|
| 240 | "LOCK btrq %[bit], %[target]\n\t" |
---|
| 241 | :"=@ccc" (result) |
---|
| 242 | : [target] "m" (target), [bit] "r" (bit) |
---|
| 243 | ); |
---|
| 244 | return result != 0; |
---|
| 245 | /*/ |
---|
| 246 | size_t mask = 1ul << bit; |
---|
| 247 | size_t ret = target.fetch_and(~mask, std::memory_order_relaxed); |
---|
| 248 | return (ret & mask) != 0; |
---|
| 249 | //*/ |
---|
| 250 | } |
---|