forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToposortEngine.cpp
More file actions
223 lines (186 loc) · 6.51 KB
/
ToposortEngine.cpp
File metadata and controls
223 lines (186 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright Sebastian Jeckel 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if 0
#include "react/engine/ToposortEngine.h"
#include "tbb/parallel_for.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
namespace toposort {
///////////////////////////////////////////////////////////////////////////////////////////////////
/// SeqTurn
///////////////////////////////////////////////////////////////////////////////////////////////////
SeqTurn::SeqTurn(TurnIdT id, TransactionFlagsT flags) :
TurnBase( id, flags )
{}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ParTurn
///////////////////////////////////////////////////////////////////////////////////////////////////
ParTurn::ParTurn(TurnIdT id, TransactionFlagsT flags) :
TurnBase( id, flags )
{}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode, typename TTurn>
void EngineBase<TNode,TTurn>::AttachNode(TNode& node, TNode& parent)
{
parent.Successors.Add(node);
if (node.Level <= parent.Level)
node.Level = parent.Level + 1;
}
template <typename TNode, typename TTurn>
void EngineBase<TNode,TTurn>::DetachNode(TNode& node, TNode& parent)
{
parent.Successors.Remove(node);
}
template <typename TNode, typename TTurn>
void EngineBase<TNode,TTurn>::OnInputChange(TNode& node, TTurn& turn)
{
processChildren(node, turn);
}
template <typename TNode, typename TTurn>
void EngineBase<TNode,TTurn>::OnNodePulse(TNode& node, TTurn& turn)
{
processChildren(node, turn);
}
// Explicit instantiation
template class EngineBase<SeqNode,SeqTurn>;
template class EngineBase<ParNode,ParTurn>;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// SeqEngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
void SeqEngineBase::Propagate(SeqTurn& turn)
{
while (scheduledNodes_.FetchNext())
{
for (auto* curNode : scheduledNodes_.NextValues())
{
if (curNode->Level < curNode->NewLevel)
{
curNode->Level = curNode->NewLevel;
invalidateSuccessors(*curNode);
scheduledNodes_.Push(curNode);
continue;
}
curNode->Queued = false;
curNode->Tick(&turn);
}
}
}
void SeqEngineBase::OnDynamicNodeAttach(SeqNode& node, SeqNode& parent, SeqTurn& turn)
{
this->AttachNode(node, parent);
invalidateSuccessors(node);
// Re-schedule this node
node.Queued = true;
scheduledNodes_.Push(&node);
}
void SeqEngineBase::OnDynamicNodeDetach(SeqNode& node, SeqNode& parent, SeqTurn& turn)
{
this->DetachNode(node, parent);
}
void SeqEngineBase::processChildren(SeqNode& node, SeqTurn& turn)
{
// Add children to queue
for (auto* succ : node.Successors)
{
if (!succ->Queued)
{
succ->Queued = true;
scheduledNodes_.Push(succ);
}
}
}
void SeqEngineBase::invalidateSuccessors(SeqNode& node)
{
for (auto* succ : node.Successors)
{
if (succ->NewLevel <= node.Level)
succ->NewLevel = node.Level + 1;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ParEngineBase
///////////////////////////////////////////////////////////////////////////////////////////////////
void ParEngineBase::Propagate(ParTurn& turn)
{
while (topoQueue_.FetchNext())
{
//using RangeT = tbb::blocked_range<vector<ParNode*>::const_iterator>;
using RangeT = ParEngineBase::TopoQueueT::NextRangeT;
// Iterate all nodes of current level and start processing them in parallel
tbb::parallel_for(
topoQueue_.NextRange(),
[&] (const RangeT& range)
{
for (const auto& e : range)
{
auto* curNode = e.first;
if (curNode->Level < curNode->NewLevel)
{
curNode->Level = curNode->NewLevel;
invalidateSuccessors(*curNode);
topoQueue_.Push(curNode);
continue;
}
curNode->Collected = false;
// Tick -> if changed: OnNodePulse -> adds child nodes to the queue
curNode->Tick(&turn);
}
}
);
if (dynRequests_.size() > 0)
{
for (auto req : dynRequests_)
{
if (req.ShouldAttach)
applyDynamicAttach(*req.Node, *req.Parent, turn);
else
applyDynamicDetach(*req.Node, *req.Parent, turn);
}
dynRequests_.clear();
}
}
}
void ParEngineBase::OnDynamicNodeAttach(ParNode& node, ParNode& parent, ParTurn& turn)
{
DynRequestData data{ true, &node, &parent };
dynRequests_.push_back(data);
}
void ParEngineBase::OnDynamicNodeDetach(ParNode& node, ParNode& parent, ParTurn& turn)
{
DynRequestData data{ false, &node, &parent };
dynRequests_.push_back(data);
}
void ParEngineBase::applyDynamicAttach(ParNode& node, ParNode& parent, ParTurn& turn)
{
this->AttachNode(node, parent);
invalidateSuccessors(node);
// Re-schedule this node
node.Collected = true;
topoQueue_.Push(&node);
}
void ParEngineBase::applyDynamicDetach(ParNode& node, ParNode& parent, ParTurn& turn)
{
this->DetachNode(node, parent);
}
void ParEngineBase::processChildren(ParNode& node, ParTurn& turn)
{
// Add children to queue
for (auto* succ : node.Successors)
if (!succ->Collected.exchange(true, std::memory_order_relaxed))
topoQueue_.Push(succ);
}
void ParEngineBase::invalidateSuccessors(ParNode& node)
{
for (auto* succ : node.Successors)
{// succ->InvalidateMutex
ParNode::InvalidateMutexT::scoped_lock lock(succ->InvalidateMutex);
if (succ->NewLevel <= node.Level)
succ->NewLevel = node.Level + 1;
}// ~succ->InvalidateMutex
}
} // ~namespace toposort
/****************************************/ REACT_IMPL_END /***************************************/
#endif