From cb64f0e952617f7695670d625a9be3657e0bebbe Mon Sep 17 00:00:00 2001 From: Chunel Date: Wed, 1 Jul 2026 00:21:45 +0800 Subject: [PATCH] [perf] optimize test-perf-04 by deliver_running_primary_thread_enable --- python/PyCGraph.cpp | 3 ++- src/UtilsCtrl/ThreadPool/UThreadPool.cpp | 9 +++++---- src/UtilsCtrl/ThreadPool/UThreadPoolConfig.h | 1 + src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h | 1 + test/Performance/test-performance-04.cpp | 1 + 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/python/PyCGraph.cpp b/python/PyCGraph.cpp index 36bbe7ec..bdc6bcf8 100644 --- a/python/PyCGraph.cpp +++ b/python/PyCGraph.cpp @@ -40,7 +40,8 @@ PYBIND11_MODULE(pycgraph, cg) { .def_readwrite("secondary_thread_priority", &UThreadPoolConfig::secondary_thread_priority_) .def_readwrite("bind_cpu_enable", &UThreadPoolConfig::bind_cpu_enable_) .def_readwrite("batch_task_enable", &UThreadPoolConfig::batch_task_enable_) - .def_readwrite("monitor_enable", &UThreadPoolConfig::monitor_enable_); + .def_readwrite("monitor_enable", &UThreadPoolConfig::monitor_enable_) + .def_readwrite("deliver_running_primary_thread_enable", &UThreadPoolConfig::deliver_running_primary_thread_enable_); py::class_(cg, "GElementRelation") .def(py::init<>()) diff --git a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp index 0b546b17..3233d757 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPool.cpp +++ b/src/UtilsCtrl/ThreadPool/UThreadPool.cpp @@ -210,9 +210,10 @@ CIndex UThreadPool::dispatch(const CIndex origIndex) { CIndex realIndex = 0; if (CGRAPH_DEFAULT_TASK_STRATEGY == origIndex) { realIndex = cur_index_.fetch_add(1, std::memory_order_relaxed) % config_.max_thread_size_; - if (realIndex >= 0 && realIndex < config_.default_thread_size_ + if (!config_.deliver_running_primary_thread_enable_ + && realIndex >= 0 && realIndex < config_.default_thread_size_ && primary_threads_[realIndex]->is_running_.load(std::memory_order_relaxed)) { - // 如果是默认调度,并且被放置到 正在running 的pt中,则切换为 trigger_one 的策略,防止阻塞 + // 如果是默认调度,并且被放置到 正在running 的pt中,则切换为 trigger_all 的策略,防止阻塞 realIndex = CGRAPH_TRIGGER_ALL_THREAD_STRATEGY; } } else { @@ -274,7 +275,7 @@ CVoid UThreadPool::monitor() { CSize UThreadPool::wakeupAllThread() { CSize size = 0; if (wakeup_mutex_.try_lock()) { - for (const auto& pt : primary_threads_) { + for (auto* pt : primary_threads_) { if (pt->wakeup()) { ++size; } @@ -285,7 +286,7 @@ CSize UThreadPool::wakeupAllThread() { ++size; } } - wakeup_mutex_.unlock(); + wakeup_mutex_.unlock(); } return size; diff --git a/src/UtilsCtrl/ThreadPool/UThreadPoolConfig.h b/src/UtilsCtrl/ThreadPool/UThreadPoolConfig.h index 14147ef9..c7d2fdd0 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPoolConfig.h +++ b/src/UtilsCtrl/ThreadPool/UThreadPoolConfig.h @@ -38,6 +38,7 @@ struct UThreadPoolConfig : public CStruct { CBool bind_cpu_enable_ = CGRAPH_BIND_CPU_ENABLE; CBool batch_task_enable_ = CGRAPH_BATCH_TASK_ENABLE; CBool monitor_enable_ = CGRAPH_MONITOR_ENABLE; + CBool deliver_running_primary_thread_enable_ = CGRAPH_DELIVER_RUNNING_PRIMARY_THREAD_ENABLE; CStatus check() const { CGRAPH_FUNCTION_BEGIN diff --git a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h index 5b5e6352..7bd46025 100644 --- a/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h +++ b/src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h @@ -62,6 +62,7 @@ static const CBool CGRAPH_MONITOR_ENABLE = false; static const CSec CGRAPH_MONITOR_SPAN = 5; // 监控线程执行间隔,单位为s static const CMSec CGRAPH_QUEUE_EMPTY_INTERVAL = 1000; // 队列为空时,等待的时间。仅针对辅助线程,单位为ms static const CBool CGRAPH_BIND_CPU_ENABLE = false; // 是否开启绑定cpu模式(仅针对主线程) +static const CBool CGRAPH_DELIVER_RUNNING_PRIMARY_THREAD_ENABLE = false; // 是否可以将任务投递到正在运行主线程的队列中。开启后,遇到描述情况,则退化为投递到公共队列,并且触发所有线程 static const CInt CGRAPH_PRIMARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 主线程调度策略 static const CInt CGRAPH_SECONDARY_THREAD_POLICY = CGRAPH_THREAD_SCHED_OTHER; // 辅助线程调度策略 static const CInt CGRAPH_PRIMARY_THREAD_PRIORITY = CGRAPH_THREAD_MIN_PRIORITY; // 主线程调度优先级(取值范围0~99,配合调度策略一起使用,不建议不了解相关内容的童鞋做修改) diff --git a/test/Performance/test-performance-04.cpp b/test/Performance/test-performance-04.cpp index 677879ca..a84924e4 100644 --- a/test/Performance/test-performance-04.cpp +++ b/test/Performance/test-performance-04.cpp @@ -23,6 +23,7 @@ void test_performance_04() { config.max_task_steal_range_ = nodePerLayer - 1; config.primary_thread_busy_epoch_ = 500; config.primary_thread_empty_interval_ = 0; + config.deliver_running_primary_thread_enable_ = true; pipeline->setUniqueThreadPoolConfig(config); // 实现一个全连接