Skip to content

Commit

Permalink
add avx_enabled()
Browse files Browse the repository at this point in the history
  • Loading branch information
lschneiderbauer committed Dec 29, 2024
1 parent 6e12980 commit 51190fe
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 39 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ S3method(plot,fcwtr_scalogram)
S3method(print,fcwtr_scalogram)
S3method(rbind,fcwtr_scalogram)
S3method(tibble::as_tibble,fcwtr_scalogram)
export(avx_enabled)
export(du)
export(fcwt)
export(fcwt_batch)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

- add more convenience S3 methods: `print()`, `[]`, `as.matrix()`, `rbind()`, `as_tibble()`.

- `openmp_enabled()`: new function to check for OpenMP support.
- `openmp_enabled()`/`avx_enabled()` new function to check for system OpenMP/AVX support.

- add a [package logo](https://lschneiderbauer.github.io/fCWTr/logo.svg)

Expand Down
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ has_openmp <- function() {
.Call(`_fCWTr_has_openmp`)
}

has_avx <- function() {
.Call(`_fCWTr_has_avx`)
}

fcwt_raw <- function(signal, fs, f0, f1, fn, sigma, nthreads, scaletype, optplans, abs) {
.Call(`_fCWTr_fcwt_raw`, signal, fs, f0, f1, fn, sigma, nthreads, scaletype, optplans, abs)
}
28 changes: 0 additions & 28 deletions R/openmp.R

This file was deleted.

58 changes: 58 additions & 0 deletions R/sys_support.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

#' Check for OpenMP support
#'
#' This function checks if OpenMP support is enabled. It is responsible for
#' the multithreading capabilities of [fcwt()]. If OpenMP is not enabled
#' the parameter `n_threads` of [fcwt()] is ignored.
#'
#' @details
#' OpenMP can be used by the underlying fCWT library if
#' * the fftw library on your platform was compiled with OpenMP support and
#' * the fCWTr package itself was compiled with OpenMP support.
#'
#' When using pre-built package binaries (like it is typical when you are using
#' R on Windows), it depends on the (CRAN) build server whether or not
#' these conditions are met. They cannot be influenced by the package author.
#'
#' If the user is building the R package yourself, the user needs to make sure
#' that the fftw library on her platform are built with OpenMP support.
#' The fCWTr package is configured to use OpenMP if fftw-OpenMP support is
#' available.
#'
#' @return ( `TRUE` | `FALSE` )
#' Returns `TRUE` if OpenMP support is enabled, `FALSE` otherwise.
#'
#' @examples
#' openmp_enabled()
#'
#' @export
#' @concept sys_support
openmp_enabled <- function() {
has_openmp()
}


#' Check for AVX instruction set support
#'
#' This function checks if fCWTr was compiled with AVX instruction set support.
#' AVX instructions need to be supported by the users' CPU in order for this
#' to work.
#'
#' @details
#' By default, most compiler setups do not make use of AVX to increase
#' portability of the binary. If you are an R user that has a CPU supporting
#' AVX and want to make use of it, you might need to manually enable compiler
#' flags to let R know about it, and install the package from source
#' (so that it gets compiled on your machine).
#'
#' @return ( `TRUE` | `FALSE` )
#' Returns `TRUE` if AVX support is enabled, `FALSE` otherwise.
#'
#' @examples
#' avx_enabled()
#'
#' @export
#' @concept sys_support
avx_enabled <- function() {
has_avx()
}
5 changes: 3 additions & 2 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ reference:
- print.fcwtr_scalogram
- plot.fcwtr_scalogram
- autoplot.fcwtr_scalogram
- title: "Multithreading support"
- title: "System support"
desc: "Helper functions that let you check if your platform provides OpenMP or AVX support."
contents:
- openmp_enabled
- has_concept("sys_support")
- title: "Sample data"
desc: >
Sample signal data mainly used to generate examples. The signals consist
Expand Down
29 changes: 29 additions & 0 deletions man/avx_enabled.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions man/openmp_enabled.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ extern "C" SEXP _fCWTr_has_openmp() {
END_CPP11
}
// interface_fcwt.cpp
cpp11::r_bool has_avx();
extern "C" SEXP _fCWTr_has_avx() {
BEGIN_CPP11
return cpp11::as_sexp(has_avx());
END_CPP11
}
// interface_fcwt.cpp
std::vector<double> fcwt_raw(std::vector<double> signal, int fs, double f0, double f1, int fn, double sigma, int nthreads, bool scaletype, bool optplans, bool abs);
extern "C" SEXP _fCWTr_fcwt_raw(SEXP signal, SEXP fs, SEXP f0, SEXP f1, SEXP fn, SEXP sigma, SEXP nthreads, SEXP scaletype, SEXP optplans, SEXP abs) {
BEGIN_CPP11
Expand All @@ -23,6 +30,7 @@ extern "C" SEXP _fCWTr_fcwt_raw(SEXP signal, SEXP fs, SEXP f0, SEXP f1, SEXP fn,
extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_fCWTr_fcwt_raw", (DL_FUNC) &_fCWTr_fcwt_raw, 10},
{"_fCWTr_has_avx", (DL_FUNC) &_fCWTr_has_avx, 0},
{"_fCWTr_has_openmp", (DL_FUNC) &_fCWTr_has_openmp, 0},
{NULL, NULL, 0}
};
Expand Down
9 changes: 9 additions & 0 deletions src/interface_fcwt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ cpp11::r_bool has_openmp() {
#endif
}

[[cpp11::register]]
cpp11::r_bool has_avx() {
#ifdef AVX
return(false);
#else
return(true);
#endif
}

[[cpp11::register]]
std::vector<double> fcwt_raw(
std::vector<double> signal,
Expand Down
5 changes: 0 additions & 5 deletions tests/testthat/test-openmp.R

This file was deleted.

11 changes: 11 additions & 0 deletions tests/testthat/test-sys_support.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("openmp_enabled() works", {
expect_true(
openmp_enabled() == TRUE | openmp_enabled() == FALSE
)
})

test_that("avx_enabled() works", {
expect_true(
avx_enabled() == TRUE | avx_enabled() == FALSE
)
})

0 comments on commit 51190fe

Please sign in to comment.