Skip to content

Commit

Permalink
[feat] add removeDepend() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Sep 8, 2024
1 parent ffb3594 commit 1a4ae30
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 76 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ int main() {
* 提供`pipeline`的静态执行的方式,提供微任务机制
* 优化`event`(事件)机制,异步事件可以等待结束
* 提供`pipeline`剪裁功能,用于删除`element`之间重复的依赖
* 提供`element`删除依赖的方法
* 发布 [CGraph-lite](https://github.com/ChunelFeng/CGraph-lite) 项目,提供简单DAG构图和参数传递功能。接口完全兼容,可无缝切换至本项目

</details>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GSingleton : public GAdapter {
CStatus run() final;
CStatus destroy() final;

CStatus addElementInfo(const std::set<GElementPtr> &dependElements,
CStatus addElementInfo(const std::set<GElementPtr> &depends,
const std::string &name, CSize loop) final;

CStatus addManagers(GParamManagerPtr paramManager,
Expand Down
4 changes: 2 additions & 2 deletions src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.inl
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ CStatus GSingleton<T>::destroy() {


template <typename T>
CStatus GSingleton<T>::addElementInfo(const std::set<GElementPtr> &dependElements,
CStatus GSingleton<T>::addElementInfo(const std::set<GElementPtr> &depends,
const std::string &name, CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(false)

status = this->addDependGElements(dependElements);
status = this->addDependGElements(depends);
CGRAPH_FUNCTION_CHECK_STATUS

// 这里,内部和外部均需要设定name信息
Expand Down
32 changes: 16 additions & 16 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ CStatus GElement::addDependGElements(const GElementPtrSet& elements) {
}


CStatus GElement::addElementInfo(const GElementPtrSet& dependElements,
CStatus GElement::addElementInfo(const GElementPtrSet& depends,
const std::string& name, CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(false)

// 添加依赖的时候,可能会出现异常情况。故在这里提前添加 && 做判定
status = this->addDependGElements(dependElements);
status = this->addDependGElements(depends);
CGRAPH_FUNCTION_CHECK_STATUS

this->setLoop(loop);
Expand Down Expand Up @@ -481,6 +481,20 @@ GElementRelation GElement::getRelation() const {
}


CStatus GElement::removeDepend(GElementPtr element) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(element)
CGRAPH_ASSERT_INIT(false)
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(!dependence_.hasValue(element),
element->getName() + " is not in [" + getName() + "]'s depends.")

dependence_.remove(element);
element->run_before_.remove(this);
left_depend_.store(dependence_.size(), std::memory_order_release);
CGRAPH_FUNCTION_END
}


CBool GElement::isSerializable() const {
return true;
}
Expand Down Expand Up @@ -568,18 +582,4 @@ CBool GElement::isDefaultBinding() const {
return CGRAPH_DEFAULT_BINDING_INDEX == binding_index_;
}


CBool GElement::removeDepend(GElementPtr element) {
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(element)
CGRAPH_ASSERT_INIT_THROW_ERROR(false)
if (!dependence_.hasValue(element)) {
return false;
}

dependence_.remove(element);
element->run_before_.remove(this);
left_depend_.store(dependence_.size(), std::memory_order_release);
return true;
}

CGRAPH_NAMESPACE_END
19 changes: 10 additions & 9 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ class GElement : public GElementObject,
*/
GElementState getCurState() const;

/**
* 删除一个依赖的节点信息
* @param element
* @return
* @notice 删除依赖关系之后,可能会出现 dag 无法连通的情况
*/
CStatus removeDepend(GElement* element);

/**
* 获取对应的ptr类型
* @tparam T
Expand Down Expand Up @@ -301,12 +309,12 @@ class GElement : public GElementObject,

/**
* 设置element信息
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
virtual CStatus addElementInfo(const std::set<GElement *>& dependElements,
virtual CStatus addElementInfo(const std::set<GElement *>& depends,
const std::string& name, CSize loop);

/**
Expand Down Expand Up @@ -419,13 +427,6 @@ class GElement : public GElementObject,
*/
CBool isDefaultBinding() const;

/**
* 删除一个依赖的节点信息
* @param element
* @return
*/
CBool removeDepend(GElement* element);

private:
/** 状态相关信息 */
CBool done_ { false }; // 判定被执行结束
Expand Down
4 changes: 2 additions & 2 deletions src/GraphCtrl/GraphElement/_GOptimizer/GTrimOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class GTrimOptimizer : public GOptimizer {
auto graph = buildGraph(elements, paths, 1, 0, 0);

for (auto* cur : elements) {
CSize idx = std::distance(elements.begin(), elements.find(cur));
CGRAPH_ASSERT_NOT_NULL_THROW_ERROR(cur)
GElementPtrArr candidates;
for (CSize i = 0; i < cur->dependence_.size(); i++) {
CSize x = std::distance(elements.begin(), elements.find(cur->dependence_[i]));
Expand All @@ -43,7 +43,7 @@ class GTrimOptimizer : public GOptimizer {
}

for (auto* candidate : candidates) {
if (cur->removeDepend(candidate)) {
if (cur->removeDepend(candidate).isOK()) {
trimNum++;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/GraphCtrl/GraphPipeline/GPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CStatus GPipeline::process(CSize runTimes) {
}


CStatus GPipeline::registerGNode(GElementPPtr nodeRef, const GElementPtrSet &dependElements,
CStatus GPipeline::registerGNode(GElementPPtr nodeRef, const GElementPtrSet &depends,
const std::string &name, CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(false)
Expand All @@ -130,12 +130,12 @@ CStatus GPipeline::registerGNode(GElementPPtr nodeRef, const GElementPtrSet &dep
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(nullptr != node->belong_, "[" + node->getName() + "] can not register to pipeline for its belong to [" + node->belong_->getName() + "]")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(node->isRegistered(), "[" + node->getName() + "] register duplicate")

status = innerRegister(node, dependElements, name, loop);
status = innerRegister(node, depends, name, loop);
CGRAPH_FUNCTION_END
}


CStatus GPipeline::registerGGroup(GElementPPtr groupRef, const GElementPtrSet &dependElements,
CStatus GPipeline::registerGGroup(GElementPPtr groupRef, const GElementPtrSet &depends,
const std::string &name, CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(false)
Expand All @@ -146,7 +146,7 @@ CStatus GPipeline::registerGGroup(GElementPPtr groupRef, const GElementPtrSet &d
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(nullptr != group->belong_, "[" + group->getName() + "] can not register to pipeline for its belong to [" + group->belong_->getName() + "]")
CGRAPH_RETURN_ERROR_STATUS_BY_CONDITION(group->isRegistered(), "[" + group->getName() + "] register duplicate")

status = innerRegister(group, dependElements, name, loop);
status = innerRegister(group, depends, name, loop);
CGRAPH_FUNCTION_END
}

Expand Down Expand Up @@ -345,14 +345,14 @@ CStatus GPipeline::initEnv() {
}


CStatus GPipeline::innerRegister(GElementPtr element, const GElementPtrSet &dependElements,
CStatus GPipeline::innerRegister(GElementPtr element, const GElementPtrSet &depends,
const std::string &name, CSize loop) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_NOT_NULL(element)
CGRAPH_ASSERT_INIT(false)

const std::string& curName = name.empty() ? element->getName() : name;
status = element->addElementInfo(dependElements, curName, loop);
status = element->addElementInfo(depends, curName, loop);
CGRAPH_FUNCTION_CHECK_STATUS

status = element->addManagers(param_manager_, event_manager_);
Expand Down
44 changes: 22 additions & 22 deletions src/GraphCtrl/GraphPipeline/GPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ class GPipeline : public GPipelineObject,
* 根据传入的信息,创建Group信息
* @tparam T
* @param elements
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename TGroup,
c_enable_if_t<std::is_base_of<GGroup, TGroup>::value, int> = 0>
TGroup* createGGroup(const GElementPtrArr &elements,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

Expand All @@ -140,15 +140,15 @@ class GPipeline : public GPipelineObject,
* 如果注册的是GGroup信息,则需外部提前生成,然后注册进来
* @tparam T
* @param elementRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename T,
c_enable_if_t<std::is_base_of<GElement, T>::value, int> = 0>
CStatus registerGElement(GElementPPtr elementRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

Expand All @@ -157,95 +157,95 @@ class GPipeline : public GPipelineObject,
* @tparam TNode
* @tparam Args
* @param elementRef
* @param dependElements
* @param depends
* @return
*/
template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GTemplateNode<Args ...>, TNode>::value, int> = 0>
CStatus registerGElement(GTemplateNodePtr<Args ...> *elementRef,
const GElementPtrSet &dependElements,
const GElementPtrSet &depends,
Args... args);

/**
* 注册一个 node
* @tparam T
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename TNode,
c_enable_if_t<std::is_base_of<GNode, TNode>::value, int> = 0>
TNode* registerGNode(const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
TNode* registerGNode(const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 注册一个 node
* @tparam TNode
* @tparam Args
* @param dependElements
* @param depends
* @param args
* @return
*/
template<typename TNode, typename ...Args,
c_enable_if_t<std::is_base_of<GTemplateNode<Args ...>, TNode>::value, int> = 0>
TNode* registerGNode(const GElementPtrSet &dependElements,
TNode* registerGNode(const GElementPtrSet &depends,
Args... args);

/**
* 注册一个节点信息
* @param nodeRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
CStatus registerGNode(GElementPPtr nodeRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 注册一个组信息(推荐使用)
* @param groupRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
CStatus registerGGroup(GElementPPtr groupRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 注册function类型的内容,模板特化
* @tparam GFunction
* @param functionRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename GFunction>
CStatus registerGElement(GFunctionPPtr functionRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

/**
* 注册fence类型的内容,模板特化
* @tparam GFence
* @param fenceRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename GFence>
CStatus registerGElement(GFencePPtr fenceRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

Expand All @@ -254,14 +254,14 @@ class GPipeline : public GPipelineObject,
* @tparam GCoordinator
* @tparam SIZE
* @param coordinatorRef
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
template<typename GCoordinator, CInt SIZE>
CStatus registerGElement(GCoordinatorPPtr<SIZE> coordinatorRef,
const GElementPtrSet &dependElements = std::initializer_list<GElementPtr>(),
const GElementPtrSet &depends = std::initializer_list<GElementPtr>(),
const std::string &name = CGRAPH_EMPTY,
CSize loop = CGRAPH_DEFAULT_LOOP_TIMES);

Expand Down Expand Up @@ -402,12 +402,12 @@ class GPipeline : public GPipelineObject,
/**
* 内部真实一个 element 信息
* @param element
* @param dependElements
* @param depends
* @param name
* @param loop
* @return
*/
CStatus innerRegister(GElementPtr element, const GElementPtrSet &dependElements,
CStatus innerRegister(GElementPtr element, const GElementPtrSet &depends,
const std::string &name, CSize loop);

/** 不允许外部赋值和构造 */
Expand Down
Loading

0 comments on commit 1a4ae30

Please sign in to comment.