ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ConcurrentFlatTable< K, V, H, Eq, C > Class Template Reference

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...

#include <ADS/concurrent/ConcurrentFlatTable.h>

Classes

struct  Entry
 a pair of a table neither packed nor inlined, in a node that never moves More...

Public types

using Key = K
using Value = V
 < the type of the keys
using Clear = C
 < the type of the values
using Ref = std::conditional_t< packed || inlined , V , Entry & >
 what an operation returns of the pair held: a copy of the value if packed or inlined, the pair itself otherwise

Public member functions

 ConcurrentFlatTable (std::size_t n=0, H h=H(), Eq eq=Eq())
 the empty table, with room for \( n \) pairs (and their nodes) before it grows, the hash h and the equality eq
 ConcurrentFlatTable (const ConcurrentFlatTable &)=delete
 no copy, since the threads share it by its address
ConcurrentFlatTable & operator= (const ConcurrentFlatTable &)=delete
 no copy, since the threads share it by its address
 ~ConcurrentFlatTable ()
 the frees of the pairs and of the vectors
std::uint64_t hash (const K &k) const
 the hash of 64 bits of the key \( k \) that the operations take
std::pair< Ref, bool > findOrInsert (const K &k, std::uint64_t h, const V &v)
 the pair of the key \( k \), of hash \( h \), inserted with the value \( v \) if absent; true if it was inserted
std::pair< Ref, bool > findOrInsert (const K &k, const V &v)
 findOrInsert() above, with the hash computed
std::pair< K, bool > findOrInsertKey (const K &k, std::uint64_t h)
 the key held equal to \( k \), of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a packed set (an empty value)
bool insert (const K &k, const V &v)
 inserts the pair \( ( k , v ) \) if \( k \) is absent; true if it was, as findOrInsert()
std::optional< V > find (const K &k, std::uint64_t h) const
 a copy of the value of the key \( k \) of hash \( h \), or nothing
std::optional< V > find (const K &k) const
 find() above, with the hash computed
std::optional< K > findKey (const K &k, std::uint64_t h) const
 a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing; never inserts
std::optional< K > findKey (const K &k) const
 findKey() above, with the hash computed
bool contains (const K &k, std::uint64_t h) const
 whether the key \( k \) of hash \( h \) is there, as find()
bool contains (const K &k) const
 contains() above, with the hash computed
void prefetch (std::uint64_t h) const
 the lines that a lookup of the hash \( h \) reads first (its tags and its word) fetched into the cache, with no wait: a thread about to look up or insert several keys prefetches them all first, so that their misses overlap
Entry * entry (const K &k, std::uint64_t h) const
 the pair of the key \( k \) of hash \( h \), or nil, as find(); for a table neither packed nor inlined
template<class F>
requires ( packed || inlined )
bool update (const K &k, F &&f)
 sets the value \( x \) of the key \( k \) to f( x ), by a compare-and-swap of its word (of its place), for a packed (an inlined) table; true if \( k \) is there
std::size_t size (void) const
 the number of the pairs, the sum of the counters, exact when no operation runs
bool empty (void) const
 whether size() is 0
std::size_t capacity (void) const
 the number of the places of the vector in use
std::size_t rehashes (void) const
 the number of the growths of the table so far
void reserve (std::size_t n)
 room for \( n \) pairs before the table grows: the vector grown to \( 2 n \) places at least, as by an insertion, and the nodes of \( n \) pairs allocated
void clear (void)
 removes every pair, keeping the capacity; with no other operation running.
std::uint64_t epoch (void) const
 the epoch of the table, raised by each clear() and back to 1 after the largest; always 1 without EpochClear
template<class F>
void forEach (F &&f) const
 applies f( const K & , const V & ) to every pair; with no insertion running
bool consistent (void) const
 each key once, reached from its place without a free place in between, and the counters summing to the pairs; with no other operation running

Static public attributes

static constexpr bool epochs = detail::ClearStamps< C >::epochs
 < the policy of clear()
static constexpr bool packed
 true if the pairs lie in the words: the key and the value trivially copyable, the bytes of the key all significant, 8 bytes at most
static constexpr bool inlined
 true if the pairs lie in places of two words written by one compare-and-swap of 16 bytes: not packed, the key and the value trivially copyable, 8 bytes at most each, the bytes of the key all significant, and the instruction there

Detailed description

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
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

Member typedef documentation

◆ Clear

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::ConcurrentFlatTable< K, V, H, Eq, C >::Clear = C

