forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGElementManager.cpp
More file actions
224 lines (170 loc) · 6.34 KB
/
GElementManager.cpp
File metadata and controls
224 lines (170 loc) · 6.34 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
224
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: GElementManager.cpp
@Time: 2021/6/2 10:33 下午
@Desc:
***************************/
#include "GElementManager.h"
CGRAPH_NAMESPACE_BEGIN
GElementManager::GElementManager() : CObject() {
}
GElementManager::~GElementManager() = default;
GElementManager::GElementManager(const GElementManager& manager) {
this->manager_elements_ = manager.manager_elements_;
}
GElementManager& GElementManager::operator=(const GElementManager& manager) {
if (this == &manager) {
return (*this);
}
this->manager_elements_ = manager.manager_elements_;
return (*this);
}
CStatus GElementManager::init() {
CGRAPH_FUNCTION_BEGIN
status = preRunCheck();
CGRAPH_FUNCTION_CHECK_STATUS
status = analyse();
CGRAPH_FUNCTION_CHECK_STATUS
for (GElementPtr element : manager_elements_) {
CGRAPH_ASSERT_NOT_NULL(element)
status = element->doAspect(GAspectType::BEGIN_INIT);
CGRAPH_FUNCTION_CHECK_STATUS
status = element->init();
element->doAspect(GAspectType::FINISH_INIT, status);
CGRAPH_FUNCTION_CHECK_STATUS
}
CGRAPH_FUNCTION_END
}
CStatus GElementManager::deinit() {
CGRAPH_FUNCTION_BEGIN
for (GElementPtr element : manager_elements_) {
CGRAPH_ASSERT_NOT_NULL(element)
status = element->doAspect(GAspectType::BEGIN_DEINIT);
CGRAPH_FUNCTION_CHECK_STATUS
status = element->deinit();
element->doAspect(GAspectType::FINISH_DEINIT, status);
CGRAPH_FUNCTION_CHECK_STATUS
}
CGRAPH_FUNCTION_END
}
/**
* 无法执行manager的run方法
* @return
*/
CStatus GElementManager::run() {
CGRAPH_NO_SUPPORT
}
CStatus GElementManager::preRunCheck() {
CGRAPH_FUNCTION_BEGIN
/**
* 认定图可以连通的判定条件:
* 1,当前节点仅有一个依赖
* 2,当前节点依赖的节点,只有一个后继
* 3,当前节点的依赖的后继,仍是当前节点
*/
for (GElement* element : manager_elements_) {
if (1 == element->dependence_.size()
&& 1 == (*element->dependence_.begin())->run_before_.size()
&& (*(element->dependence_.begin()))->run_before_.find(element) != (*(element->dependence_.begin()))->run_before_.end()) {
element->linkable_ = true;
}
}
CGRAPH_FUNCTION_END
}
CStatus GElementManager::analyse() {
CGRAPH_FUNCTION_BEGIN
int runElementSize = 0;
int totalElementSize = (int)manager_elements_.size();
para_cluster_arrs_.clear();
GClusterArr curClusterArr; // 记录每一层,可以并行的逻辑
for (GElementPtr element : manager_elements_) {
if (!element->isRunnable() || element->isLinkable()) {
continue;
}
GCluster curCluster;
GElementPtr curElement = element;
curCluster.addElement(curElement);
/* 将linkable的节点,统一放到一个cluster中 */
while (1 == curElement->run_before_.size()
&& (*curElement->run_before_.begin())->isLinkable()) {
// 将下一个放到cluster中处理
curElement = (*curElement->run_before_.begin());
curCluster.addElement(curElement);
}
curClusterArr.emplace_back(curCluster);
}
para_cluster_arrs_.emplace_back(curClusterArr);
GClusterArr runnableClusterArr;
while (!curClusterArr.empty() && runElementSize <= totalElementSize) {
runnableClusterArr = curClusterArr;
curClusterArr.clear();
for (GClusterRef cluster : runnableClusterArr) {
status = cluster.process(true); // 不执行run方法的process
CGRAPH_FUNCTION_CHECK_STATUS
}
runElementSize += (int)runnableClusterArr.size();
GElementPtrSet duplications;
for (GClusterRef cluster : runnableClusterArr) {
for (GElementPtr element : cluster.cluster_elements_) {
for (GElementPtr cur : element->run_before_) {
/**
* 判断element是否需要被加入
* 1,该元素是可以执行的
* 2,改元素本次循环是第一次被遍历
*/
if (cur->isRunnable() && duplications.end() == duplications.find(cur)) {
GCluster curCluster;
GElementPtr curElement = cur;
curCluster.addElement(curElement);
duplications.insert(curElement);
while (1 == curElement->run_before_.size()
&& (*curElement->run_before_.begin())->isLinkable()) {
curElement = (*curElement->run_before_.begin());
curCluster.addElement(curElement);
duplications.insert(curElement);
}
curClusterArr.emplace_back(curCluster);
}
}
}
}
/* 为空的话,直接退出循环;不为空的话,放入para信息中 */
if (!curClusterArr.empty()) {
para_cluster_arrs_.emplace_back(curClusterArr);
}
}
CGRAPH_FUNCTION_END
}
CStatus GElementManager::afterRunCheck(int runNodeSize) {
CGRAPH_FUNCTION_BEGIN
/* 验证是否所有的内容均被执行过 */
if (runNodeSize != manager_elements_.size()) {
return CStatus("pipeline run element size check failed...");
}
/* 需要验证每个cluster里的每个内容是否被执行过一次 */
for (GClusterArrRef clusterArr : para_cluster_arrs_) {
for (GClusterRef cluster : clusterArr) {
if (!cluster.isElementsDone()) {
return CStatus("pipeline done status check failed...");
}
}
}
CGRAPH_FUNCTION_END
}
CStatus GElementManager::addElement(GElementPtr element) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(element)
this->manager_elements_.emplace(element);
CGRAPH_FUNCTION_END
}
bool GElementManager::hasElement(GElementPtr element) const {
if (nullptr == element) {
return false;
}
return manager_elements_.end() != manager_elements_.find(element);
}
void GElementManager::deleteElement(GElementPtr element) {
manager_elements_.erase(element);
}
CGRAPH_NAMESPACE_END