ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ConcurrentFlatTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------- File ConcurrentFlatTable.h -----------------------*/
3/*--------------------------------------------------------------------------*/
166/*--------------------------------------------------------------------------*/
167/*----------------------------- DEFINITIONS --------------------------------*/
168/*--------------------------------------------------------------------------*/
169
170#ifndef __ADS_ConcurrentFlatTable
171 #define __ADS_ConcurrentFlatTable
172/* self-identification: #endif constant */
173
174/*--------------------------------------------------------------------------*/
175/*------------------------------ INCLUDES ----------------------------------*/
176/*--------------------------------------------------------------------------*/
177
178#include "ADS/Abi.h"
179#include "ADS/Contract.h"
180#include "ADS/Portable.h"
183
184#include <atomic>
185#include <concepts>
186#include <cstddef>
187#include <cstdint>
188#include <functional>
189#include <limits>
190#include <memory>
191#include <optional>
192#include <type_traits>
193#include <utility>
194#include <vector>
195
196#if ADS_FLAT_CAS16
197 #include <boost/atomic/atomic_ref.hpp>
198#endif
199
200/*--------------------------------------------------------------------------*/
201/*--------------------------- NAMESPACE ------------------------------------*/
202/*--------------------------------------------------------------------------*/
203
204namespace ADS
205{
206
208
209/*--------------------------------------------------------------------------*/
210/*------------------------ THE POLICIES OF clear() -------------------------*/
211/*--------------------------------------------------------------------------*/
212
216{ };
217
221template< std::unsigned_integral S = std::uint32_t >
222 requires( ! std::same_as< S , bool > )
224{
225 using Stamp = S;
226 };
227
229
230namespace detail
231{
233
236template< class C >
237struct ClearStamps
238{
239 using Stamp = std::uint8_t;
240 static constexpr bool epochs = false;
241 };
242
243template< class S >
244 requires( ! std::same_as< S , bool > )
245struct ClearStamps< EpochClear< S > >
246{
247 using Stamp = S;
248 static constexpr bool epochs = true;
249 };
250
252} // end( namespace detail )
253
255
257template< class C >
258concept FlatClear =
259 std::same_as< C , ZeroingClear > || detail::ClearStamps< C >::epochs;
260
261/*--------------------------------------------------------------------------*/
262/*----------------------- CLASS ConcurrentFlatTable ------------------------*/
263/*--------------------------------------------------------------------------*/
264
268template< class K , class V , class H = DefaultHash< K > ,
269 class Eq = std::equal_to< K > , class C = ZeroingClear >
271{
272
273 static_assert( FlatClear< C > ,
274 "the policy of clear() is ZeroingClear or EpochClear" );
275
277 static constexpr std::size_t valueBytes( void ) {
278 return( std::is_empty_v< V > ? 0 : sizeof( V ) );
279 }
280
282 static constexpr bool plainKey( void ) {
283 return( std::has_unique_object_representations_v< K > ||
284 std::is_floating_point_v< K > );
285 }
286
287/*--------------------------------------------------------------------------*/
288 public:
289 using Key = K;
290 using Value = V;
291 using Clear = C;
292
294 static constexpr bool epochs = detail::ClearStamps< C >::epochs;
295
296 static_assert( ! epochs || ( std::is_trivially_destructible_v< K > &&
297 std::is_trivially_destructible_v< V > ) ,
298 "the epochs drop the pairs with no destructor" );
299
302 static constexpr bool packed =
303 std::is_trivially_copyable_v< K > && std::is_trivially_copyable_v< V > &&
304 plainKey() && ( sizeof( K ) + valueBytes() <= 8 );
305
310 static constexpr bool inlined =
311 ( ADS_FLAT_CAS16 != 0 ) && ! packed && std::is_trivially_copyable_v< K > &&
312 std::is_trivially_copyable_v< V > && plainKey() && ( sizeof( K ) <= 8 ) &&
313 ( sizeof( V ) <= 8 );
314
317 struct Entry
318 {
319 const K key;
321 };
322
325 using Ref = std::conditional_t< packed || inlined , V , Entry & >;
326
329 explicit ConcurrentFlatTable( std::size_t n = 0 , H h = H() , Eq eq = Eq() );
330
335
338
340 [[nodiscard]] std::uint64_t hash( const K & k ) const;
341
400 std::pair< Ref , bool > findOrInsert( const K & k , std::uint64_t h ,
401 const V & v );
402
404 std::pair< Ref , bool > findOrInsert( const K & k , const V & v );
405
416 std::pair< K , bool > findOrInsertKey( const K & k , std::uint64_t h )
417 requires( packed && ( valueBytes() == 0 ) );
418
421 bool insert( const K & k , const V & v );
422
444 [[nodiscard]] std::optional< V > find( const K & k , std::uint64_t h ) const;
445
447 [[nodiscard]] std::optional< V > find( const K & k ) const;
448
462 [[nodiscard]] std::optional< K > findKey( const K & k ,
463 std::uint64_t h ) const;
464
466 [[nodiscard]] std::optional< K > findKey( const K & k ) const;
467
470 [[nodiscard]] bool contains( const K & k , std::uint64_t h ) const;
471
473 [[nodiscard]] bool contains( const K & k ) const;
474
479 void prefetch( std::uint64_t h ) const;
480
484 [[nodiscard]] Entry * entry( const K & k , std::uint64_t h ) const
485 requires( ! packed && ! inlined );
486
490 template< class F >
491 bool update( const K & k , F && f )
492 requires( packed || inlined );
493
496 [[nodiscard]] std::size_t size( void ) const;
497
499 [[nodiscard]] bool empty( void ) const { return( size() == 0 ); }
500
502 [[nodiscard]] std::size_t capacity( void ) const;
503
505 [[nodiscard]] std::size_t rehashes( void ) const;
506
510 void reserve( std::size_t n );
511
517 void clear( void );
518
521 [[nodiscard]] std::uint64_t epoch( void ) const { return( now() ); }
522
525 template< class F >
526 void forEach( F && f ) const;
527
531 [[nodiscard]] bool consistent( void ) const;
532
533/*--------------------------------------------------------------------------*/
534 private:
535 static constexpr int kLeast = 6;
536 static constexpr int kStripes = 64;
537 static constexpr int kProbe = 128;
539 static constexpr int kBlock = 4096;
540 static constexpr int kSlab = 64;
541 static constexpr int kLgSeg = 12;
542 static constexpr int kSegs = 29;
543 static constexpr int kFp = 40;
544
545 using Stamp = typename detail::ClearStamps< C >::Stamp;
546
548 static constexpr Stamp kBusy = std::numeric_limits< Stamp >::max();
549
551 struct NoStamps
552 { };
553
555 using Stamps = std::conditional_t< epochs , std::atomic< Stamp > * ,
556 NoStamps >;
557
559 [[nodiscard]] Stamp now( void ) const {
560 if constexpr( epochs )
561 return( f_epoch );
562 else
563 return( 1 );
564 }
565
567 struct Node
568 {
570 std::uint64_t h;
572 Entry e;
573 };
574
577 struct alignas( 64 ) Stripe
578 {
580 std::atomic< std::uint64_t > w{ 0 };
581 };
582
584 struct Table
585 {
587 int lg;
589 std::atomic< std::uint64_t > * W;
593 std::atomic< std::uint64_t > * G;
596 ADS_NO_UNIQUE_ADDRESS Stamps E{};
598 std::atomic< Table * > next{ nullptr };
600 std::atomic< std::size_t > claimed{ 0 } , copied{ 0 };
602 Table( int l , Stamp e );
604 Table( const Table & ) = delete;
606 Table & operator=( const Table & ) = delete;
608 ~Table();
610 [[nodiscard]] std::size_t places( void ) const {
611 return( std::size_t( 1 ) << lg );
612 }
614 [[nodiscard]] std::size_t groups( void ) const { return( places() >> 3 ); }
616 [[nodiscard]] std::uint8_t tagAt( std::size_t i ) const {
617 return( std::uint8_t( G[ i >> 3 ].load( std::memory_order_acquire ) >>
618 ( 8 * ( i & 7 ) ) ) );
619 }
621 [[nodiscard]] std::size_t blocks( void ) const {
622 return( ( places() + kBlock - 1 ) / kBlock );
623 }
624 };
625
627 struct Slab
628 {
630 std::uint64_t id = 0;
632 std::uint64_t next = 0 , end = 0;
633 };
634
636 template< class X >
637 [[nodiscard]] static std::uint64_t bits( const X & x , std::size_t at );
638
642 static constexpr bool kTagged = packed || inlined;
643
645 static constexpr std::size_t kWords = inlined ? 2 : 1;
646
648 [[nodiscard]] static V inValue( std::uint64_t w );
649
653 static bool cas16( std::atomic< std::uint64_t > * p , std::uint64_t & k0 ,
654 std::uint64_t & v0 , std::uint64_t k1 , std::uint64_t v1 );
655
657 // the insertion of findOrInsert() into a table not `inlined`: the word
658 // that holds the key, and true if it was inserted
659 std::pair< std::uint64_t , bool > placeWord( const K & k , std::uint64_t h ,
660 const V & v );
661
662 std::pair< Ref , bool > placeInline( const K & k , std::uint64_t h ,
663 const V & v );
664
667 [[nodiscard]] const std::atomic< std::uint64_t > *
668 locate( const K & k , std::uint64_t h ) const;
669
671 [[nodiscard]] static std::uint64_t pack( const K & k , const V & v );
672
674 [[nodiscard]] static K keyOf( std::uint64_t w );
675
677 [[nodiscard]] static V valueOf( std::uint64_t w );
678
680 [[nodiscard]] static std::uint64_t mark( void );
681
683 [[nodiscard]] static bool zeroKey( const K & k );
684
686 [[nodiscard]] static std::size_t home( std::uint64_t h , const Table & T );
687
689 [[nodiscard]] static std::uint8_t tag( std::uint64_t h );
690
693 std::uint8_t tagged( Table & T , std::size_t i , std::uint64_t w ) const;
694
697 std::uint8_t tagWith( Table & T , std::size_t i , std::uint64_t h ) const;
698
700 [[nodiscard]] static std::uint64_t print( std::uint64_t h );
701
703 [[nodiscard]] Node * node( std::uint64_t i ) const;
704
706 [[nodiscard]] bool holds( std::uint64_t w , const K & k ,
707 std::uint64_t h ) const;
708
710 [[nodiscard]] std::uint64_t hashOf( std::uint64_t w ) const;
711
713 [[nodiscard]] std::uint64_t lookup( const K & k , std::uint64_t h ) const;
714
716 [[nodiscard]] Stripe & stripe( void ) const;
717
719 Table * enter( Stripe & s );
720
723 void segment( int j , bool touch );
724
727 void nodes( std::size_t n );
728
730 std::uint64_t claim( void );
731
734 void consume( std::uint64_t i );
735
737 [[nodiscard]] static Slab * slabs( void );
738
740 void help( void );
741
743 void copy( Table & T , Table & N );
744
746 void grow( Table * T , int lg );
747
750 [[nodiscard]] static std::uint64_t period( int lg );
751
753 [[nodiscard]] std::size_t counted( void ) const;
754
757 [[nodiscard]] bool live( const Table & T , std::size_t g ) const;
758
782 void renew( Table & T , std::size_t g ) const;
783
786 [[nodiscard]] std::uint64_t wordAt( const Table & T , std::size_t i ) const;
787
790 [[nodiscard]] bool reached( const Table & T , std::size_t i ) const;
791
792 std::unique_ptr< Stripe[] > f_S;
793 std::atomic< bool > f_grow{ false };
794 std::atomic< Table * > f_table{ nullptr };
795 std::vector< std::unique_ptr< Table > > f_old;
796 std::unique_ptr< Table > f_cur;
799 alignas( 16 ) std::atomic< std::uint64_t > f_zero[ 2 ];
800 std::atomic< Node * > f_seg[ kSegs ];
801 std::atomic< std::uint64_t > f_nodes{ 0 };
802 std::atomic< std::uint64_t > f_id;
803 std::atomic< std::size_t > f_rehashes{ 0 };
805 ADS_NO_UNIQUE_ADDRESS std::conditional_t< epochs , Stamp ,
806 NoStamps > f_epoch{};
807 H f_h;
808 Eq f_eq;
809
810 }; // end( class ConcurrentFlatTable )
811
812/*--------------------------------------------------------------------------*/
813
815
816} // end( namespace ADS )
817
818/*--------------------------------------------------------------------------*/
819/*-------------------------- IMPLEMENTATION --------------------------------*/
820/*--------------------------------------------------------------------------*/
821
822#include "ADS/concurrent/impl/ConcurrentFlatTable.tpp"
823
824/*--------------------------------------------------------------------------*/
825
826#endif /* ConcurrentFlatTable.h included */
827
828/*--------------------------------------------------------------------------*/
829/*---------------------- End File ConcurrentFlatTable.h --------------------*/
830/*--------------------------------------------------------------------------*/
The name of the configuration of the layouts, an inline namespace: the headers whose classes change t...
#define ADS_FLAT_CAS16
Definition Abi.h:78
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The engineered hash functions of ADS, beyond the book [see HashFunctions.h for those of section 7....
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
std::optional< V > find(const K &k) const
find() above, with the hash computed
static constexpr bool epochs
< the policy of clear()
Definition ConcurrentFlatTable.h:294
bool update(const K &k, F &&f)
sets the value of the key to f( x ), by a compare-and-swap of its word (of its place),...
std::optional< V > find(const K &k, std::uint64_t h) const
a copy of the value of the key of hash , or nothing
~ConcurrentFlatTable()
the frees of the pairs and of the vectors
V Value
< the type of the keys
Definition ConcurrentFlatTable.h:290
std::pair< Ref, bool > findOrInsert(const K &k, std::uint64_t h, const V &v)
the pair of the key , of hash , inserted with the value if absent; true if it was inserted
void clear(void)
removes every pair, keeping the capacity; with no other operation running.
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
C Clear
< the type of the values
Definition ConcurrentFlatTable.h:291
bool contains(const K &k) const
contains() above, with the hash computed
K Key
Definition ConcurrentFlatTable.h:289
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 Epoc...
Definition ConcurrentFlatTable.h:521
ConcurrentFlatTable & operator=(const ConcurrentFlatTable &)=delete
no copy, since the threads share it by its address
std::optional< K > findKey(const K &k, std::uint64_t h) const
a copy of the key held equal to by Eq, of hash , or nothing; never inserts
ConcurrentFlatTable(const ConcurrentFlatTable &)=delete
no copy, since the threads share it by its address
bool insert(const K &k, const V &v)
inserts the pair if is absent; true if it was, as findOrInsert()
std::pair< K, bool > findOrInsertKey(const K &k, std::uint64_t h)
the key held equal to , of hash , which is itself if absent and then inserted; true if it was insert...
std::size_t rehashes(void) const
the number of the growths of the table so far
void reserve(std::size_t n)
room for pairs before the table grows: the vector grown to places at least, as by an insertion,...
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,...
Definition ConcurrentFlatTable.h:310
ConcurrentFlatTable(std::size_t n=0, H h=H(), Eq eq=Eq())
the empty table, with room for pairs (and their nodes) before it grows, the hash h and the equality ...
std::pair< Ref, bool > findOrInsert(const K &k, const V &v)
findOrInsert() above, with the hash computed
Entry * entry(const K &k, std::uint64_t h) const
the pair of the key of hash , or nil, as find(); for a table neither packed nor inlined
std::size_t capacity(void) const
the number of the places of the vector in use
std::size_t size(void) const
the number of the pairs, the sum of the counters, exact when no operation runs
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 th...
bool empty(void) const
whether size() is 0
Definition ConcurrentFlatTable.h:499
std::uint64_t hash(const K &k) const
the hash of 64 bits of the key that the operations take
std::conditional_t< packed||inlined, V, Entry & > Ref
what an operation returns of the pair held: a copy of the value if packed or inlined,...
Definition ConcurrentFlatTable.h:325
bool contains(const K &k, std::uint64_t h) const
whether the key of hash is there, as find()
std::optional< K > findKey(const K &k) const
findKey() above, with the hash computed
void prefetch(std::uint64_t h) const
the lines that a lookup of the hash reads first (its tags and its word) fetched into the cache,...
a policy of clear() of ConcurrentFlatTable
Definition ConcurrentFlatTable.h:258
#define ADS_ABI_BEGIN
opens the inline namespace of the configuration
Definition Abi.h:106
#define ADS_ABI_END
closes the inline namespace of the configuration
Definition Abi.h:111
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
std::vector< std::vector< int > > Table
the table of the costs
Definition ApproximateMatching.h:80
Definition ADT.h:96
a pair of a table neither packed nor inlined, in a node that never moves
Definition ConcurrentFlatTable.h:318
const K key
the key
Definition ConcurrentFlatTable.h:319
V value
the value, the caller's to synchronize
Definition ConcurrentFlatTable.h:320
the clear() of ConcurrentFlatTable in by epochs: a stamp of the unsigned type per group of 8 places...
Definition ConcurrentFlatTable.h:224
S Stamp
Definition ConcurrentFlatTable.h:225
the clear() of ConcurrentFlatTable that writes 0 to every place, in time linear in its capacity; the ...
Definition ConcurrentFlatTable.h:216