|
ADS
Algorithms and Data Structures
|
a DICTIONARY shared by threads, by cuckoo hashing with a lock per region More...
#include <ADS/concurrent/ConcurrentCuckooTable.h>
Public types | |
| using | Key = K |
| the type of the keys | |
| using | Value = V |
| the type of the values | |
Public member functions | |
| ConcurrentCuckooTable (std::size_t n=0, H h=H(), Eq eq=Eq()) | |
| the empty table, with room for \( n \) pairs before it grows, the hash h and the equality eq | |
| ConcurrentCuckooTable (const ConcurrentCuckooTable &)=delete | |
| no copy, since the threads share it by its address | |
| ConcurrentCuckooTable & | operator= (const ConcurrentCuckooTable &)=delete |
| no copy, since the threads share it by its address | |
| std::optional< V > | find (const K &k) const |
| a copy of the value of the key \( k \), or nothing | |
| bool | contains (const K &k) const |
| whether the key \( k \) is there, as find() | |
| bool | insert (const K &k, const V &v) |
| associates the value \( v \) to the key \( k \) if \( k \) is not there; true if it was not, and otherwise the value of \( k \) stays | |
| bool | insertOrAssign (const K &k, const V &v) |
| associates the value \( v \) to the key \( k \); true if \( k \) was not there | |
| template<class F> | |
| bool | update (const K &k, F &&f) |
| applies f( V & ) to the value of the key \( k \) under its locks; true if \( k \) is there | |
| bool | erase (const K &k) |
| removes the key \( k \), if it is there; true if it was | |
| std::size_t | size (void) const |
| the number of the pairs, the sum of the counters of the regions and of the stash, exact when no operation runs | |
| bool | empty (void) const |
| whether size() is 0 | |
| std::size_t | capacity (void) const |
| the number of the places, 4 per bucket | |
| std::size_t | rehashes (void) const |
| the number of the doublings of the table so far | |
| std::size_t | stashed (void) const |
| the pairs in the stash, 0 but for degenerate hashes | |
| void | reserve (std::size_t n) |
| room for \( n \) pairs before the table grows: the buckets doubled, under all the locks, up to the power of 2 at least \( n / 3.6 \) | |
| void | clear (void) |
| removes every pair, keeping the capacity, under all the locks | |
| template<class F> | |
| void | forEach (F &&f) |
| applies f( const K & , V & ) to every pair, under all the locks | |
| template<class F> | |
| void | forEach (F &&f) const |
| applies f( const K & , const V & ) to every pair, under all the locks | |
| bool | consistent (void) const |
| each key once in one of its two buckets with its tag or in the stash, and the counters summing to the pairs; under all the locks | |
Static public attributes | |
| static constexpr bool | optimistic = refLockFree< K >() && refLockFree< V >() |
| true if the lookups read without locks, under the versions of the stripes: the keys and the values trivially copyable, and read and written atomically without locks | |
a DICTIONARY shared by threads, by cuckoo hashing with a lock per region
| using ADS::ConcurrentCuckooTable< K, V, H, Eq >::Key = K |
the type of the keys
| using ADS::ConcurrentCuckooTable< K, V, H, Eq >::Value = V |
the type of the values
|
explicit |
the empty table, with room for \( n \) pairs before it grows, the hash h and the equality eq
|
delete |
no copy, since the threads share it by its address
|
nodiscard |
the number of the places, 4 per bucket
| void ADS::ConcurrentCuckooTable< K, V, H, Eq >::clear | ( | void | ) |
removes every pair, keeping the capacity, under all the locks
|
nodiscard |
each key once in one of its two buckets with its tag or in the stash, and the counters summing to the pairs; under all the locks
|
nodiscard |
whether the key \( k \) is there, as find()
|
nodiscard |
whether size() is 0
| bool ADS::ConcurrentCuckooTable< K, V, H, Eq >::erase | ( | const K & | k | ) |
removes the key \( k \), if it is there; true if it was
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{erase}( k ) \cr 2 & \quad \text{lock the region } R \text{ of } h = \mathrm{hash}( k ) \text{ in the table in use} \cr 3 & \quad \textbf{if}\ k \text{ is in the stash}\ \textbf{then} \ \text{take it out, under its mutex};\ \text{unlock}; \ \textbf{return true} \cr 4 & \quad \textbf{if}\ k \text{ is not in its buckets}\ \textbf{then} \ \text{unlock};\ \textbf{return false} \cr 5 & \quad \text{set the tag of its place to } 0;\ n_R \gets n_R - 1 \cr 6 & \quad \text{unlock};\ \textbf{return true} \end{array}} \]
|
nodiscard |
a copy of the value of the key \( k \), or nothing
In pseudocode (the seqlock of Boehm on the partial-key cuckoo hashing, beyond the book), \( v_R \) the version of the region \( R \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( k ) \cr 2 & \quad h \gets \mathrm{hash}( k );\ t \gets \mathrm{tag}( h ) \cr 3 & \quad \textbf{if}\ \mathit{optimistic}\ \textbf{then} \cr 4 & \qquad \textbf{for}\ a \gets 1\ \textbf{to}\ 4\ \textbf{do} \cr 5 & \qquad\quad T \gets \text{the table in use};\ b_1 \gets \text{the first bucket of } h \text{ in } T;\ \text{fetch its lines} \cr 6 & \qquad\quad \textbf{if}\ t \text{ is not among the tags of } b_1 \text{, its bit of overflow for } t \text{ is clear and } T \text{ is still in use}\ \textbf{then return nil} \cr 7 & \qquad\quad v \gets v_R\quad \triangleright\ \text{acquire, } R \text{ the region of } b_1 \cr 8 & \qquad\quad \textbf{if}\ v \text{ is odd}\ \textbf{or}\ T \text{ is no longer in use}\ \textbf{then}\ \text{a pause}; \ \textbf{continue} \cr 9 & \qquad\quad x \gets \text{the value of the place of } b_1 \text{, or of } b_2 \text{ or of the stash if that bit is set, with} \text{ the tag } t \text{ and the key } k \text{, or}\textbf{ nil} \quad \triangleright\ \text{acquire} \cr 10 & \qquad\quad \textbf{if}\ v_R = v\ \textbf{then return}\ x \cr 11 & \quad \text{lock the region of } h \text{ in the table in use} \cr 12 & \quad \textbf{return}\ \text{the value of } k \text{ in } b_1 , b_2 \text{ or the stash, or}\ \textbf{nil}\quad \triangleright \ \text{and unlock} \end{array}} \]
| void ADS::ConcurrentCuckooTable< K, V, H, Eq >::forEach | ( | F && | f | ) |
applies f( const K & , V & ) to every pair, under all the locks
| void ADS::ConcurrentCuckooTable< K, V, H, Eq >::forEach | ( | F && | f | ) | const |
applies f( const K & , const V & ) to every pair, under all the locks
| bool ADS::ConcurrentCuckooTable< K, V, H, Eq >::insert | ( | const K & | k, |
| const V & | v ) |
associates the value \( v \) to the key \( k \) if \( k \) is not there; true if it was not, and otherwise the value of \( k \) stays
In pseudocode (the partial-key cuckoo hashing of MemC3 with the search in breadth of Li et al., beyond the book), \( n_R \) the pairs of the region \( R \) of \( 4 |R| \) places:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( k , v ) \cr 2 & \quad h \gets \mathrm{hash}( k );\ t \gets \mathrm{tag}( h ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad T \gets \text{the table in use, with the lock of the region} \text{ } R \text{ of } h \cr 5 & \qquad b_1 \gets \text{the top bits of } h;\ b_2 \gets b_1 \oplus ( g( t ) \text{ in } R ) \cr 6 & \qquad \textbf{if}\ k \text{ is in } b_1 \text{, in } b_2 \text{ or} \text{ in the stash}\ \textbf{then}\ \text{unlock};\ \textbf{return false} \cr 7 & \qquad p \gets \text{a free place of } b_1 \text{, else of } b_2 \text{ after the bit of overflow of } b_1 \text{ for } t \cr 8 & \qquad \textbf{if}\ p = \textbf{nil}\ \textbf{and}\ n_R + 1 \le 0.98 \cdot 4 |R|\ \textbf{then}\ p \gets \mathrm{makeRoom}( T , h ) \cr 9 & \qquad \textbf{if}\ p \ne \textbf{nil}\ \textbf{then} \cr 10 & \qquad\quad \text{put } ( k , v ) \text{ in } p \text{ with the} \text{ tag } t;\ n_R \gets n_R + 1;\ \text{unlock}; \textbf{return true} \cr 11 & \qquad \text{unlock};\ \textbf{if}\ \mathrm{grow}( T , k , v ) \ \textbf{then return true} \cr 12 & \textbf{function}\ \mathrm{makeRoom}( T , h ) \cr 13 & \quad Q \gets \text{a queue of the buckets } b_1 , b_2 \text{ of } h \text{ at the depth } 0 \cr 14 & \quad \textbf{while}\ Q \ne \emptyset\ \textbf{do} \cr 15 & \qquad b \gets \mathrm{dequeue}( Q ) \cr 16 & \qquad \textbf{if}\ b \text{ has a free place}\ \textbf{then} \cr 17 & \qquad\quad \textbf{foreach}\ \text{bucket } c \ne b_1 , b_2 \text{ on the path to } b \text{, from } b\ \textbf{do} \cr 18 & \qquad\qquad \text{move into } c \text{ the key of its parent that} \text{ led to it, setting the bit of overflow of the parent for} \text{ its tag} \cr 19 & \qquad\quad \textbf{return}\ \text{the place freed in } b_1 \text{ or } b_2 \cr 20 & \qquad \textbf{if}\ \text{the depth of } b < 5\ \textbf{then} \cr 21 & \qquad\quad \textbf{foreach}\ \text{tag } u \text{ of the 4 keys} \text{ of } b\ \textbf{do} \cr 22 & \qquad\qquad c \gets \text{the other bucket of } u \text{ from } b \cr 23 & \qquad\qquad \textbf{if}\ c \text{ is not on the path to } b \ \textbf{and}\ |Q| \text{ in all} < 1024\ \textbf{then} \ \mathrm{enqueue}( Q , c ) \cr 24 & \quad \textbf{return nil} \cr 25 & \textbf{function}\ \mathrm{grow}( T , k , v ) \cr 26 & \quad \text{lock all the stripes in use, in increasing order} \cr 27 & \quad \textbf{if}\ T \text{ is not in use}\ \textbf{then} \ \text{unlock all};\ \textbf{return false} \cr 28 & \quad \textbf{if}\ 4 B \ge 256\ \textbf{and}\ 16 n < 4 B \ \textbf{then}\quad \triangleright\ \text{too many equal hashes} \cr 29 & \qquad \textbf{if}\ k \text{ is not in } T \text{ nor in the} \text{ stash, and finds no place in } T \textbf{ then} \text{ set} \text{ its bit of overflow in } b_1 \cr 30 & \qquad\quad \text{put } ( k , v ) \text{ in the stash, under its} \text{ mutex} \cr 31 & \qquad \textbf{else if}\ k \text{ is not there}\ \textbf{then} \ \text{put } ( k , v ) \text{ in its place} \cr 32 & \qquad \text{unlock all};\ \textbf{return true} \cr 33 & \quad T' \gets \text{a table of } 2 B \text{ buckets; place each} \text{ pair of } T \text{ in } T' \text{, with the threads that} \text{ wait for a lock, each on its parts of } T \cr 34 & \quad \text{place the pairs of the stash in } T' \text{; those} \text{ that find no place, there and in the parts, go to the new} \text{ stash} \cr 35 & \quad \text{lock the new stripes too};\ \text{count the pairs of} \text{ each region};\ \text{make } T' \text{ the table in use}\quad \triangleright\ \text{release} \cr 36 & \quad \text{unlock all};\ \textbf{return false} \end{array}} \]
| bool ADS::ConcurrentCuckooTable< K, V, H, Eq >::insertOrAssign | ( | const K & | k, |
| const V & | v ) |
associates the value \( v \) to the key \( k \); true if \( k \) was not there
It is insert() above, but for a key found, whose value becomes \( v \) (at line 6, before the unlock).
|
delete |
no copy, since the threads share it by its address
|
nodiscard |
the number of the doublings of the table so far
| void ADS::ConcurrentCuckooTable< K, V, H, Eq >::reserve | ( | std::size_t | n | ) |
room for \( n \) pairs before the table grows: the buckets doubled, under all the locks, up to the power of 2 at least \( n / 3.6 \)
|
nodiscard |
the number of the pairs, the sum of the counters of the regions and of the stash, exact when no operation runs
|
nodiscard |
the pairs in the stash, 0 but for degenerate hashes
| bool ADS::ConcurrentCuckooTable< K, V, H, Eq >::update | ( | const K & | k, |
| F && | f ) |
applies f( V & ) to the value of the key \( k \) under its locks; true if \( k \) is there
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{update}( k , f ) \cr 2 & \quad \text{lock the region of } h = \mathrm{hash}( k ) \text{ in } \text{the table in use} \cr 3 & \quad \textbf{if}\ k \text{ is not in its buckets nor in the} \text{ stash}\ \textbf{then}\ \text{unlock};\ \textbf{return false} \cr 4 & \quad x \gets \text{the value of } k;\ f( x );\ \text{store } x \text{ as the value of } k\quad \triangleright\ \text{atomically if} \text{ optimistic} \cr 5 & \quad \text{unlock};\ \textbf{return true} \end{array}} \]
|
staticconstexpr |
true if the lookups read without locks, under the versions of the stripes: the keys and the values trivially copyable, and read and written atomically without locks