Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] [R-package] Fix memory leaks found by valgrind #3443

Merged
merged 81 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
52a981e
fix int64 write error
guolinke Sep 29, 2020
d8dff36
attempt
guolinke Sep 29, 2020
6af267d
Merge branch 'master' into fix-memory-leakage
jameslamb Oct 6, 2020
7718339
[WIP] [ci] [R-package] Add CI job that runs valgrind tests
jameslamb Oct 8, 2020
5fcd2fe
update all-successful
jameslamb Oct 8, 2020
1d3762e
install
jameslamb Oct 8, 2020
3f217a3
executable
jameslamb Oct 8, 2020
f5729ed
merge master
jameslamb Oct 10, 2020
e80b68d
fix redirect stuff
jameslamb Oct 10, 2020
d8b9a84
Merge branch 'master' into fix-memory-leakage
jameslamb Oct 11, 2020
4f20e36
Merge branch 'fix-memory-leakage' into ci/r-valgrind-test
jameslamb Oct 11, 2020
1c65707
Apply suggestions from code review
jameslamb Oct 11, 2020
53f82f5
more flags
jameslamb Oct 11, 2020
5828e43
Merge branch 'ci/r-valgrind-test' of github.com:microsoft/LightGBM in…
jameslamb Oct 11, 2020
74676c3
add mc to msvc proj
guolinke Oct 11, 2020
7a91f40
fix memory leak in mc
guolinke Oct 11, 2020
e64183c
Update monotone_constraints.hpp
guolinke Oct 11, 2020
23290db
Update r_package.yml
guolinke Oct 11, 2020
4355c75
remove R_INT64_PTR
jameslamb Oct 11, 2020
3179f4e
Merge branch 'ci/r-valgrind-test' of github.com:microsoft/LightGBM in…
jameslamb Oct 11, 2020
bd53bfe
disable openmp
jameslamb Oct 11, 2020
d53c9eb
Update gbdt_model_text.cpp
guolinke Oct 12, 2020
e4fff30
Update gbdt_model_text.cpp
guolinke Oct 12, 2020
b7b0bf3
Apply suggestions from code review
guolinke Oct 12, 2020
ec77314
try to free vector
guolinke Oct 12, 2020
78a66c8
free more memories.
guolinke Oct 12, 2020
70caf8e
Update src/boosting/gbdt_model_text.cpp
guolinke Oct 12, 2020
ec6ee58
fix using
guolinke Oct 12, 2020
24b9513
Merge branch 'ci/r-valgrind-test' of https://github.com/microsoft/Lig…
guolinke Oct 12, 2020
e93db3a
try the UNPROTECT(1);
guolinke Oct 12, 2020
d621d5f
fix a const pointer
guolinke Oct 12, 2020
c0e9219
fix Common
guolinke Oct 12, 2020
a45ec0a
reduce UNPROTECT
guolinke Oct 12, 2020
650cb32
remove UNPROTECT(1);
guolinke Oct 12, 2020
ba3ee79
fix null handle
guolinke Oct 12, 2020
1daf3c0
fix predictor
guolinke Oct 12, 2020
e5c6bf1
use NULL after free
guolinke Oct 12, 2020
d91c9ff
fix a leaking in test
guolinke Oct 12, 2020
a55801c
try more fixes
guolinke Oct 12, 2020
ef74a8f
test the effect of tests
guolinke Oct 12, 2020
432bf22
throw exception in Fatal
guolinke Oct 12, 2020
3a24bb6
add test back
guolinke Oct 12, 2020
5bcf90b
Apply suggestions from code review
guolinke Oct 12, 2020
7221adb
commet some tests
guolinke Oct 12, 2020
a024e5c
Apply suggestions from code review
guolinke Oct 12, 2020
a1e2b59
Apply suggestions from code review
guolinke Oct 12, 2020
9e3d97a
trying to comment out tests
jameslamb Oct 13, 2020
767938b
Update openmp_wrapper.h
guolinke Oct 13, 2020
11874c7
Apply suggestions from code review
guolinke Oct 13, 2020
4e30578
Update configure
guolinke Oct 13, 2020
5f888a9
Update configure.ac
guolinke Oct 13, 2020
e9343f1
Merge branch 'master' into ci/r-valgrind-test
jameslamb Oct 14, 2020
66aeb47
trying to uncomment
jameslamb Oct 14, 2020
25d2ff9
Merge branch 'ci/r-valgrind-test' of github.com:microsoft/LightGBM in…
jameslamb Oct 14, 2020
c1eeb58
more comments
jameslamb Oct 14, 2020
77f2c81
more uncommenting
jameslamb Oct 14, 2020
e6dfaca
more uncommenting
jameslamb Oct 14, 2020
6fb20eb
fix comment
jameslamb Oct 14, 2020
020ffae
merge
jameslamb Oct 14, 2020
06f783e
more uncommenting
jameslamb Oct 14, 2020
350e330
uncomment fully-commented out stuff
jameslamb Oct 14, 2020
5dd74aa
try uncommenting more dataset tests
jameslamb Oct 14, 2020
2164ba8
uncommenting more tests
jameslamb Oct 14, 2020
14cb184
ok getting closer
jameslamb Oct 14, 2020
a35ffb3
more uncommenting
jameslamb Oct 15, 2020
af1bbf7
free dataset
jameslamb Oct 15, 2020
a2faca1
skipping a test, more uncommenting
jameslamb Oct 15, 2020
d865182
more skipping
jameslamb Oct 15, 2020
d7e7a0c
re-enable OpenMP
jameslamb Oct 15, 2020
9526762
allow on OpenMP thing
jameslamb Oct 15, 2020
cbbbd4d
move valgrind to comment-only job
jameslamb Oct 16, 2020
b946388
Apply suggestions from code review
jameslamb Oct 17, 2020
b03d863
changes from code review
jameslamb Oct 17, 2020
974ab17
Merge branch 'ci/r-valgrind-test' of github.com:microsoft/LightGBM in…
jameslamb Oct 17, 2020
44afb1d
Merge branch 'master' into ci/r-valgrind-test
jameslamb Oct 17, 2020
cefb3dd
Apply suggestions from code review
jameslamb Oct 17, 2020
d8856bd
Merge branch 'ci/r-valgrind-test' of github.com:microsoft/LightGBM in…
jameslamb Oct 17, 2020
0374e87
linting
jameslamb Oct 17, 2020
e9c46aa
issue comments too
jameslamb Oct 17, 2020
5d435f6
remove issue_comment
jameslamb Oct 17, 2020
265400c
Merge branch 'master' into ci/r-valgrind-test
jameslamb Oct 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .ci/test_r_package_valgrind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

