Skip to content

Commit

Permalink
[R-package] adding routine registration in R package (fixes #1910) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslamb authored Apr 1, 2020
1 parent 6ba84d5 commit 84bdf25
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 27 deletions.
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)
}

} 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

0 comments on commit 84bdf25

Please sign in to comment.