Skip to contents

ミライ


( 未来 )

Minimalist Async Evaluation Framework for R

Designed for simplicity, a ‘mirai’ evaluates an R expression asynchronously in a parallel process, on the local machine or distributed over the network, with the result automatically available upon completion.

Modern networking and concurrency built on nanonext and NNG (Nanomsg Next Gen) ensures reliable and efficient scheduling, over fast inter-process communications or TCP/IP secured by TLS.

Advantages include being inherently queued, allowing the sending of many more tasks than available processes, no arbitrary connection limits, no storage on the file system, support for otherwise non-exportable reference objects, an event-driven ‘promises’ implementation, and very low overhead.

Incorporates asynchronous parallel map functionality.

Quick Start

Use mirai() to evaluate an expression asynchronously in a separate, clean R process.

The following expression mimics an expensive calculation that eventually returns a random value.

library(mirai)

x <- list(time = 2, mean = 4)

m <- mirai({Sys.sleep(time); rnorm(1, mean)}, time = x$time, mean = x$mean)

Above, the variables time and mean in the mirai expression are defined by name = value pairs passed as part of the mirai() call.

A ‘mirai’ object is returned immediately - creating a mirai never blocks the session.

Whilst the async operation is ongoing, attempting to access a mirai’s data yields an ‘unresolved’ logical NA.

m
#> < mirai [] >
m$data
#> 'unresolved' logi NA

To check whether a mirai remains unresolved i.e. its async operation is still ongoing:

unresolved(m)
#> [1] TRUE

To wait for and collect the return value, use the mirai’s [] method:

m[]
#> [1] 4.983207

As a mirai represents an async operation, it is never necessary to wait for it. Other code can continue to be run. Once it completes, the return value automatically becomes available at $data.

while (unresolved(m)) {
  # do work here that does not depend on 'm'
}
m
#> < mirai [$data] >
m$data
#> [1] 4.983207

Daemons

Daemons are persistent background processes for receiving mirai requests, and are created as easily as:

daemons(4)
#> [1] 4

Daemons may also be deployed remotely for distributed computing and launchers can start daemons across the network via (tunnelled) SSH or a cluster resource manager.

Secure TLS connections can be used for remote daemon connections, with zero configuration required.

Async Parallel Map

mirai_map() maps a function over a list or vector, with each element processed in a separate parallel process.

m <- mirai_map(
  1:4,
  \(x) {res <- rnorm(1e8, mean = x, sd = x + 3) + z; max(res) - min(res)},
  z = rnorm(1e7)
)

A ‘mirai_map’ object is returned immediately. Other code can continue to run at this point. Its value may be retrieved at any time using its [] method to return a list, just like purrr::map() or base::lapply().

m
#> < mirai map [0/4] >
m[]
#> [[1]]
#> [1] 46.98116
#> 
#> [[2]]
#> [1] 58.23877
#> 
#> [[3]]
#> [1] 70.20797
#> 
#> [[4]]
#> [1] 81.99365

mirai_map() is designed to facilitate recovery from partial failure, and also provides options for early stopping and/or progress indicators. It also has some other advantages over alternative implementations.

Design Concepts

mirai is designed from the ground up to provide a production-grade experience.

  • Fast
    • Over 100x more responsive than common alternatives [1]
    • Built for low-latency applications such as real time inference and Shiny apps
  • Reliable
    • Consistent behaviour with no reliance on global options or variables
    • Each mirai call is evaluated explicitly for transparent and predictable results
  • Scalable
    • Launch millions of tasks simultaneously over thousands of connections
    • Proven track record handling heavy-duty workloads in the life sciences industry

Joe Cheng on mirai with Shiny   Will Landau on mirai in clinical trials

mirai パッケージを試してみたところ、かなり速くて驚きました

Integrations

The following core integrations are documented, with usage examples in the linked vignettes:

R parallel   Provides an alternative communications backend for R, implementing a new parallel cluster type, a feature request by R-Core at R Project Sprint 2023. ‘miraiCluster’ may also be used with foreach via doParallel.

promises   Implements the next generation of completely event-driven, non-polling promises. ‘mirai’ may be used interchageably with ‘promises’, including with the promise pipe %...>%.

Shiny   Asynchronous parallel / distributed backend, supporting the next level of responsiveness and scalability for Shiny. Launches ExtendedTasks, or plugs directly into the reactive framework for advanced uses.

Plumber   Asynchronous parallel / distributed backend, capable of scaling Plumber applications in production usage.

Arrow   Allows queries using the Apache Arrow format to be handled seamlessly over ADBC database connections hosted in background processes.

torch   Allows Torch tensors and complex objects such as models and optimizers to be used seamlessly across parallel processes.

Powering Crew and Targets High Performance Computing

targets   Targets, a Make-like pipeline tool for statistics and data science, has integrated and adopted crew as its default high-performance computing backend.

crew   Crew is a distributed worker-launcher extending mirai to different distributed computing platforms, from traditional clusters to cloud services.

crew.cluster   crew.cluster enables mirai-based workflows on traditional high-performance computing clusters using LFS, PBS/TORQUE, SGE and Slurm.

crew.aws.batch   crew.aws.batch extends mirai to cloud computing using AWS Batch.

Thanks

We would like to thank in particular:

Will Landau for being instrumental in shaping development of the package, from initiating the original request for persistent daemons, through to orchestrating robustness testing for the high performance computing requirements of crew and targets.

Joe Cheng for optimising the promises method to work seamlessly within Shiny, and prototyping event-driven promises, which is implemented across nanonext and mirai.

Luke Tierney of R Core, for discussion on L’Ecuyer-CMRG streams to ensure statistical independence in parallel processing, and making it possible for mirai to be the first ‘alternative communications backend for R’.

Henrik Bengtsson for valuable insights leading to the interface accepting broader usage patterns.

Daniel Falbel for discussion around an efficient solution to serialization and transmission of torch tensors.

Kirill Müller for discussion on using ‘daemons’ to host Arrow database connections.

R Consortium  for funding work on the TLS implementation in nanonext, used to provide secure connections in mirai.

Installation

Install the latest release from CRAN or R-multiverse:

The current development version is available from R-universe:

install.packages("mirai", repos = "https://shikokuchuo.r-universe.dev")

◈ mirai R package: https://shikokuchuo.net/mirai/
◈ nanonext R package: https://shikokuchuo.net/nanonext/

mirai is listed in CRAN High Performance Computing Task View:
https://cran.r-project.org/view=HighPerformanceComputing

[1] Benchmark available in appendix of: https://shikokuchuo.net/user2024-conference/

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.