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] Very small performance tweaks #9837

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 15 additions & 25 deletions R-package/src/xgboost_R.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,6 @@ SEXP SafeMkChar(const char *c_str, SEXP continuation_token) {

using dmlc::BeginPtr;

xgboost::Context const *DMatrixCtx(DMatrixHandle handle) {
CHECK_HANDLE();
auto p_m = static_cast<std::shared_ptr<xgboost::DMatrix> *>(handle);
CHECK(p_m);
return p_m->get()->Ctx();
}

XGB_DLL SEXP XGCheckNullPtr_R(SEXP handle) {
return ScalarLogical(R_ExternalPtrAddr(handle) == NULL);
}
Expand Down Expand Up @@ -318,6 +311,9 @@ XGB_DLL SEXP XGDMatrixSliceDMatrix_R(SEXP handle, SEXP idxset) {
int res_code;
{
std::vector<int> idxvec(len);
#ifndef _MSC_VER
#pragma omp simd
#endif
for (R_xlen_t i = 0; i < len; ++i) {
idxvec[i] = idxset_[i] - 1;
}
Expand Down Expand Up @@ -375,6 +371,7 @@ XGB_DLL SEXP XGDMatrixSetStrFeatureInfo_R(SEXP handle, SEXP field, SEXP array) {
int res_code;
{
std::vector<std::string> str_info;
str_info.reserve(len);
for (size_t i = 0; i < len; ++i) {
str_info.emplace_back(CHAR(VECTOR_ELT(str_info_holder, i)));
}
Expand Down Expand Up @@ -457,9 +454,9 @@ XGB_DLL SEXP XGBoosterCreate_R(SEXP dmats) {

int res_code;
{
std::vector<void*> dvec;
std::vector<void*> dvec(len);
for (R_xlen_t i = 0; i < len; ++i) {
dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i)));
dvec[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
}
res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle);
}
Expand All @@ -478,9 +475,9 @@ XGB_DLL SEXP XGBoosterCreateInEmptyObj_R(SEXP dmats, SEXP R_handle) {

int res_code;
{
std::vector<void*> dvec;
std::vector<void*> dvec(len);
for (R_xlen_t i = 0; i < len; ++i) {
dvec.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i)));
dvec[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
}
res_code = XGBoosterCreate(BeginPtr(dvec), dvec.size(), &handle);
}
Expand Down Expand Up @@ -552,15 +549,16 @@ XGB_DLL SEXP XGBoosterEvalOneIter_R(SEXP handle, SEXP iter, SEXP dmats, SEXP evn

int res_code;
{
std::vector<void*> vec_dmats;
std::vector<void*> vec_dmats(len);
std::vector<std::string> vec_names;
std::vector<const char*> vec_sptr;
vec_names.reserve(len);
std::vector<const char*> vec_sptr(len);
for (R_xlen_t i = 0; i < len; ++i) {
vec_dmats.push_back(R_ExternalPtrAddr(VECTOR_ELT(dmats, i)));
vec_dmats[i] = R_ExternalPtrAddr(VECTOR_ELT(dmats, i));
vec_names.emplace_back(CHAR(VECTOR_ELT(evnames_lst, i)));
}
for (R_xlen_t i = 0; i < len; ++i) {
vec_sptr.push_back(vec_names[i].c_str());
vec_sptr[i] = vec_names[i].c_str();
}
res_code = XGBoosterEvalOneIter(R_ExternalPtrAddr(handle),
asInteger(iter),
Expand Down Expand Up @@ -598,11 +596,7 @@ XGB_DLL SEXP XGBoosterPredictFromDMatrix_R(SEXP handle, SEXP dmat, SEXP json_con
len *= out_shape[i];
}
r_out_result = PROTECT(allocVector(REALSXP, len));
auto ctx = xgboost::detail::BoosterCtx(R_ExternalPtrAddr(handle));
double *r_out_result_ = REAL(r_out_result);
xgboost::common::ParallelFor(len, ctx->Threads(), [&](xgboost::omp_ulong i) {
r_out_result_[i] = out_result[i];
});
std::copy(out_result, out_result + len, REAL(r_out_result));

SET_VECTOR_ELT(r_out, 0, r_out_shape);
SET_VECTOR_ELT(r_out, 1, r_out_result);
Expand Down Expand Up @@ -831,11 +825,7 @@ XGB_DLL SEXP XGBoosterFeatureScore_R(SEXP handle, SEXP json_config) {
}

out_scores_sexp = PROTECT(allocVector(REALSXP, len));
auto ctx = xgboost::detail::BoosterCtx(R_ExternalPtrAddr(handle));
double *out_scores_sexp_ = REAL(out_scores_sexp);
xgboost::common::ParallelFor(len, ctx->Threads(), [&](xgboost::omp_ulong i) {
out_scores_sexp_[i] = out_scores[i];
});
std::copy(out_scores, out_scores + len, REAL(out_scores_sexp));

SET_VECTOR_ELT(r_out, 0, out_features_sexp);
SET_VECTOR_ELT(r_out, 1, out_shape_sexp);
Expand Down