[b2a37b0] | 1 | #pragma once |
---|
[b232745] | 2 | #define LIST_VARIANT relaxed_list |
---|
[0092853] | 3 | |
---|
| 4 | #define VANILLA 0 |
---|
| 5 | #define SNZI 1 |
---|
| 6 | #define BITMASK 2 |
---|
| 7 | #define DISCOVER 3 |
---|
[47a541d] | 8 | #define SNZM 4 |
---|
[b232745] | 9 | #define BIAS 5 |
---|
[a82a8f4] | 10 | #define BACK 6 |
---|
| 11 | #define BACKBIAS 7 |
---|
[8e1b1bb] | 12 | |
---|
| 13 | #ifndef VARIANT |
---|
| 14 | #define VARIANT VANILLA |
---|
| 15 | #endif |
---|
| 16 | |
---|
[50aeb6f] | 17 | #ifndef NO_STATS |
---|
[b2a37b0] | 18 | #include <iostream> |
---|
[50aeb6f] | 19 | #endif |
---|
| 20 | |
---|
[8f4f3e0] | 21 | #include <cmath> |
---|
[2e5fd8b6] | 22 | #include <functional> |
---|
[b2a37b0] | 23 | #include <memory> |
---|
| 24 | #include <mutex> |
---|
[2e5fd8b6] | 25 | #include <thread> |
---|
[b2a37b0] | 26 | #include <type_traits> |
---|
| 27 | |
---|
| 28 | #include "assert.hpp" |
---|
| 29 | #include "utils.hpp" |
---|
[b232745] | 30 | #include "links.hpp" |
---|
[33e62f1b] | 31 | #include "snzi.hpp" |
---|
[f0c3120] | 32 | #include "snzi-packed.hpp" |
---|
[47a541d] | 33 | #include "snzm.hpp" |
---|
[b2a37b0] | 34 | |
---|
| 35 | using namespace std; |
---|
| 36 | |
---|
[50aeb6f] | 37 | struct pick_stat { |
---|
| 38 | struct { |
---|
| 39 | size_t attempt = 0; |
---|
| 40 | size_t success = 0; |
---|
[b232745] | 41 | size_t local = 0; |
---|
[50aeb6f] | 42 | } push; |
---|
| 43 | struct { |
---|
| 44 | size_t attempt = 0; |
---|
| 45 | size_t success = 0; |
---|
[9421f3d8] | 46 | size_t mask_attempt = 0; |
---|
[9da5a50] | 47 | size_t mask_reset = 0; |
---|
[b232745] | 48 | size_t local = 0; |
---|
[9421f3d8] | 49 | } pop; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | struct empty_stat { |
---|
| 53 | struct { |
---|
| 54 | size_t value = 0; |
---|
| 55 | size_t count = 0; |
---|
| 56 | } push; |
---|
| 57 | struct { |
---|
| 58 | size_t value = 0; |
---|
| 59 | size_t count = 0; |
---|
[50aeb6f] | 60 | } pop; |
---|
[b2a37b0] | 61 | }; |
---|
| 62 | |
---|
[50aeb6f] | 63 | template<typename node_t> |
---|
| 64 | class __attribute__((aligned(128))) relaxed_list { |
---|
[b2a37b0] | 65 | static_assert(std::is_same<decltype(node_t::_links), _LinksFields_t<node_t>>::value, "Node must have a links field"); |
---|
| 66 | |
---|
| 67 | public: |
---|
[8e1b1bb] | 68 | static const char * name() { |
---|
[0092853] | 69 | const char * names[] = { |
---|
[b232745] | 70 | "RELAXED: VANILLA", |
---|
| 71 | "RELAXED: SNZI", |
---|
| 72 | "RELAXED: BITMASK", |
---|
| 73 | "RELAXED: SNZI + DISCOVERED MASK", |
---|
| 74 | "RELAXED: SNZI + MASK", |
---|
[a82a8f4] | 75 | "RELAXED: SNZI + LOCAL BIAS", |
---|
| 76 | "RELAXED: SNZI + REVERSE RNG", |
---|
| 77 | "RELAXED: SNZI + LOCAL BIAS + REVERSE RNG" |
---|
[0092853] | 78 | }; |
---|
| 79 | return names[VARIANT]; |
---|
[8e1b1bb] | 80 | } |
---|
| 81 | |
---|
[b232745] | 82 | relaxed_list(unsigned numThreads, unsigned numQueues) |
---|
| 83 | : numLists(numThreads * numQueues) |
---|
| 84 | , lists(new intrusive_queue_t<node_t>[numLists]) |
---|
[f0c3120] | 85 | #if VARIANT == SNZI || VARIANT == BACK |
---|
| 86 | , snzi( std::log2( numLists / (2 * numQueues) ), 2 ) |
---|
| 87 | #elif VARIANT == BIAS || VARIANT == BACKBIAS |
---|
| 88 | #ifdef SNZI_PACKED |
---|
| 89 | , snzi( std::ceil( std::log2(numLists) ) ) |
---|
| 90 | #else |
---|
| 91 | , snzi( std::log2( numLists / (2 * numQueues) ), 2 ) |
---|
| 92 | #endif |
---|
[03045f18] | 93 | #elif VARIANT == SNZM || VARIANT == DISCOVER |
---|
[47a541d] | 94 | , snzm( numLists ) |
---|
[33e62f1b] | 95 | #endif |
---|
[9421f3d8] | 96 | { |
---|
| 97 | assertf(7 * 8 * 8 >= numLists, "List currently only supports 448 sublists"); |
---|
[807a632] | 98 | std::cout << "Constructing Relaxed List with " << numLists << std::endl; |
---|
[9421f3d8] | 99 | } |
---|
[b2a37b0] | 100 | |
---|
[50aeb6f] | 101 | ~relaxed_list() { |
---|
[807a632] | 102 | std::cout << "Destroying Relaxed List" << std::endl; |
---|
[50aeb6f] | 103 | lists.reset(); |
---|
| 104 | } |
---|
[b2a37b0] | 105 | |
---|
[50aeb6f] | 106 | __attribute__((noinline, hot)) void push(node_t * node) { |
---|
| 107 | node->_links.ts = rdtscl(); |
---|
[b2a37b0] | 108 | |
---|
[50aeb6f] | 109 | while(true) { |
---|
| 110 | // Pick a random list |
---|
[a82a8f4] | 111 | unsigned i = idx_from_r(tls.rng1.next(), VARIANT == BIAS || VARIANT == BACKBIAS); |
---|
[50aeb6f] | 112 | |
---|
| 113 | #ifndef NO_STATS |
---|
| 114 | tls.pick.push.attempt++; |
---|
| 115 | #endif |
---|
| 116 | |
---|
| 117 | // If we can't lock it retry |
---|
| 118 | if( !lists[i].lock.try_lock() ) continue; |
---|
[b2a37b0] | 119 | |
---|
[a82a8f4] | 120 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 121 | __attribute__((unused)) int num = numNonEmpty; |
---|
| 122 | #endif |
---|
[9421f3d8] | 123 | |
---|
[50aeb6f] | 124 | // Actually push it |
---|
[9421f3d8] | 125 | if(lists[i].push(node)) { |
---|
[8e1b1bb] | 126 | #if VARIANT == DISCOVER |
---|
[9da5a50] | 127 | size_t qword = i >> 6ull; |
---|
| 128 | size_t bit = i & 63ull; |
---|
| 129 | assert(qword == 0); |
---|
| 130 | bts(tls.mask, bit); |
---|
[03045f18] | 131 | snzm.arrive(i); |
---|
[b232745] | 132 | #elif VARIANT == SNZI || VARIANT == BIAS |
---|
[33e62f1b] | 133 | snzi.arrive(i); |
---|
[a82a8f4] | 134 | #elif VARIANT == BACK || VARIANT == BACKBIAS |
---|
| 135 | snzi.arrive(i); |
---|
| 136 | tls.rng2.set_raw_state( tls.rng1.get_raw_state()); |
---|
[47a541d] | 137 | #elif VARIANT == SNZM |
---|
| 138 | snzm.arrive(i); |
---|
[8e1b1bb] | 139 | #elif VARIANT == BITMASK |
---|
[9da5a50] | 140 | numNonEmpty++; |
---|
| 141 | size_t qword = i >> 6ull; |
---|
| 142 | size_t bit = i & 63ull; |
---|
| 143 | assertf((list_mask[qword] & (1ul << bit)) == 0, "Before set %zu:%zu (%u), %zx & %zx", qword, bit, i, list_mask[qword].load(), (1ul << bit)); |
---|
| 144 | __attribute__((unused)) bool ret = bts(list_mask[qword], bit); |
---|
| 145 | assert(!ret); |
---|
| 146 | assertf((list_mask[qword] & (1ul << bit)) != 0, "After set %zu:%zu (%u), %zx & %zx", qword, bit, i, list_mask[qword].load(), (1ul << bit)); |
---|
| 147 | #else |
---|
| 148 | numNonEmpty++; |
---|
| 149 | #endif |
---|
[9421f3d8] | 150 | } |
---|
[a82a8f4] | 151 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 152 | assert(numNonEmpty <= (int)numLists); |
---|
| 153 | #endif |
---|
[50aeb6f] | 154 | |
---|
| 155 | // Unlock and return |
---|
| 156 | lists[i].lock.unlock(); |
---|
| 157 | |
---|
| 158 | #ifndef NO_STATS |
---|
| 159 | tls.pick.push.success++; |
---|
[a82a8f4] | 160 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 161 | tls.empty.push.value += num; |
---|
| 162 | tls.empty.push.count += 1; |
---|
| 163 | #endif |
---|
[50aeb6f] | 164 | #endif |
---|
| 165 | return; |
---|
| 166 | } |
---|
[b2a37b0] | 167 | } |
---|
| 168 | |
---|
[50aeb6f] | 169 | __attribute__((noinline, hot)) node_t * pop() { |
---|
[8e1b1bb] | 170 | #if VARIANT == DISCOVER |
---|
[9da5a50] | 171 | assert(numLists <= 64); |
---|
[03045f18] | 172 | while(snzm.query()) { |
---|
[9da5a50] | 173 | tls.pick.pop.mask_attempt++; |
---|
| 174 | unsigned i, j; |
---|
| 175 | { |
---|
| 176 | // Pick first list totally randomly |
---|
[a82a8f4] | 177 | i = tls.rng1.next() % numLists; |
---|
[9da5a50] | 178 | |
---|
| 179 | // Pick the other according to the bitmask |
---|
[a82a8f4] | 180 | unsigned r = tls.rng1.next(); |
---|
[9da5a50] | 181 | |
---|
| 182 | size_t mask = tls.mask.load(std::memory_order_relaxed); |
---|
| 183 | if(mask == 0) { |
---|
| 184 | tls.pick.pop.mask_reset++; |
---|
| 185 | mask = (1U << numLists) - 1; |
---|
[03045f18] | 186 | tls.mask.store(mask, std::memory_order_relaxed); |
---|
[9da5a50] | 187 | } |
---|
| 188 | |
---|
| 189 | unsigned b = rand_bit(r, mask); |
---|
| 190 | |
---|
| 191 | assertf(b < 64, "%zu %u", mask, b); |
---|
| 192 | |
---|
| 193 | j = b; |
---|
| 194 | |
---|
| 195 | assert(j < numLists); |
---|
| 196 | } |
---|
| 197 | |
---|
[33e62f1b] | 198 | if(auto node = try_pop(i, j)) return node; |
---|
| 199 | } |
---|
[8e1b1bb] | 200 | #elif VARIANT == SNZI |
---|
[33e62f1b] | 201 | while(snzi.query()) { |
---|
| 202 | // Pick two lists at random |
---|
[a82a8f4] | 203 | int i = tls.rng1.next() % numLists; |
---|
| 204 | int j = tls.rng1.next() % numLists; |
---|
| 205 | |
---|
| 206 | if(auto node = try_pop(i, j)) return node; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | #elif VARIANT == BACK |
---|
| 210 | while(snzi.query()) { |
---|
| 211 | // Pick two lists at random |
---|
| 212 | int i = tls.rng2.prev() % numLists; |
---|
| 213 | int j = tls.rng2.prev() % numLists; |
---|
| 214 | |
---|
| 215 | if(auto node = try_pop(i, j)) return node; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | #elif VARIANT == BACKBIAS |
---|
| 219 | while(snzi.query()) { |
---|
| 220 | // Pick two lists at random |
---|
| 221 | int i = idx_from_r(tls.rng2.prev(), true); |
---|
| 222 | int j = idx_from_r(tls.rng2.prev(), true); |
---|
[b232745] | 223 | |
---|
| 224 | if(auto node = try_pop(i, j)) return node; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | #elif VARIANT == BIAS |
---|
| 228 | while(snzi.query()) { |
---|
| 229 | // Pick two lists at random |
---|
[a82a8f4] | 230 | unsigned ri = tls.rng1.next(); |
---|
[b232745] | 231 | unsigned i; |
---|
[a82a8f4] | 232 | unsigned j = tls.rng1.next(); |
---|
[b232745] | 233 | if(0 == (ri & 0xF)) { |
---|
| 234 | i = (ri >> 4) % numLists; |
---|
| 235 | } else { |
---|
| 236 | i = tls.my_queue + ((ri >> 4) % 4); |
---|
| 237 | j = tls.my_queue + ((j >> 4) % 4); |
---|
| 238 | tls.pick.pop.local++; |
---|
| 239 | } |
---|
| 240 | i %= numLists; |
---|
| 241 | j %= numLists; |
---|
[33e62f1b] | 242 | |
---|
[47a541d] | 243 | if(auto node = try_pop(i, j)) return node; |
---|
| 244 | } |
---|
| 245 | #elif VARIANT == SNZM |
---|
[03045f18] | 246 | //* |
---|
[47a541d] | 247 | while(snzm.query()) { |
---|
| 248 | tls.pick.pop.mask_attempt++; |
---|
| 249 | unsigned i, j; |
---|
| 250 | { |
---|
| 251 | // Pick two random number |
---|
[a82a8f4] | 252 | unsigned ri = tls.rng1.next(); |
---|
| 253 | unsigned rj = tls.rng1.next(); |
---|
[47a541d] | 254 | |
---|
| 255 | // Pick two nodes from it |
---|
| 256 | unsigned wdxi = ri & snzm.mask; |
---|
[b232745] | 257 | // unsigned wdxj = rj & snzm.mask; |
---|
[47a541d] | 258 | |
---|
| 259 | // Get the masks from the nodes |
---|
[b232745] | 260 | // size_t maski = snzm.masks(wdxi); |
---|
[47a541d] | 261 | size_t maskj = snzm.masks(wdxj); |
---|
| 262 | |
---|
| 263 | if(maski == 0 && maskj == 0) continue; |
---|
| 264 | |
---|
[5f259f3] | 265 | #if defined(__BMI2__) |
---|
| 266 | uint64_t idxsi = _pext_u64(snzm.indexes, maski); |
---|
[b232745] | 267 | // uint64_t idxsj = _pext_u64(snzm.indexes, maskj); |
---|
[47a541d] | 268 | |
---|
[5f259f3] | 269 | auto pi = __builtin_popcountll(maski); |
---|
[b232745] | 270 | // auto pj = __builtin_popcountll(maskj); |
---|
[5f259f3] | 271 | |
---|
| 272 | ri = pi ? ri & ((pi >> 3) - 1) : 0; |
---|
| 273 | rj = pj ? rj & ((pj >> 3) - 1) : 0; |
---|
| 274 | |
---|
| 275 | unsigned bi = (idxsi >> (ri << 3)) & 0xff; |
---|
| 276 | unsigned bj = (idxsj >> (rj << 3)) & 0xff; |
---|
| 277 | #else |
---|
| 278 | unsigned bi = rand_bit(ri >> snzm.depth, maski); |
---|
| 279 | unsigned bj = rand_bit(rj >> snzm.depth, maskj); |
---|
| 280 | #endif |
---|
[47a541d] | 281 | |
---|
| 282 | i = (bi << snzm.depth) | wdxi; |
---|
| 283 | j = (bj << snzm.depth) | wdxj; |
---|
| 284 | |
---|
[5f259f3] | 285 | /* paranoid */ assertf(i < numLists, "%u %u", bj, wdxi); |
---|
| 286 | /* paranoid */ assertf(j < numLists, "%u %u", bj, wdxj); |
---|
[47a541d] | 287 | } |
---|
| 288 | |
---|
[9da5a50] | 289 | if(auto node = try_pop(i, j)) return node; |
---|
| 290 | } |
---|
[03045f18] | 291 | /*/ |
---|
| 292 | while(snzm.query()) { |
---|
| 293 | // Pick two lists at random |
---|
[a82a8f4] | 294 | int i = tls.rng1.next() % numLists; |
---|
| 295 | int j = tls.rng1.next() % numLists; |
---|
[03045f18] | 296 | |
---|
| 297 | if(auto node = try_pop(i, j)) return node; |
---|
| 298 | } |
---|
| 299 | //*/ |
---|
[8e1b1bb] | 300 | #elif VARIANT == BITMASK |
---|
[9421f3d8] | 301 | int nnempty; |
---|
| 302 | while(0 != (nnempty = numNonEmpty)) { |
---|
[807a632] | 303 | tls.pick.pop.mask_attempt++; |
---|
[9421f3d8] | 304 | unsigned i, j; |
---|
[807a632] | 305 | { |
---|
[9421f3d8] | 306 | // Pick two lists at random |
---|
| 307 | unsigned num = ((numLists - 1) >> 6) + 1; |
---|
| 308 | |
---|
[a82a8f4] | 309 | unsigned ri = tls.rng1.next(); |
---|
| 310 | unsigned rj = tls.rng1.next(); |
---|
[9421f3d8] | 311 | |
---|
| 312 | unsigned wdxi = (ri >> 6u) % num; |
---|
| 313 | unsigned wdxj = (rj >> 6u) % num; |
---|
| 314 | |
---|
| 315 | size_t maski = list_mask[wdxi].load(std::memory_order_relaxed); |
---|
| 316 | size_t maskj = list_mask[wdxj].load(std::memory_order_relaxed); |
---|
[50aeb6f] | 317 | |
---|
[9421f3d8] | 318 | if(maski == 0 && maskj == 0) continue; |
---|
[50aeb6f] | 319 | |
---|
[807a632] | 320 | unsigned bi = rand_bit(ri, maski); |
---|
| 321 | unsigned bj = rand_bit(rj, maskj); |
---|
[50aeb6f] | 322 | |
---|
[807a632] | 323 | assertf(bi < 64, "%zu %u", maski, bi); |
---|
| 324 | assertf(bj < 64, "%zu %u", maskj, bj); |
---|
[9421f3d8] | 325 | |
---|
| 326 | i = bi | (wdxi << 6); |
---|
| 327 | j = bj | (wdxj << 6); |
---|
| 328 | |
---|
| 329 | assertf(i < numLists, "%u", wdxi << 6); |
---|
| 330 | assertf(j < numLists, "%u", wdxj << 6); |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | if(auto node = try_pop(i, j)) return node; |
---|
[50aeb6f] | 334 | } |
---|
[9421f3d8] | 335 | #else |
---|
| 336 | while(numNonEmpty != 0) { |
---|
| 337 | // Pick two lists at random |
---|
[a82a8f4] | 338 | int i = tls.rng1.next() % numLists; |
---|
| 339 | int j = tls.rng1.next() % numLists; |
---|
[50aeb6f] | 340 | |
---|
[9421f3d8] | 341 | if(auto node = try_pop(i, j)) return node; |
---|
| 342 | } |
---|
| 343 | #endif |
---|
[50aeb6f] | 344 | |
---|
[9421f3d8] | 345 | return nullptr; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | private: |
---|
| 349 | node_t * try_pop(unsigned i, unsigned j) { |
---|
| 350 | #ifndef NO_STATS |
---|
| 351 | tls.pick.pop.attempt++; |
---|
| 352 | #endif |
---|
| 353 | |
---|
[8e1b1bb] | 354 | #if VARIANT == DISCOVER |
---|
[9da5a50] | 355 | if(lists[i].ts() > 0) bts(tls.mask, i); else btr(tls.mask, i); |
---|
| 356 | if(lists[j].ts() > 0) bts(tls.mask, j); else btr(tls.mask, j); |
---|
| 357 | #endif |
---|
| 358 | |
---|
[9421f3d8] | 359 | // Pick the bet list |
---|
| 360 | int w = i; |
---|
| 361 | if( __builtin_expect(lists[j].ts() != 0, true) ) { |
---|
| 362 | w = (lists[i].ts() < lists[j].ts()) ? i : j; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | auto & list = lists[w]; |
---|
| 366 | // If list looks empty retry |
---|
| 367 | if( list.ts() == 0 ) return nullptr; |
---|
| 368 | |
---|
| 369 | // If we can't get the lock retry |
---|
| 370 | if( !list.lock.try_lock() ) return nullptr; |
---|
| 371 | |
---|
[a82a8f4] | 372 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 373 | __attribute__((unused)) int num = numNonEmpty; |
---|
| 374 | #endif |
---|
[9421f3d8] | 375 | |
---|
| 376 | // If list is empty, unlock and retry |
---|
| 377 | if( list.ts() == 0 ) { |
---|
[50aeb6f] | 378 | list.lock.unlock(); |
---|
[9421f3d8] | 379 | return nullptr; |
---|
[b2a37b0] | 380 | } |
---|
| 381 | |
---|
[9421f3d8] | 382 | // Actually pop the list |
---|
| 383 | node_t * node; |
---|
| 384 | bool emptied; |
---|
| 385 | std::tie(node, emptied) = list.pop(); |
---|
| 386 | assert(node); |
---|
| 387 | |
---|
| 388 | if(emptied) { |
---|
[8e1b1bb] | 389 | #if VARIANT == DISCOVER |
---|
[9da5a50] | 390 | size_t qword = w >> 6ull; |
---|
| 391 | size_t bit = w & 63ull; |
---|
| 392 | assert(qword == 0); |
---|
| 393 | __attribute__((unused)) bool ret = btr(tls.mask, bit); |
---|
[03045f18] | 394 | snzm.depart(w); |
---|
[a82a8f4] | 395 | #elif VARIANT == SNZI || VARIANT == BIAS || VARIANT == BACK || VARIANT == BACKBIAS |
---|
[33e62f1b] | 396 | snzi.depart(w); |
---|
[47a541d] | 397 | #elif VARIANT == SNZM |
---|
| 398 | snzm.depart(w); |
---|
[8e1b1bb] | 399 | #elif VARIANT == BITMASK |
---|
[9da5a50] | 400 | numNonEmpty--; |
---|
| 401 | size_t qword = w >> 6ull; |
---|
| 402 | size_t bit = w & 63ull; |
---|
| 403 | assert((list_mask[qword] & (1ul << bit)) != 0); |
---|
| 404 | __attribute__((unused)) bool ret = btr(list_mask[qword], bit); |
---|
| 405 | assert(ret); |
---|
| 406 | assert((list_mask[qword] & (1ul << bit)) == 0); |
---|
| 407 | #else |
---|
| 408 | numNonEmpty--; |
---|
| 409 | #endif |
---|
[9421f3d8] | 410 | } |
---|
| 411 | |
---|
| 412 | // Unlock and return |
---|
| 413 | list.lock.unlock(); |
---|
[a82a8f4] | 414 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 415 | assert(numNonEmpty >= 0); |
---|
| 416 | #endif |
---|
[9421f3d8] | 417 | #ifndef NO_STATS |
---|
| 418 | tls.pick.pop.success++; |
---|
[a82a8f4] | 419 | #if VARIANT == VANILLA || VARIANT == BITMASK |
---|
[33e62f1b] | 420 | tls.empty.pop.value += num; |
---|
| 421 | tls.empty.pop.count += 1; |
---|
| 422 | #endif |
---|
[9421f3d8] | 423 | #endif |
---|
| 424 | return node; |
---|
| 425 | } |
---|
[b2a37b0] | 426 | |
---|
[a82a8f4] | 427 | inline unsigned idx_from_r(unsigned r, bool bias) { |
---|
| 428 | unsigned i; |
---|
| 429 | if(bias) { |
---|
[f0c3120] | 430 | if(0 == (r & 0x3F)) { |
---|
| 431 | i = r >> 6; |
---|
[a82a8f4] | 432 | } else { |
---|
[f0c3120] | 433 | i = tls.my_queue + ((r >> 6) % 4); |
---|
[a82a8f4] | 434 | tls.pick.push.local++; |
---|
| 435 | } |
---|
| 436 | } else { |
---|
| 437 | i = r; |
---|
| 438 | } |
---|
| 439 | return i % numLists; |
---|
| 440 | } |
---|
[b2a37b0] | 441 | |
---|
[50aeb6f] | 442 | public: |
---|
[b2a37b0] | 443 | |
---|
[50aeb6f] | 444 | static __attribute__((aligned(128))) thread_local struct TLS { |
---|
[2e5fd8b6] | 445 | Random rng1 = { unsigned(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ rdtscl()) }; |
---|
| 446 | Random rng2 = { unsigned(std::hash<std::thread::id>{}(std::this_thread::get_id()) ^ rdtscl()) }; |
---|
[b232745] | 447 | unsigned my_queue = (ticket++) * 4; |
---|
[9421f3d8] | 448 | pick_stat pick; |
---|
| 449 | empty_stat empty; |
---|
[9da5a50] | 450 | __attribute__((aligned(64))) std::atomic_size_t mask = { 0 }; |
---|
[50aeb6f] | 451 | } tls; |
---|
[b2a37b0] | 452 | |
---|
| 453 | private: |
---|
[50aeb6f] | 454 | const unsigned numLists; |
---|
[b232745] | 455 | __attribute__((aligned(64))) std::unique_ptr<intrusive_queue_t<node_t> []> lists; |
---|
[33e62f1b] | 456 | private: |
---|
[f0c3120] | 457 | #if VARIANT == SNZI || VARIANT == BACK |
---|
[33e62f1b] | 458 | snzi_t snzi; |
---|
[f0c3120] | 459 | #elif VARIANT == BIAS || VARIANT == BACKBIAS |
---|
| 460 | #ifdef SNZI_PACKED |
---|
| 461 | snzip_t snzi; |
---|
| 462 | #else |
---|
| 463 | snzi_t snzi; |
---|
| 464 | #endif |
---|
[03045f18] | 465 | #elif VARIANT == SNZM || VARIANT == DISCOVER |
---|
[47a541d] | 466 | snzm_t snzm; |
---|
[33e62f1b] | 467 | #else |
---|
| 468 | std::atomic_int numNonEmpty = { 0 }; // number of non-empty lists |
---|
[8e1b1bb] | 469 | #endif |
---|
| 470 | #if VARIANT == BITMASK |
---|
[33e62f1b] | 471 | std::atomic_size_t list_mask[7] = { {0}, {0}, {0}, {0}, {0}, {0}, {0} }; // which queues are empty |
---|
| 472 | #endif |
---|
[b2a37b0] | 473 | |
---|
[50aeb6f] | 474 | public: |
---|
[b232745] | 475 | static const constexpr size_t sizeof_queue = sizeof(intrusive_queue_t<node_t>); |
---|
| 476 | static std::atomic_uint32_t ticket; |
---|
[807a632] | 477 | |
---|
| 478 | #ifndef NO_STATS |
---|
| 479 | static void stats_tls_tally() { |
---|
| 480 | global_stats.pick.push.attempt += tls.pick.push.attempt; |
---|
| 481 | global_stats.pick.push.success += tls.pick.push.success; |
---|
[b232745] | 482 | global_stats.pick.push.local += tls.pick.push.local; |
---|
[807a632] | 483 | global_stats.pick.pop .attempt += tls.pick.pop.attempt; |
---|
| 484 | global_stats.pick.pop .success += tls.pick.pop.success; |
---|
| 485 | global_stats.pick.pop .mask_attempt += tls.pick.pop.mask_attempt; |
---|
[9da5a50] | 486 | global_stats.pick.pop .mask_reset += tls.pick.pop.mask_reset; |
---|
[b232745] | 487 | global_stats.pick.pop .local += tls.pick.pop.local; |
---|
[807a632] | 488 | |
---|
| 489 | global_stats.qstat.push.value += tls.empty.push.value; |
---|
| 490 | global_stats.qstat.push.count += tls.empty.push.count; |
---|
| 491 | global_stats.qstat.pop .value += tls.empty.pop .value; |
---|
| 492 | global_stats.qstat.pop .count += tls.empty.pop .count; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | private: |
---|
| 496 | static struct GlobalStats { |
---|
| 497 | struct { |
---|
| 498 | struct { |
---|
| 499 | std::atomic_size_t attempt = { 0 }; |
---|
| 500 | std::atomic_size_t success = { 0 }; |
---|
[b232745] | 501 | std::atomic_size_t local = { 0 }; |
---|
[807a632] | 502 | } push; |
---|
| 503 | struct { |
---|
| 504 | std::atomic_size_t attempt = { 0 }; |
---|
| 505 | std::atomic_size_t success = { 0 }; |
---|
| 506 | std::atomic_size_t mask_attempt = { 0 }; |
---|
[9da5a50] | 507 | std::atomic_size_t mask_reset = { 0 }; |
---|
[b232745] | 508 | std::atomic_size_t local = { 0 }; |
---|
[807a632] | 509 | } pop; |
---|
| 510 | } pick; |
---|
| 511 | struct { |
---|
| 512 | struct { |
---|
| 513 | std::atomic_size_t value = { 0 }; |
---|
| 514 | std::atomic_size_t count = { 0 }; |
---|
| 515 | } push; |
---|
| 516 | struct { |
---|
| 517 | std::atomic_size_t value = { 0 }; |
---|
| 518 | std::atomic_size_t count = { 0 }; |
---|
| 519 | } pop; |
---|
| 520 | } qstat; |
---|
| 521 | } global_stats; |
---|
| 522 | |
---|
[b232745] | 523 | public: |
---|
| 524 | static void stats_print(std::ostream & os ) { |
---|
[807a632] | 525 | std::cout << "----- Relaxed List Stats -----" << std::endl; |
---|
| 526 | |
---|
| 527 | const auto & global = global_stats; |
---|
| 528 | |
---|
| 529 | double push_sur = (100.0 * double(global.pick.push.success) / global.pick.push.attempt); |
---|
| 530 | double pop_sur = (100.0 * double(global.pick.pop .success) / global.pick.pop .attempt); |
---|
| 531 | double mpop_sur = (100.0 * double(global.pick.pop .success) / global.pick.pop .mask_attempt); |
---|
[03045f18] | 532 | double rpop_sur = (100.0 * double(global.pick.pop .success) / global.pick.pop .mask_reset); |
---|
| 533 | |
---|
| 534 | double push_len = double(global.pick.push.attempt ) / global.pick.push.success; |
---|
| 535 | double pop_len = double(global.pick.pop .attempt ) / global.pick.pop .success; |
---|
| 536 | double mpop_len = double(global.pick.pop .mask_attempt) / global.pick.pop .success; |
---|
| 537 | double rpop_len = double(global.pick.pop .mask_reset ) / global.pick.pop .success; |
---|
[807a632] | 538 | |
---|
[03045f18] | 539 | os << "Push Pick : " << push_sur << " %, len " << push_len << " (" << global.pick.push.attempt << " / " << global.pick.push.success << ")\n"; |
---|
| 540 | os << "Pop Pick : " << pop_sur << " %, len " << pop_len << " (" << global.pick.pop .attempt << " / " << global.pick.pop .success << ")\n"; |
---|
| 541 | os << "TryPop Pick : " << mpop_sur << " %, len " << mpop_len << " (" << global.pick.pop .mask_attempt << " / " << global.pick.pop .success << ")\n"; |
---|
| 542 | os << "Pop M Reset : " << rpop_sur << " %, len " << rpop_len << " (" << global.pick.pop .mask_reset << " / " << global.pick.pop .success << ")\n"; |
---|
[807a632] | 543 | |
---|
| 544 | double avgQ_push = double(global.qstat.push.value) / global.qstat.push.count; |
---|
| 545 | double avgQ_pop = double(global.qstat.pop .value) / global.qstat.pop .count; |
---|
| 546 | double avgQ = double(global.qstat.push.value + global.qstat.pop .value) / (global.qstat.push.count + global.qstat.pop .count); |
---|
| 547 | os << "Push Avg Qs : " << avgQ_push << " (" << global.qstat.push.count << "ops)\n"; |
---|
| 548 | os << "Pop Avg Qs : " << avgQ_pop << " (" << global.qstat.pop .count << "ops)\n"; |
---|
| 549 | os << "Global Avg Qs : " << avgQ << " (" << (global.qstat.push.count + global.qstat.pop .count) << "ops)\n"; |
---|
[b232745] | 550 | |
---|
| 551 | os << "Local Push : " << global.pick.push.local << "\n"; |
---|
| 552 | os << "Local Pop : " << global.pick.pop .local << "\n"; |
---|
[807a632] | 553 | } |
---|
| 554 | #endif |
---|
[50aeb6f] | 555 | }; |
---|