Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
WorkScheduler.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <Common/assertion.h>
7#include <Common/primitive_type.h>
8
9#include <vector>
10
11namespace ph
12{
13
21{
22public:
26
29 WorkScheduler(std::size_t numWorkers, const WorkUnit& totalWorkUnit);
30
31 bool dispatch(WorkUnit* out_workUnit) override;
32
39 bool schedule(WorkUnit* out_workUnit);
40
41 void scheduleAll(std::vector<WorkUnit>& out_workUnits);
42 void submit(const WorkUnit& workUnit);
43
44 float getScheduledFraction() const;
45 float getSubmittedFraction() const;
46
51 bool isExhausted() const;
52
53protected:
54 std::size_t m_numWorkers;
56 std::size_t m_totalVolume;
57 std::size_t m_scheduledVolume;
58 std::size_t m_submittedVolume;
59
60private:
61 virtual void scheduleOne(WorkUnit* out_workUnit) = 0;
62};
63
64// In-header Implementations:
65
69
70inline WorkScheduler::WorkScheduler(const std::size_t numWorkers, const WorkUnit& totalWorkUnit) :
71 m_numWorkers (numWorkers),
72 m_totalWorkUnit (totalWorkUnit),
73 m_totalVolume (totalWorkUnit.getVolume()),
74 m_scheduledVolume(0),
75 m_submittedVolume(0)
76{
77 PH_ASSERT_GE(numWorkers, 1);
78}
79
80inline bool WorkScheduler::dispatch(WorkUnit* const out_workUnit)
81{
82 return schedule(out_workUnit);
83}
84
85inline bool WorkScheduler::schedule(WorkUnit* const out_workUnit)
86{
87 PH_ASSERT(out_workUnit);
88
89 while(!isExhausted())
90 {
91 scheduleOne(out_workUnit);
92
93 const std::size_t volume = out_workUnit->getVolume();
94 if(volume > 0)
95 {
96 m_scheduledVolume += volume;
97 return true;
98 }
99 }
100
101 PH_ASSERT_EQ(m_scheduledVolume, m_totalVolume);
102 return false;
103}
104
105inline void WorkScheduler::scheduleAll(std::vector<WorkUnit>& out_workUnits)
106{
107 WorkUnit workUnit;
108 while(schedule(&workUnit))
109 {
110 out_workUnits.push_back(workUnit);
111 }
112}
113
114inline void WorkScheduler::submit(const WorkUnit& workUnit)
115{
116 m_submittedVolume += workUnit.getVolume();
117
118 PH_ASSERT_LE(m_submittedVolume, m_totalVolume);
119}
120
122{
123 return static_cast<float>(m_scheduledVolume) / static_cast<float>(m_totalVolume);
124}
125
127{
128 return static_cast<float>(m_submittedVolume) / static_cast<float>(m_totalVolume);
129}
130
131inline bool WorkScheduler::isExhausted() const
132{
134}
135
136}// end namespace ph
A manager that distributes work.
Definition IWorkDispatcher.h:14
A manager that distributes a fixed amount of work to workers.
Definition WorkScheduler.h:21
std::size_t m_scheduledVolume
Definition WorkScheduler.h:57
bool schedule(WorkUnit *out_workUnit)
Get some amount of work.
Definition WorkScheduler.h:85
WorkUnit m_totalWorkUnit
Definition WorkScheduler.h:55
std::size_t m_totalVolume
Definition WorkScheduler.h:56
std::size_t m_submittedVolume
Definition WorkScheduler.h:58
float getScheduledFraction() const
Definition WorkScheduler.h:121
WorkScheduler()
A scheduler with no work load.
Definition WorkScheduler.h:66
void scheduleAll(std::vector< WorkUnit > &out_workUnits)
Definition WorkScheduler.h:105
bool isExhausted() const
Checks if all works had been scheduled.
Definition WorkScheduler.h:131
float getSubmittedFraction() const
Definition WorkScheduler.h:126
bool dispatch(WorkUnit *out_workUnit) override
Get some amount of work.
Definition WorkScheduler.h:80
std::size_t m_numWorkers
Definition WorkScheduler.h:54
void submit(const WorkUnit &workUnit)
Definition WorkScheduler.h:114
Represents some amount of work.
Definition WorkUnit.h:17
std::size_t getVolume() const
Definition WorkUnit.h:82
The root for all renderer implementations.
Definition EEngineProject.h:6