20 template <
typename... TArgs>
24 template <
typename TArg>
35 template <
typename Type>
47 template <
typename... TArgs>
48 Local(TArgs&&... args)
49 :
value(
std::forward<TArgs>(args)...) {}
59 INLINE constexpr Type operator()(
const Type& t1,
const Type& t2)
const {
69 template <
typename... TArgs>
71 : scheduler(scheduler) {
79 template <
typename TFunctor>
81 : scheduler(scheduler) {
91 return locals[idx.
value()].value;
98 return locals[idx.
value()].value;
106 return locals[threadId].value;
122 template <
typename TPredicate>
123 Type
accumulate(
const Type& initial,
const TPredicate& predicate)
const {
125 for (
const Type&
value : *
this) {
131 template <
typename T>
137 using Return = std::conditional_t<std::is_const<T>::value,
const Type&, Type&>;
140 return this->
data->value;
146 return locals.
begin();
151 return locals.
begin();
165 template <
typename... TArgs>
169 for (
Size i = 0; i < threadCnt; ++i) {
175 template <
typename TFunctor>
179 for (
Size i = 0; i < threadCnt; ++i) {
186 template <
typename Type,
typename TFunctor>
191 TFunctor&& functor) {
193 parallelFor(scheduler, storage, from, to, granularity, std::forward<TFunctor>(functor));
197 template <
typename Type,
typename TFunctor>
202 const Size granularity,
203 TFunctor&& functor) {
208 Type& value = storage.
local();
209 for (
Size i = n1; i < n2; ++i) {
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
Ordinary iterator for custom containers.
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
Interface for executing tasks (potentially) asynchronously.
INLINE void parallelFor(IScheduler &scheduler, ThreadLocal< Type > &storage, const Size from, const Size to, TFunctor &&functor)
Overload of parallelFor that passes thread-local storage into the functor.
void reserve(const TCounter newMaxSize)
Allocates enough memory to store the given number of elements.
INLINE Iterator< StorageType > end() noexcept
StorageType & emplaceBack(TArgs &&... args)
Constructs a new element at the end of the array in place, using the provided arguments.
INLINE TCounter size() const noexcept
INLINE Iterator< StorageType > begin() noexcept
Interface that allows unified implementation of sequential and parallelized versions of algorithms.
virtual Optional< Size > getThreadIdx() const =0
Returns the index of the calling thread.
virtual void parallelFor(const Size from, const Size to, const Size granularity, const Function< void(Size n1, Size n2)> &functor)
Processes the given range concurrently.
virtual Size getRecommendedGranularity() const =0
Returns a value of granularity that is expected to perform well with the current thread count.
virtual Size getThreadCnt() const =0
Returns the number of threads used by this scheduler.
Simple (forward) iterator over continuous array of objects of type T.
INLINE Type & value()
Returns the reference to the stored value.
LocalIterator(Iterator< T > iter)
INLINE Return operator*() const
std::conditional_t< std::is_const< T >::value, const Type &, Type & > Return
Template for storing a copy of a value for every thread in given scheduler.
Type accumulate(const Type &initial, const TPredicate &predicate) const
Performs an accumulation of thread-local values.
LocalIterator< Local > begin()
Returns the iterator to the first element in the thread-local storage.
ThreadLocal(IScheduler &scheduler, TFunctor &&functor)
Constructs a thread-local storage using a functor.
LocalIterator< Local > end()
Returns the iterator to the first element in the thread-local storage.
INLINE Type & local()
Return a value for current thread.
LocalIterator< const Local > end() const
Returns the iterator to the first element in the thread-local storage.
Type accumulate(const Type &initial=Type(0._f)) const
Performs an accumulation of thread-local values.
ThreadLocal(IScheduler &scheduler, TArgs &&... args)
Constructs a thread-local storage from a list of values.
INLINE Type & value(const Size threadId)
Returns the storage corresponding to the thread with given index.
INLINE const Type & local() const
Return a value for current thread.
LocalIterator< const Local > begin() const
Returns the iterator to the first element in the thread-local storage.
Overload of std::swap for Sph::Array.
std::conditional_t< IsCallable< TArg >::value, FunctorInitTag, ValueInitTag > Tag