GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Executor.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_EXECUTION_EXECUTOR_HPP
5#define GREM_EXECUTION_EXECUTOR_HPP
6
8
17#include <GREM/core/math.hpp>
21
22#include <iterator> // std::data, std::size
23#include <type_traits> // std::remove_pointer_t, std::decay_t
24#include <utility> // std::move, std::declval, std::...index_sequence
25
26namespace grem::execution {
27
28struct Statistics; // Forward declaration, to avoid including Statistics.hpp.
29
34class Executor {
35public:
37 virtual ~Executor() = default;
38
64 template <typename EntReg, typename ResReg>
65 void executeSchedule(const Schedule<EntReg, ResReg>& schedule, EntReg& entities, ResReg& resources, Statistics* statistics = nullptr) {
66 typename Schedule<EntReg, ResReg>::TaskContext taskContext{
67 .entities = entities,
68 .resources = resources,
69 };
70 executeTaskGraph(schedule.getTasks(), &taskContext, schedule.getRequiredSharedMemorySize(), schedule.getName(), statistics);
71 }
72
91 template <typename Range, typename UnaryProcedure>
92 void executeOperation(Range&& range, UnaryProcedure operation) { // NOLINT(cppcoreguidelines-missing-std-forward)
93 using T = std::remove_pointer_t<decltype(std::data(range))>;
94
95 const Span<T> elements{std::data(range), std::size(range)};
96 for (T& element : elements) {
97 operation(element);
98 }
99 }
100
122 template <typename Range, typename UnaryProcedure>
123 void executeParallelOperation(Range&& range, UnaryProcedure operation) { // NOLINT(cppcoreguidelines-missing-std-forward)
124#ifdef GREM_USE_MULTITHREADING
125 using T = std::remove_pointer_t<decltype(std::data(range))>;
126
127 const Span<T> elements{std::data(range), std::size(range)};
128 const size_t chunkCount = getMaxParallelism();
129 if (chunkCount <= 1 || elements.size() < chunkCount) {
130 [[unlikely]];
131 return executeOperation(range, std::forward<UnaryProcedure>(operation));
132 }
133
134 struct ParallelTaskContext {
135 UnaryProcedure operation;
136 SmallArrayList<Span<T>, 32> chunks{};
137 } parallelTaskContext{std::move(operation)};
138
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)));
142 }
143
145 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
146 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
147 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
148 for (T& element : parallelTaskContext.chunks[subTaskIndex]) {
149 parallelTaskContext.operation(element);
150 }
151 },
152 0, nullptr);
153#else
154 return executeOperation(range, std::forward<UnaryProcedure>(operation));
155#endif
156 }
157
183 template <typename IndexRange, typename Range, typename UnaryProcedure>
184 void executeSparseOperation(const IndexRange& indices, Range&& range, UnaryProcedure operation) { // NOLINT(cppcoreguidelines-missing-std-forward)
185 using T = std::remove_pointer_t<decltype(std::data(range))>;
186 using Index = std::decay_t<decltype(*std::data(indices))>;
187
188 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
189 const Span<T> elements{std::data(range), std::size(range)};
190 for (const Index index : elementIndices) {
191 operation(elements[index]);
192 }
193 }
194
223 template <typename IndexRange, typename Range, typename UnaryProcedure>
224 void executeParallelSparseOperation(const IndexRange& indices, Range&& range, UnaryProcedure operation) { // NOLINT(cppcoreguidelines-missing-std-forward)
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))>;
228
229 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
230 const Span<T> elements{std::data(range), std::size(range)};
231 const size_t chunkCount = getMaxParallelism();
232 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
233 [[unlikely]];
234 return executeSparseOperation(indices, range, std::forward<UnaryProcedure>(operation));
235 }
236
237 struct ParallelTaskContext {
238 UnaryProcedure operation;
239 T* pointer;
241 } parallelTaskContext{std::move(operation), elements.data()};
242
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)));
246 }
247
249 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
250 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
251 ParallelTaskContext& parallelTaskContext = *static_cast<ParallelTaskContext*>(subTaskContext);
252 for (const Index index : parallelTaskContext.chunks[subTaskIndex]) {
253 parallelTaskContext.operation(parallelTaskContext.pointer[index]);
254 }
255 },
256 0, nullptr);
257#else
258 return executeSparseOperation(indices, range, std::forward<UnaryProcedure>(operation));
259#endif
260 }
261
283 template <typename IndexedProcedure>
284 void executeIndexedOperation(size_t begin, size_t end, IndexedProcedure operation) {
285 GREM_ASSERT(begin <= end);
286 for (size_t i = begin; i < end; ++i) {
287 operation(size_t{i});
288 }
289 }
290
315 template <typename IndexedProcedure>
316 void executeParallelIndexedOperation(size_t begin, size_t end, IndexedProcedure operation) {
317#ifdef GREM_USE_MULTITHREADING
318 GREM_ASSERT(begin <= end);
319 const size_t size = end - begin;
320 const size_t chunkCount = getMaxParallelism();
321 if (chunkCount <= 1 || size < chunkCount) {
322 [[unlikely]];
323 return executeIndexedOperation(begin, end, std::forward<IndexedProcedure>(operation));
324 }
325
326 struct ParallelTaskContext {
327 IndexedProcedure operation;
328 SmallArrayList<Pair<size_t>, 32> chunks{};
329 } parallelTaskContext{std::move(operation)};
330
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));
334 }
335
337 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
338 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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});
344 }
345 },
346 0, nullptr);
347#else
348 return executeIndexedOperation(begin, end, std::forward<IndexedProcedure>(operation));
349#endif
350 }
351
374 template <typename Procedure, typename FirstRange, typename... OtherRanges>
375 void executeApplication(Procedure operation, FirstRange&& range, OtherRanges&&... ranges) { // NOLINT(cppcoreguidelines-missing-std-forward)
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 {
380 operation(*(get<Indices>(pointers) + i)...);
381 }(std::make_index_sequence<1 + sizeof...(OtherRanges)>{});
382 }
383 }
384
410 template <typename Procedure, typename FirstRange, typename... OtherRanges>
411 void executeParallelApplication(Procedure operation, FirstRange&& range, OtherRanges&&... ranges) {
412#ifdef GREM_USE_MULTITHREADING
413 const size_t size = std::size(range);
414 const size_t chunkCount = getMaxParallelism();
415 if (chunkCount <= 1 || size < chunkCount) {
416 [[unlikely]];
417 return executeApplication(std::forward<Procedure>(operation), std::forward<FirstRange>(range), std::forward<OtherRanges>(ranges)...);
418 }
419
420 struct ParallelTaskContext {
421 Procedure operation;
422 Tuple<decltype(std::data(range)), decltype(std::data(ranges))...> pointers;
423 SmallArrayList<Pair<size_t>, 32> chunks{};
424 } parallelTaskContext{std::move(operation), Tuple{std::data(range), std::data(ranges)...}};
425
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));
429 }
430
432 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
433 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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)>{});
441 }
442 },
443 0, nullptr);
444#else
445 return executeApplication(std::forward<Procedure>(operation), std::forward<FirstRange>(range), std::forward<OtherRanges>(ranges)...);
446#endif
447 }
448
478 template <typename IndexRange, typename Procedure, typename FirstRange, typename... OtherRanges>
479 void executeSparseApplication(const IndexRange& indices, Procedure operation, //
480 FirstRange&& range, OtherRanges&&... ranges) { // NOLINT(cppcoreguidelines-missing-std-forward)
481 using Index = std::decay_t<decltype(*std::data(indices))>;
482
483 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
484 for (const Index index : elementIndices) {
485 operation(range[index], ranges[index]...);
486 }
487 }
488
520 template <typename IndexRange, typename Procedure, typename FirstRange, typename... OtherRanges>
521 void executeParallelSparseApplication(const IndexRange& indices, Procedure operation, FirstRange&& range, OtherRanges&&... ranges) {
522#ifdef GREM_USE_MULTITHREADING
523 using Index = std::decay_t<decltype(*std::data(indices))>;
524
525 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
526 const size_t chunkCount = getMaxParallelism();
527 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
528 [[unlikely]];
529 return executeSparseApplication(indices, std::forward<Procedure>(operation), std::forward<FirstRange>(range), std::forward<OtherRanges>(ranges)...);
530 }
531
532 struct ParallelTaskContext {
533 Procedure operation;
534 Tuple<decltype(std::data(range)), decltype(std::data(ranges))...> pointers;
536 } parallelTaskContext{std::move(operation), Tuple{std::data(range), std::data(ranges)...}};
537
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)));
541 }
542
544 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
545 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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)>{});
551 }
552 },
553 0, nullptr);
554#else
555 return executeSparseApplication(indices, std::forward<Procedure>(operation), std::forward<FirstRange>(range), std::forward<OtherRanges>(ranges)...);
556#endif
557 }
558
581 template <typename Range, typename RandomAccessIterator, typename UnaryMapping>
582 void executeTransformation(const Range& range, RandomAccessIterator output, UnaryMapping transformationFunction) {
583 using T = std::remove_pointer_t<decltype(std::data(range))>;
584
585 const Span<T> elements{std::data(range), std::size(range)};
586 for (size_t i = 0; i < elements.size(); ++i) {
587 output[static_cast<ptrdiff_t>(i)] = transformationFunction(elements[i]);
588 }
589 }
590
616 template <typename Range, typename RandomAccessIterator, typename UnaryMapping>
617 void executeParallelTransformation(const Range& range, RandomAccessIterator output, UnaryMapping transformationFunction) {
618#ifdef GREM_USE_MULTITHREADING
619 using T = std::remove_pointer_t<decltype(std::data(range))>;
620
621 const Span<T> elements{std::data(range), std::size(range)};
622 const size_t chunkCount = getMaxParallelism();
623 if (chunkCount <= 1 || elements.size() < chunkCount) {
624 [[unlikely]];
625 return executeTransformation(range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
626 }
627
628 struct ParallelTaskContext {
629 UnaryMapping transformationFunction;
630 RandomAccessIterator output;
631 SmallArrayList<Span<T>, 32> chunks{};
632 } parallelTaskContext{std::move(transformationFunction), std::move(output)};
633
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)));
637 }
638
640 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
641 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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]);
646 }
647 },
648 0, nullptr);
649#else
650 return executeTransformation(range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
651#endif
652 }
653
681 template <typename IndexRange, typename Range, typename RandomAccessIterator, typename UnaryMapping>
682 void executeSparseTransformation(const IndexRange& indices, const Range& range, RandomAccessIterator output, UnaryMapping transformationFunction) {
683 using T = std::remove_pointer_t<decltype(std::data(range))>;
684 using Index = std::decay_t<decltype(*std::data(indices))>;
685
686 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
687 const Span<T> elements{std::data(range), std::size(range)};
688 for (size_t i = 0; i < elementIndices.size(); ++i) {
689 output[static_cast<ptrdiff_t>(i)] = transformationFunction(elements[elementIndices[i]]);
690 }
691 }
692
722 template <typename IndexRange, typename Range, typename RandomAccessIterator, typename UnaryMapping>
723 void executeParallelSparseTransformation(const IndexRange& indices, const Range& range, RandomAccessIterator output, UnaryMapping transformationFunction) {
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))>;
727
728 const Span<const Index> elementIndices{std::data(indices), std::size(indices)};
729 const Span<T> elements{std::data(range), std::size(range)};
730 const size_t chunkCount = getMaxParallelism();
731 if (chunkCount <= 1 || elementIndices.size() < chunkCount) {
732 [[unlikely]];
733 return executeSparseTransformation(indices, range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
734 }
735
736 struct ParallelTaskContext {
737 UnaryMapping transformationFunction;
738 T* pointer;
739 RandomAccessIterator output;
741 } parallelTaskContext{std::move(transformationFunction), elements.data(), std::move(output)};
742
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)));
746 }
747
749 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
750 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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]]);
755 }
756 },
757 0, nullptr);
758#else
759 return executeSparseTransformation(indices, range, std::forward<RandomAccessIterator>(output), std::forward<UnaryMapping>(transformationFunction));
760#endif
761 }
762
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))>;
786
787 const Span<T> elements{std::data(range), std::size(range)};
788 InitialValue result = initialValue;
789 for (T& element : elements) {
790 result = reductionFunction(std::move(result), element);
791 }
792 return result;
793 }
794
818 template <typename Range, typename InitialValue, typename AssociativeBinaryFunction>
819 [[nodiscard]] InitialValue executeParallelReduction(const Range& range, const InitialValue& initialValue, AssociativeBinaryFunction reductionFunction) {
820#ifdef GREM_USE_MULTITHREADING
821 using T = std::remove_pointer_t<decltype(std::data(range))>;
822
823 const Span<T> elements{std::data(range), std::size(range)};
824 const size_t chunkCount = getMaxParallelism();
825 if (chunkCount <= 1 || elements.size() < chunkCount) {
826 [[unlikely]];
827 return executeReduction(range, initialValue, std::forward<AssociativeBinaryFunction>(reductionFunction));
828 }
829
830 struct alignas(64) IntermediateResult {
831 InitialValue value;
832 };
833
834 struct ParallelTaskContext {
835 AssociativeBinaryFunction reductionFunction;
836 SmallArrayList<Span<T>, 32> chunks{};
837 SmallArrayList<IntermediateResult, 32> intermediateResults{};
838 } parallelTaskContext{std::move(reductionFunction)};
839
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)));
843 }
844
845 if (parallelTaskContext.chunks.empty()) {
846 return initialValue;
847 }
848 parallelTaskContext.intermediateResults.resize(parallelTaskContext.chunks.size(), IntermediateResult{initialValue});
849
851 &parallelTaskContext, static_cast<Task::ParallelCount>(parallelTaskContext.chunks.size()),
852 +[](void* subTaskContext, byte*, Task::ParallelIndex subTaskIndex, Task::ParallelCount) -> void {
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);
858 }
859 intermediateResult = std::move(localResult);
860 },
861 0, nullptr);
862
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));
866 }
867 return result;
868#else
869 return executeReduction(range, initialValue, std::forward<AssociativeBinaryFunction>(reductionFunction));
870#endif
871 }
872
909 virtual void executeTaskGraph(Span<const Task> tasks, void* context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics* statistics) = 0;
910
944 virtual void executeParallelTasks(void* subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize,
945 Statistics* statistics) = 0;
946
953 [[nodiscard]] virtual Task::ParallelCount getMaxParallelism() const noexcept = 0;
954
955protected:
991 GREM_API(execution)
992 static void executeTaskGraphSequentially(Span<const Task> tasks, void* context, byte* sharedMemory, CStringView name, Statistics* statistics);
993
1024 GREM_API(execution)
1025 static void executeParallelTasksSequentially(void* subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, byte* taskSharedMemory, Statistics* statistics);
1026};
1027
1034class SequentialExecutor final : public Executor {
1035public:
1036 void executeTaskGraph(Span<const Task> tasks, void* context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics* statistics) override {
1037 if (requiredSharedMemorySize > sharedMemory.size()) {
1038 sharedMemory.resize(static_cast<size_t>(requiredSharedMemorySize));
1039 }
1040 executeTaskGraphSequentially(tasks, context, sharedMemory.data(), name, statistics);
1041 }
1042
1043 void executeParallelTasks(void* subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize,
1044 Statistics* statistics) override {
1045 if (taskRequiredSharedMemorySize > sharedMemory.size()) {
1046 sharedMemory.resize(static_cast<size_t>(taskRequiredSharedMemorySize));
1047 }
1048 executeParallelTasksSequentially(subTaskContext, subTaskCount, subTask, sharedMemory.data(), statistics);
1049 }
1050
1051 [[nodiscard]] Task::ParallelCount getMaxParallelism() const noexcept override {
1052 return 1;
1053 }
1054
1062};
1063
1089
1097 [[nodiscard]] constexpr bool operator==(const DynamicExecutorOptions& other) const = default;
1098};
1099
1106class DynamicExecutor final : public Executor {
1107public:
1118 GREM_API(execution) explicit DynamicExecutor(const DynamicExecutorOptions& options);
1119
1129 : implementation(std::move(implementation)) {
1130 GREM_ASSERT(this->implementation);
1131 }
1132
1133 void executeTaskGraph(Span<const Task> tasks, void* context, Task::SharedMemorySize requiredSharedMemorySize, CStringView name, Statistics* statistics) override {
1134 GREM_ASSERT(implementation);
1135 implementation->executeTaskGraph(tasks, context, requiredSharedMemorySize, name, statistics);
1136 }
1137
1138 void executeParallelTasks(void* subTaskContext, Task::ParallelCount subTaskCount, ParallelTask subTask, Task::SharedMemorySize taskRequiredSharedMemorySize,
1139 Statistics* statistics) override {
1140 GREM_ASSERT(implementation);
1141 implementation->executeParallelTasks(subTaskContext, subTaskCount, subTask, taskRequiredSharedMemorySize, statistics);
1142 }
1143
1144 [[nodiscard]] Task::ParallelCount getMaxParallelism() const noexcept override {
1145 return (implementation) ? implementation->getMaxParallelism() : 0;
1146 }
1147
1153 [[nodiscard]] Executor* get() noexcept {
1154 return implementation.get();
1155 }
1156
1162 [[nodiscard]] const Executor* get() const noexcept {
1163 return implementation.get();
1164 }
1165
1166private:
1167 UniquePointer<Executor> implementation{};
1168};
1169
1170} // namespace grem::execution
1171
1172#endif
#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
Definition Span.hpp:146
constexpr pointer data() const noexcept
Definition Span.hpp:203
static GREM_ALWAYS_INLINE unsigned int hardware_concurrency() noexcept
Definition Thread.hpp:113
Definition Tuple.hpp:210
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
Definition Chunk.hpp:19
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