4#ifndef GREM_EXECUTION_EXECUTOR_HPP
5#define GREM_EXECUTION_EXECUTOR_HPP
64 template <
typename EntReg,
typename ResReg>
68 .resources = resources,
91 template <
typename Range,
typename UnaryProcedure>
93 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
96 for (T& element : elements) {
122 template <
typename Range,
typename UnaryProcedure>
124#ifdef GREM_USE_MULTITHREADING
125 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
129 if (chunkCount <= 1 || elements.size() < chunkCount) {
134 struct ParallelTaskContext {
135 UnaryProcedure operation;
137 } parallelTaskContext{std::move(operation)};
139 const size_t chunkSize = (elements.size() + chunkCount - 1) / chunkCount;
140 for (
size_t chunkOffset = 0; chunkOffset < elements.size(); chunkOffset += chunkSize) {
141 parallelTaskContext.chunks.push_back(elements.subspan(chunkOffset,
min(chunkSize, elements.size() - chunkOffset)));
147 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
148 for (T& element : parallelTaskContext.chunks[subTaskIndex]) {
149 parallelTaskContext.operation(element);
183 template <
typename IndexRange,
typename Range,
typename UnaryProcedure>
185 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
186 using Index = std::decay_t<
decltype(*std::data(indices))>;
190 for (
const Index index : elementIndices) {
191 operation(elements[index]);
223 template <
typename IndexRange,
typename Range,
typename UnaryProcedure>
225#ifdef GREM_USE_MULTITHREADING
226 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
227 using Index = std::decay_t<
decltype(*std::data(indices))>;
232 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
237 struct ParallelTaskContext {
238 UnaryProcedure operation;
241 } parallelTaskContext{std::move(operation), elements.
data()};
243 const size_t chunkSize = (elementIndices.size() + chunkCount - 1) / chunkCount;
244 for (
size_t chunkOffset = 0; chunkOffset < elementIndices.size(); chunkOffset += chunkSize) {
245 parallelTaskContext.chunks.push_back(elementIndices.subspan(chunkOffset,
min(chunkSize, elementIndices.size() - chunkOffset)));
251 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
252 for (const Index index : parallelTaskContext.chunks[subTaskIndex]) {
253 parallelTaskContext.operation(parallelTaskContext.pointer[index]);
258 return executeSparseOperation(indices,
range, std::forward<UnaryProcedure>(operation));
283 template <
typename IndexedProcedure>
286 for (
size_t i = begin; i < end; ++i) {
287 operation(
size_t{i});
315 template <
typename IndexedProcedure>
317#ifdef GREM_USE_MULTITHREADING
319 const size_t size = end - begin;
321 if (chunkCount <= 1 || size < chunkCount) {
326 struct ParallelTaskContext {
327 IndexedProcedure operation;
329 } parallelTaskContext{std::move(operation)};
331 const size_t chunkSize = (size + chunkCount - 1) / chunkCount;
332 for (
size_t chunkOffset = begin; chunkOffset < end; chunkOffset += chunkSize) {
333 parallelTaskContext.chunks.emplace_back(chunkOffset,
min(chunkSize, end - chunkOffset));
339 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
340 const Pair<size_t> chunk = parallelTaskContext.chunks[subTaskIndex];
341 const size_t end = chunk.first + chunk.second;
342 for (size_t i = chunk.first; i < end; ++i) {
343 parallelTaskContext.operation(size_t{i});
348 return executeIndexedOperation(begin, end, std::forward<IndexedProcedure>(operation));
374 template <
typename Procedure,
typename FirstRange,
typename... OtherRanges>
376 const size_t size = std::size(
range);
377 Tuple pointers{std::data(
range), std::data(ranges)...};
378 for (
size_t i = 0; i < size; ++i) {
379 [&]<
size_t... Indices>(std::index_sequence<Indices...>) ->
void {
381 }(std::make_index_sequence<1 +
sizeof...(OtherRanges)>{});
410 template <
typename Procedure,
typename FirstRange,
typename... OtherRanges>
412#ifdef GREM_USE_MULTITHREADING
413 const size_t size = std::size(
range);
415 if (chunkCount <= 1 || size < chunkCount) {
417 return executeApplication(std::forward<Procedure>(operation), std::forward<FirstRange>(
range), std::forward<OtherRanges>(ranges)...);
420 struct ParallelTaskContext {
422 Tuple<
decltype(std::data(
range)),
decltype(std::data(ranges))...> pointers;
424 } parallelTaskContext{std::move(operation),
Tuple{std::data(
range), std::data(ranges)...}};
426 const size_t chunkSize = (size + chunkCount - 1) / chunkCount;
427 for (
size_t chunkOffset = 0; chunkOffset < size; chunkOffset += chunkSize) {
428 parallelTaskContext.chunks.emplace_back(chunkOffset,
min(chunkSize, size - chunkOffset));
434 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
435 const Pair<size_t> chunk = parallelTaskContext.chunks[subTaskIndex];
436 const size_t end = chunk.first + chunk.second;
437 for (size_t i = chunk.first; i < end; ++i) {
438 [&]<size_t... Indices>(std::index_sequence<Indices...>) -> void {
439 parallelTaskContext.operation(*(get<Indices>(parallelTaskContext.pointers) + i)...);
440 }(std::make_index_sequence<1 + sizeof...(OtherRanges)>{});
445 return executeApplication(std::forward<Procedure>(operation), std::forward<FirstRange>(
range), std::forward<OtherRanges>(ranges)...);
478 template <
typename IndexRange,
typename Procedure,
typename FirstRange,
typename... OtherRanges>
480 FirstRange&&
range, OtherRanges&&... ranges) {
481 using Index = std::decay_t<
decltype(*std::data(indices))>;
484 for (
const Index index : elementIndices) {
485 operation(
range[index], ranges[index]...);
520 template <
typename IndexRange,
typename Procedure,
typename FirstRange,
typename... OtherRanges>
522#ifdef GREM_USE_MULTITHREADING
523 using Index = std::decay_t<
decltype(*std::data(indices))>;
527 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
529 return executeSparseApplication(indices, std::forward<Procedure>(operation), std::forward<FirstRange>(
range), std::forward<OtherRanges>(ranges)...);
532 struct ParallelTaskContext {
534 Tuple<
decltype(std::data(
range)),
decltype(std::data(ranges))...> pointers;
536 } parallelTaskContext{std::move(operation),
Tuple{std::data(
range), std::data(ranges)...}};
538 const size_t chunkSize = (elementIndices.size() + chunkCount - 1) / chunkCount;
539 for (
size_t chunkOffset = 0; chunkOffset < elementIndices.size(); chunkOffset += chunkSize) {
540 parallelTaskContext.chunks.push_back(elementIndices.subspan(chunkOffset,
min(chunkSize, elementIndices.size() - chunkOffset)));
546 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
547 for (const Index index : parallelTaskContext.chunks[subTaskIndex]) {
548 [&]<size_t... Indices>(std::index_sequence<Indices...>) -> void {
549 parallelTaskContext.operation(*(get<Indices>(parallelTaskContext.pointers) + index)...);
550 }(std::make_index_sequence<1 + sizeof...(OtherRanges)>{});
555 return executeSparseApplication(indices, std::forward<Procedure>(operation), std::forward<FirstRange>(
range), std::forward<OtherRanges>(ranges)...);
581 template <
typename Range,
typename RandomAccessIterator,
typename UnaryMapping>
583 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
586 for (
size_t i = 0; i < elements.size(); ++i) {
587 output[
static_cast<ptrdiff_t
>(i)] = transformationFunction(elements[i]);
616 template <
typename Range,
typename RandomAccessIterator,
typename UnaryMapping>
618#ifdef GREM_USE_MULTITHREADING
619 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
623 if (chunkCount <= 1 || elements.size() < chunkCount) {
625 return executeTransformation(
range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
628 struct ParallelTaskContext {
629 UnaryMapping transformationFunction;
630 RandomAccessIterator output;
632 } parallelTaskContext{std::move(transformationFunction), std::move(output)};
634 const size_t chunkSize = (elements.size() + chunkCount - 1) / chunkCount;
635 for (
size_t chunkOffset = 0; chunkOffset < elements.size(); chunkOffset += chunkSize) {
636 parallelTaskContext.chunks.push_back(elements.subspan(chunkOffset,
min(chunkSize, elements.size() - chunkOffset)));
642 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
643 const Span<T> chunk = parallelTaskContext.chunks[subTaskIndex];
644 for (size_t i = 0; i < chunk.size(); ++i) {
645 parallelTaskContext.output[static_cast<ptrdiff_t>(i)] = parallelTaskContext.transformationFunction(chunk[i]);
650 return executeTransformation(
range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
681 template <
typename IndexRange,
typename Range,
typename RandomAccessIterator,
typename UnaryMapping>
683 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
684 using Index = std::decay_t<
decltype(*std::data(indices))>;
688 for (
size_t i = 0; i < elementIndices.size(); ++i) {
689 output[
static_cast<ptrdiff_t
>(i)] = transformationFunction(elements[elementIndices[i]]);
722 template <
typename IndexRange,
typename Range,
typename RandomAccessIterator,
typename UnaryMapping>
724#ifdef GREM_USE_MULTITHREADING
725 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
726 using Index = std::decay_t<
decltype(*std::data(indices))>;
731 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
736 struct ParallelTaskContext {
737 UnaryMapping transformationFunction;
739 RandomAccessIterator output;
741 } parallelTaskContext{std::move(transformationFunction), elements.
data(), std::move(output)};
743 const size_t chunkSize = (elementIndices.size() + chunkCount - 1) / chunkCount;
744 for (
size_t chunkOffset = 0; chunkOffset < elementIndices.size(); chunkOffset += chunkSize) {
745 parallelTaskContext.chunks.push_back(elementIndices.subspan(chunkOffset,
min(chunkSize, elementIndices.size() - chunkOffset)));
751 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
752 const Span<const Index> chunk = parallelTaskContext.chunks[subTaskIndex];
753 for (size_t i = 0; i < chunk.size(); ++i) {
754 parallelTaskContext.output[static_cast<ptrdiff_t>(i)] = parallelTaskContext.transformationFunction(parallelTaskContext.pointer[chunk[i]]);
759 return executeSparseTransformation(indices,
range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
783 template <
typename Range,
typename InitialValue,
typename AssociativeBinaryFunction>
784 [[nodiscard]] InitialValue
executeReduction(
const Range&
range,
const InitialValue& initialValue, AssociativeBinaryFunction reductionFunction) {
785 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
788 InitialValue result = initialValue;
789 for (T& element : elements) {
790 result = reductionFunction(std::move(result), element);
818 template <
typename Range,
typename InitialValue,
typename AssociativeBinaryFunction>
820#ifdef GREM_USE_MULTITHREADING
821 using T = std::remove_pointer_t<
decltype(std::data(
range))>;
825 if (chunkCount <= 1 || elements.size() < chunkCount) {
827 return executeReduction(
range, initialValue, std::forward<AssociativeBinaryFunction>(reductionFunction));
830 struct alignas(64) IntermediateResult {
834 struct ParallelTaskContext {
835 AssociativeBinaryFunction reductionFunction;
838 } parallelTaskContext{std::move(reductionFunction)};
840 const size_t chunkSize = (elements.size() + chunkCount - 1) / chunkCount;
841 for (
size_t chunkOffset = 0; chunkOffset < elements.size(); chunkOffset += chunkSize) {
842 parallelTaskContext.chunks.push_back(elements.subspan(chunkOffset,
min(chunkSize, elements.size() - chunkOffset)));
845 if (parallelTaskContext.chunks.empty()) {
848 parallelTaskContext.intermediateResults.
resize(parallelTaskContext.chunks.size(), IntermediateResult{initialValue});
853 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
854 InitialValue& intermediateResult = parallelTaskContext.intermediateResults[subTaskIndex].value;
855 InitialValue localResult = std::move(intermediateResult);
856 for (T& element : parallelTaskContext.chunks[subTaskIndex]) {
857 localResult = parallelTaskContext.reductionFunction(std::move(localResult), element);
859 intermediateResult = std::move(localResult);
863 InitialValue result = std::move(parallelTaskContext.intermediateResults.front().value);
864 for (
size_t i = 1; i < parallelTaskContext.chunks.size(); ++i) {
865 result = parallelTaskContext.reductionFunction(std::move(result), std::move(parallelTaskContext.intermediateResults[i].value));
869 return executeReduction(
range, initialValue, std::forward<AssociativeBinaryFunction>(reductionFunction));
1038 sharedMemory.resize(
static_cast<size_t>(requiredSharedMemorySize));
1045 if (taskRequiredSharedMemorySize >
sharedMemory.size()) {
1046 sharedMemory.resize(
static_cast<size_t>(taskRequiredSharedMemorySize));
1129 : implementation(std::
move(implementation)) {
1135 implementation->executeTaskGraph(tasks, context, requiredSharedMemorySize, name, statistics);
1141 implementation->executeParallelTasks(subTaskContext, subTaskCount, subTask, taskRequiredSharedMemorySize, statistics);
1145 return (implementation) ? implementation->getMaxParallelism() : 0;
1154 return implementation.get();
1163 return implementation.get();
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition Allocation.hpp:25
Definition SmallArrayList.hpp:26
void resize(size_type count)
Definition SmallArrayList.hpp:535
constexpr pointer data() const noexcept
Definition Span.hpp:203
static GREM_ALWAYS_INLINE unsigned int hardware_concurrency() noexcept
Definition Thread.hpp:113
Nullable RAII data pointer with exclusive ownership of an object that is automatically destroyed on p...
Definition UniquePointer.hpp:77
Executor * get() noexcept
Get a pointer to the underlying Executor implementation.
Definition Executor.hpp:1153
void executeParallelTasks(void *subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize, Statistics *statistics) override
Execute a set parallel sub-tasks through a ParallelTask function that performs a specific chunk of wo...
Definition Executor.hpp:1138
Task::ParallelCount getMaxParallelism() const noexcept override
Get the maximum number of parallel threads of execution that this executor can run tasks on simultane...
Definition Executor.hpp:1144
DynamicExecutor(const DynamicExecutorOptions &options)
Construct a dynamic executor with an implementation chosen based on a set of requirements.
DynamicExecutor(UniquePointer< Executor > implementation)
Construct a dynamic executor with a specific implementation.
Definition Executor.hpp:1128
void executeTaskGraph(Span< const Task > tasks, void *context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics *statistics) override
Execute a pre-built task graph using this executor.
Definition Executor.hpp:1133
const Executor * get() const noexcept
Get a pointer to the underlying Executor implementation.
Definition Executor.hpp:1162
Interface to a pool of execution resources (e.g. threads) for executing ad-hoc parallel tasks and pre...
Definition Executor.hpp:34
void executeParallelTransformation(const Range &range, RandomAccessIterator output, UnaryMapping transformationFunction)
Transform each element in a given range, in up to getMaxParallelism() parallel chunks using this exec...
Definition Executor.hpp:617
void executeOperation(Range &&range, UnaryProcedure operation)
Execute an operation on each element in a given range, sequentially.
Definition Executor.hpp:92
void executeParallelOperation(Range &&range, UnaryProcedure operation)
Execute an operation on each element in a given range, in up to getMaxParallelism() parallel chunks u...
Definition Executor.hpp:123
void executeParallelIndexedOperation(size_t begin, size_t end, IndexedProcedure operation)
Execute an operation for each index in a given interval, in up to getMaxParallelism() parallel chunks...
Definition Executor.hpp:316
void executeSparseTransformation(const IndexRange &indices, const Range &range, RandomAccessIterator output, UnaryMapping transformationFunction)
Transform each indexed element specified by a given range of indices, sequentially.
Definition Executor.hpp:682
InitialValue executeReduction(const Range &range, const InitialValue &initialValue, AssociativeBinaryFunction reductionFunction)
Perform a reduction over each element in a given range, sequentially.
Definition Executor.hpp:784
void executeParallelSparseOperation(const IndexRange &indices, Range &&range, UnaryProcedure operation)
Execute an operation on each indexed element specified by a given range of indices,...
Definition Executor.hpp:224
virtual void executeParallelTasks(void *subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize, Statistics *statistics)=0
Execute a set parallel sub-tasks through a ParallelTask function that performs a specific chunk of wo...
void executeSparseApplication(const IndexRange &indices, Procedure operation, FirstRange &&range, OtherRanges &&... ranges)
Execute an operation for each indexed row of elements specified by a given range of indices in a give...
Definition Executor.hpp:479
void executeParallelApplication(Procedure operation, FirstRange &&range, OtherRanges &&... ranges)
Execute an operation for each row of elements in a given set of equally sized column ranges,...
Definition Executor.hpp:411
virtual Task::ParallelCount getMaxParallelism() const noexcept=0
Get the maximum number of parallel threads of execution that this executor can run tasks on simultane...
void executeSparseOperation(const IndexRange &indices, Range &&range, UnaryProcedure operation)
Execute an operation on each indexed element specified by a given range of indices,...
Definition Executor.hpp:184
void executeIndexedOperation(size_t begin, size_t end, IndexedProcedure operation)
Execute an operation for each index in a given interval, sequentially.
Definition Executor.hpp:284
void executeApplication(Procedure operation, FirstRange &&range, OtherRanges &&... ranges)
Execute an operation for each row of elements in a given set of equally sized column ranges,...
Definition Executor.hpp:375
static void executeTaskGraphSequentially(Span< const Task > tasks, void *context, byte *sharedMemory, CStringView name, Statistics *statistics)
Basic implementation of executeTaskGraph() that executes the tasks sequentially, directly on the call...
static void executeParallelTasksSequentially(void *subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, byte *taskSharedMemory, Statistics *statistics)
Basic implementation of executeParallelTasks() that executes the sub-tasks sequentially,...
virtual void executeTaskGraph(Span< const Task > tasks, void *context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics *statistics)=0
Execute a pre-built task graph using this executor.
void executeTransformation(const Range &range, RandomAccessIterator output, UnaryMapping transformationFunction)
Transform each element in a given range, sequentially.
Definition Executor.hpp:582
InitialValue executeParallelReduction(const Range &range, const InitialValue &initialValue, AssociativeBinaryFunction reductionFunction)
Perform a reduction over each element in a given range, in up to getMaxParallelism() parallel chunks ...
Definition Executor.hpp:819
virtual ~Executor()=default
Virtual destructor.
void executeSchedule(const Schedule< EntReg, ResReg > &schedule, EntReg &entities, ResReg &resources, Statistics *statistics=nullptr)
Execute a pre-built task Schedule using this executor.
Definition Executor.hpp:65
void executeParallelSparseApplication(const IndexRange &indices, Procedure operation, FirstRange &&range, OtherRanges &&... ranges)
Execute an operation for each indexed row of elements specified by a given range of indices in a give...
Definition Executor.hpp:521
void executeParallelSparseTransformation(const IndexRange &indices, const Range &range, RandomAccessIterator output, UnaryMapping transformationFunction)
Transform each indexed element specified by a given range of indices, in up to getMaxParallelism() pa...
Definition Executor.hpp:723
Chunkable task whose chunks can be run in parallel.
Definition Task.hpp:145
Statically scheduled task graph built by a Scheduler.
Definition Schedule.hpp:35
Span< const Task > getTasks() const noexcept
Get the list of scheduled tasks.
Definition Schedule.hpp:83
CStringView getName() const noexcept
Get the human-readable name of the schedule.
Definition Schedule.hpp:103
Task::SharedMemorySize getRequiredSharedMemorySize() const noexcept
Get the total amount of contiguous shared memory required by the byte* sharedMemory parameter of Task...
Definition Schedule.hpp:93
Basic Executor implementation that runs all tasks sequentially, directly on the caller's thread.
Definition Executor.hpp:1034
Task::ParallelCount getMaxParallelism() const noexcept override
Get the maximum number of parallel threads of execution that this executor can run tasks on simultane...
Definition Executor.hpp:1051
void executeTaskGraph(Span< const Task > tasks, void *context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics *statistics) override
Execute a pre-built task graph using this executor.
Definition Executor.hpp:1036
void executeParallelTasks(void *subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize, Statistics *statistics) override
Execute a set parallel sub-tasks through a ParallelTask function that performs a specific chunk of wo...
Definition Executor.hpp:1043
Allocation< byte > sharedMemory
Allocated shared memory available for reuse in subsequent executions.
Definition Executor.hpp:1061
Statically scheduled unit of work to be executed by an Executor as part of a task graph.
Definition Task.hpp:30
uint32_t SharedMemorySize
Size, in bytes, of shared memory allocated for a task.
Definition Task.hpp:51
ParallelIndex ParallelCount
Number of a parallel sub-tasks.
Definition Task.hpp:36
uint16_t ParallelIndex
Index of a parallel sub-task.
Definition Task.hpp:33
Definition concepts.hpp:106
Definition concepts.hpp:243
constexpr decltype(auto) get(EntityTable< Row, Allocator > &t, std::size_t rowIndex)
Definition EntityTable.hpp:518
GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T &&value, Low &&minValue, High &&maxValue)
Clamp a value to a specific interval.
Definition fundamentals.hpp:630
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Configuration options for a DynamicExecutor.
Definition Executor.hpp:1067
Task::ParallelCount targetParallelism
Desired level of parallelism of the chosen executor implementation.
Definition Executor.hpp:1088
constexpr bool operator==(const DynamicExecutorOptions &other) const =default
Compare these options to another set of options for equality.
Task context that the scheduled tasks expect to receive a pointer to through their void* context para...
Definition Schedule.hpp:41
Performance statistics produced by an Executor invocation.
Definition Statistics.hpp:18