From 58628dd4b64b32d6d06b201273cf1d2d549cd10f Mon Sep 17 00:00:00 2001 From: James Lamb Date: Sat, 1 May 2021 20:55:58 -0500 Subject: [PATCH 1/4] real pointer for matrix --- R-package/src/lightgbm_R.cpp | 4 ++-- R-package/src/lightgbm_R.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/R-package/src/lightgbm_R.cpp b/R-package/src/lightgbm_R.cpp index d52c7bbeba97..b99725231a08 100644 --- a/R-package/src/lightgbm_R.cpp +++ b/R-package/src/lightgbm_R.cpp @@ -97,7 +97,7 @@ SEXP LGBM_DatasetCreateFromCSC_R(LGBM_SE indptr, R_API_END(); } -SEXP LGBM_DatasetCreateFromMat_R(LGBM_SE data, +SEXP LGBM_DatasetCreateFromMat_R(SEXP data, LGBM_SE num_row, LGBM_SE num_col, LGBM_SE parameters, @@ -106,7 +106,7 @@ SEXP LGBM_DatasetCreateFromMat_R(LGBM_SE data, R_API_BEGIN(); int32_t nrow = static_cast(R_AS_INT(num_row)); int32_t ncol = static_cast(R_AS_INT(num_col)); - double* p_mat = R_REAL_PTR(data); + double* p_mat = REAL(data); DatasetHandle handle = nullptr; CHECK_CALL(LGBM_DatasetCreateFromMat(p_mat, C_API_DTYPE_FLOAT64, nrow, ncol, COL_MAJOR, R_CHAR_PTR(parameters), R_GET_PTR(reference), &handle)); diff --git a/R-package/src/lightgbm_R.h b/R-package/src/lightgbm_R.h index 32910df59168..a4d18f23c725 100644 --- a/R-package/src/lightgbm_R.h +++ b/R-package/src/lightgbm_R.h @@ -74,7 +74,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromCSC_R( * \return 0 when succeed, -1 when failure happens */ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromMat_R( - LGBM_SE data, + SEXP data, LGBM_SE nrow, LGBM_SE ncol, LGBM_SE parameters, From 15bd537fb6c89bbde8df8075cff23b816c3c11c9 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Sat, 1 May 2021 21:12:32 -0500 Subject: [PATCH 2/4] remove R_REAL_PTR --- R-package/src/R_object_helper.h | 2 -- R-package/src/lightgbm_R.cpp | 56 ++++++++++++++++----------------- R-package/src/lightgbm_R.h | 26 +++++++-------- 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/R-package/src/R_object_helper.h b/R-package/src/R_object_helper.h index ce8a326a82f4..a40b8f6709af 100644 --- a/R-package/src/R_object_helper.h +++ b/R-package/src/R_object_helper.h @@ -104,8 +104,6 @@ typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN; #define R_INT_PTR(x) (reinterpret_cast DATAPTR(x)) -#define R_REAL_PTR(x) (reinterpret_cast DATAPTR(x)) - #define R_AS_INT(x) (*(reinterpret_cast DATAPTR(x))) #define R_IS_NULL(x) ((*reinterpret_cast(x)).sxpinfo.type == 0) diff --git a/R-package/src/lightgbm_R.cpp b/R-package/src/lightgbm_R.cpp index b99725231a08..50ea5a0acbd3 100644 --- a/R-package/src/lightgbm_R.cpp +++ b/R-package/src/lightgbm_R.cpp @@ -74,7 +74,7 @@ SEXP LGBM_DatasetCreateFromFile_R(LGBM_SE filename, SEXP LGBM_DatasetCreateFromCSC_R(LGBM_SE indptr, LGBM_SE indices, - LGBM_SE data, + SEXP data, LGBM_SE num_indptr, LGBM_SE nelem, LGBM_SE num_row, @@ -84,7 +84,7 @@ SEXP LGBM_DatasetCreateFromCSC_R(LGBM_SE indptr, R_API_BEGIN(); const int* p_indptr = R_INT_PTR(indptr); const int* p_indices = R_INT_PTR(indices); - const double* p_data = R_REAL_PTR(data); + const double* p_data = REAL(data); int64_t nindptr = static_cast(R_AS_INT(num_indptr)); int64_t ndata = static_cast(R_AS_INT(nelem)); @@ -197,7 +197,7 @@ SEXP LGBM_DatasetFree_R(LGBM_SE handle) { SEXP LGBM_DatasetSetField_R(LGBM_SE handle, LGBM_SE field_name, - LGBM_SE field_data, + SEXP field_data, LGBM_SE num_element) { R_API_BEGIN(); int len = static_cast(R_AS_INT(num_element)); @@ -210,12 +210,12 @@ SEXP LGBM_DatasetSetField_R(LGBM_SE handle, } CHECK_CALL(LGBM_DatasetSetField(R_GET_PTR(handle), name, vec.data(), len, C_API_DTYPE_INT32)); } else if (!strcmp("init_score", name)) { - CHECK_CALL(LGBM_DatasetSetField(R_GET_PTR(handle), name, R_REAL_PTR(field_data), len, C_API_DTYPE_FLOAT64)); + CHECK_CALL(LGBM_DatasetSetField(R_GET_PTR(handle), name, REAL(field_data), len, C_API_DTYPE_FLOAT64)); } else { std::vector vec(len); #pragma omp parallel for schedule(static, 512) if (len >= 1024) for (int i = 0; i < len; ++i) { - vec[i] = static_cast(R_REAL_PTR(field_data)[i]); + vec[i] = static_cast(REAL(field_data)[i]); } CHECK_CALL(LGBM_DatasetSetField(R_GET_PTR(handle), name, vec.data(), len, C_API_DTYPE_FLOAT32)); } @@ -224,7 +224,7 @@ SEXP LGBM_DatasetSetField_R(LGBM_SE handle, SEXP LGBM_DatasetGetField_R(LGBM_SE handle, LGBM_SE field_name, - LGBM_SE field_data) { + SEXP field_data) { R_API_BEGIN(); const char* name = R_CHAR_PTR(field_name); int out_len = 0; @@ -243,13 +243,13 @@ SEXP LGBM_DatasetGetField_R(LGBM_SE handle, auto p_data = reinterpret_cast(res); #pragma omp parallel for schedule(static, 512) if (out_len >= 1024) for (int i = 0; i < out_len; ++i) { - R_REAL_PTR(field_data)[i] = p_data[i]; + REAL(field_data)[i] = p_data[i]; } } else { auto p_data = reinterpret_cast(res); #pragma omp parallel for schedule(static, 512) if (out_len >= 1024) for (int i = 0; i < out_len; ++i) { - R_REAL_PTR(field_data)[i] = p_data[i]; + REAL(field_data)[i] = p_data[i]; } } R_API_END(); @@ -381,8 +381,8 @@ SEXP LGBM_BoosterUpdateOneIter_R(LGBM_SE handle) { } SEXP LGBM_BoosterUpdateOneIterCustom_R(LGBM_SE handle, - LGBM_SE grad, - LGBM_SE hess, + SEXP grad, + SEXP hess, LGBM_SE len) { int is_finished = 0; R_API_BEGIN(); @@ -390,8 +390,8 @@ SEXP LGBM_BoosterUpdateOneIterCustom_R(LGBM_SE handle, std::vector tgrad(int_len), thess(int_len); #pragma omp parallel for schedule(static, 512) if (int_len >= 1024) for (int j = 0; j < int_len; ++j) { - tgrad[j] = static_cast(R_REAL_PTR(grad)[j]); - thess[j] = static_cast(R_REAL_PTR(hess)[j]); + tgrad[j] = static_cast(REAL(grad)[j]); + thess[j] = static_cast(REAL(hess)[j]); } CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_GET_PTR(handle), tgrad.data(), thess.data(), &is_finished)); R_API_END(); @@ -413,17 +413,17 @@ SEXP LGBM_BoosterGetCurrentIteration_R(LGBM_SE handle, } SEXP LGBM_BoosterGetUpperBoundValue_R(LGBM_SE handle, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); - double* ptr_ret = R_REAL_PTR(out_result); + double* ptr_ret = REAL(out_result); CHECK_CALL(LGBM_BoosterGetUpperBoundValue(R_GET_PTR(handle), ptr_ret)); R_API_END(); } SEXP LGBM_BoosterGetLowerBoundValue_R(LGBM_SE handle, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); - double* ptr_ret = R_REAL_PTR(out_result); + double* ptr_ret = REAL(out_result); CHECK_CALL(LGBM_BoosterGetLowerBoundValue(R_GET_PTR(handle), ptr_ret)); R_API_END(); } @@ -461,11 +461,11 @@ SEXP LGBM_BoosterGetEvalNames_R(LGBM_SE handle, SEXP LGBM_BoosterGetEval_R(LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); int len; CHECK_CALL(LGBM_BoosterGetEvalCounts(R_GET_PTR(handle), &len)); - double* ptr_ret = R_REAL_PTR(out_result); + double* ptr_ret = REAL(out_result); int out_len; CHECK_CALL(LGBM_BoosterGetEval(R_GET_PTR(handle), R_AS_INT(data_idx), &out_len, ptr_ret)); CHECK_EQ(out_len, len); @@ -484,9 +484,9 @@ SEXP LGBM_BoosterGetNumPredict_R(LGBM_SE handle, SEXP LGBM_BoosterGetPredict_R(LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); - double* ptr_ret = R_REAL_PTR(out_result); + double* ptr_ret = REAL(out_result); int64_t out_len; CHECK_CALL(LGBM_BoosterGetPredict(R_GET_PTR(handle), R_AS_INT(data_idx), &out_len, ptr_ret)); R_API_END(); @@ -544,7 +544,7 @@ SEXP LGBM_BoosterCalcNumPredict_R(LGBM_SE handle, SEXP LGBM_BoosterPredictForCSC_R(LGBM_SE handle, LGBM_SE indptr, LGBM_SE indices, - LGBM_SE data, + SEXP data, LGBM_SE num_indptr, LGBM_SE nelem, LGBM_SE num_row, @@ -554,18 +554,18 @@ SEXP LGBM_BoosterPredictForCSC_R(LGBM_SE handle, LGBM_SE start_iteration, LGBM_SE num_iteration, LGBM_SE parameter, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); int pred_type = GetPredictType(is_rawscore, is_leafidx, is_predcontrib); const int* p_indptr = R_INT_PTR(indptr); const int* p_indices = R_INT_PTR(indices); - const double* p_data = R_REAL_PTR(data); + const double* p_data = REAL(data); int64_t nindptr = R_AS_INT(num_indptr); int64_t ndata = R_AS_INT(nelem); int64_t nrow = R_AS_INT(num_row); - double* ptr_ret = R_REAL_PTR(out_result); + double* ptr_ret = REAL(out_result); int64_t out_len; CHECK_CALL(LGBM_BoosterPredictForCSC(R_GET_PTR(handle), p_indptr, C_API_DTYPE_INT32, p_indices, @@ -575,7 +575,7 @@ SEXP LGBM_BoosterPredictForCSC_R(LGBM_SE handle, } SEXP LGBM_BoosterPredictForMat_R(LGBM_SE handle, - LGBM_SE data, + SEXP data, LGBM_SE num_row, LGBM_SE num_col, LGBM_SE is_rawscore, @@ -584,15 +584,15 @@ SEXP LGBM_BoosterPredictForMat_R(LGBM_SE handle, LGBM_SE start_iteration, LGBM_SE num_iteration, LGBM_SE parameter, - LGBM_SE out_result) { + SEXP out_result) { R_API_BEGIN(); int pred_type = GetPredictType(is_rawscore, is_leafidx, is_predcontrib); int32_t nrow = R_AS_INT(num_row); int32_t ncol = R_AS_INT(num_col); - const double* p_mat = R_REAL_PTR(data); - double* ptr_ret = R_REAL_PTR(out_result); + const double* p_mat = REAL(data); + double* ptr_ret = REAL(out_result); int64_t out_len; CHECK_CALL(LGBM_BoosterPredictForMat(R_GET_PTR(handle), p_mat, C_API_DTYPE_FLOAT64, nrow, ncol, COL_MAJOR, diff --git a/R-package/src/lightgbm_R.h b/R-package/src/lightgbm_R.h index a4d18f23c725..e8dc50493dc9 100644 --- a/R-package/src/lightgbm_R.h +++ b/R-package/src/lightgbm_R.h @@ -54,7 +54,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromFile_R( LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromCSC_R( LGBM_SE indptr, LGBM_SE indices, - LGBM_SE data, + SEXP data, LGBM_SE nindptr, LGBM_SE nelem, LGBM_SE num_row, @@ -156,7 +156,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetFree_R( LIGHTGBM_C_EXPORT SEXP LGBM_DatasetSetField_R( LGBM_SE handle, LGBM_SE field_name, - LGBM_SE field_data, + SEXP field_data, LGBM_SE num_element ); @@ -183,7 +183,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetFieldSize_R( LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetField_R( LGBM_SE handle, LGBM_SE field_name, - LGBM_SE field_data + SEXP field_data ); /*! @@ -340,8 +340,8 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterUpdateOneIter_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterUpdateOneIterCustom_R( LGBM_SE handle, - LGBM_SE grad, - LGBM_SE hess, + SEXP grad, + SEXP hess, LGBM_SE len ); @@ -372,7 +372,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetCurrentIteration_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetUpperBoundValue_R( LGBM_SE handle, - LGBM_SE out_result + SEXP out_result ); /*! @@ -383,7 +383,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetUpperBoundValue_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetLowerBoundValue_R( LGBM_SE handle, - LGBM_SE out_result + SEXP out_result ); /*! @@ -408,7 +408,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetEvalNames_R( LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetEval_R( LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out_result + SEXP out_result ); /*! @@ -435,7 +435,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetNumPredict_R( LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetPredict_R( LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out_result + SEXP out_result ); /*! @@ -505,7 +505,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForCSC_R( LGBM_SE handle, LGBM_SE indptr, LGBM_SE indices, - LGBM_SE data, + SEXP data, LGBM_SE nindptr, LGBM_SE nelem, LGBM_SE num_row, @@ -515,7 +515,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForCSC_R( LGBM_SE start_iteration, LGBM_SE num_iteration, LGBM_SE parameter, - LGBM_SE out_result + SEXP out_result ); /*! @@ -535,7 +535,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForCSC_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForMat_R( LGBM_SE handle, - LGBM_SE data, + SEXP data, LGBM_SE nrow, LGBM_SE ncol, LGBM_SE is_rawscore, @@ -544,7 +544,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForMat_R( LGBM_SE start_iteration, LGBM_SE num_iteration, LGBM_SE parameter, - LGBM_SE out_result + SEXP out_result ); /*! From 47d2bf94c32805557513c9748617d87ea0037772 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Sat, 1 May 2021 21:44:35 -0500 Subject: [PATCH 3/4] remove R_INT_PTR --- R-package/src/R_object_helper.h | 2 - R-package/src/lightgbm_R.cpp | 66 ++++++++++++++++----------------- R-package/src/lightgbm_R.h | 34 ++++++++--------- 3 files changed, 50 insertions(+), 52 deletions(-) diff --git a/R-package/src/R_object_helper.h b/R-package/src/R_object_helper.h index a40b8f6709af..811acab6510e 100644 --- a/R-package/src/R_object_helper.h +++ b/R-package/src/R_object_helper.h @@ -102,8 +102,6 @@ typedef union { VECTOR_SER s; double align; } SEXPREC_ALIGN; #define R_CHAR_PTR(x) (reinterpret_castDATAPTR(x)) -#define R_INT_PTR(x) (reinterpret_cast DATAPTR(x)) - #define R_AS_INT(x) (*(reinterpret_cast DATAPTR(x))) #define R_IS_NULL(x) ((*reinterpret_cast(x)).sxpinfo.type == 0) diff --git a/R-package/src/lightgbm_R.cpp b/R-package/src/lightgbm_R.cpp index 50ea5a0acbd3..911d5f25b5fc 100644 --- a/R-package/src/lightgbm_R.cpp +++ b/R-package/src/lightgbm_R.cpp @@ -43,11 +43,11 @@ using LightGBM::Common::Join; using LightGBM::Common::Split; using LightGBM::Log; -LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actual_len, size_t str_len) { +LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, SEXP actual_len, size_t str_len) { if (str_len > INT32_MAX) { Log::Fatal("Don't support large string in R-package"); } - R_INT_PTR(actual_len)[0] = static_cast(str_len); + INTEGER(actual_len)[0] = static_cast(str_len); if (R_AS_INT(buf_len) < static_cast(str_len)) { return dest; } @@ -56,7 +56,7 @@ LGBM_SE EncodeChar(LGBM_SE dest, const char* src, LGBM_SE buf_len, LGBM_SE actua return dest; } -LGBM_SE LGBM_GetLastError_R(LGBM_SE buf_len, LGBM_SE actual_len, LGBM_SE err_msg) { +LGBM_SE LGBM_GetLastError_R(LGBM_SE buf_len, SEXP actual_len, LGBM_SE err_msg) { return EncodeChar(err_msg, LGBM_GetLastError(), buf_len, actual_len, std::strlen(LGBM_GetLastError()) + 1); } @@ -72,8 +72,8 @@ SEXP LGBM_DatasetCreateFromFile_R(LGBM_SE filename, R_API_END(); } -SEXP LGBM_DatasetCreateFromCSC_R(LGBM_SE indptr, - LGBM_SE indices, +SEXP LGBM_DatasetCreateFromCSC_R(SEXP indptr, + SEXP indices, SEXP data, LGBM_SE num_indptr, LGBM_SE nelem, @@ -82,8 +82,8 @@ SEXP LGBM_DatasetCreateFromCSC_R(LGBM_SE indptr, LGBM_SE reference, LGBM_SE out) { R_API_BEGIN(); - const int* p_indptr = R_INT_PTR(indptr); - const int* p_indices = R_INT_PTR(indices); + const int* p_indptr = INTEGER(indptr); + const int* p_indices = INTEGER(indices); const double* p_data = REAL(data); int64_t nindptr = static_cast(R_AS_INT(num_indptr)); @@ -115,7 +115,7 @@ SEXP LGBM_DatasetCreateFromMat_R(SEXP data, } SEXP LGBM_DatasetGetSubset_R(LGBM_SE handle, - LGBM_SE used_row_indices, + SEXP used_row_indices, LGBM_SE len_used_row_indices, LGBM_SE parameters, LGBM_SE out) { @@ -125,7 +125,7 @@ SEXP LGBM_DatasetGetSubset_R(LGBM_SE handle, // convert from one-based to zero-based index #pragma omp parallel for schedule(static, 512) if (len >= 1024) for (int i = 0; i < len; ++i) { - idxvec[i] = R_INT_PTR(used_row_indices)[i] - 1; + idxvec[i] = INTEGER(used_row_indices)[i] - 1; } DatasetHandle res = nullptr; CHECK_CALL(LGBM_DatasetGetSubset(R_GET_PTR(handle), @@ -151,7 +151,7 @@ SEXP LGBM_DatasetSetFeatureNames_R(LGBM_SE handle, SEXP LGBM_DatasetGetFeatureNames_R(LGBM_SE handle, LGBM_SE buf_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE feature_names) { R_API_BEGIN(); int len = 0; @@ -206,7 +206,7 @@ SEXP LGBM_DatasetSetField_R(LGBM_SE handle, std::vector vec(len); #pragma omp parallel for schedule(static, 512) if (len >= 1024) for (int i = 0; i < len; ++i) { - vec[i] = static_cast(R_INT_PTR(field_data)[i]); + vec[i] = static_cast(INTEGER(field_data)[i]); } CHECK_CALL(LGBM_DatasetSetField(R_GET_PTR(handle), name, vec.data(), len, C_API_DTYPE_INT32)); } else if (!strcmp("init_score", name)) { @@ -237,7 +237,7 @@ SEXP LGBM_DatasetGetField_R(LGBM_SE handle, // convert from boundaries to size #pragma omp parallel for schedule(static, 512) if (out_len >= 1024) for (int i = 0; i < out_len - 1; ++i) { - R_INT_PTR(field_data)[i] = p_data[i + 1] - p_data[i]; + INTEGER(field_data)[i] = p_data[i + 1] - p_data[i]; } } else if (!strcmp("init_score", name)) { auto p_data = reinterpret_cast(res); @@ -257,7 +257,7 @@ SEXP LGBM_DatasetGetField_R(LGBM_SE handle, SEXP LGBM_DatasetGetFieldSize_R(LGBM_SE handle, LGBM_SE field_name, - LGBM_SE out) { + SEXP out) { R_API_BEGIN(); const char* name = R_CHAR_PTR(field_name); int out_len = 0; @@ -267,7 +267,7 @@ SEXP LGBM_DatasetGetFieldSize_R(LGBM_SE handle, if (!strcmp("group", name) || !strcmp("query", name)) { out_len -= 1; } - R_INT_PTR(out)[0] = static_cast(out_len); + INTEGER(out)[0] = static_cast(out_len); R_API_END(); } @@ -278,20 +278,20 @@ SEXP LGBM_DatasetUpdateParamChecking_R(LGBM_SE old_params, R_API_END(); } -SEXP LGBM_DatasetGetNumData_R(LGBM_SE handle, LGBM_SE out) { +SEXP LGBM_DatasetGetNumData_R(LGBM_SE handle, SEXP out) { int nrow; R_API_BEGIN(); CHECK_CALL(LGBM_DatasetGetNumData(R_GET_PTR(handle), &nrow)); - R_INT_PTR(out)[0] = static_cast(nrow); + INTEGER(out)[0] = static_cast(nrow); R_API_END(); } SEXP LGBM_DatasetGetNumFeature_R(LGBM_SE handle, - LGBM_SE out) { + SEXP out) { int nfeature; R_API_BEGIN(); CHECK_CALL(LGBM_DatasetGetNumFeature(R_GET_PTR(handle), &nfeature)); - R_INT_PTR(out)[0] = static_cast(nfeature); + INTEGER(out)[0] = static_cast(nfeature); R_API_END(); } @@ -365,11 +365,11 @@ SEXP LGBM_BoosterResetParameter_R(LGBM_SE handle, } SEXP LGBM_BoosterGetNumClasses_R(LGBM_SE handle, - LGBM_SE out) { + SEXP out) { int num_class; R_API_BEGIN(); CHECK_CALL(LGBM_BoosterGetNumClasses(R_GET_PTR(handle), &num_class)); - R_INT_PTR(out)[0] = static_cast(num_class); + INTEGER(out)[0] = static_cast(num_class); R_API_END(); } @@ -404,11 +404,11 @@ SEXP LGBM_BoosterRollbackOneIter_R(LGBM_SE handle) { } SEXP LGBM_BoosterGetCurrentIteration_R(LGBM_SE handle, - LGBM_SE out) { + SEXP out) { int out_iteration; R_API_BEGIN(); CHECK_CALL(LGBM_BoosterGetCurrentIteration(R_GET_PTR(handle), &out_iteration)); - R_INT_PTR(out)[0] = static_cast(out_iteration); + INTEGER(out)[0] = static_cast(out_iteration); R_API_END(); } @@ -430,7 +430,7 @@ SEXP LGBM_BoosterGetLowerBoundValue_R(LGBM_SE handle, SEXP LGBM_BoosterGetEvalNames_R(LGBM_SE handle, LGBM_SE buf_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE eval_names) { R_API_BEGIN(); int len; @@ -474,11 +474,11 @@ SEXP LGBM_BoosterGetEval_R(LGBM_SE handle, SEXP LGBM_BoosterGetNumPredict_R(LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out) { + SEXP out) { R_API_BEGIN(); int64_t len; CHECK_CALL(LGBM_BoosterGetNumPredict(R_GET_PTR(handle), R_AS_INT(data_idx), &len)); - R_INT_PTR(out)[0] = static_cast(len); + INTEGER(out)[0] = static_cast(len); R_API_END(); } @@ -531,19 +531,19 @@ SEXP LGBM_BoosterCalcNumPredict_R(LGBM_SE handle, LGBM_SE is_predcontrib, LGBM_SE start_iteration, LGBM_SE num_iteration, - LGBM_SE out_len) { + SEXP out_len) { R_API_BEGIN(); int pred_type = GetPredictType(is_rawscore, is_leafidx, is_predcontrib); int64_t len = 0; CHECK_CALL(LGBM_BoosterCalcNumPredict(R_GET_PTR(handle), R_AS_INT(num_row), pred_type, R_AS_INT(start_iteration), R_AS_INT(num_iteration), &len)); - R_INT_PTR(out_len)[0] = static_cast(len); + INTEGER(out_len)[0] = static_cast(len); R_API_END(); } SEXP LGBM_BoosterPredictForCSC_R(LGBM_SE handle, - LGBM_SE indptr, - LGBM_SE indices, + SEXP indptr, + SEXP indices, SEXP data, LGBM_SE num_indptr, LGBM_SE nelem, @@ -558,8 +558,8 @@ SEXP LGBM_BoosterPredictForCSC_R(LGBM_SE handle, R_API_BEGIN(); int pred_type = GetPredictType(is_rawscore, is_leafidx, is_predcontrib); - const int* p_indptr = R_INT_PTR(indptr); - const int* p_indices = R_INT_PTR(indices); + const int* p_indptr = INTEGER(indptr); + const int* p_indices = INTEGER(indices); const double* p_data = REAL(data); int64_t nindptr = R_AS_INT(num_indptr); @@ -614,7 +614,7 @@ SEXP LGBM_BoosterSaveModelToString_R(LGBM_SE handle, LGBM_SE num_iteration, LGBM_SE feature_importance_type, LGBM_SE buffer_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE out_str) { R_API_BEGIN(); int64_t out_len = 0; @@ -628,7 +628,7 @@ SEXP LGBM_BoosterDumpModel_R(LGBM_SE handle, LGBM_SE num_iteration, LGBM_SE feature_importance_type, LGBM_SE buffer_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE out_str) { R_API_BEGIN(); int64_t out_len = 0; diff --git a/R-package/src/lightgbm_R.h b/R-package/src/lightgbm_R.h index e8dc50493dc9..eb11da898ae2 100644 --- a/R-package/src/lightgbm_R.h +++ b/R-package/src/lightgbm_R.h @@ -17,7 +17,7 @@ */ LIGHTGBM_C_EXPORT LGBM_SE LGBM_GetLastError_R( LGBM_SE buf_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE err_msg ); @@ -52,8 +52,8 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromFile_R( * \return 0 when succeed, -1 when failure happens */ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromCSC_R( - LGBM_SE indptr, - LGBM_SE indices, + SEXP indptr, + SEXP indices, SEXP data, LGBM_SE nindptr, LGBM_SE nelem, @@ -93,7 +93,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetCreateFromMat_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetSubset_R( LGBM_SE handle, - LGBM_SE used_row_indices, + SEXP used_row_indices, LGBM_SE len_used_row_indices, LGBM_SE parameters, LGBM_SE out @@ -119,7 +119,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetSetFeatureNames_R( LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetFeatureNames_R( LGBM_SE handle, LGBM_SE buf_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE feature_names ); @@ -170,7 +170,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetSetField_R( LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetFieldSize_R( LGBM_SE handle, LGBM_SE field_name, - LGBM_SE out + SEXP out ); /*! @@ -205,7 +205,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetUpdateParamChecking_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetNumData_R( LGBM_SE handle, - LGBM_SE out + SEXP out ); /*! @@ -216,7 +216,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetNumData_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_DatasetGetNumFeature_R( LGBM_SE handle, - LGBM_SE out + SEXP out ); // --- start Booster interfaces @@ -317,7 +317,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterResetParameter_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetNumClasses_R( LGBM_SE handle, - LGBM_SE out + SEXP out ); /*! @@ -361,7 +361,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterRollbackOneIter_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetCurrentIteration_R( LGBM_SE handle, - LGBM_SE out + SEXP out ); /*! @@ -394,7 +394,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetLowerBoundValue_R( LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetEvalNames_R( LGBM_SE handle, LGBM_SE buf_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE eval_names ); @@ -421,7 +421,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetEval_R( LIGHTGBM_C_EXPORT SEXP LGBM_BoosterGetNumPredict_R( LGBM_SE handle, LGBM_SE data_idx, - LGBM_SE out + SEXP out ); /*! @@ -480,7 +480,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterCalcNumPredict_R( LGBM_SE is_predcontrib, LGBM_SE start_iteration, LGBM_SE num_iteration, - LGBM_SE out_len + SEXP out_len ); /*! @@ -503,8 +503,8 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterCalcNumPredict_R( */ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterPredictForCSC_R( LGBM_SE handle, - LGBM_SE indptr, - LGBM_SE indices, + SEXP indptr, + SEXP indices, SEXP data, LGBM_SE nindptr, LGBM_SE nelem, @@ -573,7 +573,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterSaveModelToString_R( LGBM_SE num_iteration, LGBM_SE feature_importance_type, LGBM_SE buffer_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE out_str ); @@ -589,7 +589,7 @@ LIGHTGBM_C_EXPORT SEXP LGBM_BoosterDumpModel_R( LGBM_SE num_iteration, LGBM_SE feature_importance_type, LGBM_SE buffer_len, - LGBM_SE actual_len, + SEXP actual_len, LGBM_SE out_str ); From dcc05be0ccb5147b0d08e6cf4184fd76cc89fdd8 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Sat, 1 May 2021 23:26:54 -0500 Subject: [PATCH 4/4] add test --- R-package/tests/testthat/test_basic.R | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/R-package/tests/testthat/test_basic.R b/R-package/tests/testthat/test_basic.R index 8e857450ce61..a49c728d2e19 100644 --- a/R-package/tests/testthat/test_basic.R +++ b/R-package/tests/testthat/test_basic.R @@ -1247,6 +1247,38 @@ test_that("lgb.train() supports non-ASCII feature names", { } }) +test_that("lgb.train() works with integer, double, and numeric data", { + data(mtcars) + X <- as.matrix(mtcars[, -1L]) + y <- mtcars[, 1L, drop = TRUE] + expected_mae <- 4.263667 + for (data_mode in c("numeric", "double", "integer")) { + mode(X) <- data_mode + nrounds <- 10L + bst <- lightgbm( + data = X + , label = y + , params = list( + objective = "regression" + , min_data = 1L + , learning_rate = 0.01 + , seed = 708L + ) + , nrounds = nrounds + ) + + # should have trained for 10 iterations and found splits + modelDT <- lgb.model.dt.tree(bst) + expect_equal(modelDT[, max(tree_index)], nrounds - 1L) + expect_gt(nrow(modelDT), nrounds * 3L) + + # should have achieved expected performance + preds <- predict(bst, X) + mae <- mean(abs(y - preds)) + expect_true(abs(mae - expected_mae) < TOLERANCE) + } +}) + test_that("when early stopping is not activated, best_iter and best_score come from valids and not training data", { set.seed(708L) trainDF <- data.frame(