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

[R-package] manage Dataset and Booster handles as R external pointers (fixes #3016) #4265

Merged
merged 15 commits into from
May 12, 2021

Conversation

jameslamb
Copy link
Collaborator

Fixes #3016.

Contributes to #4208, might help with #4034.

Description

When the R package calls functions from the C++ side that create persistent objects in memory, like a Dataset or Booster, it stores a pointer to the memory address of such objects in a "handle". Today, these handles are stored in a double on 64-bit systems and as an int32 on 32-bit systems.

lgb.null.handle <- function() {
if (.Machine$sizeof.pointer == 8L) {
return(NA_real_)
} else {
return(NA_integer_)
}
}

Today, the R package uses LightGBM's custom R-to-C interface to manage these handles. This can lead to issues like #4208, and generally exposes LightGBM to compatibility problems if new versions of R introduce changes that require corresponding changes in that custom interface.

This PR proposes replacing that custom management of "handles" with R's built-in pattern for such things. R's C API has a concept called "external pointers" designed for exactly this purpose.

From https://cran.r-project.org/doc/manuals/r-release/R-exts.html#External-pointers-and-weak-references

External pointer SEXPs are intended to handle references to C structures such as ‘handles’

Notes for Reviewers

This only fixes #3016 if is is merged after #4256.

The "References" section below contains links to the resources that helped me to understand these concepts better, and they might help reviewers to understand these changes.

References

@jameslamb
Copy link
Collaborator Author

jameslamb commented May 8, 2021

/gha run r-solaris

Workflow Solaris CRAN check has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/822296181

solaris-x86-patched: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-e77325aba0e449bebe84249015a5240b
solaris-x86-patched-ods: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-e494d83effcd4f10b747c25aa2cab9da
Reports also have been sent to LightGBM public e-mail: http://www.yopmail.com/lightgbm_rhub_checks
Status: success ✔️.

@jameslamb
Copy link
Collaborator Author

jameslamb commented May 8, 2021

/gha run r-valgrind

Workflow R valgrind tests has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/822296748

Status: success ✔️.

@jameslamb jameslamb changed the title WIP: [R-package] manage Dataset and Booster handles as R external pointers (fixes #3016) [R-package] manage Dataset and Booster handles as R external pointers (fixes #3016) May 8, 2021
@jameslamb jameslamb marked this pull request as ready for review May 8, 2021 04:33
@jameslamb jameslamb requested a review from Laurae2 as a code owner May 8, 2021 04:33
@jameslamb jameslamb requested review from StrikerRUS and shiyu1994 May 8, 2021 04:34
@jameslamb
Copy link
Collaborator Author

jameslamb commented May 10, 2021

/gha run r-valgrind

Workflow R valgrind tests has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/826655460

Status: success ✔️.

@jameslamb
Copy link
Collaborator Author

jameslamb commented May 10, 2021

/gha run r-solaris

Workflow Solaris CRAN check has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/826655969

solaris-x86-patched: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-d68ab45fd2514e2982267336296db17b
solaris-x86-patched-ods: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-7798e3815ca24329a8b4477895b98b35
Reports also have been sent to LightGBM public e-mail: http://www.yopmail.com/lightgbm_rhub_checks
Status: success ✔️.

@StrikerRUS
Copy link
Collaborator

Wonderful!
Unfortunately, this PR doesn't fix the following compilation warning

/home/runner/work/LightGBM/LightGBM/lightgbm.Rcheck/00_pkg_src/lightgbm/src/src/lightgbm_R.cpp:720:52: warning: cast between incompatible function types from ‘SEXPREC* (*)(SEXP)’ {aka ‘SEXPREC* (*)(SEXPREC*)’} to ‘DL_FUNC’ {aka ‘void* (*)()’} [-Wcast-function-type]

as it was suspected in #3016 (comment)

I suspect, though I don't know for sure, that this would also resolve these warnings we're getting from some compilers:

Any ideas why it doesn't and whether we should create a separate issue for these warnings as this PR is closing #3016?

@jameslamb
Copy link
Collaborator Author

Any ideas why it doesn't and whether we should create a separate issue for these warnings as this PR is closing #3016?

Please create a separate issue for it and I can investigate it (and document which specific combinations of operating system + R version + compiler + installation method it occurs for) in the future.

@jameslamb
Copy link
Collaborator Author

Please create a separate issue for it and I can investigate it

@StrikerRUS I'm back at a computer now so I can make this issue.

@jameslamb
Copy link
Collaborator Author

created #4273 to track the compiler warning

@StrikerRUS
Copy link
Collaborator

I'm back at a computer now so I can make this issue.

Thank you very much!

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

Very nice PR!
Please consider checking some my questions and cleanup suggestions below.

R-package/src/R_object_helper.h Show resolved Hide resolved
R-package/src/lightgbm_R.cpp Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.cpp Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
R-package/src/lightgbm_R.h Outdated Show resolved Hide resolved
@jameslamb
Copy link
Collaborator Author

jameslamb commented May 11, 2021

/gha run r-valgrind

Workflow R valgrind tests has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/832600392

Status: failure ❌.

@jameslamb
Copy link
Collaborator Author

jameslamb commented May 11, 2021

/gha run r-solaris

Workflow Solaris CRAN check has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/832601079

solaris-x86-patched: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-847f0335b7b8431fab8a6bd16e415eb6
solaris-x86-patched-ods: https://builder.r-hub.io/status/lightgbm_3.2.1.99.tar.gz-ab08a050699a44bfbf8d33c119c97f5c
Reports also have been sent to LightGBM public e-mail: http://www.yopmail.com/lightgbm_rhub_checks
Status: success ✔️.

@jameslamb jameslamb requested a review from StrikerRUS May 11, 2021 17:29
@StrikerRUS
Copy link
Collaborator

Hmmm, valgrind test is failing due to the time limit.

The job running on runner GitHub Actions 78 has exceeded the maximum execution time of 120 minutes.

Rerunning...

@StrikerRUS
Copy link
Collaborator

StrikerRUS commented May 11, 2021

/gha run r-valgrind

Workflow R valgrind tests has been triggered! 🚀
https://github.com/microsoft/LightGBM/actions/runs/833251558

Status: success ✔️.

@jameslamb
Copy link
Collaborator Author

Hmmm, valgrind test is failing due to the time limit.

😬 I hope it was something temporary, but I could understand if it wasn't. As of this PR, R is taking on more responsibility for allocating and freeing memory, and we know that the version of R instrumented with valgrind runs significantly slower.

@jameslamb
Copy link
Collaborator Author

whew, seems that it succeeded.

Total duration of https://github.com/microsoft/LightGBM/actions/runs/833251558 was 1 hour, 50 minutes, 46 seconds.

I think that if we see timeout issues again in the future, we should increase the allowed time by another 30 minutes.

@StrikerRUS
Copy link
Collaborator

I think that if we see timeout issues again in the future, we should increase the allowed time by another 30 minutes.

Agree.

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

Woo-hoo! Thank you!

@StrikerRUS StrikerRUS merged commit 676c95f into microsoft:master May 12, 2021
@jameslamb
Copy link
Collaborator Author

Excited about this one!!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[R-package] Factor out custom R interface to lib_lightgbm
2 participants