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] Remove non-beneficial parallelization #6209

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
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
61 changes: 21 additions & 40 deletions R-package/src/lightgbm_R.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,19 @@ SEXP LGBM_DatasetGetSubset_R(SEXP handle,
_AssertDatasetHandleNotNull(handle);
SEXP ret = PROTECT(R_MakeExternalPtr(nullptr, R_NilValue, R_NilValue));
int32_t len = static_cast<int32_t>(Rf_asInteger(len_used_row_indices));
std::vector<int32_t> idxvec(len);
std::unique_ptr<int32_t[]> idxvec(new int32_t[len]);
// convert from one-based to zero-based index
const int *used_row_indices_ = INTEGER(used_row_indices);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
#ifndef _MSC_VER
#pragma omp simd
#endif
for (int32_t i = 0; i < len; ++i) {
idxvec[i] = static_cast<int32_t>(used_row_indices_[i] - 1);
}
const char* parameters_ptr = CHAR(PROTECT(Rf_asChar(parameters)));
DatasetHandle res = nullptr;
CHECK_CALL(LGBM_DatasetGetSubset(R_ExternalPtrAddr(handle),
idxvec.data(), len, parameters_ptr,
idxvec.get(), len, parameters_ptr,
&res));
R_SetExternalPtrAddr(ret, res);
R_RegisterCFinalizerEx(ret, _DatasetFinalizer, TRUE);
Expand All @@ -248,13 +250,13 @@ SEXP LGBM_DatasetSetFeatureNames_R(SEXP handle,
R_API_BEGIN();
_AssertDatasetHandleNotNull(handle);
auto vec_names = Split(CHAR(PROTECT(Rf_asChar(feature_names))), '\t');
std::vector<const char*> vec_sptr;
int len = static_cast<int>(vec_names.size());
std::unique_ptr<const char*[]> vec_sptr(new const char*[len]);
for (int i = 0; i < len; ++i) {
vec_sptr.push_back(vec_names[i].c_str());
vec_sptr[i] = vec_names[i].c_str();
}
CHECK_CALL(LGBM_DatasetSetFeatureNames(R_ExternalPtrAddr(handle),
vec_sptr.data(), len));
vec_sptr.get(), len));
UNPROTECT(1);
return R_NilValue;
R_API_END();
Expand Down Expand Up @@ -339,23 +341,13 @@ SEXP LGBM_DatasetSetField_R(SEXP handle,
int len = Rf_asInteger(num_element);
const char* name = CHAR(PROTECT(Rf_asChar(field_name)));
if (!strcmp("group", name) || !strcmp("query", name)) {
std::vector<int32_t> vec(len);
const int *field_data_ = INTEGER(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
for (int i = 0; i < len; ++i) {
vec[i] = static_cast<int32_t>(field_data_[i]);
}
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.data(), len, C_API_DTYPE_INT32));
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, INTEGER(field_data), len, C_API_DTYPE_INT32));
} else if (!strcmp("init_score", name)) {
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, REAL(field_data), len, C_API_DTYPE_FLOAT64));
} else {
std::vector<float> vec(len);
const double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (len >= 1024)
for (int i = 0; i < len; ++i) {
vec[i] = static_cast<float>(field_data_[i]);
}
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.data(), len, C_API_DTYPE_FLOAT32));
std::unique_ptr<float[]> vec(new float[len]);
std::copy(REAL(field_data), REAL(field_data) + len, vec.get());
CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.get(), len, C_API_DTYPE_FLOAT32));
}
UNPROTECT(1);
return R_NilValue;
Expand All @@ -376,24 +368,18 @@ SEXP LGBM_DatasetGetField_R(SEXP handle,
auto p_data = reinterpret_cast<const int32_t*>(res);
// convert from boundaries to size
int *field_data_ = INTEGER(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
#ifndef _MSC_VER
#pragma omp simd
#endif
for (int i = 0; i < out_len - 1; ++i) {
field_data_[i] = p_data[i + 1] - p_data[i];
}
} else if (!strcmp("init_score", name)) {
auto p_data = reinterpret_cast<const double*>(res);
double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
for (int i = 0; i < out_len; ++i) {
field_data_[i] = p_data[i];
}
std::copy(p_data, p_data + out_len, REAL(field_data));
} else {
auto p_data = reinterpret_cast<const float*>(res);
double *field_data_ = REAL(field_data);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (out_len >= 1024)
for (int i = 0; i < out_len; ++i) {
field_data_[i] = p_data[i];
}
std::copy(p_data, p_data + out_len, REAL(field_data));
}
UNPROTECT(1);
return R_NilValue;
Expand Down Expand Up @@ -616,15 +602,10 @@ SEXP LGBM_BoosterUpdateOneIterCustom_R(SEXP handle,
_AssertBoosterHandleNotNull(handle);
int is_finished = 0;
int int_len = Rf_asInteger(len);
std::vector<float> tgrad(int_len), thess(int_len);
const double *grad_ = REAL(grad);
const double *hess_ = REAL(hess);
#pragma omp parallel for num_threads(OMP_NUM_THREADS()) schedule(static, 512) if (int_len >= 1024)
for (int j = 0; j < int_len; ++j) {
tgrad[j] = static_cast<float>(grad_[j]);
thess[j] = static_cast<float>(hess_[j]);
}
CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_ExternalPtrAddr(handle), tgrad.data(), thess.data(), &is_finished));
std::unique_ptr<float[]> tgrad(new float[int_len]), thess(new float[int_len]);
std::copy(REAL(grad), REAL(grad) + int_len, tgrad.get());
std::copy(REAL(hess), REAL(hess) + int_len, thess.get());
CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_ExternalPtrAddr(handle), tgrad.get(), thess.get(), &is_finished));
return R_NilValue;
R_API_END();
}
Expand Down