ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ConcurrentCuckooTable.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File ConcurrentCuckooTable.h ----------------------*/
3/*--------------------------------------------------------------------------*/
211/*--------------------------------------------------------------------------*/
212/*----------------------------- DEFINITIONS --------------------------------*/
213/*--------------------------------------------------------------------------*/
214
215#ifndef __ADS_ConcurrentCuckooTable
216 #define __ADS_ConcurrentCuckooTable
217/* self-identification: #endif constant */
218
219/*--------------------------------------------------------------------------*/
220/*------------------------------ INCLUDES ----------------------------------*/
221/*--------------------------------------------------------------------------*/
222
223#include "ADS/Capabilities.h"
224#include "ADS/Contract.h"
225#include "ADS/Portable.h"
229
230#include <atomic>
231#include <bit>
232#include <cstddef>
233#include <cstdint>
234#include <functional>
235#include <memory>
236#include <mutex>
237#include <optional>
238#include <type_traits>
239#include <vector>
240
241/*--------------------------------------------------------------------------*/
242/*--------------------------- NAMESPACE ------------------------------------*/
243/*--------------------------------------------------------------------------*/
244
245namespace ADS
246{
247
248/*--------------------------------------------------------------------------*/
249/*---------------------- CLASS ConcurrentCuckooTable -----------------------*/
250/*--------------------------------------------------------------------------*/
251
253template< class K , class V , class H = DefaultHash< K > ,
254 class Eq = std::equal_to< K > >
256{
257
258/*--------------------------------------------------------------------------*/
259 public:
260 using Key = K;
261 using Value = V;
262
265 explicit ConcurrentCuckooTable( std::size_t n = 0 , H h = H() ,
266 Eq eq = Eq() );
267
272
305 [[nodiscard]] std::optional< V > find( const K & k ) const;
306
308 [[nodiscard]] bool contains( const K & k ) const;
309
388 bool insert( const K & k , const V & v );
389
395 bool insertOrAssign( const K & k , const V & v );
396
415 template< class F >
416 bool update( const K & k , F && f );
417
435 bool erase( const K & k );
436
439 [[nodiscard]] std::size_t size( void ) const;
440
442 [[nodiscard]] bool empty( void ) const;
443
445 [[nodiscard]] std::size_t capacity( void ) const;
446
448 [[nodiscard]] std::size_t rehashes( void ) const;
449
451 [[nodiscard]] std::size_t stashed( void ) const;
452
455 void reserve( std::size_t n );
456
458 void clear( void );
459
461 template< class F >
462 void forEach( F && f );
463
465 template< class F >
466 void forEach( F && f ) const;
467
470 [[nodiscard]] bool consistent( void ) const;
471
472/*--------------------------------------------------------------------------*/
473 private:
475 template< class T >
476 static constexpr std::size_t refAlign( void ) {
477 if constexpr( std::is_trivially_copyable_v< T > )
478 return( std::atomic_ref< T >::required_alignment );
479 else
480 return( alignof( T ) );
481 }
482
484 template< class T >
485 static constexpr bool refLockFree( void ) {
486 if constexpr( std::is_trivially_copyable_v< T > )
487 return( std::atomic_ref< T >::is_always_lock_free );
488 else
489 return( false );
490 }
491
492 public:
496 static constexpr bool optimistic = refLockFree< K >() && refLockFree< V >();
497
498/*--------------------------------------------------------------------------*/
499 private:
500 struct Slot
501 {
503 alignas( refAlign< K >() ) K key{};
505 alignas( refAlign< V >() ) V value{};
506 };
507
508 static constexpr std::size_t line = 64;
509
512 struct alignas( ( 4 * sizeof( Slot ) <= line ) &&
514 std::has_single_bit( 4 * sizeof( Slot ) )
516 ? 4 * sizeof( Slot )
518 : alignof( Slot ) ) Bucket
519 {
521 Slot s[ 4 ];
522 };
523 static constexpr std::size_t kStripes = 2048;
524 static constexpr int kLgStripes = 11;
525 static constexpr int kLgRegion = 8;
527 static constexpr int kDepth = 5;
528 static constexpr int kNodes = 1024;
529 static constexpr int kOverflow = 4;
531
533 struct alignas( line ) Stripe
534 {
536 std::atomic< std::uint64_t > version{ 0 };
538 std::atomic< std::int64_t > count{ 0 };
539 };
540
542 struct Table
543 {
545 int lg , rg;
547 std::uint32_t * T;
549 std::uint64_t * O;
551 Bucket * B;
554 explicit Table( int l , bool init = true );
556 Table( const Table & ) = delete;
558 Table & operator=( const Table & ) = delete;
560 [[nodiscard]] std::size_t overflowWords( void ) const {
561 return( ( ( buckets() * kOverflow ) + 63 ) >> 6 );
562 }
564 ~Table();
566 [[nodiscard]] Slot & S( std::size_t i ) const {
567 return( B[ i >> 2 ].s[ i & 3 ] );
568 }
570 [[nodiscard]] std::size_t buckets( void ) const {
571 return( std::size_t( 1 ) << lg );
572 }
573 };
574
576 class Hold
577 {
578 public:
580 explicit Hold( Stripe & s ) : f_s( s ) { }
582 Hold( const Hold & ) = delete;
584 Hold & operator=( const Hold & ) = delete;
586 ~Hold() { unlock( f_s ); }
587
588 private:
589 Stripe & f_s;
590 };
591
594 class HoldAll
595 {
596 public:
598 explicit HoldAll( const ConcurrentCuckooTable & t );
600 HoldAll( const HoldAll & ) = delete;
602 HoldAll & operator=( const HoldAll & ) = delete;
604 ~HoldAll();
606 void extend( int lg );
608 [[nodiscard]] std::size_t held( void ) const { return( f_n ); }
609
610 private:
611 Stripe * f_L;
612 std::size_t f_n = 0;
613 };
614
617 struct Node
618 {
620 std::size_t b;
623 int parent , from , depth;
625 std::uint32_t tag;
626 };
627
628 static void lock( Stripe & s );
629 static void unlock( Stripe & s );
630
632 void lockHelping( Stripe & s ) const;
633
636 bool claim( std::size_t & c ) const;
637
639 void help( void ) const;
640
644 void migrate( std::size_t c ) const;
645
647 [[nodiscard]] static int regionLg( int lg );
648
650 [[nodiscard]] std::uint64_t hash( const K & k ) const;
651
653 [[nodiscard]] static std::uint32_t tag( std::uint64_t h );
654
656 [[nodiscard]] static std::size_t first( std::uint64_t h , int lg );
657
660 [[nodiscard]] static std::size_t other( std::size_t b , std::uint32_t t ,
661 int rg );
662
664 [[nodiscard]] static std::uint32_t tags( const Table & T , std::size_t b );
665
667 static void setTags( Table & T , std::size_t b , std::uint32_t w );
668
670 [[nodiscard]] static std::size_t overflowBit( std::size_t b ,
671 std::uint32_t t );
672
675 [[nodiscard]] static bool overflowed( const Table & T , std::size_t b ,
676 std::uint32_t t );
677
679 static void markOverflow( Table & T , std::size_t b , std::uint32_t t );
680
682 [[nodiscard]] Table * lockRegion( std::uint64_t h ) const;
683
685 [[nodiscard]] Table * waitRegion( std::uint64_t h ) const;
686
689 [[nodiscard]] std::size_t locate( const Table & T , const K & k ,
690 std::uint32_t t , std::size_t b1 ,
691 std::size_t b2 , bool bit = true ) const;
692
694 static void fetch( const Table & T , std::size_t b );
695
699 int peek( const K & k , std::uint64_t h , V * out ) const;
700
703 bool lockedFind( const K & k , std::uint64_t h , V * out ) const;
704
706 [[nodiscard]] static int freePlace( const Table & T , std::size_t b );
707
709 template< class KK , class VV >
710 static void put( Table & T , std::size_t b , int i , std::uint32_t t ,
711 KK && k ,
712 VV && v );
713
715 static void assign( Table & T , std::size_t i , const V & v );
716
718 static void clearPlace( Table & T , std::size_t i );
719
722 static void move( Table & T , std::size_t a , int i , std::size_t d , int j );
723
727 [[nodiscard]] int search( const Table & T , std::uint64_t h ,
728 Node * N ) const;
729
732 std::size_t makeRoom( Table & T , std::uint64_t h ) const;
733
737 std::size_t room( Table & T , std::uint64_t h , std::int64_t n ) const;
738
742 bool placeInto( Table & N , std::uint64_t h , Slot && x ) const;
743
746 bool add( const K & k , const V & v , bool assign );
747
750 void rehash( int lg , HoldAll & g );
751
756 int grow( int lg , std::uint64_t h , const K & k , const V & v ,
757 bool assign );
758
760 bool fromStash( const K & k , V * out ) const;
761
764 template< class F >
765 bool inStash( const K & k , F && f );
766
769 void stashPut( Table & T , std::uint64_t h , Slot && x );
770
772 [[nodiscard]] bool inBucket( const Table & T , std::size_t b ) const;
773
774 std::unique_ptr< Stripe[] > f_L;
775 std::atomic< int > f_lg{ 1 };
776 std::atomic< Table * > f_table{ nullptr };
777 std::vector< std::unique_ptr< Table > > f_old;
779 std::unique_ptr< Table > f_cur;
780 std::atomic< std::size_t > f_rehashes{ 0 };
781
784 mutable std::atomic< std::uint64_t > f_claim{ 0 };
785 mutable std::atomic< std::size_t > f_done{ 0 };
786 mutable std::atomic< Table * > f_from{ nullptr };
787 mutable std::atomic< Table * > f_to{ nullptr };
788 mutable std::atomic< std::int64_t * > f_count{ nullptr };
790 mutable std::atomic< std::size_t > f_part{ 0 };
792 mutable std::vector< Slot > f_left;
794 mutable std::atomic< bool > f_leftLock{ false };
795 mutable std::mutex f_xm;
796 std::vector< Slot > f_X;
797 std::atomic< std::size_t > f_xn{ 0 };
798 H f_h;
799 Eq f_eq;
800
801 }; // end( class ConcurrentCuckooTable )
802
807template< class K , class V , class H , class Eq >
808inline constexpr bool
810
811/*--------------------------------------------------------------------------*/
812
813} // end( namespace ADS )
814
815/*--------------------------------------------------------------------------*/
816/*-------------------------- IMPLEMENTATION --------------------------------*/
817/*--------------------------------------------------------------------------*/
818
819#include "ADS/concurrent/impl/ConcurrentCuckooTable.tpp"
820
821/*--------------------------------------------------------------------------*/
822
823#endif /* ConcurrentCuckooTable.h included */
824
/*--------------------------------------------------------------------------*//*-------------------- End File ConcurrentCuckooTable.h --------------------*//*--------------------------------------------------------------------------*/
The cuckoo hashing with buckets of 4 places and a search in breadth of Li, Andersen,...
The capabilities of the structures, beyond the book: what an algorithm may ask of its argument to cho...
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::size_t rehashes(void) const
the number of the doublings of the table so far
static constexpr bool optimistic
true if the lookups read without locks, under the versions of the stripes: the keys and the values tr...
Definition ConcurrentCuckooTable.h:496
K Key
the type of the keys
Definition ConcurrentCuckooTable.h:260
std::size_t capacity(void) const
the number of the places, 4 per bucket
std::size_t stashed(void) const
the pairs in the stash, 0 but for degenerate hashes
std::optional< V > find(const K &k) const
a copy of the value of the key , or nothing
bool contains(const K &k) const
whether the key is there, as find()
bool erase(const K &k)
removes the key , if it is there; true if it was
bool insert(const K &k, const V &v)
associates the value to the key if is not there; true if it was not, and otherwise the value of s...
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 opera...
bool update(const K &k, F &&f)
applies f( V & ) to the value of the key under its locks; true if is there
ConcurrentCuckooTable(const ConcurrentCuckooTable &)=delete
no copy, since the threads share it by its address
V Value
the type of the values
Definition ConcurrentCuckooTable.h:261
void reserve(std::size_t n)
room for pairs before the table grows: the buckets doubled, under all the locks, up to the power of ...
void clear(void)
removes every pair, keeping the capacity, under all the locks
void forEach(F &&f)
applies f( const K & , 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...
ConcurrentCuckooTable(std::size_t n=0, H h=H(), Eq eq=Eq())
the empty table, with room for pairs before it grows, the hash h and the equality eq
ConcurrentCuckooTable & operator=(const ConcurrentCuckooTable &)=delete
no copy, since the threads share it by its address
bool insertOrAssign(const K &k, const V &v)
associates the value to the key ; true if was not there
void forEach(F &&f) const
applies f( const K & , const V & ) to every pair, under all the locks
bool empty(void) const
whether size() is 0
constexpr bool nonmonotone_values
whether the values of the nodes of the table T may grow as well as fall, so that its concurrent field...
Definition Capabilities.h:216
std::vector< std::vector< int > > Table
the table of the costs
Definition ApproximateMatching.h:80
Definition ADT.h:96