Skip to content

Commit

Permalink
new function openmp_enabled() that informs the user if OpenMP is av…
Browse files Browse the repository at this point in the history
…ailable or not.
  • Loading branch information
lschneiderbauer committed Dec 29, 2024
1 parent 730cdc7 commit 6e12980
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ S3method(tibble::as_tibble,fcwtr_scalogram)
export(du)
export(fcwt)
export(fcwt_batch)
export(openmp_enabled)
export(u)
importFrom(graphics,plot)
importFrom(utils,setTxtProgressBar)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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

- `openmp_enabled()`: new function to check for OpenMP support.

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

# fcwtr 0.2.1
Expand Down
4 changes: 4 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Generated by cpp11: do not edit by hand

has_openmp <- function() {
.Call(`_fCWTr_has_openmp`)
}

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)
}
4 changes: 3 additions & 1 deletion R/fcwt.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
#'
#' @param n_threads
#' Number of threads used by the computation, if supported by your platform.
#' Defaults to 2 threads (to accomodate CRAN requirements).
#' Defaults to 2 threads (to accommodate CRAN requirements).
#' If [openmp_enabled()] returns `FALSE`, this argument is ignored, and
#' only a single thread is used.
#'
#' @return
#' The spectogram, a numeric real-valued matrix with dimensions
Expand Down
28 changes: 28 additions & 0 deletions R/openmp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#' 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), whether or not these conditions are met, depend on the (CRAN)
#' build server. 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.
#'
#' @export
openmp_enabled <- function() {
has_openmp()
}
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ reference:
- print.fcwtr_scalogram
- plot.fcwtr_scalogram
- autoplot.fcwtr_scalogram
- title: "Multithreading support"
contents:
- openmp_enabled
- title: "Sample data"
desc: >
Sample signal data mainly used to generate examples. The signals consist
Expand Down
4 changes: 3 additions & 1 deletion man/fcwt.Rd

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

4 changes: 3 additions & 1 deletion man/fcwt_batch.Rd

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

33 changes: 33 additions & 0 deletions man/openmp_enabled.Rd

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

10 changes: 9 additions & 1 deletion src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include "cpp11/declarations.hpp"
#include <R_ext/Visibility.h>

// interface_fcwt.cpp
cpp11::r_bool has_openmp();
extern "C" SEXP _fCWTr_has_openmp() {
BEGIN_CPP11
return cpp11::as_sexp(has_openmp());
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) {
Expand All @@ -15,7 +22,8 @@ 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_fcwt_raw", (DL_FUNC) &_fCWTr_fcwt_raw, 10},
{"_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 @@ -19,6 +19,15 @@ float dbl_to_float(const double &dbl) {
return static_cast<float>(dbl);
}

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

[[cpp11::register]]
std::vector<double> fcwt_raw(
std::vector<double> signal,
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-openmp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_that("openmp() works", {
expect_true(
openmp_enabled() == TRUE | openmp_enabled() == FALSE
)
})

0 comments on commit 6e12980

Please sign in to comment.