Skip to content

Commit

Permalink
Conditional C++ chunks if linking against rTRNG works.
Browse files Browse the repository at this point in the history
* New exported function `try`ing a minimal `sourceCpp()`.
* Should fix macOS check issues (re)building the vignettes (#10).
  • Loading branch information
riccardoporreca committed Mar 3, 2020
1 parent e529adf commit d9e24dd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(TRNGjump)
export(TRNGkind)
export(TRNGseed)
export(TRNGsplit)
export(check_rTRNG_linking)
export(defaultKind)
export(lagfib2plus_19937_64)
export(lagfib2xor_19937_64)
Expand Down
31 changes: 31 additions & 0 deletions R/LdFlags.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,34 @@ asBuildPath <- function(path) {
}
return(path)
}

sourceCpp_minimal_code <- '
// [[Rcpp::depends(rTRNG)]]
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <trng/yarn2.hpp>
// [[Rcpp::export]]
void exampleCpp() {
trng::yarn2 rng;
}
'

#' Check rTRNG linking.
#'
#' Check whether C++ code using the TRNG library can be built and linked against
#' \pkg{rTRNG} on the current system.
#'
#' @inheritParams base::try
#'
#' @return A scalar logical with the result of the check. If \code{FALSE}, using
#' the TRNG library from C++ code sourced via \code{\link[Rcpp]{sourceCpp}} or
#' part of an \R package is not expected to work.
#'
#' @export
check_rTRNG_linking <- function(silent = FALSE) {
check <- try(
silent = silent,
Rcpp::sourceCpp(code = sourceCpp_minimal_code)
)
!inherits(check, "try-error")
}
21 changes: 21 additions & 0 deletions man/check_rTRNG_linking.Rd

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

17 changes: 10 additions & 7 deletions vignettes/mcMat.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ vignette: >
```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE)
options(digits = 5) # for kable
linking_ok <- rTRNG::check_rTRNG_linking()
```


```{r include=FALSE, cache=FALSE}
source("utils/read_chunk_wrap.R", echo = FALSE, print.eval = FALSE)
read_chunk_wrap("code/mcMat.R")
Rcpp::sourceCpp("code/mcMat.cpp", verbose = FALSE, embeddedR = FALSE)
read_chunk_wrap("code/mcMat.cpp")
Rcpp::sourceCpp("code/mcMatParallel.cpp", verbose = FALSE, embeddedR = FALSE)
read_chunk_wrap("code/mcMatParallel.cpp")
if (linking_ok) {
Rcpp::sourceCpp("code/mcMat.cpp", verbose = FALSE, embeddedR = FALSE)
Rcpp::sourceCpp("code/mcMatParallel.cpp", verbose = FALSE, embeddedR = FALSE)
}
```

Consider the Monte Carlo simulation of a matrix of i.i.d. normal random
Expand Down Expand Up @@ -71,9 +74,9 @@ enforces the C++11 standard required by TRNG >= 4.22.
```
As seen above for the R case, consistency of the simulation obtained via
`mcSubMatRcpp` with the full sequential simulation is guaranteed.
```{r subMatExampleRcpp}
```{r subMatExampleRcpp, eval=linking_ok}
```
```{r, echo=FALSE}
```{r, echo=FALSE, eval=linking_ok}
knitr::kable(cbind.data.frame(M = M, S = S), row.names = TRUE)
```

Expand All @@ -88,12 +91,12 @@ Monte Carlo simulations in parallel, for any subset `subCols` of the variables.
```
The parallel nature of the `yarn2` generator ensures the parallel simulation is
playing fair, i.e. is consistent with the sequential simulation.
```{r fullMatExampleRcppParallel}
```{r fullMatExampleRcppParallel, eval=linking_ok}
```
Similarly, we can achieve a consistent parallel simulation of a subset of the
variables only.
```{r subMatExampleRcppParallel}
```{r subMatExampleRcppParallel, eval=linking_ok}
```
```{r, echo=FALSE}
```{r, echo=FALSE, eval=linking_ok}
knitr::kable(cbind.data.frame(M = M, Sp = Sp), row.names = TRUE)
```
5 changes: 3 additions & 2 deletions vignettes/rTRNG.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ vignette: >

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE)
linking_ok <- rTRNG::check_rTRNG_linking()
```


Expand Down Expand Up @@ -105,7 +106,7 @@ easily be compiled, specifying the `Rcpp::depends` attribute that allows
`Rcpp::plugins(cpp11)` is needed to enforce the C++11 standard required by TRNG
>= 4.22.
```{Rcpp sourceCpp}
```{Rcpp sourceCpp, eval=linking_ok}
// [[Rcpp::depends(rTRNG)]]
// TRNG >= 4.22 requires C++11
// [[Rcpp::plugins(cpp11)]]
Expand All @@ -132,7 +133,7 @@ NumericVector exampleCpp() {
exampleCpp()
*/
```
```{r sourceCpp-R, echo=FALSE}
```{r sourceCpp-R, echo=FALSE, eval=linking_ok}
exampleCpp()
```

Expand Down

0 comments on commit d9e24dd

Please sign in to comment.