cd R-package/tests

ALL_LOGS_FILE="out.log"
VALGRIND_LOGS_FILE="valgrind-logs.log"

RDvalgrind \
--no-readline \
--vanilla \
-d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" \
-f testthat.R \
2>&1 > ${ALL_LOGS_FILE} || exit -1

cat ${ALL_LOGS_FILE}

cat ${ALL_LOGS_FILE} | grep -E "^\=" > ${VALGRIND_LOGS_FILE}

bytes_definitely_lost=$(
cat ${VALGRIND_LOGS_FILE} \
| grep -E "definitely lost\: .*" \
| sed 's/^.*definitely lost\: \(.*\) bytes.*$/\1/' \
| tr -d ","
)
if [[ ${bytes_definitely_lost} -gt 0 ]]; then
echo "valgrind found ${bytes_definitely_lost} bytes definitely lost"
exit -1
fi

bytes_indirectly_lost=$(
cat ${VALGRIND_LOGS_FILE} \
| grep -E "indirectly lost\: .*" \
| sed 's/^.*indirectly lost\: \(.*\) bytes.*$/\1/' \
| tr -d ","
)
if [[ ${bytes_indirectly_lost} -gt 0 ]]; then
echo "valgrind found ${bytes_indirectly_lost} bytes indirectly lost"
exit -1
fi

