Skip to content

Commit df52dbc

Browse files
committed
fastRCluster: add function fastr as a simpler wrapper around clusterApply
1 parent ef29137 commit df52dbc

File tree

5 files changed

+54
-19
lines changed

5 files changed

+54
-19
lines changed

com.oracle.truffle.r.pkgs/fastRCluster/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
S3method(print,fastRCluster)
44
export(defaultGraalVMHome)
55
export(fastRClusterInstallPackages)
6+
export(fastr)
67
export(getGraalVMHome)
78
export(installFastR)
89
export(makeFastRCluster)

com.oracle.truffle.r.pkgs/fastRCluster/R/fastRCluster.R

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,21 @@
6161
#' installFastR()
6262
#'
6363
#' # use the cluster package with FastR
64-
#' cl <- makeFastRCluster()
65-
#' print(cl)
64+
#' fastrNode <- makeFastRCluster()
65+
#' print(fastrNode)
6666
#' # prints: FastR socket cluster with 1 nodes on host ‘localhost’
6767
#'
6868
#' # install required packages on FastR
6969
#' fastRClusterInstallPackages('rlang')
7070
#'
7171
#' # use the cluster package with FastR
7272
#' # R.version will show that we are running that code on FastR
73-
#' parallel::clusterApply(cl, 'dummy', function(...) R.version)
73+
#' parallel::clusterApply(fastrNode, 'dummy', function(...) R.version)
74+
#'
75+
#' # use 'fastr' convenient wrapper around clusterApply
76+
#' gg <- fastr(fastrNode, ggplot2::qplot(mpg, data=mtcars, geom="density",
77+
#' main=paste0("Generated by ", R.version$engine, "[", Sys.getpid(), "]")))
78+
#' plot(gg)
7479
#'
7580
#' # transfer data and a helper function to the global environmnet of the cluster nodes
7681
#' largeDataSet <- matrix(runif(1000000), 1000, 1000)
@@ -87,9 +92,9 @@
8792
#' }
8893
#' res
8994
#' }
90-
#' parallel::clusterExport(cl, c('largeDataSet', 'myComputation'))
95+
#' parallel::clusterExport(fastrNode, c('largeDataSet', 'myComputation'))
9196
#' # now you can refer to 'largeDataSet' and 'myComputation'
92-
#' parallel::clusterApply(cl, 'dummy', function(...) myComputation(largeDataSet))
97+
#' fastr(fastrNode, myComputation(largeDataSet))
9398
#'
9499
#' # use the future package with FastR
95100
#' if (require(future)) {
@@ -190,6 +195,8 @@ installFastR <- function(path = defaultGraalVMHome()) {
190195
#' @param ... Parameters passed to the R function \code{install.packages} that is run on the FastR engine.
191196
#' @return Invisible \code{NULL}
192197
#' @export
198+
#' @examples
199+
#' fastRClusterInstallPackages(c('rlang', 'ggplot2'), INSTALL_opts='--no-test-load')
193200
fastRClusterInstallPackages <- function(...) {
194201
cl <- makeFastRCluster(1, metehods=F)
195202
on.exit(stopCluster(cl))
@@ -213,8 +220,9 @@ fastRClusterInstallPackages <- function(...) {
213220
#' @seealso \code{\link{getGraalVMHome}}
214221
#' @export
215222
#' @examples
216-
#' cl <- makeFastRCluster()
217-
#' parallel::clusterApply(cl, 'dummy', function(...) R.version)
223+
#' fastrNode <- makeFastRCluster()
224+
#' parallel::clusterApply(fastrNode, 'dummy', function(...) R.version)
225+
#' fastr(fastrNode, R.version)
218226
makeFastRCluster <- function (nnodes = 1L, graalVMHome = getGraalVMHome(), mode = c('jvm', 'native'), polyglot = FALSE, fastROptions = NULL, ...) {
219227
nnodes <- as.integer(nnodes)
220228
if(is.na(nnodes) || nnodes < 1L) {
@@ -224,20 +232,21 @@ makeFastRCluster <- function (nnodes = 1L, graalVMHome = getGraalVMHome(), mode
224232

225233
if (!dir.exists(graalVMHome)) {
226234
if (graalVMHome == defaultGraalVMHome()) {
227-
stop(sprintf(paste0("It seems that FastR was not installed yet.\n",
235+
stop(sprintf(paste0("It seems that FastR was not installed yet. ",
228236
"Use installFastR() to install GraalVM and FastR to the default location '%s', ",
229-
"or set argument 'path' to a directory that contains GraalVM and FastR installation.", defaultGraalVMHome())))
237+
"or set argument 'graalVMHome' to a directory that contains GraalVM and FastR installation. ",
238+
"See ?getGraalVMHome for more details."), defaultGraalVMHome()))
230239
} else {
231-
stop(sprintf(paste0("The GraalVM directory '%s' does not exist.\n",
240+
stop(sprintf(paste0("The GraalVM directory '%s' does not exist. ",
232241
"Use installFastR('%s') to install GraalVM and FastR to that directory."),
233242
graalVMHome, graalVMHome))
234243
}
235244
}
236245
if (!file.exists(file.path(graalVMHome, 'bin', 'gu'))) {
237-
stop(sprintf("The GraalVM directory '%s' appears to be corrupt.\nYou can remove it and use installFastR('%s') to re-install GraalVM and FastR.", graalVMHome, graalVMHome))
246+
stop(sprintf("The GraalVM directory '%s' appears to be corrupt. You can remove it and use installFastR('%s') to re-install GraalVM and FastR.", graalVMHome, graalVMHome))
238247
}
239248
if (!file.exists(file.path(graalVMHome, 'bin', 'Rscript'))) {
240-
stop(sprintf("The GraalVM installation '%s' does not contain FastR.\nUse installFastR('%s') to install FastR.", graalVMHome, graalVMHome))
249+
stop(sprintf("The GraalVM installation '%s' does not contain FastR. Use installFastR('%s') to install FastR.", graalVMHome, graalVMHome))
241250
}
242251
if (any(c('--jvm', '--native') %in% fastROptions)) {
243252
warning("Ignoring --jvm/--native in 'fastROptions' argument. Use the 'mode' argument instead.")
@@ -265,6 +274,22 @@ makeFastRCluster <- function (nnodes = 1L, graalVMHome = getGraalVMHome(), mode
265274
result
266275
}
267276

277+
#' Runs given code in the FastR engine.
278+
#'
279+
#' This is a convenient wrapper around \code{clusterApply} that runs
280+
#' the given code on the first node in the cluster.
281+
#'
282+
#' @param cl FastR cluster object. Use \code{\link{makeFastRCluster}()} to get one.
283+
#' @param code The code that will be run on the FastR node. It will not be evaluated in the current session.
284+
#' @return The result of evaluating the code
285+
#' @export
286+
#' @examples
287+
#' fastrNode <- makeFastRCluster()
288+
#' fastr(fastrNode, R.version)
289+
fastr <- function(cl, code) {
290+
parallel::clusterApply(cl, 'dummy', function(...) code)[[1L]]
291+
}
292+
268293
#' @export
269294
print.fastRCluster <- function(x, ...) {
270295
cat("FastR "); NextMethod(x, ...)

com.oracle.truffle.r.pkgs/fastRCluster/man/fastRCluster-package.Rd

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.oracle.truffle.r.pkgs/fastRCluster/man/fastRClusterInstallPackages.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.oracle.truffle.r.pkgs/fastRCluster/man/makeFastRCluster.Rd

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)