Skip to content

Commit

Permalink
[perf] add element repo class, to store all elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Jun 24, 2023
1 parent ab23535 commit 17f2711
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 99 deletions.
1 change: 0 additions & 1 deletion src/DomainCtrl/Ann/DAnnInclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "DAnnDefine.h"
#include "DAnnNode.h"
#include "DAnnTemplateNode.h"
#include "DAnnParam.h"

#endif //CGRAPH_DANNINCLUDE_H
30 changes: 0 additions & 30 deletions src/DomainCtrl/Ann/DAnnTemplateNode.h

This file was deleted.

1 change: 1 addition & 0 deletions src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ class GElement : public GElementObject,
friend class GStaticEngine;
friend class GDynamicEngine;
friend class GMaxParaOptimizer;
friend class GElementRepository;
template<typename T> friend class GSingleton;
template<GMultiConditionType> friend class GMultiCondition;

Expand Down
1 change: 1 addition & 0 deletions src/GraphCtrl/GraphElement/GElementInclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define CGRAPH_GELEMENTINCLUDE_H

#include "GElement.h"
#include "GElementRepository.h"
#include "GNode/GNodeInclude.h"
#include "GGroup/GGroupInclude.h"
#include "GAdapter/GAdapterInclude.h"
Expand Down
75 changes: 75 additions & 0 deletions src/GraphCtrl/GraphElement/GElementRepository.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: GElementRepository.cpp
@Time: 2023/6/24 17:51
@Desc:
***************************/

#include "GElementRepository.h"

CGRAPH_NAMESPACE_BEGIN

CVoid GElementRepository::insert(GElementPtr ptr) {
if (nullptr != ptr) {
elements_.insert(ptr);
}
}


CBool GElementRepository::find(GElementPtr ptr) {
if (nullptr == ptr) {
return false;
}
return elements_.find(ptr) != elements_.end();
}


GElementRepositoryPtr GElementRepository::setThreadPool(UThreadPoolPtr ptr) {
CGRAPH_ASSERT_NOT_NULL_RETURN_NULL(ptr)
for (auto& cur : this->elements_) {
cur->setThreadPool(ptr);
}
return this;
}


CStatus GElementRepository::setup() {
CGRAPH_FUNCTION_BEGIN
if (elements_.empty()
|| GElementState::NORMAL != (*elements_.begin())->cur_state_.load()) {
return status;
}

status = pushAllState(GElementState::NORMAL);
CGRAPH_FUNCTION_END
}


CStatus GElementRepository::pushAllState(GElementState state) {
CGRAPH_FUNCTION_BEGIN

for (auto* cur : elements_) {
cur->cur_state_.store(state);
if (GElementState::YIELD != state) {
// 目前仅非yield状态,需要切换的。如果一直处于 yield状态,是不需要被通知的
cur->yield_cv_.notify_one();
}
}
CGRAPH_FUNCTION_END
}


GElementRepository::~GElementRepository() {
// 删除所有内部的element信息
for (GElementPtr element : elements_) {
CGRAPH_DELETE_PTR(element)
}
}


CStatus GElementRepository::run() {
CGRAPH_NO_SUPPORT
}

CGRAPH_NAMESPACE_END
67 changes: 67 additions & 0 deletions src/GraphCtrl/GraphElement/GElementRepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: GElementRepository.h
@Time: 2023/6/24 16:42
@Desc:
***************************/

#ifndef CGRAPH_GELEMENTREPOSITORY_H
#define CGRAPH_GELEMENTREPOSITORY_H

#include "GElementObject.h"
#include "GElement.h"

CGRAPH_NAMESPACE_BEGIN

class GElementRepository : public GElementObject {
private:
/**
* 插入一个element
* @param ptr
* @return
*/
CVoid insert(GElementPtr ptr);

/**
* 查找对应的element
* @param ptr
* @return
*/
CBool find(GElementPtr ptr);

/**
* 给所有的element,设定线程池
* @param ptr
* @return
*/
GElementRepository* setThreadPool(UThreadPoolPtr ptr);

/**
* 准备执行流程
* @return
*/
CStatus setup();

/**
* 设置所有内部的element状态
* @param state
* @return
*/
CStatus pushAllState(GElementState state);

~GElementRepository() override;

CStatus run() override;

private:
GElementPtrSet elements_; // 用于记录所有的element信息

friend class GPipeline;
};

using GElementRepositoryPtr = GElementRepository *;

CGRAPH_NAMESPACE_END

