Skip to content

Commit

Permalink
[feat] add MultiLinearCondition and MultiParallelCondition. optimize …
Browse files Browse the repository at this point in the history
…mingw compiler notice.
  • Loading branch information
ChunelFeng committed Jun 16, 2023
1 parent 6d00108 commit 5a36ae9
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ message("* \\____/ \\____/ /_/ \\__,_/ / .___/ /_/ /_/ *")
message("* /_/ by Chunel Feng *")
message("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *")

cmake_minimum_required(VERSION 3.5.0)
cmake_minimum_required(VERSION 3.2.5)

project(CGraph VERSION 2.4.1)
project(CGraph VERSION 2.4.2)

# CGraph默认使用C++11版本,推荐使用C++17版本。暂不支持C++11以下版本
set(CMAKE_CXX_STANDARD 11)
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ int main() {
* 提供`pipeline`最大并发度获取方法。感谢 [Hanano-Yuuki](https://github.com/Hanano-Yuuki) 提供相关解决方案
* 提供`pipeline`异步执行功能和执行时退出功能
[2023.06.17 - v2.4.2 - Chunel]
* 提供`MultiLinearCondition`(串行条件)和`MultiParallelCondition`(并行条件)功能
</details>
------------
Expand Down
5 changes: 5 additions & 0 deletions cmake/CGraph-env-include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ ELSEIF(UNIX)
# linux平台,加入多线程内容
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -pthread -Wno-format-overflow")
add_definitions(-D_ENABLE_LIKELY_)
ELSEIF(MINGW)
MESSAGE("[notice] do not support MinGW now, please set compiler info in CGraph/cmake/CGraph-env-include.cmake")
# 由于平台限制,暂不支持MINGW直接编译。请手动设置如下 CMAKE_C_COMPILER 和 CMAKE_CXX_COMPILER信息
# set(CMAKE_C_COMPILER "my_path_to/mingw/bin/gcc")
# set(CMAKE_CXX_COMPILER "my_path_to/mingw/bin/g++")
ELSEIF(WIN32)
# windows平台,加入utf-8设置。否则无法通过编译
# 直接Download ZIP文件,导致无法编译通过问题的解决方法,参考:https://github.com/ChunelFeng/CGraph/issues/12
Expand Down
Binary file modified doc/xmind/CGraph Intro.xmind
Binary file not shown.
2 changes: 2 additions & 0 deletions src/GraphCtrl/GraphDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ static const char* CGRAPH_STR_NODE = "node";
static const char* CGRAPH_STR_CLUSTER = "cluster";
static const char* CGRAPH_STR_REGION = "region";
static const char* CGRAPH_STR_CONDITION = "condition";
static const char* CGRAPH_STR_LINEAR_CONDITION = "linear_condition";
static const char* CGRAPH_STR_PARALLEL_CONDITION = "parallel_condition";
static const char* CGRAPH_STR_FUNCTION = "function";
static const char* CGRAPH_STR_SINGLETON = "singleton";
static const char* CGRAPH_STR_DAEMON = "daemon";
Expand Down
10 changes: 8 additions & 2 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,16 @@ CBool GElement::isHold() {
}


CStatus GElement::crashed(const CException& ex) {
CBool GElement::isMatch() {
/**
* 默认直接返回
* 默认仅返回false
* 主要面对写入 Multi*Condition 的时候,做判断当前element是否被执行
*/
return false;
}


CStatus GElement::crashed(const CException& ex) {
return CStatus(STATUS_CRASH, ex.what());
}

Expand Down
12 changes: 11 additions & 1 deletion src/GraphCtrl/GraphElement/GElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class GElement : public GElementObject,
/**
* 当前element是否是一个 group逻辑
* @return
*/
*/
CBool isGroup();

protected:
Expand Down Expand Up @@ -137,10 +137,18 @@ class GElement : public GElementObject,
*/
virtual CBool isHold();

/**
* 用于在Multi*Condition中被判定,是否可以执行。
* @return
* @notice 默认返回false,不执行
*/
virtual CBool isMatch();

/**
* 崩溃流程处理
* @param ex
* @return
* @notice 可以自行覆写crashed方法,但不推荐。如果需要复写的话,返回值需要填写 STATUS_CRASH,否则可能出现执行异常
*/
virtual CStatus crashed(const CException& ex);

Expand Down Expand Up @@ -280,6 +288,8 @@ class GElement : public GElementObject,
friend class GCluster;
friend class GRegion;
friend class GCondition;
friend class GMultiLinearCondition;
friend class GMultiParallelCondition;
friend class GElementManager;
friend class GGroup;
friend class GPipeline;
Expand Down
2 changes: 2 additions & 0 deletions src/GraphCtrl/GraphElement/GElementDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum class GElementType {
CLUSTER = 0x00020001, // 簇
REGION = 0x00020002, // 区域
CONDITION = 0x00020004, // 条件
LINEAR_CONDITION = 0x00020014, // 串行条件
PARALLEL_CONDITION = 0x00020024, // 并行条件
ADAPTER = 0x00040000, // 适配器
FUNCTION = 0x00040001, // 函数
SINGLETON = 0x00040002, // 单例
Expand Down
16 changes: 16 additions & 0 deletions src/GraphCtrl/GraphElement/GGroup/GCondition/GConditionInclude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: GConditionInclude.h
@Time: 2023/6/16 22:10
@Desc:
***************************/

#ifndef CGRAPH_GCONDITIONINCLUDE_H
#define CGRAPH_GCONDITIONINCLUDE_H

#include "GCondition.h"
#include "GMultiLinearCondition.h"
#include "GMultiParallelCondition.h"

#endif //CGRAPH_GCONDITIONINCLUDE_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: GMultiLinearCondition.cpp
@Time: 2023/6/16 22:09
@Desc:
***************************/

#include "GMultiLinearCondition.h"

CGRAPH_NAMESPACE_BEGIN

GMultiLinearCondition::GMultiLinearCondition() {
element_type_ = GElementType::LINEAR_CONDITION;
session_ = URandom<>::generateSession(CGRAPH_STR_LINEAR_CONDITION);
}


CStatus GMultiLinearCondition::run() {
CGRAPH_FUNCTION_BEGIN

for (auto cur: this->group_elements_arr_) {
if (cur->isMatch()) {
// 如果在
status = cur->fatProcessor(CFunctionType::RUN);
CGRAPH_FUNCTION_CHECK_STATUS
}
}

CGRAPH_FUNCTION_END
}


CIndex GMultiLinearCondition::choose() {
CGRAPH_THROW_EXCEPTION("GMultiLinearCondition no support choose function")
return 0;
}

CGRAPH_NAMESPACE_END


Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: GMultiLinearCondition.h
@Time: 2023/6/16 22:09
@Desc:
***************************/

#ifndef CGRAPH_GMULTILINEARCONDITION_H
#define CGRAPH_GMULTILINEARCONDITION_H

#include "GCondition.h"

CGRAPH_NAMESPACE_BEGIN

class GMultiLinearCondition : public GCondition {
protected:
explicit GMultiLinearCondition();

CStatus run() override;

private:
CIndex choose() final;

friend class GPipeline;
friend class UAllocator;
};

CGRAPH_NAMESPACE_END

#endif //CGRAPH_GMULTILINEARCONDITION_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: GMultiParallelCondition.cpp
@Time: 2023/6/16 00:05
@Desc:
***************************/

#include "GMultiParallelCondition.h"

CGRAPH_NAMESPACE_BEGIN

GMultiParallelCondition::GMultiParallelCondition() {
element_type_ = GElementType::PARALLEL_CONDITION;
session_ = URandom<>::generateSession(CGRAPH_STR_PARALLEL_CONDITION);
}


CStatus GMultiParallelCondition::run() {
CGRAPH_FUNCTION_BEGIN
std::vector<std::future<CStatus> > futures;
for (GElementPtr cur : this->group_elements_arr_) {
if (!cur->isMatch()) {
continue; // 不满足条件,则不执行
}

futures.emplace_back(this->thread_pool_->commit([cur] {
return cur->fatProcessor(CFunctionType::RUN);
}, cur->getBindingIndex()));
}

for (auto& fut: futures) {
/**
* 如果有异常值的化,也等待所有内容计算完成后,统一返回
* 暂时没有处理超时的情况。预计今后会统一处理
*/
status += fut.get();
}

CGRAPH_FUNCTION_END
}


CIndex GMultiParallelCondition::choose() {
CGRAPH_THROW_EXCEPTION("GMultiParallelCondition no need choose function")
return 0;
}

CGRAPH_NAMESPACE_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: GMultiParallelCondition.h
@Time: 2023/6/16 00:05
@Desc:
***************************/

#ifndef CGRAPH_GMULTIPARALLELCONDITION_H
#define CGRAPH_GMULTIPARALLELCONDITION_H

#include "GCondition.h"

CGRAPH_NAMESPACE_BEGIN

class GMultiParallelCondition : public GCondition {
protected:
explicit GMultiParallelCondition();

CStatus run() override;

private:
CIndex choose() final;

friend class GPipeline;
friend class UAllocator;
};

CGRAPH_NAMESPACE_END

#endif //CGRAPH_GMULTIPARALLELCONDITION_H
2 changes: 1 addition & 1 deletion src/GraphCtrl/GraphElement/GGroup/GGroupInclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
#include "GGroup.h"
#include "GCluster/GCluster.h"
#include "GRegion/GRegion.h"
#include "GCondition/GCondition.h"
#include "GCondition/GConditionInclude.h"

#endif //CGRAPH_GGROUPINCLUDE_H

0 comments on commit 5a36ae9

Please sign in to comment.