|
ADS
Algorithms and Data Structures
|
A hash table that many threads read and grow at once, beyond the book: open addressing with linear probing on a vector of atomic words of 64 bits, the folklore table that Maier, Sanders and Dementiev found the fastest to read, made to grow while the threads work and to keep its pairs where they are. More...
#include "ADS/Abi.h"#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/dictionary/FastHash.h"#include "ADS/memory/HugePageAllocator.h"#include <atomic>#include <concepts>#include <cstddef>#include <cstdint>#include <functional>#include <limits>#include <memory>#include <optional>#include <type_traits>#include <utility>#include <vector>#include "ADS/concurrent/impl/ConcurrentFlatTable.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::ZeroingClear |
| the clear() of ConcurrentFlatTable that writes 0 to every place, in time linear in its capacity; the default More... | |
| struct | ADS::EpochClear< S > |
| the clear() of ConcurrentFlatTable in \( O( 1 ) \) by epochs: a stamp of the unsigned type \( S \) per group of 8 places, the group in use only if its stamp is the epoch that clear() raises More... | |
| class | ADS::ConcurrentFlatTable< K, V, H, Eq, C > |
| a DICTIONARY shared by threads, by linear probing on atomic words, that grows while they work and never removes; cleared in \( O( 1 ) \) by the policy EpochClear More... | |
| struct | ADS::ConcurrentFlatTable< K, V, H, Eq, C >::Entry |
| a pair of a table neither packed nor inlined, in a node that never moves More... | |
Namespaces | |
| namespace | ADS |
Concepts | |
| concept | ADS::FlatClear |
| a policy of clear() of ConcurrentFlatTable | |
A hash table that many threads read and grow at once, beyond the book: open addressing with linear probing on a vector of atomic words of 64 bits, the folklore table that Maier, Sanders and Dementiev found the fastest to read, made to grow while the threads work and to keep its pairs where they are.
It is meant as the CLOSED set of a parallel search: findOrInsert( k , h , v ) takes a key with its hash of 64 bits, computed once by the caller, and either finds the key or inserts it with the value \( v \), returning in both cases the pair that the table holds, so that the threads that reach the same state at once agree on one pair and only one of them is told that it inserted it.
A word of the vector is 0 when its place is free, and is written once, by a compare-and-swap from 0, never changed afterwards but by update(): there are no removals. The pairs are kept in one of three ways, chosen by their types and by the processor.
The place of a key with the hash \( h \) in a vector of \( 2^l \) words is the top \( l \) bits of \( h \). When the pairs lie in the words (packed or inlined), each place has also a tag of a byte, 0 or 7 bits of the hash of its key with the high bit set, kept apart from the words, 8 to an atomic word (the metadata of the tables of Swiss and F14 kind, a byte per place). A lookup reads the tags from the place on, 8 at a time, compares them all with its own in one step, reads only the words of the places whose tag matches, and stops at the first tag 0: an absent key costs a line of the tags (\( 1/8 \) of the words, or less, which the cache keeps) and one branch, so that the misses of consecutive lookups overlap, while the line of the words of the place, fetched ahead as the tags are read, makes a key that is there cost about one miss. The pairs in nodes have no tags: their lookups read the words from the place on, up to the key or a word 0, since a tag would add a miss to the two of a key found. Its reads are atomic loads with acquire: no lock, no read-modify-write, no store, and no check of versions, since a word or a tag, once written, stays. A tag is written after the word it tags, by the insertion or by any thread that passes the place while inserting, with the same value: a lookup that stops at a tag 0 stops before every key whose insertion has taken effect, and a lookup that ends there reads the vector in use again, and starts over on the new one if the table grew meanwhile (see Linear probing that grows). A lookup is thus wait-free when the table does not grow.
The insertions (and update()) exclude a growth, and the growth excludes them, by a protocol of Dekker over 64 counters on lines of their own, as in the growth of Maier et al. that synchronizes the threads: a thread adds 1 to the counter of its own (a sequentially consistent read-modify-write) and then reads the flag of the growth, while the thread that grows the table raises the flag and then reads the counters; in the single order of the sequentially consistent accesses one of the two comes first, and the other sees it, so that no insertion runs on a vector that is being copied. The thread that grows the table waits for the counters to drop to 0, allocates a vector twice as large, and the threads that come to insert meanwhile help it copy the old vector in blocks of 4096 words, claimed by a counter; the new vector replaces the old one when all the blocks are copied. The lookups never wait: they read the old vector, which no longer changes, until the new one is in use. The vectors replaced are kept until the table is destroyed, since a reader may still be on one; with sizes that double they occupy less than the vector in use.
The table grows when its pairs pass half its places, a load at which a lookup of linear probing reads 1.5 words when it succeeds and 2.5 when it fails, on average, most of them on the same line (Knuth); the pairs are counted on the 64 counters above, the count in their upper 48 bits, so that the insertion that leaves the critical section counts its pair in the same read-modify-write, and the sum, which reads 64 lines, is taken once per 1024 insertions of a counter (fewer in a vector of less than \( 2^{20} \) places, whose half 64 counters would otherwise pass by much). An insertion that reads more than 128 words also grows the table, if it is at least one eighth full; a table less full than that, which only keys of equal hash can make so crowded, is read on to its first free place instead (see Linear probing that grows).
clear() writes 0 to every place, in \( O( 2^l ) \) time, unless the policy C of the table is EpochClear< S >, which makes it \( O( 1 ) \) for a table cleared often and filled little in between (the restarts of an incremental search): each group of 8 places has a stamp of the type \( S \) (32 bits by default, half a byte per place), and the group is in use only if its stamp is the epoch of the table, which clear() raises, so that all the groups fall out of use at once. A lookup reads a group of the tags, or a word of a table without tags, only if its stamp is the epoch (a group out of use is free: the lookup stops there), and an insertion that reaches a group out of use first renews it: it takes the group by a compare-and-swap of its stamp to a mark, writes 0 to its words and to its tags, and stores the epoch with release; the other insertions that reach the group meanwhile wait for the store, a few words long, and the lookups see the group free until then, which is right, since no insertion of the new epoch has taken effect in it. A growth copies only the groups in use, into a vector whose groups all have the epoch. When the epoch would reach the mark, the largest value of \( S \), clear() writes 0 to the whole vector and starts again from the epoch 1, once per \( 2^{|S|} - 2 \) clears, so that no stamp left from an old epoch can equal a new one. The epochs need keys and values trivially destructible, since the pairs of a table neither packed nor inlined are dropped with their nodes, which the insertions of the next epoch reuse, with no destructor called; with the default policy, ZeroingClear, the table is the same as without it.
Each operation of a key is linearizable: a lookup at the read of the word that holds the key or of the free place (of its tag, when the places have tags), an insertion that adds its key at the write of the tag of its word (at its compare-and-swap when the places have no tags), and one that finds its key at the read of the word that holds it. clear(), forEach() and consistent() may not run with other operations. The hash of a key is \( h( k ) \), taken as it is if H declares is_avalanching [see FastHash.h] and mixed by hashWord() otherwise; hash() gives it, and the hash passed to the operations must be that one.
The contracts [see Contract.h]: the hash passed is hash( k ) (audit); the invariant is that each key is reached from its place without a free place in between, checked on the key of each operation, and consistent() checks it on every pair, with the counters. The operations are defined in ConcurrentFlatTable.tpp.