An open source method of characteristics neutron transport code.
|
A thread-safe hash map supporting insertion and lookup operations. More...
#include "src/ParallelHashMap.h"
Public Member Functions | |
ParallelHashMap (size_t M=64, size_t L=64) | |
Constructor generates initial underlying table as a fixed-sized hash map and initializes concurrency structures. | |
virtual | ~ParallelHashMap () |
Destructor frees memory associated with fixed-sized hash map and concurrency structures. | |
bool | contains (K &key) |
Determine whether the parallel hash map contains a given key. More... | |
V | at (K &key) |
Determine the value associated with a given key. More... | |
void | update (K &key, V value) |
Updates the value associated with a key in the parallel hash map. More... | |
void | insert (K key, V value) |
Insert a given key/value pair into the parallel hash map. More... | |
long | insert_and_get_count (K key, V value) |
Insert a given key/value pair into the parallel hash map and return the order number. More... | |
size_t | size () |
Returns the number of key/value pairs in the underlying table. More... | |
size_t | bucket_count () |
Returns the number of buckets in the underlying table. More... | |
size_t | num_locks () |
Returns the number of locks in the parallel hash map. More... | |
K * | keys () |
Returns an array of the keys in the underlying table. More... | |
V * | values () |
Returns an array of the values in the underlying table. More... | |
void | clear () |
Clears all key/value pairs form the hash table. | |
void | setFixedSize () |
Prevents the parallel hash map from further resizing. | |
void | print_buckets () |
Prints the contents of each bucket to the screen. More... | |
void | realloc (size_t M) |
Reallocates the underlying table to the desired size. More... | |
A thread-safe hash map supporting insertion and lookup operations.
The ParallelHashMap class is built on top of the FixedHashMap class, supporting insertion and lookup operations but not deletion as deletion is not needed in the OpenMOC application. This hash table uses chaining for collisions, as defined in FixedHashMap. It offers lock free lookups in O(1) time on average and fine-grained locking for insertions in O(1) time on average as well. Resizing is conducted periodically during inserts, although the starting table size can be chosen to limit the number of resizing operations.
V ParallelHashMap< K, V >::at | ( | K & | key | ) |
Determine the value associated with a given key.
This function follows the same algorithm as "contains" except that the value associated with the searched key is returned. First the thread accessing the table acquires the lock corresponding with the associated bucket based on the key. Then the linked list in the bucket is searched for the key. An exception is thrown if the key is not found. When the thread has finished accessing the table, it releases the lock.
key | key to be searched |
size_t ParallelHashMap< K, V >::bucket_count | ( | ) |
Returns the number of buckets in the underlying table.
bool ParallelHashMap< K, V >::contains | ( | K & | key | ) |
Determine whether the parallel hash map contains a given key.
First the thread accessing the table announces its presence and which table it is reading. Then the linked list in the bucket associated with the key is searched without setting any locks to determine whether the key is present. When the thread has finished accessing the table, the announcement is reset to NULL. The announcement ensures that the data in the map is not freed during a resize until all threads have finished accessing the map.
key | key to be searched |
void ParallelHashMap< K, V >::insert | ( | K | key, |
V | value | ||
) |
Insert a given key/value pair into the parallel hash map.
First the underlying table is checked to determine if a resize should be conducted. Then, the table is checked to see if it already contains the key. If so, the key/value pair is not inserted and the function returns. Otherwise, the lock of the associated bucket is acquired and the key/value pair is added to the bucket.
key | key of the key/value pair to be inserted |
value | value of the key/value pair to be inserted |
long ParallelHashMap< K, V >::insert_and_get_count | ( | K | key, |
V | value | ||
) |
Insert a given key/value pair into the parallel hash map and return the order number.
First the underlying table is checked to determine if a resize should be conducted. Then, the table is checked to see if it already contains the key. If so, the key/value pair is not inserted and the function returns. Otherwise, the lock of the associated bucket is acquired and the key/value pair is added to the bucket.
key | key of the key/value pair to be inserted |
value | value of the key/value pair to be inserted |
K * ParallelHashMap< K, V >::keys | ( | ) |
Returns an array of the keys in the underlying table.
All buckets are scanned in order to form a list of all keys present in the table and then the list is returned. Threads announce their presence to ensure table memory is not freed during access. WARNING: The user is responsible for freeing the allocated memory once the array is no longer needed.
size_t ParallelHashMap< K, V >::num_locks | ( | ) |
Returns the number of locks in the parallel hash map.
void ParallelHashMap< K, V >::print_buckets | ( | ) |
Prints the contents of each bucket to the screen.
All buckets are scanned and the contents of the buckets are printed, which are pointers to linked lists. If the pointer is NULL suggesting that the linked list is empty, NULL is printed to the screen. Threads announce their presence to ensure table memory is not freed during access.
void ParallelHashMap< K, V >::realloc | ( | size_t | M | ) |
Reallocates the underlying table to the desired size.
M | The requested new size |
size_t ParallelHashMap< K, V >::size | ( | ) |
Returns the number of key/value pairs in the underlying table.
void ParallelHashMap< K, V >::update | ( | K & | key, |
V | value | ||
) |
Updates the value associated with a key in the parallel hash map.
The thread first acquires the lock for the bucket associated with the key is acquired, then the linked list in the bucket is searched for the key. If the key is not found, an exception is returned. When the key is found, the value is updated and the lock is released.
key | the key of the key/value pair to be updated |
value | the new value for the key/value pair |
V * ParallelHashMap< K, V >::values | ( | ) |
Returns an array of the values in the underlying table.
All buckets are scanned in order to form a list of all values present in the table and then the list is returned. Threads announce their presence to ensure table memory is not freed during access. WARNING: The user is responsible for freeing the allocated memory once the array is no longer needed.