diff --git a/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.h b/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.h index 22946b3b..62273167 100644 --- a/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.h +++ b/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.h @@ -18,13 +18,12 @@ class GSingleton : public GAdapter { private: explicit GSingleton(); - CStatus init() override; - CStatus run() override; - CStatus destroy() override; + CStatus init() final; + CStatus run() final; + CStatus destroy() final; CStatus addElementInfo(const std::set &dependElements, - const std::string &name, - CSize loop) final; + const std::string &name, CSize loop) final; CStatus addManagers(GParamManagerPtr paramManager, GEventManagerPtr eventManager) final; @@ -35,6 +34,8 @@ class GSingleton : public GAdapter { CBool isRegistered() const final; + const std::string& getName() const final; + private: static USingleton s_singleton_; // 单例 static std::atomic s_is_init_; // 标志是否被初始化过 diff --git a/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.inl b/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.inl index fce68c50..ed4ba795 100644 --- a/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.inl +++ b/src/GraphCtrl/GraphElement/GAdapter/GSingleton/GSingleton.inl @@ -36,7 +36,7 @@ CStatus GSingleton::init() { } /* 因为采取的是饥汉模式,不需要判断ptr是否为空了 */ - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); status = element->init(); if (status.isOK()) { s_is_init_ = true; @@ -63,7 +63,7 @@ CStatus GSingleton::destroy() { CGRAPH_FUNCTION_END } - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); status = element->destroy(); if (status.isOK()) { s_is_init_ = false; @@ -75,8 +75,7 @@ CStatus GSingleton::destroy() { template CStatus GSingleton::addElementInfo(const std::set &dependElements, - const std::string &name, - CSize loop) { + const std::string &name, CSize loop) { CGRAPH_FUNCTION_BEGIN CGRAPH_ASSERT_INIT(false) @@ -86,7 +85,7 @@ CStatus GSingleton::addElementInfo(const std::set &dependElement // 这里,内部和外部均需要设定name信息 this->setName(name)->setLoop(loop); // 获取单例信息,然后将信息node中信息 - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); element->name_ = name; CGRAPH_FUNCTION_END } @@ -99,7 +98,7 @@ CStatus GSingleton::addManagers(GParamManagerPtr paramManager, CGRAPH_ASSERT_INIT(false) CGRAPH_ASSERT_NOT_NULL(paramManager, eventManager) - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); status = element->addManagers(paramManager, eventManager); CGRAPH_FUNCTION_END @@ -108,24 +107,31 @@ CStatus GSingleton::addManagers(GParamManagerPtr paramManager, template CBool GSingleton::isHold(){ - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); return element->isHold(); } template CBool GSingleton::isMatch() { - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); return element->isMatch(); } template CBool GSingleton::isRegistered() const { - auto element = dynamic_cast(s_singleton_.get()); + auto element = dynamic_cast(s_singleton_.get()); return element->isRegistered(); } + +template +const std::string& GSingleton::getName() const { + auto element = dynamic_cast(s_singleton_.get()); + return element->getName(); +} + CGRAPH_NAMESPACE_END #endif //CGRAPH_GSINGLETON_INL diff --git a/src/GraphCtrl/GraphElement/GElement.cpp b/src/GraphCtrl/GraphElement/GElement.cpp index 0609ea52..01862085 100644 --- a/src/GraphCtrl/GraphElement/GElement.cpp +++ b/src/GraphCtrl/GraphElement/GElement.cpp @@ -134,11 +134,6 @@ GElement& GElement::operator*(CSize loop) noexcept { } -CBool GElement::isRunnable() const { - return 0 >= this->left_depend_.load(std::memory_order_acquire) && !this->done_; -} - - CBool GElement::isLinkable() const { return this->linkable_; } diff --git a/src/GraphCtrl/GraphElement/GElement.h b/src/GraphCtrl/GraphElement/GElement.h index 5b620fa3..560876e3 100644 --- a/src/GraphCtrl/GraphElement/GElement.h +++ b/src/GraphCtrl/GraphElement/GElement.h @@ -239,13 +239,6 @@ class GElement : public GElementObject, */ virtual CVoid afterRun(); - /** - * 判定element是否可以运行 - * 可执行的条件为:自身未被执行且依赖节点全部被执行 - * @return - */ - CBool isRunnable() const; - /** * 判定node是否可以和前面节点一起执行 * @return @@ -447,7 +440,6 @@ class GElement : public GElementObject, template friend class GCoordinator; template friend class GSingleton; friend class GEngine; - friend class GStaticEngine; friend class GDynamicEngine; friend class GTopoEngine; friend class GAspectObject; diff --git a/src/GraphCtrl/GraphElement/GElementManager.cpp b/src/GraphCtrl/GraphElement/GElementManager.cpp index 792cc26f..59d65510 100644 --- a/src/GraphCtrl/GraphElement/GElementManager.cpp +++ b/src/GraphCtrl/GraphElement/GElementManager.cpp @@ -128,7 +128,6 @@ CStatus GElementManager::initEngine() { CGRAPH_DELETE_PTR(engine_) switch (engine_type_) { - case GEngineType::STATIC : engine_ = CGRAPH_SAFE_MALLOC_COBJECT(GStaticEngine) break; case GEngineType::DYNAMIC : engine_ = CGRAPH_SAFE_MALLOC_COBJECT(GDynamicEngine) break; case GEngineType::TOPO: engine_ = CGRAPH_SAFE_MALLOC_COBJECT(GTopoEngine) break; default: CGRAPH_RETURN_ERROR_STATUS("unknown engine type") diff --git a/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.cpp b/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.cpp index 2d23484d..2b975030 100644 --- a/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.cpp +++ b/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.cpp @@ -42,44 +42,6 @@ CStatus GCluster::run() { } -CStatus GCluster::process(CBool isMock) { - CGRAPH_FUNCTION_BEGIN - - this->beforeRun(); - if (likely(!isMock)) { - // 如果是mock执行,则不进入这里 - for (GElementPtr element : this->group_elements_arr_) { - status = element->fatProcessor(CFunctionType::RUN); - CGRAPH_FUNCTION_CHECK_STATUS - } - } - - this->afterRun(); - CGRAPH_FUNCTION_END -} - - -CVoid GCluster::beforeRun() { - this->done_ = false; - this->left_depend_ = dependence_.size(); - for (GElementPtr element : this->group_elements_arr_) { - element->beforeRun(); - } -} - - -CVoid GCluster::afterRun() { - for (GElementPtr element : this->group_elements_arr_) { - element->afterRun(); - } - - for (auto& element : this->run_before_) { - element->left_depend_--; - } - this->done_ = true; -} - - CStatus GCluster::addElement(GElementPtr element) { CGRAPH_FUNCTION_BEGIN CGRAPH_ASSERT_NOT_NULL(element) @@ -90,21 +52,6 @@ CStatus GCluster::addElement(GElementPtr element) { } -CSize GCluster::getElementNum() { - auto num = group_elements_arr_.size(); - return num; -} - - -CBool GCluster::isDone() { - /* 所有的element均被执行过,则提示true */ - return std::all_of(group_elements_arr_.begin(), group_elements_arr_.end(), - [](GElementPtr element) { - return element->done_; - }); -} - - CVoid GCluster::dump(std::ostream& oss) { dumpElement(oss); dumpGroupLabelBegin(oss); diff --git a/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.h b/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.h index e3188a04..9e655f5b 100644 --- a/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.h +++ b/src/GraphCtrl/GraphElement/GGroup/GCluster/GCluster.h @@ -16,59 +16,26 @@ CGRAPH_NAMESPACE_BEGIN class GCluster : public GGroup { -public: +protected: /** 涉及到与线程池的联动,cluster类无法将构造函数申明成protected类型 */ GCluster(const GCluster& cluster); GCluster& operator=(const GCluster& cluster); - -protected: explicit GCluster(); - /** - * 获取element个数信息 - * @return - */ - CSize getElementNum(); - private: - /** - * 线程池中的运行函数,依次执行beforeRun,run和afterRun方法, - * 其中有任何返回值问题,则直接返回 - * @param isMock 是否真实执行run方法。默认执行的 - * @return - */ - CStatus process(CBool isMock); - CStatus run() final; CStatus addElement(GElementPtr element) final; - CVoid beforeRun() final; - - CVoid afterRun() final; - CVoid dump(std::ostream& oss) final; CBool isSeparate(GElementCPtr a, GElementCPtr b) const final; - /** - * 判断是否所有element均执行结束了 - * @return - */ - CBool isDone(); - friend class GPipeline; - friend class GStaticEngine; friend class GDynamicEngine; friend class UAllocator; }; -using GClusterPtr = GCluster *; -using GClusterRef = GCluster &; -using GClusterArr = std::vector; -using GClusterArrRef = GClusterArr &; -using ParaWorkedClusterArrs = std::vector; - CGRAPH_NAMESPACE_END #endif //CGRAPH_GCLUSTER_H diff --git a/src/GraphCtrl/GraphElement/GGroup/GGroup.h b/src/GraphCtrl/GraphElement/GGroup/GGroup.h index bdb7e6e7..a8527409 100644 --- a/src/GraphCtrl/GraphElement/GGroup/GGroup.h +++ b/src/GraphCtrl/GraphElement/GGroup/GGroup.h @@ -64,7 +64,6 @@ class GGroup : public GElement { private: GElementPtrArr group_elements_arr_; // 存放 element的数组 - friend class GStaticEngine; friend class GPipeline; friend class GCluster; friend class GRegion; diff --git a/src/GraphCtrl/GraphElement/GGroup/GSome/GSome.h b/src/GraphCtrl/GraphElement/GGroup/GSome/GSome.h index a135ef8e..33283518 100644 --- a/src/GraphCtrl/GraphElement/GGroup/GSome/GSome.h +++ b/src/GraphCtrl/GraphElement/GGroup/GSome/GSome.h @@ -44,7 +44,6 @@ class GSome : public GGroup { std::condition_variable cv_; friend class GPipeline; - friend class GStaticEngine; friend class GDynamicEngine; friend class UAllocator; }; diff --git a/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp b/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp index ca8e2ff0..86262020 100644 --- a/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp +++ b/src/GraphCtrl/GraphElement/_GEngine/GDynamicEngine/GDynamicEngine.cpp @@ -126,12 +126,7 @@ CVoid GDynamicEngine::analysisDagType(const GSortedGElementPtrSet& elements) { CVoid GDynamicEngine::process(GElementPtr element, CBool affinity) { - if (unlikely(cur_status_.isErr() || element->done_)) { - /** - * 如果已经有异常逻辑, - * 或者传入的element,是已经执行过的了(理论上不会出现这种情况,由于提升性能的原因,取消了atomic计数的逻辑,故添加这一处判定,防止意外情况) - * 则直接停止当前流程 - */ + if (unlikely(cur_status_.isErr())) { return; } diff --git a/src/GraphCtrl/GraphElement/_GEngine/GEngineDefine.h b/src/GraphCtrl/GraphElement/_GEngine/GEngineDefine.h index a1a2da82..4576ca53 100644 --- a/src/GraphCtrl/GraphElement/_GEngine/GEngineDefine.h +++ b/src/GraphCtrl/GraphElement/_GEngine/GEngineDefine.h @@ -14,9 +14,8 @@ CGRAPH_NAMESPACE_BEGIN enum class GEngineType { - STATIC = 0, /** 静态图运行 */ - DYNAMIC = 1, /** 动态图运行 */ - TOPO = 2, /** 拓扑运行 */ + DYNAMIC = 0, /** 动态图运行 */ + TOPO = 1, /** 拓扑运行 */ }; CGRAPH_INTERNAL_NAMESPACE_BEGIN diff --git a/src/GraphCtrl/GraphElement/_GEngine/GEngineInclude.h b/src/GraphCtrl/GraphElement/_GEngine/GEngineInclude.h index 5f3e1fac..b92be69f 100644 --- a/src/GraphCtrl/GraphElement/_GEngine/GEngineInclude.h +++ b/src/GraphCtrl/GraphElement/_GEngine/GEngineInclude.h @@ -11,7 +11,6 @@ #include "GEngine.h" #include "GEngineDefine.h" -#include "GStaticEngine/GStaticEngine.h" #include "GDynamicEngine/GDynamicEngine.h" #include "GTopoEngine/GTopoEngine.h" diff --git a/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.cpp b/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.cpp deleted file mode 100644 index 2b2cacd6..00000000 --- a/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.cpp +++ /dev/null @@ -1,144 +0,0 @@ -/*************************** -@Author: Chunel -@Contact: chunel@foxmail.com -@File: GStaticEngine.cpp -@Time: 2022/12/11 16:43 -@Desc: -***************************/ - -#include "GStaticEngine.h" - -CGRAPH_NAMESPACE_BEGIN - -CStatus GStaticEngine::setup(const GSortedGElementPtrSet& elements) { - CGRAPH_FUNCTION_BEGIN - link(elements); - status = analyse(elements); - CGRAPH_FUNCTION_END -} - - -CStatus GStaticEngine::analyse(const GSortedGElementPtrSet& elements) { - CGRAPH_FUNCTION_BEGIN - run_element_size_ = 0; - para_cluster_arrs_.clear(); - total_element_size_ = (CUint)elements.size(); // 仅赋值一次,不会改变了 - - GClusterArr curClusterArr; // 记录每一层,可以并行的逻辑 - for (GElementPtr element : 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() && run_element_size_ <= total_element_size_) { - runnableClusterArr = curClusterArr; - curClusterArr.clear(); - - for (GClusterRef cluster : runnableClusterArr) { - status = cluster.process(true); // 不执行run方法的process - CGRAPH_FUNCTION_CHECK_STATUS - } - run_element_size_ += (CUint)runnableClusterArr.size(); - - GElementPtrSet duplications; - for (GClusterRef cluster : runnableClusterArr) { - for (GElementPtr element : cluster.group_elements_arr_) { - 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 GStaticEngine::run() { - CGRAPH_FUNCTION_BEGIN - run_element_size_ = 0; // 每次执行的时候,记录执行了多少个element信息 - std::vector > futures; - - for (GClusterArrRef clusterArr : para_cluster_arrs_) { - futures.clear(); - - /** 将分解后的pipeline信息,以cluster为维度,放入线程池依次执行 */ - for (GClusterRef cluster : clusterArr) { - futures.emplace_back(thread_pool_->commit([&cluster] { - return cluster.process(false); - }, calcIndex(&cluster))); - - run_element_size_ += cluster.getElementNum(); - } - - for (auto& fut : futures) { - status += fut.get(); - } - CGRAPH_FUNCTION_CHECK_STATUS - } - - CGRAPH_FUNCTION_END -} - - -CStatus GStaticEngine::afterRunCheck() { - CGRAPH_FUNCTION_BEGIN - /* 验证是否所有的内容均被执行过 */ - if (run_element_size_ != total_element_size_) { - const std::string& errInfo = "run size = [" + std::to_string(run_element_size_) - + "], total size = [" + std::to_string(total_element_size_) + "] ..."; - CGRAPH_RETURN_ERROR_STATUS("static engine run element size check failed, " + errInfo) - } - - /* 需要验证每个cluster里的每个内容是否被执行过一次 */ - for (GClusterArrRef clusterArr : para_cluster_arrs_) { - for (GClusterRef cluster : clusterArr) { - if (!cluster.isDone()) { - CGRAPH_RETURN_ERROR_STATUS("static engine run done status check failed..."); - } - } - } - - CGRAPH_FUNCTION_END -} - -CGRAPH_NAMESPACE_END \ No newline at end of file diff --git a/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.h b/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.h deleted file mode 100644 index 0135b037..00000000 --- a/src/GraphCtrl/GraphElement/_GEngine/GStaticEngine/GStaticEngine.h +++ /dev/null @@ -1,44 +0,0 @@ -/*************************** -@Author: Chunel -@Contact: chunel@foxmail.com -@File: GStaticEngine.h -@Time: 2022/12/11 16:34 -@Desc: -***************************/ - -#ifndef CGRAPH_GSTATICENGINE_H -#define CGRAPH_GSTATICENGINE_H - -#include "../GEngine.h" -#include "../../GGroup/GCluster/GCluster.h" - -CGRAPH_NAMESPACE_BEGIN - -class GStaticEngine : public GEngine { -protected: - explicit GStaticEngine() = default; - - CStatus setup(const GSortedGElementPtrSet& elements) override; - - CStatus run() override; - - CStatus afterRunCheck() override; - - /** - * 将所有注册到 pipeline 中的信息,解析到 para_cluster_arrs_ 中 - * @param elements - * @return - */ - CStatus analyse(const GSortedGElementPtrSet& elements); - -private: - ParaWorkedClusterArrs para_cluster_arrs_; // 可以并行的cluster数组 - CUint run_element_size_ = 0; // 当前已经执行的element的数量 - CUint total_element_size_ = 0; // 总的element的数量 - - friend class UAllocator; -}; - -CGRAPH_NAMESPACE_END - -#endif //CGRAPH_GSTATICENGINE_H