From da6c6ea3c92e6c53e1bdf8f1b2bf243cad8b034d Mon Sep 17 00:00:00 2001 From: Guolin Ke Date: Sat, 7 Nov 2020 11:16:32 +0800 Subject: [PATCH] fix invalid read detected by valgrind (#3526) Co-authored-by: StrikerRUS --- src/treelearner/leaf_splits.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/treelearner/leaf_splits.hpp b/src/treelearner/leaf_splits.hpp index 78d41066cc8e..afb687f2437d 100644 --- a/src/treelearner/leaf_splits.hpp +++ b/src/treelearner/leaf_splits.hpp @@ -22,8 +22,13 @@ namespace LightGBM { class LeafSplits { public: LeafSplits(data_size_t num_data, const Config* config) - :config_(config), num_data_in_leaf_(num_data), num_data_(num_data), + : deterministic_(false), + num_data_in_leaf_(num_data), + num_data_(num_data), data_indices_(nullptr), weight_(0) { + if (config != nullptr) { + deterministic_ = config->deterministic; + } } void ResetNumData(data_size_t num_data) { num_data_ = num_data; @@ -71,7 +76,7 @@ class LeafSplits { data_indices_ = nullptr; double tmp_sum_gradients = 0.0f; double tmp_sum_hessians = 0.0f; -#pragma omp parallel for schedule(static, 512) reduction(+:tmp_sum_gradients, tmp_sum_hessians) if (num_data_in_leaf_ >= 1024 && !config_->deterministic) +#pragma omp parallel for schedule(static, 512) reduction(+:tmp_sum_gradients, tmp_sum_hessians) if (num_data_in_leaf_ >= 1024 && !deterministic_) for (data_size_t i = 0; i < num_data_in_leaf_; ++i) { tmp_sum_gradients += gradients[i]; tmp_sum_hessians += hessians[i]; @@ -93,7 +98,7 @@ class LeafSplits { data_indices_ = data_partition->GetIndexOnLeaf(leaf, &num_data_in_leaf_); double tmp_sum_gradients = 0.0f; double tmp_sum_hessians = 0.0f; -#pragma omp parallel for schedule(static, 512) reduction(+:tmp_sum_gradients, tmp_sum_hessians) if (num_data_in_leaf_ >= 1024 && !config_->deterministic) +#pragma omp parallel for schedule(static, 512) reduction(+:tmp_sum_gradients, tmp_sum_hessians) if (num_data_in_leaf_ >= 1024 && !deterministic_) for (data_size_t i = 0; i < num_data_in_leaf_; ++i) { const data_size_t idx = data_indices_[i]; tmp_sum_gradients += gradients[idx]; @@ -146,7 +151,7 @@ class LeafSplits { private: - const Config* config_; + bool deterministic_; /*! \brief current leaf index */ int leaf_index_; /*! \brief number of data on current leaf */