1313#include " Indicator.h"
1414#include " IndParam.h"
1515#include " ../Stock.h"
16- #include " ../GlobalInitializer.h"
1716#include " imp/ICval.h"
1817#include " imp/IContext.h"
1918
@@ -24,7 +23,6 @@ BOOST_CLASS_EXPORT(hku::IndicatorImp)
2423namespace hku {
2524
2625bool IndicatorImp::ms_enable_increment_calculate{true };
27- GlobalStealThreadPool *IndicatorImp::ms_tg = nullptr ;
2826
2927string HKU_API getOPTypeName (IndicatorImp::OPType op) {
3028 string name;
@@ -104,27 +102,6 @@ string HKU_API getOPTypeName(IndicatorImp::OPType op) {
104102 return name;
105103}
106104
107- void IndicatorImp::initDynEngine () {
108- size_t cpu_num = std::thread::hardware_concurrency () * 3 / 2 ;
109- if (cpu_num > 128 ) {
110- cpu_num = 128 ;
111- } else if (cpu_num > 64 ) {
112- cpu_num = cpu_num * 10 / 8 ;
113- }
114-
115- // 由于 GlobalInitializer 机制,目前借用在此处初始化全局任务组
116- init_global_task_group (cpu_num);
117- ms_tg = get_global_task_group ();
118- }
119-
120- void IndicatorImp::releaseDynEngine () {
121- HKU_TRACE (" releaseDynEngine" );
122- // 目前的 GlobalInitializer 机制,global_task_group 实际可能已经释放
123- // 可能导致 double free, 这里只停止,不负责释放
124- // release_global_task_group();
125- // ms_tg = nullptr;
126- }
127-
128105HKU_API std::ostream &operator <<(std::ostream &os, const IndicatorImp &imp) {
129106 os << imp.str ();
130107 return os;
@@ -1908,10 +1885,8 @@ void IndicatorImp::_dyn_calculate(const Indicator &ind) {
19081885
19091886 const value_t *param_data = ind_param->data ();
19101887
1911- static const size_t minCircleLength = 400 ;
1912- size_t workerNum = ms_tg->worker_num ();
1913- if (total < minCircleLength || isSerial () || workerNum == 1 ) {
1914- // HKU_INFO("single_thread");
1888+ static constexpr size_t minCircleLength = 400 ;
1889+ if (total < minCircleLength || isSerial ()) {
19151890 for (size_t i = ind.discard (); i < total; i++) {
19161891 if (std::isnan (param_data[i])) {
19171892 _set (Null<value_t >(), i);
@@ -1924,39 +1899,17 @@ void IndicatorImp::_dyn_calculate(const Indicator &ind) {
19241899 return ;
19251900 }
19261901
1927- // HKU_INFO("multi_thread");
1928- size_t circleLength = minCircleLength;
1929- if (minCircleLength * workerNum < total) {
1930- size_t tailCount = total % workerNum;
1931- circleLength = tailCount == 0 ? total / workerNum : total / workerNum + 1 ;
1932- }
1933-
1934- std::vector<std::future<void >> tasks;
1935- for (size_t group = 0 ; group < workerNum; group++) {
1936- size_t first = circleLength * group;
1937- if (first >= total) {
1938- break ;
1939- }
1940- tasks.push_back (
1941- ms_tg->submit ([this , &ind, first, circleLength, total, group, param_data]() {
1942- size_t endPos = first + circleLength;
1943- if (endPos > total) {
1944- endPos = total;
1945- }
1946- for (size_t i = circleLength * group; i < endPos; i++) {
1947- if (std::isnan (param_data[i])) {
1948- _set (Null<value_t >(), i);
1949- } else {
1950- size_t step = size_t (param_data[i]);
1951- _dyn_run_one_step (ind, i, step);
1952- }
1953- }
1954- }));
1955- }
1956-
1957- for (auto &task : tasks) {
1958- task.get ();
1959- }
1902+ global_parallel_for_index_void (
1903+ ind.discard (), total,
1904+ [&ind, param_data, this ](size_t i) {
1905+ if (std::isnan (param_data[i])) {
1906+ _set (Null<value_t >(), i);
1907+ } else {
1908+ size_t step = size_t (param_data[i]);
1909+ _dyn_run_one_step (ind, i, step);
1910+ }
1911+ },
1912+ minCircleLength);
19601913
19611914 _update_discard ();
19621915}
0 commit comments