ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SingleWriterFlatTable.h File Reference

A hash table that one thread writes and many threads read at once, beyond the book: the open addressing with linear probing of ConcurrentFlatTable [see ConcurrentFlatTable.h] for a table that only its owner fills, e.g., the CLOSED set of a worker of a parallel search that the others read to prune their own states. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/concurrent/ConcurrentFlatTable.h"
#include "ADS/dictionary/FastHash.h"
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <optional>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include "ADS/concurrent/impl/SingleWriterFlatTable.tpp"

Go to the source code of this file.

Classes

class  ADS::SingleWriterFlatTable< K, V, H, Eq, C >
 a DICTIONARY that one thread writes and many threads read at once, by linear probing on atomic words, with no read-modify-write per insertion, readers that read on while it grows, and no removal; cleared in \( O( 1 ) \) by the policy EpochClear More...

Namespaces

namespace  ADS

Detailed description

A hash table that one thread writes and many threads read at once, beyond the book: the open addressing with linear probing of ConcurrentFlatTable [see ConcurrentFlatTable.h] for a table that only its owner fills, e.g., the CLOSED set of a worker of a parallel search that the others read to prune their own states.

Since one thread makes every insertion, an insertion needs no read-modify-write: it reads the words from the place of its key, and at the first free one stores the value and then the key, the key by a store with release, which a reader reads with acquire; the counters of the threads and the protocol of Dekker by which ConcurrentFlatTable keeps its insertions apart from a growth, a sequentially consistent read-modify-write per insertion, are gone. On x86-64 an insertion is thus made of plain loads and stores.

The pairs lie in the words: a key and a value trivially copyable, each of at most 8 bytes, the key with a unique representation (or of floating point), as the pointers and the integers. A place is one word, the key, when the value is empty (a set), and two words otherwise, the key and the value, on the same line; a word is 0 when its place is free, so that the key made of zero bytes is kept apart, in two words of its own. A key word, once written, never changes until clear(); a value word changes by update(), of the writer, by a store with release.

The place of a key with the hash \( h \) in a vector of \( 2^l \) places is the top \( l \) bits of \( h \), and the table grows when its pairs pass half the places: the writer makes a vector twice as large, copies the pairs into it, and then makes it the vector in use by a store with release. The readers never wait: a reader that took the old vector reads it on, since the writer no longer writes it, and a lookup that ends at a free place reads the vector in use again, and starts over on the new one if the table grew meanwhile (see One writer and many readers). The vectors replaced are kept until clear() or the destruction, since a reader may still be on one; with sizes that double they occupy less than the vector in use. A lookup is wait-free while the table does not grow, and starts over at most once per growth.

clear() writes 0 to every place, unless the policy C is EpochClear< S >, by which, as in ConcurrentFlatTable, each group of 8 places has a stamp, and the group is in use only if its stamp is the epoch, which clear() raises: the writer renews a group out of use when an insertion first reaches it, writing 0 to its words and then storing the epoch with release, and a reader takes a group out of use for free.

Each operation is linearizable: an insertion at the store of its key word, a lookup that finds its key at the read of the key word, and one that does not at the read of the free place (or of the stamp of a group out of use). A key found, and its value, were written whole before the read, and so was everything that the writer wrote before inserting it, e.g., the object that a pointer key points to.

The contracts [see Contract.h]: one thread at a time calls the operations of the writer (findOrInsert(), findOrInsertKey(), insert(), update(), reserve() and clear()), any thread calls the lookups (find(), findKey(), contains(), size()), with no other thread during clear(), forEach() and consistent(); the hash passed is hash( k ) (audit); consistent() checks that each key is reached from its place with no free place in between, and the count.

See also
T. Maier, P. Sanders, R. Dementiev, Concurrent hash tables: fast and general(?)!, ACM Transactions on Parallel Computing 5(4), 16, 2019, doi:10.1145/3309206
D. E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching, 2nd edition, Addison-Wesley, 1998, section 6.4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa