Template Class ThreadVec
Defined in File ThreadVec.hpp
Class Documentation
-
template<class T>
class ThreadVec A class to automate private thread copies of objects.
This class is a container to automate the process of each thread having its own private copy of an object. It is a vector that on creation automatically resizes itself large enough for the program’s own maximum possible OpenMP team size (see the constructor’s own comment), and when accessed by a thread automatically returns the element of the vector that is private to that thread.
- Template Parameters:
T – Type of object the ThreadVec holds
Public Functions
-
inline ThreadVec()
Construct an empty ThreadVec.
Sized via omp_get_thread_limit() rather than omp_get_max_threads(): the latter only reflects the default team size at the moment of construction, and two things can raise the real team size above that later – omp_set_num_threads() called after this ThreadVec already exists, or a parallel region with an explicit num_threads(n) clause requesting more threads than omp_get_max_threads() currently reports. Every access below is keyed by the real omp_get_thread_num() of whichever team ends up running, so an undersized ThreadVec would then be indexed out of bounds. omp_get_thread_limit() instead reflects the program-wide ceiling on OpenMP threads (bounded by OMP_THREAD_LIMIT if set), which is stable regardless of any of that – it also needs no parallel region of its own, so it stays correct regardless of whether construction happens inside or outside one (a concern for omp_get_max_threads() too: constructing from inside an already-active parallel region makes a fresh, nested one collapse to a team of one by default, sizing this ThreadVec for a single thread against the outer team’s own larger omp_get_thread_num() range).
Most runtimes report a very large (effectively unbounded) sentinel for omp_get_thread_limit() when OMP_THREAD_LIMIT isn’t set, so the result is clamped against a hardware_concurrency-based cap (see threadVecCapOversubscription) to avoid allocating an enormous vector in the common case.
-
~ThreadVec() = default
-
inline auto size() const
Return the number of distinct objects stored.
- Returns:
The number of objects stored
-
inline auto operator()() -> auto&
Return the element private to this thread.
- Returns:
Return the element of the ThreadVec private to this thread
-
inline auto operator()() const -> const auto&
Return the element private to this thread.
- Returns:
Return the element of the ThreadVec private to this thread
-
inline auto operator[](const int i) -> auto&
Return the object belonging to the specified thread.
- Parameters:
i – The thread number whose object should be returned
- Returns:
The object belonging to thread i
-
inline auto operator[](const int i) const -> const auto&
Return the object belonging to the specified thread.
- Parameters:
i – The thread number whose object should be returned
- Returns:
The object belonging to thread i
-
inline auto begin()
Return the beginning of the thread vector.
-
inline auto end()
Return the end of the thread vector.