# one error caused by a false positive between valgrind and openmp is allowed
# ==1312== 352 bytes in 1 blocks are possibly lost in loss record 146 of 2,458
# ==1312== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
# ==1312== by 0x40149CA: allocate_dtv (dl-tls.c:286)
# ==1312== by 0x40149CA: _dl_allocate_tls (dl-tls.c:532)
# ==1312== by 0x5702322: allocate_stack (allocatestack.c:622)
# ==1312== by 0x5702322: pthread_create@@GLIBC_2.2.5 (pthread_create.c:660)
# ==1312== by 0x56D0DDA: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
# ==1312== by 0x56C88E0: GOMP_parallel (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
# ==1312== by 0x154351B8: LGBM_DatasetCreateFromCSC (c_api.cpp:1286)
# ==1312== by 0x1545789C: LGBM_DatasetCreateFromCSC_R (lightgbm_R.cpp:91)
# ==1312== by 0x4941E2F: R_doDotCall (dotcode.c:634)
# ==1312== by 0x494CCC6: do_dotcall (dotcode.c:1281)
# ==1312== by 0x499FB01: bcEval (eval.c:7078)
# ==1312== by 0x498B67F: Rf_eval (eval.c:727)
# ==1312== by 0x498E414: R_execClosure (eval.c:1895)
bytes_possibly_lost=$(
cat ${VALGRIND_LOGS_FILE} \
| grep -E "possibly lost\: .*" \
| sed 's/^.*possibly lost\: \(.*\) bytes.*$/\1/' \
| tr -d ","
)
if [[ ${bytes_possibly_lost} -gt 352 ]]; then
echo "valgrind found ${bytes_possibly_lost} bytes possibly lost"
exit -1
fi
28 changes: 28 additions & 0 deletions .github/workflows/r_valgrind.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: R valgrind tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/gha run r-valgrind-tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/gha run r-valgrind

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/gha run-r-valgrind

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ @StrikerRUS notice that this comment I just left triggered a run that was skipped, because it wasn't part of a review:

https://github.com/microsoft/LightGBM/actions/runs/312992159

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/gha run r-valgrind

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ but this comment triggered a real run because it was part of a review

https://github.com/microsoft/LightGBM/actions/runs/312993170

I see now that I am able to keep responding to a thread with review comments as long as it's on the "Files Changed" tab (like you mentioned), so I think it's ok!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jameslamb
Please notice that
/gha run-r-valgrind != /gha run r-valgrind


on:
pull_request_review_comment:
types: [created]