#endif //CGRAPH_GELEMENTREPOSITORY_H
53 changes: 12 additions & 41 deletions src/GraphCtrl/GraphPipeline/GPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ GPipeline::GPipeline() {


GPipeline::~GPipeline() {
// 结束的时候,会自动释放所有的element信息
CGRAPH_DELETE_PTR(daemon_manager_)
CGRAPH_DELETE_PTR(element_manager_)
CGRAPH_DELETE_PTR(param_manager_)
CGRAPH_DELETE_PTR(event_manager_)

// 结束的时候,清空所有创建的节点信息。所有节点信息,仅在这一处释放
for (GElementPtr element : element_repository_) {
CGRAPH_DELETE_PTR(element)
}
}


Expand Down Expand Up @@ -61,13 +57,13 @@ CStatus GPipeline::run() {
CGRAPH_ASSERT_NOT_NULL(param_manager_)

/**
* 1. 做执行前的准备
* 1. 将所有 GElement 的状态设定为 NORMAL
* 2. 将所有的 GParam 设置为初始值
* 3. 执行dag逻辑
* 4. 将所有的 GParam 复原
*/
this->prepare();
status = param_manager_->setup();
status += repository_.setup();
status += param_manager_->setup();
CGRAPH_FUNCTION_CHECK_STATUS

status = element_manager_->run();
Expand Down Expand Up @@ -141,18 +137,21 @@ std::future<CStatus> GPipeline::asyncProcess(CSize runTimes) {


CStatus GPipeline::cancel() {
return pushAllState(GElementState::CANCEL);
CGRAPH_ASSERT_INIT(true)
return repository_.pushAllState(GElementState::CANCEL);
}


CStatus GPipeline::yield() {
return pushAllState(GElementState::YIELD);
CGRAPH_ASSERT_INIT(true)
return repository_.pushAllState(GElementState::YIELD);
}


CStatus GPipeline::resume() {
// 直接恢复正常状态好了
return pushAllState(GElementState::NORMAL);
CGRAPH_ASSERT_INIT(true)
return repository_.pushAllState(GElementState::NORMAL);
}


Expand Down Expand Up @@ -233,39 +232,11 @@ CStatus GPipeline::initSchedule() {

auto tp = schedule_.getThreadPool();
event_manager_->setThreadPool(tp);
// 这里设置线程池,是为了将tp信息,传递给内部的engine类
element_manager_->setThreadPool(tp);

// 设置所有的element 中的thread_pool
for (auto& cur : this->element_repository_) {
CGRAPH_ASSERT_NOT_NULL(cur)
cur->setThreadPool(tp);
}

CGRAPH_FUNCTION_END
}


CVoid GPipeline::prepare() {
if (element_repository_.empty()
|| GElementState::NORMAL != (*element_repository_.begin())->cur_state_.load()) {
return;
}

pushAllState(GElementState::NORMAL);
}


CStatus GPipeline::pushAllState(GElementState state) {
CGRAPH_FUNCTION_BEGIN
CGRAPH_ASSERT_INIT(true)

for (auto cur : element_repository_) {
cur->cur_state_.store(state);
if (GElementState::YIELD != state) {
// 目前仅非yield状态,需要切换的。如果一直处于 yield状态,是不需要被通知的
cur->yield_cv_.notify_one();
}
}
repository_.setThreadPool(tp);

CGRAPH_FUNCTION_END
}
Expand Down
19 changes: 3 additions & 16 deletions src/GraphCtrl/GraphPipeline/GPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ class GPipeline : public GPipelineObject,
CStatus cancel();

/**
* 暂停当前pipeline的执行
* 暂停当前pipeline的执行,多用于异步执行流程中
* @return
*/
CStatus yield();

/**
* 恢复当前pipeline的执行
* 恢复当前pipeline的执行,多用于异步执行流程中
* @return
*/
CStatus resume();
Expand Down Expand Up @@ -274,19 +274,6 @@ class GPipeline : public GPipelineObject,
*/
CStatus initSchedule();

/**
* 准备执行流程
* @return
*/
CVoid prepare();

/**
* 设置所有内部的element状态
* @param state
* @return
*/
CStatus pushAllState(GElementState state);

/** 不允许外部赋值和构造 */
CGRAPH_NO_ALLOWED_COPY(GPipeline)

Expand All @@ -297,7 +284,7 @@ class GPipeline : public GPipelineObject,
GEventManagerPtr event_manager_ = nullptr; // 事件管理类

GSchedule schedule_; // 调度管理类
GElementPtrSet element_repository_; // 标记创建的所有节点,最终释放使用
GElementRepository repository_; // 记录创建的所有element的仓库

friend class GPipelineFactory;
friend class UAllocator;
Expand Down
Loading

0 comments on commit 17f2711

Please sign in to comment.