diff --git a/R-package/src/lightgbm_R.cpp b/R-package/src/lightgbm_R.cpp index 4799f8540497..a76a56c06b24 100644 --- a/R-package/src/lightgbm_R.cpp +++ b/R-package/src/lightgbm_R.cpp @@ -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(Rf_asInteger(len_used_row_indices)); - std::vector idxvec(len); + std::unique_ptr 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(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); @@ -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 vec_sptr; int len = static_cast(vec_names.size()); + std::unique_ptr 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(); @@ -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 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(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 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(field_data_[i]); - } - CHECK_CALL(LGBM_DatasetSetField(R_ExternalPtrAddr(handle), name, vec.data(), len, C_API_DTYPE_FLOAT32)); + std::unique_ptr 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; @@ -376,24 +368,18 @@ SEXP LGBM_DatasetGetField_R(SEXP handle, auto p_data = reinterpret_cast(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(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(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; @@ -616,15 +602,10 @@ SEXP LGBM_BoosterUpdateOneIterCustom_R(SEXP handle, _AssertBoosterHandleNotNull(handle); int is_finished = 0; int int_len = Rf_asInteger(len); - std::vector 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(grad_[j]); - thess[j] = static_cast(hess_[j]); - } - CHECK_CALL(LGBM_BoosterUpdateOneIterCustom(R_ExternalPtrAddr(handle), tgrad.data(), thess.data(), &is_finished)); + std::unique_ptr 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(); }