jobs:
test-r-valgrind:
name: r-package (ubuntu-latest, R-devel, valgrind)
if: github.event.comment.body == '/gha run r-valgrind' && contains('OWNER,MEMBER,COLLABORATOR', github.event.comment.author_association)
timeout-minutes: 120
runs-on: ubuntu-latest
container: wch1/r-debug
steps:
- name: Checkout repository
uses: actions/checkout@v1
with:
fetch-depth: 5
submodules: true
- name: Install packages
shell: bash
run: |
RDscriptvalgrind -e "install.packages(c('R6', 'data.table', 'jsonlite', 'testthat'), repos = 'https://cran.r-project.org')"
sh build-cran-package.sh
RDvalgrind CMD INSTALL --preclean --install-tests lightgbm_*.tar.gz || exit -1
- name: Run tests with valgrind
shell: bash
run: ./.ci/test_r_package_valgrind.sh
2 changes: 1 addition & 1 deletion R-package/R/lgb.Predictor.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Predictor <- R6::R6Class(
params <- list(...)
private$params <- lgb.params2str(params)
# Create new lgb handle
handle <- 0.0
handle <- lgb.null.handle()
jameslamb marked this conversation as resolved.
Show resolved Hide resolved

# Check if handle is a character
if (is.character(modelfile)) {
Expand Down
6 changes: 5 additions & 1 deletion R-package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,17 @@ cd R-package/tests
RDvalgrind \
--no-readline \
--vanilla \
-d valgrind \
-d "valgrind --tool=memcheck --leak-check=full --track-origins=yes" \
-f testthat.R \
2>&1 \
| tee out.log \
| cat
```

These tests can also be triggered on any pull request by leaving a "Comment" review with the following comment:

> /gha run r-valgrind

External (Unofficial) Repositories
----------------------------------

Expand Down
2 changes: 0 additions & 2 deletions R-package/src/R_object_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN;

#define R_INT_PTR(x) (reinterpret_cast<int*> DATAPTR(x))

#define R_INT64_PTR(x) (reinterpret_cast<int64_t*> DATAPTR(x))

#define R_REAL_PTR(x) (reinterpret_cast<double*> DATAPTR(x))

#define R_AS_INT(x) (*(reinterpret_cast<int*> DATAPTR(x)))
Expand Down
4 changes: 2 additions & 2 deletions R-package/src/lightgbm_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ LGBM_SE LGBM_BoosterGetNumPredict_R(LGBM_SE handle,
R_API_BEGIN();
int64_t len;
CHECK_CALL(LGBM_BoosterGetNumPredict(R_GET_PTR(handle), R_AS_INT(data_idx), &len));
R_INT64_PTR(out)[0] = len;
R_INT_PTR(out)[0] = static_cast<int>(len);
R_API_END();
}

Expand Down Expand Up @@ -624,7 +624,7 @@ LGBM_SE LGBM_BoosterPredictForMat_R(LGBM_SE handle,
int32_t nrow = R_AS_INT(num_row);
int32_t ncol = R_AS_INT(num_col);

double* p_mat = R_REAL_PTR(data);
const double* p_mat = R_REAL_PTR(data);
double* ptr_ret = R_REAL_PTR(out_result);
int64_t out_len;
CHECK_CALL(LGBM_BoosterPredictForMat(R_GET_PTR(handle),
Expand Down
2 changes: 2 additions & 0 deletions R-package/tests/testthat/test_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ test_that("lgb.Dataset: Dataset should be able to construct from matrix and retu
, ref_handle
)
expect_false(is.na(handle))
lgb.call("LGBM_DatasetFree_R", ret = NULL, handle)
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
handle <- NULL
})

test_that("lgb.Dataset$setinfo() should convert 'group' to integer", {
Expand Down
18 changes: 18 additions & 0 deletions R-package/tests/testthat/test_lgb.Booster.R
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,31 @@ test_that("Saving a model with different feature importance types works", {
, "spore-print-color=green=1"
)
)
})

test_that("Saving a model with unknown importance type fails", {
testthat::skip("Skipping this test because it causes issues for valgrind")
set.seed(708L)
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
bst <- lightgbm(
data = as.matrix(train$data)
, label = train$label
, num_leaves = 4L
, learning_rate = 1.0
, nrounds = 2L
, objective = "binary"
, save_name = tempfile(fileext = ".model")
)
expect_true(lgb.is.Booster(bst))

UNSUPPORTED_IMPORTANCE <- 2L
expect_error({
model_string <- bst$save_model_to_string(feature_importance_type = UNSUPPORTED_IMPORTANCE)
}, "Unknown importance type")
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
})


.params_from_model_string <- function(model_str) {
file_lines <- strsplit(model_str, "\n")[[1L]]
start_indx <- which(grepl("^parameters\\:$", file_lines)) + 1L
Expand Down
4 changes: 4 additions & 0 deletions R-package/tests/testthat/test_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ test_that("lgb.last_error() throws an error if there are no errors", {
})

test_that("lgb.last_error() correctly returns errors from the C++ side", {
testthat::skip(paste0(
"Skipping this test because it causes valgrind to think "
, "there is a memory leak, and needs to be rethought"
))
data(agaricus.train, package = "lightgbm")
train <- agaricus.train
dvalid1 <- lgb.Dataset(
Expand Down
2 changes: 1 addition & 1 deletion include/LightGBM/utils/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class Log {
#ifndef LGB_R_BUILD
fprintf(stderr, "[LightGBM] [Fatal] %s\n", str_buf);
fflush(stderr);
throw std::runtime_error(std::string(str_buf));
#else
Rf_error("[LightGBM] [Fatal] %s\n", str_buf);
#endif
throw std::runtime_error(std::string(str_buf));
}

private:
Expand Down
4 changes: 2 additions & 2 deletions src/boosting/gbdt_model_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ std::string GBDT::SaveModelToString(int start_iteration, int num_iteration, int
ss << loaded_parameter_ << "\n";
ss << "end of parameters" << '\n';
}
return ss.str();
return std::move(ss.str());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guolinke @jameslamb I just noticed that std::move here produces the following compilation warnings:

-- The C compiler identification is AppleClang 9.1.0.9020039
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode_9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode_9.4.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "3.1") 
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1") 
-- Found OpenMP: TRUE (found version "3.1")  
-- Performing Test MM_PREFETCH
-- Performing Test MM_PREFETCH - Success
-- Using _mm_prefetch
-- Performing Test MM_MALLOC
-- Performing Test MM_MALLOC - Success
-- Using _mm_malloc
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/runner/work/1/s/build
Scanning dependencies of target _lightgbm
[  3%] Building CXX object CMakeFiles/_lightgbm.dir/src/application/application.cpp.o
[  6%] Building CXX object CMakeFiles/_lightgbm.dir/src/boosting/boosting.cpp.o
[  9%] Building CXX object CMakeFiles/_lightgbm.dir/src/boosting/gbdt_model_text.cpp.o
[ 12%] Building CXX object CMakeFiles/_lightgbm.dir/src/boosting/gbdt.cpp.o
/Users/runner/work/1/s/src/boosting/gbdt_model_text.cpp:396:10: warning: moving a temporary object prevents copy elision [-Wpessimizing-move]
  return std::move(ss.str());
         ^
/Users/runner/work/1/s/src/boosting/gbdt_model_text.cpp:396:10: note: remove std::move call here
  return std::move(ss.str());
         ^~~~~~~~~~        ~
/Users/runner/work/1/s/src/boosting/gbdt_model_text.cpp:621:10: warning: moving a local object in a return statement prevents copy elision [-Wpessimizing-move]
  return std::move(feature_importances);
         ^
/Users/runner/work/1/s/src/boosting/gbdt_model_text.cpp:621:10: note: remove std::move call here
  return std::move(feature_importances);
         ^~~~~~~~~~                   ~
[ 15%] Building CXX object CMakeFiles/_lightgbm.dir/src/boosting/gbdt_prediction.cpp.o
2 warnings generated.
[ 18%] Building CXX object CMakeFiles/_lightgbm.dir/src/boosting/prediction_early_stop.cpp.o
[ 21%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/bin.cpp.o
[ 24%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/config.cpp.o
[ 27%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/config_auto.cpp.o
[ 30%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/dataset.cpp.o
[ 33%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/dataset_loader.cpp.o
[ 36%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/file_io.cpp.o
[ 39%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/json11.cpp.o
[ 42%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/metadata.cpp.o
[ 45%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/parser.cpp.o
[ 48%] Building CXX object CMakeFiles/_lightgbm.dir/src/io/tree.cpp.o
[ 51%] Building CXX object CMakeFiles/_lightgbm.dir/src/metric/dcg_calculator.cpp.o
[ 54%] Building CXX object CMakeFiles/_lightgbm.dir/src/metric/metric.cpp.o
[ 57%] Building CXX object CMakeFiles/_lightgbm.dir/src/network/ifaddrs_patch.cpp.o
[ 60%] Building CXX object CMakeFiles/_lightgbm.dir/src/network/linker_topo.cpp.o
[ 63%] Building CXX object CMakeFiles/_lightgbm.dir/src/network/linkers_mpi.cpp.o
[ 66%] Building CXX object CMakeFiles/_lightgbm.dir/src/network/linkers_socket.cpp.o
[ 69%] Building CXX object CMakeFiles/_lightgbm.dir/src/network/network.cpp.o
[ 72%] Building CXX object CMakeFiles/_lightgbm.dir/src/objective/objective_function.cpp.o
[ 75%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/cuda_tree_learner.cpp.o
[ 78%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/data_parallel_tree_learner.cpp.o
[ 81%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/feature_parallel_tree_learner.cpp.o
[ 84%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/gpu_tree_learner.cpp.o
[ 87%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/serial_tree_learner.cpp.o
[ 90%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/tree_learner.cpp.o
[ 93%] Building CXX object CMakeFiles/_lightgbm.dir/src/treelearner/voting_parallel_tree_learner.cpp.o
[ 96%] Building CXX object CMakeFiles/_lightgbm.dir/src/c_api.cpp.o
[100%] Linking CXX shared library ../lib_lightgbm.so
[100%] Built target _lightgbm

I wonder is there any way to avoid both valgrind error and compilation warning?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh I'm not sure! Think @guolinke will have to answer this one

Copy link
Collaborator

@guolinke guolinke Oct 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In c++11, the compliers should be able to auto move the "local" object by return call, so it is safe to remove std::move.
However, it seems apple clang doesn't have this optimization...
To fix the warning, we can remove these std::move

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, it seems apple clang doesn't have this optimization...

I also saw these warnings with ordinary Clang on Ubuntu.

}

bool GBDT::SaveModelToFile(int start_iteration, int num_iteration, int feature_importance_type, const char* filename) const {
Expand Down Expand Up @@ -618,7 +618,7 @@ std::vector<double> GBDT::FeatureImportance(int num_iteration, int importance_ty
} else {
Log::Fatal("Unknown importance type: only support split=0 and gain=1");
}
return feature_importances;
return std::move(feature_importances);
}

} // namespace LightGBM
16 changes: 9 additions & 7 deletions src/treelearner/monotone_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -37,6 +38,7 @@ struct FeatureConstraint {
};

struct ConstraintEntry {
virtual ~ConstraintEntry() {}
virtual void Reset() = 0;
virtual void UpdateMin(double new_min) = 0;
virtual void UpdateMax(double new_max) = 0;
Expand Down Expand Up @@ -462,12 +464,12 @@ class BasicLeafConstraints : public LeafConstraintsBase {
public:
explicit BasicLeafConstraints(int num_leaves) : num_leaves_(num_leaves) {
for (int i = 0; i < num_leaves; ++i) {
entries_.push_back(new BasicConstraintEntry());
entries_.emplace_back(new BasicConstraintEntry());
}
}

void Reset() override {
for (auto entry : entries_) {
for (auto& entry : entries_) {
entry->Reset();
}
}
Expand All @@ -484,7 +486,7 @@ class BasicLeafConstraints : public LeafConstraintsBase {
int8_t monotone_type, double right_output,
double left_output, int, const SplitInfo& ,
const std::vector<SplitInfo>&) override {
entries_[new_leaf] = entries_[leaf]->clone();
entries_[new_leaf].reset(entries_[leaf]->clone());
if (is_numerical_split) {
double mid = (left_output + right_output) / 2.0f;
if (monotone_type < 0) {
Expand All @@ -498,15 +500,15 @@ class BasicLeafConstraints : public LeafConstraintsBase {
return std::vector<int>();
}

const ConstraintEntry* Get(int leaf_idx) override { return entries_[leaf_idx]; }
const ConstraintEntry* Get(int leaf_idx) override { return entries_[leaf_idx].get(); }

FeatureConstraint* GetFeatureConstraint(int leaf_idx, int feature_index) final {
return entries_[leaf_idx]->GetFeatureConstraint(feature_index);
}

protected:
int num_leaves_;
std::vector<ConstraintEntry*> entries_;
std::vector<std::unique_ptr<ConstraintEntry>> entries_;
jameslamb marked this conversation as resolved.
Show resolved Hide resolved
};

class IntermediateLeafConstraints : public BasicLeafConstraints {
Expand Down Expand Up @@ -541,7 +543,7 @@ class IntermediateLeafConstraints : public BasicLeafConstraints {
void UpdateConstraintsWithOutputs(bool is_numerical_split, int leaf,
int new_leaf, int8_t monotone_type,
double right_output, double left_output) {
entries_[new_leaf] = entries_[leaf]->clone();
entries_[new_leaf].reset(entries_[leaf]->clone());
if (is_numerical_split) {
if (monotone_type < 0) {
entries_[leaf]->UpdateMin(right_output);
Expand Down Expand Up @@ -857,7 +859,7 @@ class AdvancedLeafConstraints : public IntermediateLeafConstraints {
int num_features)
: IntermediateLeafConstraints(config, num_leaves) {
for (int i = 0; i < num_leaves; ++i) {
entries_[i] = new AdvancedConstraintEntry(num_features);
entries_[i].reset(new AdvancedConstraintEntry(num_features));
}
}

Expand Down
1 change: 1 addition & 0 deletions windows/LightGBM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
<ClInclude Include="..\src\treelearner\data_partition.hpp" />
<ClInclude Include="..\src\treelearner\feature_histogram.hpp" />
<ClInclude Include="..\src\treelearner\leaf_splits.hpp" />
<ClInclude Include="..\src\treelearner\monotone_constraints.hpp" />
<ClInclude Include="..\src\treelearner\parallel_tree_learner.h" />
<ClInclude Include="..\src\treelearner\serial_tree_learner.h" />
<ClInclude Include="..\src\treelearner\split_info.hpp" />
Expand Down
3 changes: 3 additions & 0 deletions windows/LightGBM.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@
<ClInclude Include="..\include\LightGBM\utils\yamc\alternate_shared_mutex.hpp">
<Filter>include\LightGBM\utils\yamc</Filter>
</ClInclude>
<ClInclude Include="..\src\treelearner\monotone_constraints.hpp">
<Filter>src\treelearner</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\application\application.cpp">
Expand Down