This document introduces the mirai package at a high level, explaining its purpose as a minimalist async evaluation framework for R, its role in the R ecosystem, and key architectural concepts. For detailed information on specific topics, refer to:
Sources: DESCRIPTION1-24 README.md1-40 man/mirai-package.Rd1-18
mirai is a minimalist async evaluation framework for R that enables asynchronous parallel and distributed computing. The name "mirai" (ミライ, 未来) means "future" in Japanese, reflecting its core abstraction: R expressions that will be evaluated in the future.
The package evaluates R expressions asynchronously in parallel processes, either locally on the same machine or distributed across networks. It is built on modern networking and concurrency primitives provided by the nanonext package (R bindings) and the NNG (Nanomsg Next Generation) C library.
Core Design Principles:
.args or ... parametersThe inverted topology is central to mirai's scalability: daemons can connect and disconnect from anywhere (local, SSH, HPC clusters, cloud) without the host managing individual connections.
Sources: DESCRIPTION13-23 README.md20-42 README.Rmd28-56 README.md74-108
Core Components:
| Component | Function | File Location |
|---|---|---|
| Task Creation | mirai() | R/mirai.R |
| Parallel Mapping | mirai_map() | R/map.R |
| Worker Management | daemons() | R/daemons.R |
| Background Worker | daemon() | R/daemon.R |
| Task Dispatcher | dispatcher() | R/dispatcher.R |
| Global Setup | everywhere() | R/everywhere.R |
| Status Monitoring | status(), info() | R/status.R |
Sources: README.md41-72 NEWS.md11-28
Key Architectural Patterns:
| Pattern | Implementation | File Reference |
|---|---|---|
| Inverted Topology | Daemons dial into host/dispatcher using req sockets | R/daemon.R105-150 |
| Event-Driven Resolution | mirai objects resolve via condition variables (non-polling) | R/mirai.R50-120 |
| FIFO Scheduling | Dispatcher assigns tasks to first available daemon | R/dispatcher.R80-200 |
| Profile Registry | Compute profiles stored in .. environment | R/daemons.R45-90 |
| Socket Patterns | rep/req for host-dispatcher, poly/req for dispatcher-daemon | R/dispatcher.R50-80 |
With Dispatcher (daemons(n, dispatcher = TRUE)):
rep socket that listens at a URLpoly socket that listens at a daemon URLpoly socket with req socketsWithout Dispatcher (daemons(n, dispatcher = FALSE)):
Sources: R/daemons.R45-250 R/dispatcher.R1-200 R/daemon.R1-250 NEWS.md199-233
NNG Socket Patterns Used:
| Pattern | Socket Type | Usage | Created By |
|---|---|---|---|
| Reply | nng_rep0 | Host/dispatcher listens for requests | nanonext::rep_socket() |
| Request | nng_req0 | Daemons send requests and receive replies | nanonext::req_socket() |
| Polyamorous | nng_rep0_poly | Dispatcher handles multiple daemon connections | nanonext::rep_socket(poly = TRUE) |
The req/rep pattern provides automatic connection management and message routing. When a daemon dials into a listening socket, NNG automatically establishes the bidirectional channel.
Sources: R/mirai.R1-250 R/daemon.R105-200 man/mirai-package.Rd19-28
Transport Layer Details:
| Platform | URL Scheme | Example | Latency | Implementation |
|---|---|---|---|---|
| Linux | abstract:// | abstract://mirai-8a3f2c | ~1-10 μs | Abstract namespace Unix sockets (no filesystem) |
| MacOS/POSIX | ipc:// | ipc:///tmp/mirai-8a3f2c | ~1-10 μs | Unix domain sockets via /tmp directory |
| Windows | ipc:// | ipc:///./pipe/mirai-8a3f2c | ~10-100 μs | Named pipes via \\.\pipe\ namespace |
| Remote TCP | tcp:// | tcp://192.168.1.100:5555 | ~100-1000 μs | Standard TCP/IP over network |
| Secure TLS | tls+tcp:// | tls+tcp://192.168.1.100:5555 | ~200-2000 μs | TLS 1.3 with auto-generated certificates |
| WebSocket | ws:// | ws://192.168.1.100:5555 | ~100-1000 μs | WebSocket protocol (legacy support) |
URL Construction Functions:
| Function | Purpose | Returns | Example |
|---|---|---|---|
local_url() | Generate random local IPC URL | Character string | abstract://mirai-8a3f2c (Linux) |
local_url(tcp = TRUE) | Generate random local TCP URL | Character string | tcp://127.0.0.1:33945 |
host_url() | Get all network interface IPs | Named character vector | c(eth0 = "tcp://192.168.1.100:0") |
host_url(ws = TRUE) | Get WebSocket URLs | Named character vector | c(eth0 = "ws://192.168.1.100:0") |
The :0 port notation means NNG will auto-select an available port and report it back.
Sources: R/launchers.R1-250 man/mirai-package.Rd19-28 NEWS.md467-482 NEWS.md150-156
Zero-Configuration TLS (automatic):
Custom TLS Certificates (manual):
The tls argument at daemons() and tlscert argument at daemon() accept either a length-2 character vector of file paths or a raw vector containing certificate data.
Sources: NEWS.md467-482 NEWS.md79-82 R/daemons.R100-150 R/daemon.R50-100
mirai serves as foundational infrastructure for asynchronous, parallel, and distributed computing in R:
Key Integrations:
parallel): Official alternative communications backend (R >= 4.4) via miraiCluster objects implementing the cluster APIas.promise() methods, enabling non-blocking reactive programming in Shiny and plumber2serial_config() and register_serial() enables transfer of reference objects (torch tensors, Arrow tables, Polars DataFrames) across processesotel package is availableSources: README.md109-146 README.Rmd108-129 NEWS.md42-68
mirai(): Create asynchronous tasks that evaluate R expressions in parallel processesmirai_map(): Apply functions over collections in parallel with progress tracking and error handlingeverywhere(): Broadcast expressions to all daemons for environment setupdaemons(): Configure persistent daemon pools with dispatcher or non-dispatcher modesdaemon(): Individual daemon process for task executiondispatcher(): Optional FIFO scheduler and connection managerlaunch_local() spawns daemons on the same machine using IPCssh_config() and launch_remote() deploy via SSH with tunneling supportcluster_config() integrates with Slurm, SGE, Torque/PBS, LSF resource managersremote_config() provides templating for custom deployment commands.compute parameter, stored in .. environmentwith_daemons() for temporary profile switching, local_daemons() for frame-based scopemiraiError: Evaluation errors with preserved stack traces and condition metadatamiraiInterrupt: User interruptionserrorValue: Timeout (5), connection reset (19), cancellation (20)is_mirai_error(), is_mirai_interrupt(), is_error_value()status(): Detailed connection and task statistics matrixinfo(): Succinct information for programmatic useunresolved(): Check if mirai has completedSources: README.md25-108 DESCRIPTION13-23 NEWS.md1-233
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.