< the type of the values

◆ Key

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::ConcurrentFlatTable< K, V, H, Eq, C >::Key = K

◆ Ref

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::ConcurrentFlatTable< K, V, H, Eq, C >::Ref = std::conditional_t< packed || inlined , V , Entry & >

what an operation returns of the pair held: a copy of the value if packed or inlined, the pair itself otherwise

◆ Value

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
using ADS::ConcurrentFlatTable< K, V, H, Eq, C >::Value = V

< the type of the keys

Constructor and destructor documentation

◆ ConcurrentFlatTable() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ADS::ConcurrentFlatTable< K, V, H, Eq, C >::ConcurrentFlatTable ( std::size_t n = 0,
H h = H(),
Eq eq = Eq() )
explicit

the empty table, with room for \( n \) pairs (and their nodes) before it grows, the hash h and the equality eq

◆ ConcurrentFlatTable() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ADS::ConcurrentFlatTable< K, V, H, Eq, C >::ConcurrentFlatTable ( const ConcurrentFlatTable< K, V, H, Eq, C > & )
delete

no copy, since the threads share it by its address

◆ ~ConcurrentFlatTable()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ADS::ConcurrentFlatTable< K, V, H, Eq, C >::~ConcurrentFlatTable ( )

the frees of the pairs and of the vectors

Member function documentation

◆ capacity()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::ConcurrentFlatTable< K, V, H, Eq, C >::capacity ( void ) const
nodiscard

the number of the places of the vector in use

◆ clear()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
void ADS::ConcurrentFlatTable< K, V, H, Eq, C >::clear ( void )

removes every pair, keeping the capacity; with no other operation running.

Linear in the capacity by default, \( O( 1 ) \) amortized with EpochClear (the vectors replaced by the growths are freed in both, and once per \( 2^{|S|} - 2 \) clears the vector is written whole)

◆ consistent()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::consistent ( void ) const
nodiscard

each key once, reached from its place without a free place in between, and the counters summing to the pairs; with no other operation running

◆ contains() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::contains ( const K & k) const
nodiscard

contains() above, with the hash computed

◆ contains() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::contains ( const K & k,
std::uint64_t h ) const
nodiscard

whether the key \( k \) of hash \( h \) is there, as find()

Precondition
hash( k ) is \( h \) (audit)

◆ empty()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::empty ( void ) const
inlinenodiscard

whether size() is 0

◆ entry()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
Entry * ADS::ConcurrentFlatTable< K, V, H, Eq, C >::entry ( const K & k,
std::uint64_t h ) const
nodiscard

the pair of the key \( k \) of hash \( h \), or nil, as find(); for a table neither packed nor inlined

Precondition
hash( k ) is \( h \) (audit)

◆ epoch()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::uint64_t ADS::ConcurrentFlatTable< K, V, H, Eq, C >::epoch ( void ) const
inlinenodiscard

the epoch of the table, raised by each clear() and back to 1 after the largest; always 1 without EpochClear

◆ find() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< V > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::find ( const K & k) const
nodiscard

find() above, with the hash computed

◆ find() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< V > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::find ( const K & k,
std::uint64_t h ) const
nodiscard

a copy of the value of the key \( k \) of hash \( h \), or nothing

In pseudocode (beyond the book), with the notation of findOrInsert() above:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( k , h ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad T \gets \text{the vector in use } W\quad \triangleright \ \text{acquire} \cr 4 & \qquad i \gets \lfloor h / 2^{64 - l} \rfloor \cr 5 & \qquad \textbf{while}\ T[ i ] \ne 0\ \textbf{do}\quad \triangleright\ \text{acquire} \cr 6 & \qquad\quad \textbf{if}\ T[ i ] \text{ holds } k\ \textbf{then} \ \textbf{return}\ \text{its value} \cr 7 & \qquad\quad i \gets ( i + 1 ) \bmod 2^l \cr 8 & \qquad \textbf{if}\ T \text{ is still the vector in use} \ \textbf{then return nil} \end{array}} \]

Precondition
hash( k ) is \( h \) (audit)

◆ findKey() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< K > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::findKey ( const K & k) const
nodiscard

findKey() above, with the hash computed

◆ findKey() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::optional< K > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::findKey ( const K & k,
std::uint64_t h ) const
nodiscard

a copy of the key held equal to \( k \) by Eq, of hash \( h \), or nothing; never inserts

