Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
Workflow.h
Go to the documentation of this file.
1#pragma once
2
4#include "Utility/TSpan.h"
5
6#include <vector>
7#include <cstddef>
8#include <functional>
9#include <array>
10#include <utility>
11#include <memory>
12#include <atomic>
13#include <tuple>
14#include <utility>
15
16namespace ph { class FixedSizeThreadPool; }
17
18namespace ph
19{
20
21class Workflow final : private INoCopyAndMove
22{
23public:
24 using Work = std::function<void()>;
25
28 class WorkHandle final
29 {
30 friend class Workflow;
31
32 public:
33 WorkHandle();
34
35 void runsBefore(WorkHandle succeedingWork);
36 void runsAfter(WorkHandle preceedingWork);
37 void runsBefore(TSpanView<WorkHandle> succeedingWorks);
38 void runsAfter(TSpanView<WorkHandle> preceedingWorks);
39
40 template<std::size_t N>
41 void runsBefore(std::array<WorkHandle, N> succeedingWorks);
42
43 template<std::size_t N>
44 void runsAfter(std::array<WorkHandle, N> preceedingWorks);
45
46 std::size_t getWorkId() const;
47 Workflow* getWorkflow() const;
48
49 operator bool () const;
50
51 private:
52 WorkHandle(std::size_t workId, Workflow* workflow);
53
54 std::size_t m_workId;
55 Workflow* m_workflow;
56 };
57
58public:
59 Workflow();
60 explicit Workflow(std::size_t numExpectedWorks);
61
63 WorkHandle acquireWork(std::size_t workIndex);
64
65 template<typename... WorkTypes>
66 auto addWorks(WorkTypes&&... works);
67
68 void dependsOn(WorkHandle target, WorkHandle targetDependency);
70
71 std::size_t numWorks() const;
72
73private:
74 class ManagedWork final
75 {
76 public:
77 explicit ManagedWork(WorkHandle handle);
78
79 void operator () ();
80
81 private:
82 WorkHandle m_handle;
83 };
84
85 void ensureValidWorkHandle(WorkHandle work) const;
86 void startWorkflow();
87 void finishWorkflow();
88 std::unique_ptr<std::size_t[]> determineDispatchOrderFromTopologicalSort() const;
89
90 std::vector<Work> m_works;
91
92 /* An adjacency list recording the depending works of each work. The dependencies are recorded by
93 corresponding work ID. E.g., if work "k" is the "j"-th dependency of work "i", the relation will be
94 represented as "m_idToDependencyIds[i][j] = k".
95 */
96 std::vector<std::vector<std::size_t>> m_idToDependencyIds;
97
98 std::unique_ptr<std::atomic_flag[]> m_workDoneFlags;
99};
100
101// In-header Implementations:
102
103template<typename... WorkTypes>
104inline auto Workflow::addWorks(WorkTypes&&... works)
105{
106 return std::array{addWork(std::forward<WorkTypes>(works))...};
107}
108
110{
111 for(const WorkHandle work : succeedingWorks)
112 {
113 runsBefore(work);
114 }
115}
116
118{
119 for(const WorkHandle work : preceedingWorks)
120 {
121 runsAfter(work);
122 }
123}
124
125template<std::size_t N>
126inline void Workflow::WorkHandle::runsBefore(std::array<WorkHandle, N> succeedingWorks)
127{
128 runsBefore(TSpanView<WorkHandle>(succeedingWorks));
129}
130
131template<std::size_t N>
132inline void Workflow::WorkHandle::runsAfter(std::array<WorkHandle, N> preceedingWorks)
133{
134 runsAfter(TSpanView<WorkHandle>(preceedingWorks));
135}
136
137inline std::size_t Workflow::WorkHandle::getWorkId() const
138{
139 return m_workId;
140}
141
143{
144 return m_workflow;
145}
146
147inline Workflow::WorkHandle::operator bool () const
148{
149 return m_workId != static_cast<std::size_t>(-1) && m_workflow != nullptr;
150}
151
152}// end namespace ph
A thread pool where works are accessed concurrently by blocking other threads. A thread pool that con...
Definition FixedSizeThreadPool.h:21
Marks the derived class as non-copyable and non-movable.
Definition INoCopyAndMove.h:17
Lightweight handle for an added work.
Definition Workflow.h:29
WorkHandle()
Definition Workflow.cpp:17
std::size_t getWorkId() const
Definition Workflow.h:137
void runsAfter(WorkHandle preceedingWork)
Definition Workflow.cpp:31
Workflow * getWorkflow() const
Definition Workflow.h:142
void runsBefore(WorkHandle succeedingWork)
Definition Workflow.cpp:25
Definition Workflow.h:22
Workflow()
Definition Workflow.cpp:70
WorkHandle acquireWork(std::size_t workIndex)
Definition Workflow.cpp:95
void dependsOn(WorkHandle target, WorkHandle targetDependency)
Definition Workflow.cpp:107
WorkHandle addWork(Work work)
Definition Workflow.cpp:81
std::size_t numWorks() const
Definition Workflow.cpp:141
auto addWorks(WorkTypes &&... works)
Definition Workflow.h:104
std::function< void()> Work
Definition Workflow.h:24
void runAndWaitAllWorks(FixedSizeThreadPool &workers)
Definition Workflow.cpp:116
The root for all renderer implementations.
Definition EEngineProject.h:6
std::span< const T, EXTENT > TSpanView
Same as TSpan, except that the objects are const-qualified. Note that for pointer types,...
Definition TSpan.h:19