23#ifndef OPM_THREAD_SAFE_MAP_BUILDER_HPP
24#define OPM_THREAD_SAFE_MAP_BUILDER_HPP
67 constexpr static bool value =
false;
70 template<
class Key,
class T,
class Compare,
class Allocator>
71 struct is_ordered<std::map<Key,T,Compare,Allocator>>
73 constexpr static bool value =
true;
77 using Key =
typename Map::key_type;
78 using Value =
typename Map::mapped_type;
83 : map_(map), num_threads_(num_threads), mode_(mode)
85 if (num_threads_ > 1) {
86 thread_values_.resize(num_threads_);
100 assert(mode_ == MapBuilderInsertionMode::Insert_Or_Assign);
102 if (num_threads_ > 1) {
103 thread_values_[omp_get_thread_num()].
104 emplace_back(key, std::forward<M>(value));
108 map_.insert_or_assign(key, std::forward<M>(value));
113 template<
class... Args>
116 assert(mode_ == MapBuilderInsertionMode::Emplace);
118 if (num_threads_ > 1) {
119 thread_values_[omp_get_thread_num()].
120 emplace_back(std::forward<Args>(args)...);
124 map_.emplace(std::forward<Args>(args)...);
131 if (num_threads_ > 1 && !thread_values_.empty()) {
132 if constexpr (!is_ordered<Map>::value) {
133 const std::size_t size =
134 std::accumulate(thread_values_.begin(),
135 thread_values_.end(), std::size_t(0),
136 [](std::size_t s,
const auto& v) { return s + v.size(); });
139 for (
int i = 0; i < num_threads_; ++i) {
140 for (
const auto& it : thread_values_[i]) {
141 if (mode_== MapBuilderInsertionMode::Insert_Or_Assign) {
142 map_.insert_or_assign(std::move(it.first), std::move(it.second));
144 map_.emplace(std::move(it.first), std::move(it.second));
149 thread_values_.clear();
153 std::vector<std::list<std::pair<Key,Value>>> thread_values_;
Definition ThreadSafeMapBuilder.hpp:62
~ThreadSafeMapBuilder()
The destructor (optionally) assembles the final map.
Definition ThreadSafeMapBuilder.hpp:91
void insert_or_assign(const Key &key, M &&value)
Insert a value into the map.
Definition ThreadSafeMapBuilder.hpp:98
ThreadSafeMapBuilder(Map &map, const int num_threads, const MapBuilderInsertionMode mode)
Constructor for a given map and number of threads.
Definition ThreadSafeMapBuilder.hpp:81
void emplace(Args &&... args)
Insert a value into the map.
Definition ThreadSafeMapBuilder.hpp:114
void finalize()
Assembles the final map.
Definition ThreadSafeMapBuilder.hpp:129
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30
MapBuilderInsertionMode
Utility class for adding multi-threading to loops that are building maps.
Definition ThreadSafeMapBuilder.hpp:55