Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
ParallelFor.h File Reference

Bounded, adaptive splitting of independent work over an index range. More...

#include <algorithm>
#include <atomic>
#include <cstddef>
#include <functional>
#include <thread>
#include <type_traits>
#include <utility>
#include "donner/base/Utils.h"
#include "donner/base/tasks/FalseSharing.h"
#include "donner/base/tasks/TaskScope.h"
Include dependency graph for ParallelFor.h:

Classes

struct  donner::tasks::detail::ParallelRangeSplitter< F >
 All tasks in one loop share the body and join the same scope. More...
struct  donner::tasks::detail::ParallelChunkCursor
 Keep contended claims off the cache line containing read-mostly loop state. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.

Enumerations

enum class  ParallelForStrategy {
  Adaptive ,
  SharedCursor
}

Functions

constexpr std::size_t donner::tasks::detail::ParallelForGrain (std::size_t length, std::size_t requested)
 Caps the number of leaf ranges without rounding the length past SIZE_MAX.
template<typename F>
void donner::tasks::detail::SharedCursorLoop (TaskScheduler &scheduler, std::size_t begin, std::size_t end, std::size_t grain, std::size_t pieces, const F &body)
 Claim bounded chunk ordinals so speculative claims after the last chunk cannot wrap an index.
template<typename F>
void donner::tasks::detail::ParallelForWithStrategy (ParallelForStrategy strategy, TaskScheduler &scheduler, std::size_t begin, std::size_t end, std::size_t grain, const F &body)
 Selects the partitioner for tests and comparative measurements.
template<typename F>
void donner::tasks::ParallelFor (TaskScheduler &scheduler, std::size_t begin, std::size_t end, std::size_t grain, F &&body)
 Invokes one shared, const-callable body on disjoint subranges and joins them before returning.

Detailed Description

Bounded, adaptive splitting of independent work over an index range.

Function Documentation

◆ ParallelFor()

template<typename F>
void donner::tasks::ParallelFor ( TaskScheduler & scheduler,
std::size_t begin,
std::size_t end,
std::size_t grain,
F && body )

Invokes one shared, const-callable body on disjoint subranges and joins them before returning.

An empty range returns immediately. Reversed bounds fail a release assertion. With zero workers in ordinary mode, a nonempty range is passed to the body once on the calling thread.

Worker count and steal timing can change subrange boundaries and visitation order. Deterministic per-index computations that write disjoint outputs give the same results across schedules. A reduction grouped by callback boundaries can produce different results. Seeded adaptive execution uses a fixed four-piece split budget on one thread. The grain, range and capacity bounds still apply, and the seed can change visitation order.

Parameters
schedulerThe scheduler used to run the work.
beginThe first index in the half-open range.
endThe exclusive upper bound.
grainRequested grain; zero means one. The effective grain bounds the loop to 4096 chunks.
bodyCalled with the first and exclusive last index of each subrange. Concurrent calls must write disjoint outputs or provide their own synchronization.