|
ADS
Algorithms and Data Structures
|
A hash table that many threads read and change at once, beyond the book: the cuckoo hashing with buckets of 4 places and tags of 8 bits of BucketCuckooTable [see BucketCuckooTable.h], made concurrent with one lock per region of buckets and, for keys and values that can be read atomically, lookups that take no lock at all. More...
#include "ADS/Capabilities.h"#include "ADS/Contract.h"#include "ADS/Portable.h"#include "ADS/dictionary/BucketCuckooTable.h"#include "ADS/dictionary/FastHash.h"#include "ADS/memory/HugePageAllocator.h"#include <atomic>#include <bit>#include <cstddef>#include <cstdint>#include <functional>#include <memory>#include <mutex>#include <optional>#include <type_traits>#include <vector>#include "ADS/concurrent/impl/ConcurrentCuckooTable.tpp"Go to the source code of this file.
Classes | |
| class | ADS::ConcurrentCuckooTable< K, V, H, Eq > |
| a DICTIONARY shared by threads, by cuckoo hashing with a lock per region More... | |
Namespaces | |
| namespace | ADS |
Variables | |
| template<class K, class V, class H, class Eq> | |
| constexpr bool | ADS::nonmonotone_values< ConcurrentCuckooTable< K, V, H, Eq > > = true |
| the values of the table may rise as well as fall [see Capabilities.h]: a lookup reads a pair under the version of its region, or under its lock, so that it returns a value that one write left whole, whatever the direction of the writes | |
A hash table that many threads read and change at once, beyond the book: the cuckoo hashing with buckets of 4 places and tags of 8 bits of BucketCuckooTable [see BucketCuckooTable.h], made concurrent with one lock per region of buckets and, for keys and values that can be read atomically, lookups that take no lock at all.
A key with the hash \( h \) has two buckets, \( b_1 \) given by the top bits of \( h \) and
\[ b_2 = b_1 \oplus \left( \left\lfloor g( t ) / 2^{64 - r} \right\rfloor \vee 1 \right) \]
for the tag \( t \) of \( h \), a mixing \( g \) of the tag (the partial-key cuckoo hashing of MemC3) and regions of \( R = 2^r \) buckets, so that the other bucket of a key follows from its bucket and its tag and both are in the same region.
The \( B \) buckets form \( P = \min( S , \max( 1 , B / 256 ) ) \) regions of \( R = B / P \) buckets each, \( S = 2048 \), and a region has one lock (the lock striping of Herlihy and Shavit, with a stripe per region instead of a stripe of scattered buckets). Each lock is a counter of versions on a line of cache of its own, odd while held, taken by a compare-and-swap from an even value after spinning on plain loads, with the pause of the processor and, after 64 rounds, a yield to the scheduler. An operation on a key thus takes one lock, that of its region, and the cuckoo path of an insertion, which never leaves the region, is searched in breadth and its keys moved under that lock alone, with nothing to check again (where Li et al. search without locks and lock two buckets per move). Only the doubling, reserve(), clear(), forEach() and consistent() take more locks, all those in use, in increasing order from the stripe 0: a thread that holds the stripe \( i \) and waits for the stripe \( j \) has \( i < j \), so that a cycle of waits \( i_1 < \dots < i_m < i_1 \) cannot exist, whence no deadlock. With uniform keys, two operations contend for a lock with probability \( 1 / P \).
When the keys and the values are trivially copyable and std::atomic_ref reads and writes them without locks (e.g., integers and pointers; optimistic is then true), a lookup is a read under the version of the region (a seqlock): it reads the even version \( v \), the table in use, the tags and the places of the key with atomic loads that acquire, and returns what it read if the version is still \( v \) after them; otherwise it tries again, and takes the lock after 4 tries. A writer makes the version odd and then stores with release, so that a reader that sees one of its stores sees the odd version afterwards (the analysis of Boehm, with the orders on the accesses in place of the two fences, which the sanitizer of threads does not model, at the cost of the plain loads and stores on x86-64). No access is a data race in the sense of C++, and a lookup costs no atomic read-modify-write and no store to a shared line. The tables replaced by a doubling are kept until the table is destroyed, since a reader may still be on one; with \( B_0 < \dots < B_k \) buckets they total \( \sum_{j < k} B_j < B_k \), less than the table in use. Other keys and values (e.g., std::string) are read under the lock, and the tables replaced are freed at once.
Each bucket has 4 bits of overflow, in a vector of their own of \( B / 2 \) bytes, \( 1 / 8 \) of the tags: the bit \( j \) of the bucket \( b \) is set when a key with \( b \) as its first bucket and with \( j \) in the top 2 bits of its tag goes into its other bucket, by an insertion or by a move along a path, and is cleared only by a doubling or by clear() (the remap entries of the Horton tables of Breslow et al., here reduced to bits). A key in its other bucket thus has its bit set in its first one, and a lookup reads the other bucket only when that bit is set: a key that is not there costs the tags of one bucket, and a bit read from a vector that the cache keeps. Since a key enters its other bucket after its bit is set, and leaves its first bucket by a store of the tags that releases after the bit, an absent key whose tag does not match in its first bucket, whose bit is clear, is in neither bucket at the moment of the read of those tags, if the table is still in use then: such a lookup returns without reading the version of the region, which only the lookups that read places need. The bits set by keys removed since stay, and make lookups read the second bucket for nothing until the next doubling.
A lookup, an insertion or a removal fetches ahead, at once, the tags and the line of the places of the first bucket of its key (the 4 places of a bucket are aligned to share a line of cache when their size is a power of 2 up to 64 bytes), so that the misses overlap, and an insertion the tags of the second bucket too, where the key goes if the first is full; a lookup reads the second bucket only after its bit of overflow, and its places only when a tag matches there. The tags and the places of a table of 2 MB or more are on transparent huge pages [see HugePageAllocator.h], so that the random accesses miss the TLB once per 2 MB instead of once per 4 KB (Navarro et al.).
An insertion puts the new key in a free place of its two buckets if any; otherwise it searches in breadth, from the two buckets, a path of at most \( L = 5 \) moves that ends in a bucket with a free place, and moves its keys from the end. A search in breadth over buckets of \( b \) places that looks at \( M \) places finds a path of at most
\[ L = \left\lceil \log_b \left( \frac{M}{2} - \frac{M}{2b} + 1 \right) \right\rceil \]
moves, since its two trees have \( 2 b^l \) places at the depth \( l \) and \( 2 b ( b^L - 1 ) / ( b - 1 ) \ge M \), against the \( M / 2 \) moves of a search in depth (Li et al.); the search stops at \( L = 5 \), the value for \( M = 2000 \) and \( b = 4 \), or at 1024 buckets, all in the region, whose tags are a block of \( 8 R \) bytes.
The table doubles when a region would pass the load 0.98 or no path is found: the thread takes all the locks in use, those of the new regions too before the new table is seen, checks that no other thread has doubled the table in the meantime, and places every pair again in a new table, while no other thread changes the old one. Since the first bucket of a key is the top bits of its hash, the pairs of the old buckets \( [ l , h ) \) go to the new buckets \( [ 2 l , 2 h ) \), which hold whole regions and receive no other pair; the old table is therefore cut in parts of whole regions and of at least 4096 buckets, which the threads waiting for a lock claim, by a compare-and-swap of a counter that carries the number of the doubling, and migrate with the thread that doubles, each making its part of the new table empty and counting the pairs of its new regions (the cooperative migration of Maier, Sanders and Dementiev, here under the locks); the thread that doubles waits for all the parts, and only then is the new table seen. With 2 buckets per key and 4 places per bucket, the cuckoo hashing of \( n \) random keys succeeds with high probability while \( n / ( 4 B ) < c^*_{2,4} \approx 0.9803 \), the threshold of the 4-orientability of the random graphs (Cain, Sanders and Wormald), here in each region; a region of \( 4 R \) places holds a binomial number of the \( n \) keys, of mean \( \mu = n / P \) and standard deviation \( \sqrt{ \mu ( 1 - 1 / P ) } \), i.e., a relative deviation of about \( 1 / \sqrt{ 4 R \alpha } \) at the load \( \alpha \), 3.3 % for \( R = 256 \) and \( \alpha = 0.9 \), so that the fullest region of a large table reaches 0.98 at a mean load of about 0.88 to 0.92 (the restriction of the choices to a part of the table, there a page, is also in Dietzfelbinger, Mitzenmacher and Rink). The doublings cost \( O( 1 ) \) amortized per insertion.
A doubling that would leave a table of at least 256 places less than 1/16 full is not made, since the key that finds no place then shares both buckets with 8 others at every size (e.g., by equal hashes): the key goes instead to a stash (Kirsch, Mitzenmacher and Wieder), a vector of pairs under a mutex of its own, after the bit of overflow of its first bucket is set, so that the lookups without locks treat it as a key in its other bucket and then read the stash, under its mutex, inside the read of the version. A pair enters the stash only under all the locks, and leaves it or changes there under the lock of its region and the mutex; a doubling places the pairs of the stash again with the others, and those that find no place, as those that a doubling cannot place, go to the new stash. The table thus has at most \( \max( P_0 , 512 , 32 n ) \) places, \( P_0 \) those of the empty table and \( n \) the most pairs it has held in its buckets, and a lookup reads the stash only while it is not empty, which it is with any but a degenerate hash.
Each region counts its pairs, under its lock, so that size() is the sum of \( P \) counters read without locks: exact when no operation runs, and otherwise an estimate, since the counters are read at different moments. The operations of a single key are linearizable at the moment they hold the lock of its region, or at the second read of the version for a lookup without locks; reserve(), clear(), forEach() and consistent() take all the locks, and forEach() thus visits the pairs of one moment. Maier, Sanders and Dementiev compare the concurrent hash tables, those that grow without locks among them.
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. The keys and the values must be default constructible and movable.
The contracts [see Contract.h]: the invariant is that each key is in one of its two buckets with its tag, and in the second only with its bit of overflow set in the first, or in the stash with that bit set, checked on the buckets of the key of the operation under its lock; consistent() checks it on every pair, with the counters of the regions and of the stash, under all the locks. No postcondition is stated on the key of an operation, since another thread may change it as soon as the lock is released. The operations are defined in ConcurrentCuckooTable.tpp.