The lookup of find(), which returns the key of the word found instead of its value: in a set of pointers to objects compared by Eq as the objects themselves, a thread reads the pointer that the table holds for a state equal to that of \( k \), so that the set keeps one word per pointer and no copy of it in the value. As find(), it may run with insertions and growths of any thread, takes no lock and writes nothing, is wait-free while the table does not grow, and linearizable at the read of the word that holds the key or of the free place; the key returned was inserted, whole, before that read.

Precondition
hash( k ) is \( h \) (audit)

◆ findOrInsert() [1/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< Ref, bool > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::findOrInsert ( const K & k,
const V & v )

findOrInsert() above, with the hash computed

◆ findOrInsert() [2/2]

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< Ref, bool > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::findOrInsert ( const K & k,
std::uint64_t h,
const V & v )

the pair of the key \( k \), of hash \( h \), inserted with the value \( v \) if absent; true if it was inserted

In pseudocode (the folklore table of Maier et al. that grows, beyond the book), \( W[ 0 .. 2^l - 1 ] \) the words of the vector in use, \( c_t \) the counter of the thread and \( g \) the flag of the growth:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findOrInsert}( k , h , v ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad c_t \gets c_t + 1\quad \triangleright\ \text{sequentially} \text{ consistent} \cr 4 & \qquad \textbf{if}\ g\ \textbf{then}\ c_t \gets c_t - 1; \ \mathrm{help}();\ \textbf{continue} \cr 5 & \qquad w \gets \text{the word of } ( k , v ) \text{, or of a new} \text{ node of } ( h , k , v ) \cr 6 & \qquad i \gets \lfloor h / 2^{64 - l} \rfloor;\ r \gets 0 \cr 7 & \qquad \textbf{loop} \cr 8 & \qquad\quad x \gets W[ i ] \cr 9 & \qquad\quad \textbf{if}\ x = 0\ \textbf{and}\ \mathrm{CAS}( W[ i ] , 0 , w )\ \textbf{then} \cr 10 & \qquad\qquad c_t \gets c_t - 1 + 2^{16}\quad \triangleright \ \text{the pairs of the thread from the bit 16} \cr 11 & \qquad\qquad \textbf{if}\ \text{they are a multiple of } 2^{\min( \max( l - 10 , 0 ) , 10 )} \text{ and the pairs of all the threads} \text{ pass } 2^{l-1} \ \textbf{then}\ \mathrm{grow}( l + 1 ) \cr 12 & \qquad\qquad \textbf{return}\ ( w , \textbf{true} ) \cr 13 & \qquad\quad \textbf{if}\ x = 0\ \textbf{then}\ x \gets W[ i ] \quad \triangleright\ \text{written by another thread} \cr 14 & \qquad\quad \textbf{if}\ x \text{ holds } k\ \textbf{then} \ c_t \gets c_t - 1;\ \textbf{return}\ ( x , \textbf{false} ) \cr 15 & \qquad\quad i \gets ( i + 1 ) \bmod 2^l;\ r \gets r + 1 \cr 16 & \qquad\quad \textbf{if}\ r > 128\ \textbf{and}\ \text{the table} \text{ is } 1/8 \text{ full}\ \textbf{then break} \cr 17 & \qquad c_t \gets c_t - 1;\ \mathrm{grow}( l + 1 ) \cr 18 & \textbf{procedure}\ \mathrm{grow}( l' ) \cr 19 & \quad \textbf{if}\ \mathrm{CAS}( g , \textbf{false} , \textbf{true} )\ \textbf{then} \cr 20 & \qquad \textbf{while}\ \text{some } c_t > 0\ \textbf{do} \ \text{a pause} \cr 21 & \qquad W' \gets 2^{l'} \text{ words } 0;\ \text{publish } W' \text{ for the helpers} \cr 22 & \qquad \mathrm{help}();\ \textbf{wait until}\ \text{all the} \text{ blocks are copied} \cr 23 & \qquad W \gets W';\ g \gets \textbf{false}\quad \triangleright \ \text{release} \cr 24 & \quad \textbf{else}\ \mathrm{help}() \cr 25 & \textbf{procedure}\ \mathrm{help}() \cr 26 & \quad \textbf{while}\ g\ \textbf{do} \cr 27 & \qquad \textbf{foreach}\ \text{block of } 4096 \text{ words of } W \text{ claimed by a counter}\ \textbf{do} \cr 28 & \qquad\quad \textbf{foreach}\ x \ne 0 \text{ in the block} \ \textbf{do}\ \text{put } x \text{ at the first free place of its} \text{ key in } W' \text{, by a CAS} \end{array}} \]

Precondition
hash( k ) is \( h \) (audit)

◆ findOrInsertKey()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::pair< K, bool > ADS::ConcurrentFlatTable< K, V, H, Eq, C >::findOrInsertKey ( const K & k,
std::uint64_t h )

the key held equal to \( k \), of hash \( h \), which is \( k \) itself if absent and then inserted; true if it was inserted, for a packed set (an empty value)

The key held is equal to \( k \) by Eq, not by its bytes: in a set of pointers to objects compared by Eq as the objects themselves, the threads that insert at once pointers to equal objects all get the pointer of the one that inserted it, the intrusive set that a parallel search keeps of its nodes, a word of 8 bytes for each.

Precondition
hash( k ) is \( h \) (audit)

◆ forEach()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
template<class F>
void ADS::ConcurrentFlatTable< K, V, H, Eq, C >::forEach ( F && f) const

applies f( const K & , const V & ) to every pair; with no insertion running

◆ hash()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::uint64_t ADS::ConcurrentFlatTable< K, V, H, Eq, C >::hash ( const K & k) const
nodiscard

the hash of 64 bits of the key \( k \) that the operations take

◆ insert()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::insert ( const K & k,
const V & v )

inserts the pair \( ( k , v ) \) if \( k \) is absent; true if it was, as findOrInsert()

◆ operator=()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
ConcurrentFlatTable & ADS::ConcurrentFlatTable< K, V, H, Eq, C >::operator= ( const ConcurrentFlatTable< K, V, H, Eq, C > & )
delete

no copy, since the threads share it by its address

◆ prefetch()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
void ADS::ConcurrentFlatTable< K, V, H, Eq, C >::prefetch ( std::uint64_t h) const

the lines that a lookup of the hash \( h \) reads first (its tags and its word) fetched into the cache, with no wait: a thread about to look up or insert several keys prefetches them all first, so that their misses overlap

◆ rehashes()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::ConcurrentFlatTable< K, V, H, Eq, C >::rehashes ( void ) const
nodiscard

the number of the growths of the table so far

◆ reserve()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
void ADS::ConcurrentFlatTable< K, V, H, Eq, C >::reserve ( std::size_t n)

room for \( n \) pairs before the table grows: the vector grown to \( 2 n \) places at least, as by an insertion, and the nodes of \( n \) pairs allocated

◆ size()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
std::size_t ADS::ConcurrentFlatTable< K, V, H, Eq, C >::size ( void ) const
nodiscard

the number of the pairs, the sum of the counters, exact when no operation runs

◆ update()

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
template<class F>
requires ( packed || inlined )
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::update ( const K & k,
F && f )

sets the value \( x \) of the key \( k \) to f( x ), by a compare-and-swap of its word (of its place), for a packed (an inlined) table; true if \( k \) is there

Member data documentation

◆ epochs

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::epochs = detail::ClearStamps< C >::epochs
staticconstexpr

< the policy of clear()

true if clear() is \( O( 1 ) \), by the epochs of EpochClear

◆ inlined

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::inlined
staticconstexpr
Initial value:
=
( ADS_FLAT_CAS16 != 0 ) && ! packed && std::is_trivially_copyable_v< K > &&
std::is_trivially_copyable_v< V > && plainKey() && ( sizeof( K ) <= 8 ) &&
( sizeof( V ) <= 8 )
#define ADS_FLAT_CAS16
Definition Abi.h:78
static constexpr bool packed
true if the pairs lie in the words: the key and the value trivially copyable, the bytes of the key al...
Definition ConcurrentFlatTable.h:302

true if the pairs lie in places of two words written by one compare-and-swap of 16 bytes: not packed, the key and the value trivially copyable, 8 bytes at most each, the bytes of the key all significant, and the instruction there

◆ packed

template<class K, class V, class H = DefaultHash< K >, class Eq = std::equal_to< K >, class C = ZeroingClear>
bool ADS::ConcurrentFlatTable< K, V, H, Eq, C >::packed
staticconstexpr
Initial value:
=
std::is_trivially_copyable_v< K > && std::is_trivially_copyable_v< V > &&
plainKey() && ( sizeof( K ) + valueBytes() <= 8 )

true if the pairs lie in the words: the key and the value trivially copyable, the bytes of the key all significant, 8 bytes at most


The documentation for this class was generated from the following file: