Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions R/AAA.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.onLoad <- function(lib, pkg) {
n <- suppressWarnings(as.integer(Sys.getenv("OMP_NUM_THREADS", unset = NA_character_)))
if (!is.na(n) && n >= 1L) {
resemble_set_omp_threads(n)
}
}

.onAttach <- function(lib, pkg) {
pkg_v <- pkg_info()

Expand Down
6 changes: 6 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -1150,3 +1150,9 @@ ith_pred_cpp <- function(plslib, xscale, Xu, dxrxu = NULL) {
.Call(`_resemble_ith_pred_cpp`, plslib, xscale, Xu, dxrxu)
}

#' @keywords internal
#' @noRd
resemble_set_omp_threads <- function(n) {
invisible(.Call(`_resemble_resemble_set_omp_threads`, n))
}

11 changes: 11 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,16 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// resemble_set_omp_threads
void resemble_set_omp_threads(int n);
RcppExport SEXP _resemble_resemble_set_omp_threads(SEXP nSEXP) {
BEGIN_RCPP
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type n(nSEXP);
resemble_set_omp_threads(n);
return R_NilValue;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_resemble_fast_diss", (DL_FUNC) &_resemble_fast_diss, 3},
Expand Down Expand Up @@ -605,6 +615,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_resemble_ith_local_fit", (DL_FUNC) &_resemble_ith_local_fit, 10},
{"_resemble_final_fits_cpp", (DL_FUNC) &_resemble_final_fits_cpp, 9},
{"_resemble_ith_pred_cpp", (DL_FUNC) &_resemble_ith_pred_cpp, 4},
{"_resemble_resemble_set_omp_threads", (DL_FUNC) &_resemble_resemble_set_omp_threads, 1},
{NULL, NULL, 0}
};

Expand Down
17 changes: 16 additions & 1 deletion src/diss_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,21 @@ List top_k_neighbors(
for (int j = 0; j < m; j++) {
out[j] = wrap(results[j]);
}

return out;
}

//' Set the number of OpenMP threads
//'
//' Calls \code{omp_set_num_threads()} so that the limit takes effect for all
//' subsequent parallel regions, regardless of when \code{OMP_NUM_THREADS} was
//' set in the environment.
//' @param n Number of threads.
//' @keywords internal
//' @noRd
// [[Rcpp::export(visible = false)]]
void resemble_set_omp_threads(int n) {
#ifdef _OPENMP
omp_set_num_threads(n);
#endif
}
20 changes: 10 additions & 10 deletions vignettes/ag-evolutionary-subset-search.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ vignette: >
```{r}
#| message: false
#| echo: false
library(resemble)
Sys.setenv(OMP_NUM_THREADS = 2)
Sys.setenv(OMP_THREAD_LIMIT = 2)
options(mc.cores = 2)
library(resemble)
```


Expand Down Expand Up @@ -332,18 +332,18 @@ my_control <- gesearch_control(

```{r}
#| eval: true
#| echo: false
#| echo: false
gs <- gesearch(
Xr = train_x[!is.na(train_y), ], # the spectra of the reference "set/library"
Yr = train_y[!is.na(train_y)], # the response of the reference "set/library"
Xu = test_x_ref, # the available target domain spectra
Yu = test_y_ref, # the available target domain response (accepts NAs)
k = 50, b = 100, retain = 0.95,
k = 50, b = 20, retain = 0.95,
target_size = 120,
fit_method = fit_pls(ncomp = 20, method = "simpls", scale = TRUE),
optimization = c("reconstruction", "similarity", "response"),
control = my_control,
seed = 1124,
seed = 1124,
verbose = FALSE
)
```
Expand Down Expand Up @@ -466,18 +466,18 @@ The search process can be conducted when no information on the response variable

```{r}
#| eval: true
#| echo: false
#| echo: false
gs_nr <- gesearch(
Xr = train_x[!is.na(train_y), ], # the spectra of the reference "set/library"
Yr = train_y[!is.na(train_y)], # the response of the reference "set/library"
Xu = test_x_ref, # the available target domain spectra
Yu = NULL, # NO available target domain response (accepts NAs)
k = 50, b = 80, retain = 0.98,
k = 50, b = 15, retain = 0.98,
target_size = 160,
fit_method = fit_pls(ncomp = 20, method = "simpls", scale = TRUE),
optimization = c("reconstruction", "similarity"),
control = my_control,
seed = 1124,
seed = 1124,
verbose = FALSE
)
```
Expand Down Expand Up @@ -515,19 +515,19 @@ In some cases, the response values for the target set may not be available, but

```{r}
#| eval: true
#| echo: false
#| echo: false
gs_nr2 <- gesearch(
Xr = train_x[!is.na(train_y), ], # the spectra of the reference "set/library"
Yr = train_y[!is.na(train_y)], # the response of the reference "set/library"
Xu = test_x_ref, # the available target domain spectra
Yu = NULL, # NO available target domain response (accepts NAs)
Yu_lims = c(0, 5), # the response range in the target domain
k = 50, b = 80, retain = 0.98,
k = 50, b = 15, retain = 0.98,
target_size = 160,
fit_method = fit_pls(ncomp = 20, method = "simpls", scale = TRUE),
optimization = c("reconstruction", "similarity", "range"),
control = my_control,
seed = 1124,
seed = 1124,
verbose = FALSE
)
```
Expand Down
Loading