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] adding routine registration in R package (fixes #1910) #2911

Merged
merged 28 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f7475d7
[R-package] started cutting over from custom R-to-C interface to R.h
jameslamb Feb 29, 2020
874a259
replaced LGBM_SE with SEXP
jameslamb Mar 11, 2020
42eca15
fixed error about ocnflicting definitions of length
jameslamb Mar 11, 2020
0960dd6
got linking working
jameslamb Mar 11, 2020
d08b960
more stuff
jameslamb Mar 11, 2020
d1dac98
eliminated R CMD CHECK note about printing
jameslamb Mar 12, 2020
0852023
switched from hard-coded include dir to the one from FindLibR.cmake
jameslamb Mar 12, 2020
b459a96
commented-out everything in CI that does not touch R
jameslamb Mar 12, 2020
c278fa9
more changes
jameslamb Mar 12, 2020
c2084e1
trying to get better logs
jameslamb Mar 12, 2020
3ffa91b
tried ignoring
jameslamb Mar 12, 2020
1875977
added error message to confirm a suspicion
jameslamb Mar 12, 2020
5a56d5e
restore full CI
jameslamb Mar 12, 2020
2eefb18
[R-package] started adding routine registration in R package
jameslamb Mar 12, 2020
34f04e9
more registrations
jameslamb Mar 12, 2020
3031bf2
added all functions
jameslamb Mar 13, 2020
cd42051
Eliminated note about registering routines
jameslamb Mar 13, 2020
b2ace3b
started cleaning up
jameslamb Mar 13, 2020
060ee94
reverted SEXP stuff
jameslamb Mar 13, 2020
76ec14b
more cleanup
jameslamb Mar 13, 2020
13db6bf
fixed linting
jameslamb Mar 14, 2020
3be19b4
cleaned up whitespace
jameslamb Mar 24, 2020
346a497
Update CMakeLists.txt
jameslamb Mar 25, 2020
80906c5
cleaned up handling of R package
jameslamb Mar 25, 2020
d88c3e3
fixed bad reference
jameslamb Mar 25, 2020
0238904
fixed list of SOURCES
jameslamb Mar 25, 2020
c6894c6
removed unnecessary linking
jameslamb Mar 28, 2020
1666f67
better use of source_dir and build_dir
jameslamb Apr 1, 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
2 changes: 1 addition & 1 deletion .ci/test_r_package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ if grep -q -R "WARNING" "$LOG_FILE_NAME"; then
exit -1
fi

ALLOWED_CHECK_NOTES=3
ALLOWED_CHECK_NOTES=2
NUM_CHECK_NOTES=$(
cat ${LOG_FILE_NAME} \
| grep -e '^Status: .* NOTE.*' \
Expand Down
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,15 @@ file(GLOB SOURCES
)

add_executable(lightgbm src/main.cpp ${SOURCES})
add_library(_lightgbm SHARED src/c_api.cpp src/lightgbm_R.cpp ${SOURCES})
list(APPEND SOURCES "src/c_api.cpp")

# Only build the R part of the library if building for
# use with the R package
if(BUILD_FOR_R)
list(APPEND SOURCES "src/lightgbm_R.cpp")
endif(BUILD_FOR_R)

add_library(_lightgbm SHARED ${SOURCES})

if(MSVC)
set_target_properties(_lightgbm PROPERTIES OUTPUT_NAME "lib_lightgbm")
Expand Down Expand Up @@ -308,7 +316,6 @@ if(WIN32 AND (MINGW OR CYGWIN))
endif()

if(BUILD_FOR_R)
TARGET_LINK_LIBRARIES(lightgbm ${LIBR_CORE_LIBRARY})
TARGET_LINK_LIBRARIES(_lightgbm ${LIBR_CORE_LIBRARY})
endif(BUILD_FOR_R)

Expand Down
14 changes: 8 additions & 6 deletions R-package/.Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
^pkgdown$

# Objects created by compilation
\.o$
\.so$
\.dll$
\.out$
\.bin$
^.*\.o
^.*\.so
^.*\.dll
^.*\.out
^.*\.bin

# Code copied in at build time
^src/CMakeLists.txt$
^Makefile$
^src/build/.*$

# unnecessary files from submodules
^src/compute/.appveyor.yml$
^src/compute/.coveralls.yml$
^src/compute/.travis.yml$
^src/compute/test/$
^src/compute/test/.*$
^src/compute/index.html$
^src/compute/.git$
^src/compute/.gitignore$
Expand Down
19 changes: 13 additions & 6 deletions R-package/src/install.libs.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (!use_precompile) {
# Prepare installation steps
cmake_cmd <- "cmake "
build_cmd <- "make _lightgbm"
lib_folder <- file.path(R_PACKAGE_SOURCE, "src", fsep = "/")
lib_folder <- file.path(source_dir, fsep = "/")

if (use_gpu) {
cmake_cmd <- paste0(cmake_cmd, " -DUSE_GPU=ON ")
Expand Down Expand Up @@ -98,7 +98,7 @@ if (!use_precompile) {
} else {
cmake_cmd <- paste0(cmake_cmd, local_vs_def)
build_cmd <- "cmake --build . --target _lightgbm --config Release"
lib_folder <- file.path(R_PACKAGE_SOURCE, "src/Release", fsep = "/")
lib_folder <- file.path(source_dir, "Release", fsep = "/")
}
}
}
Expand All @@ -110,9 +110,7 @@ if (!use_precompile) {
# Makefile. We don't need it here anyway since targets are built serially, so trying
# to remove it with this hack
generated_makefile <- file.path(
R_PACKAGE_SOURCE
, "src"
, "build"
build_dir
, "Makefile"
)
if (file.exists(generated_makefile)) {
Expand Down Expand Up @@ -163,12 +161,21 @@ if (!use_precompile) {
}
}

# Check installation correctness
# Packages with install.libs.R need to copy some artifacts into the
# expected places in the package structure.
# see https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Package-subdirectories,
# especially the paragraph on install.libs.R
dest <- file.path(R_PACKAGE_DIR, paste0("libs", R_ARCH), fsep = "/")
dir.create(dest, recursive = TRUE, showWarnings = FALSE)
if (file.exists(src)) {
print(paste0("Found library file: ", src, " to move to ", dest))
file.copy(src, dest, overwrite = TRUE)

symbols_file <- file.path(source_dir, "symbols.rds")
if (file.exists(symbols_file)) {
file.copy(symbols_file, dest, overwrite = TRUE)
}

jameslamb marked this conversation as resolved.
Show resolved Hide resolved
} else {
stop(paste0("Cannot find lib_lightgbm", SHLIB_EXT))
}
Expand Down
53 changes: 53 additions & 0 deletions src/lightgbm_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <utility>
#include <vector>

#include <R_ext/Rdynload.h>

#define COL_MAJOR (0)

#define R_API_BEGIN() \
Expand Down Expand Up @@ -656,3 +658,54 @@ LGBM_SE LGBM_BoosterDumpModel_R(LGBM_SE handle,
EncodeChar(out_str, inner_char_buf.data(), buffer_len, actual_len, static_cast<size_t>(out_len));
R_API_END();
}

// .Call() calls
static const R_CallMethodDef CallEntries[] = {
{"LGBM_GetLastError_R" , (DL_FUNC) &LGBM_GetLastError_R , 3},
{"LGBM_DatasetCreateFromFile_R" , (DL_FUNC) &LGBM_DatasetCreateFromFile_R , 5},
{"LGBM_DatasetCreateFromCSC_R" , (DL_FUNC) &LGBM_DatasetCreateFromCSC_R , 10},
{"LGBM_DatasetCreateFromMat_R" , (DL_FUNC) &LGBM_DatasetCreateFromMat_R , 7},
{"LGBM_DatasetGetSubset_R" , (DL_FUNC) &LGBM_DatasetGetSubset_R , 6},
{"LGBM_DatasetSetFeatureNames_R" , (DL_FUNC) &LGBM_DatasetSetFeatureNames_R , 3},
{"LGBM_DatasetGetFeatureNames_R" , (DL_FUNC) &LGBM_DatasetGetFeatureNames_R , 5},
{"LGBM_DatasetSaveBinary_R" , (DL_FUNC) &LGBM_DatasetSaveBinary_R , 3},
{"LGBM_DatasetFree_R" , (DL_FUNC) &LGBM_DatasetFree_R , 2},
{"LGBM_DatasetSetField_R" , (DL_FUNC) &LGBM_DatasetSetField_R , 5},
{"LGBM_DatasetGetFieldSize_R" , (DL_FUNC) &LGBM_DatasetGetFieldSize_R , 4},
{"LGBM_DatasetGetField_R" , (DL_FUNC) &LGBM_DatasetGetField_R , 4},
{"LGBM_DatasetUpdateParamChecking_R", (DL_FUNC) &LGBM_DatasetUpdateParamChecking_R, 3},
{"LGBM_DatasetGetNumData_R" , (DL_FUNC) &LGBM_DatasetGetNumData_R , 3},
{"LGBM_DatasetGetNumFeature_R" , (DL_FUNC) &LGBM_DatasetGetNumFeature_R , 3},
{"LGBM_BoosterCreate_R" , (DL_FUNC) &LGBM_BoosterCreate_R , 4},
{"LGBM_BoosterFree_R" , (DL_FUNC) &LGBM_BoosterFree_R , 2},
{"LGBM_BoosterCreateFromModelfile_R", (DL_FUNC) &LGBM_BoosterCreateFromModelfile_R, 3},
{"LGBM_BoosterLoadModelFromString_R", (DL_FUNC) &LGBM_BoosterLoadModelFromString_R, 3},
{"LGBM_BoosterMerge_R" , (DL_FUNC) &LGBM_BoosterMerge_R , 3},
{"LGBM_BoosterAddValidData_R" , (DL_FUNC) &LGBM_BoosterAddValidData_R , 3},
{"LGBM_BoosterResetTrainingData_R" , (DL_FUNC) &LGBM_BoosterResetTrainingData_R , 3},
{"LGBM_BoosterResetParameter_R" , (DL_FUNC) &LGBM_BoosterResetParameter_R , 3},
{"LGBM_BoosterGetNumClasses_R" , (DL_FUNC) &LGBM_BoosterGetNumClasses_R , 3},
{"LGBM_BoosterUpdateOneIter_R" , (DL_FUNC) &LGBM_BoosterUpdateOneIter_R , 2},
{"LGBM_BoosterUpdateOneIterCustom_R", (DL_FUNC) &LGBM_BoosterUpdateOneIterCustom_R, 5},
{"LGBM_BoosterRollbackOneIter_R" , (DL_FUNC) &LGBM_BoosterRollbackOneIter_R , 2},
{"LGBM_BoosterGetCurrentIteration_R", (DL_FUNC) &LGBM_BoosterGetCurrentIteration_R, 3},
{"LGBM_BoosterGetUpperBoundValue_R" , (DL_FUNC) &LGBM_BoosterGetUpperBoundValue_R , 3},
{"LGBM_BoosterGetLowerBoundValue_R" , (DL_FUNC) &LGBM_BoosterGetLowerBoundValue_R , 3},
{"LGBM_BoosterGetEvalNames_R" , (DL_FUNC) &LGBM_BoosterGetEvalNames_R , 5},
{"LGBM_BoosterGetEval_R" , (DL_FUNC) &LGBM_BoosterGetEval_R , 4},
{"LGBM_BoosterGetNumPredict_R" , (DL_FUNC) &LGBM_BoosterGetNumPredict_R , 4},
{"LGBM_BoosterGetPredict_R" , (DL_FUNC) &LGBM_BoosterGetPredict_R , 4},
{"LGBM_BoosterPredictForFile_R" , (DL_FUNC) &LGBM_BoosterPredictForFile_R , 10},
{"LGBM_BoosterCalcNumPredict_R" , (DL_FUNC) &LGBM_BoosterCalcNumPredict_R , 8},
{"LGBM_BoosterPredictForCSC_R" , (DL_FUNC) &LGBM_BoosterPredictForCSC_R , 14},
{"LGBM_BoosterPredictForMat_R" , (DL_FUNC) &LGBM_BoosterPredictForMat_R , 11},
{"LGBM_BoosterSaveModel_R" , (DL_FUNC) &LGBM_BoosterSaveModel_R , 4},
{"LGBM_BoosterSaveModelToString_R" , (DL_FUNC) &LGBM_BoosterSaveModelToString_R , 6},
{"LGBM_BoosterDumpModel_R" , (DL_FUNC) &LGBM_BoosterDumpModel_R , 6},
{NULL, NULL, 0}
};

void R_init_lightgbm(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
3 changes: 0 additions & 3 deletions windows/LightGBM.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,11 @@
<ClInclude Include="..\include\LightGBM\dataset.h" />
<ClInclude Include="..\include\LightGBM\dataset_loader.h" />
<ClInclude Include="..\include\LightGBM\feature_group.h" />
<ClInclude Include="..\include\LightGBM\lightgbm_R.h" />
<ClInclude Include="..\include\LightGBM\meta.h" />
<ClInclude Include="..\include\LightGBM\metric.h" />
<ClInclude Include="..\include\LightGBM\network.h" />
<ClInclude Include="..\include\LightGBM\objective_function.h" />
<ClInclude Include="..\include\LightGBM\prediction_early_stop.h" />
<ClInclude Include="..\include\LightGBM\R_object_helper.h" />
<ClInclude Include="..\include\LightGBM\tree.h" />
<ClInclude Include="..\include\LightGBM\tree_learner.h" />
<ClInclude Include="..\include\LightGBM\utils\array_args.h" />
Expand Down Expand Up @@ -308,7 +306,6 @@
<ClCompile Include="..\src\io\metadata.cpp" />
<ClCompile Include="..\src\io\parser.cpp" />
<ClCompile Include="..\src\io\tree.cpp" />
<ClCompile Include="..\src\lightgbm_R.cpp" />
<ClCompile Include="..\src\metric\dcg_calculator.cpp" />
<ClCompile Include="..\src\metric\metric.cpp" />
<ClCompile Include="..\src\network\network.cpp" />
Expand Down
9 changes: 0 additions & 9 deletions windows/LightGBM.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@
<ClInclude Include="..\src\metric\map_metric.hpp">
<Filter>src\metric</Filter>
</ClInclude>
<ClInclude Include="..\include\LightGBM\lightgbm_R.h">
<Filter>include\LightGBM</Filter>
</ClInclude>
<ClInclude Include="..\include\LightGBM\R_object_helper.h">
<Filter>include\LightGBM</Filter>
</ClInclude>
<ClInclude Include="..\src\boosting\rf.hpp">
<Filter>src\boosting</Filter>
</ClInclude>
Expand Down Expand Up @@ -296,9 +290,6 @@
<ClCompile Include="..\src\boosting\prediction_early_stop.cpp">
<Filter>src\boosting</Filter>
</ClCompile>
<ClCompile Include="..\src\lightgbm_R.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\boosting\gbdt_model_text.cpp">
<Filter>src\boosting</Filter>
</ClCompile>
Expand Down