Set daemons, or persistent background processes, to receive mirai()
requests. Specify n
to create daemons on the local machine. Specify url
to receive connections from remote daemons (for distributed computing across
the network). Specify remote
to optionally launch remote daemons via a
remote configuration. Dispatcher (enabled by default) ensures optimal
scheduling.
daemons(
n,
url = NULL,
remote = NULL,
dispatcher = TRUE,
...,
seed = NULL,
serial = NULL,
tls = NULL,
pass = NULL,
.compute = NULL
)
The integer number of daemons launched locally (zero if specifying
url
or using a remote launcher).
integer number of daemons to launch.
[default NULL] if specified, a character string comprising a URL
at which to listen for remote daemons, including a port accepting incoming
connections, e.g. 'tcp://hostname:5555' or 'tcp://10.75.32.70:5555'.
Specify a URL with scheme 'tls+tcp://' to use secure TLS connections (for
details see Distributed Computing section below). Auxiliary function
host_url()
may be used to construct a valid host URL.
[default NULL] required only for launching remote daemons, a
configuration generated by remote_config()
or ssh_config()
.
[default TRUE] logical value, whether to use dispatcher. Dispatcher runs in a separate process to ensure optimal scheduling, and should normally be kept on (for details see Dispatcher section below).
(optional) additional arguments passed through to
daemon()
if launching daemons. These include asyncdial
, autoexit
,
cleanup
, output
, maxtasks
, idletime
and walltime
.
[default NULL] (optional) The default of NULL
initializes
L'Ecuyer-CMRG RNG streams for each daemon, the same as base R's parallel
package. Results are statistically-sound, although generally
non-reproducible, as which tasks are sent to which daemons may be
non-deterministic, and also depends on the number of daemons.
(experimental) supply an integer value to instead initialize a
L'Ecuyer-CMRG RNG stream for the compute profile. This is advanced for each
mirai evaluation, hence allowing for reproducible results, as the random
seed is always associated with a given mirai, independently of where it is
evaluated.
[default NULL] (optional, requires dispatcher) a
configuration created by serial_config()
to register serialization and
unserialization functions for normally non-exportable reference objects,
such as Arrow Tables or torch tensors. If NULL
, configurations registered
with register_serial()
are automatically applied.
[default NULL] (optional for secure TLS connections) if not supplied, zero-configuration single-use keys and certificates are automatically generated. If supplied, either the character path to a file containing the PEM-encoded TLS certificate and associated private key (may contain additional certificates leading to a validation chain, with the TLS certificate first), or a length 2 character vector comprising [i] the TLS certificate (optionally certificate chain) and [ii] the associated private key.
[default NULL] (required only if the private key supplied to
tls
is encrypted with a password) For security, should be provided
through a function that returns this value, rather than directly.
[default NULL] character value for the compute profile to use (each has its own independent set of daemons), or NULL to use the 'default' profile.
Setting daemons, or persistent background processes, is typically more efficient as it removes the need for, and overhead of, creating new processes for each mirai evaluation. It also provides control over the total number of processes at any one time.
Supply the argument n
to set the number of daemons. New background
daemon()
processes are automatically launched on the local machine
connecting back to the host process, either directly or via dispatcher.
By default dispatcher = TRUE
launches a background process running
dispatcher()
. Dispatcher connects to daemons on behalf of the host, queues
tasks, and ensures optimal FIFO scheduling. Dispatcher also enables (i) mirai
cancellation using stop_mirai()
or when using a .timeout
argument to
mirai()
, and (ii) the use of custom serialization configurations.
Specifying dispatcher = FALSE
, daemons connect directly to the host and
tasks are distributed in a round-robin fashion, with tasks queued at each
daemon. Optimal scheduling is not guaranteed as, depending on the duration of
tasks, they can be queued at one daemon while others remain idle. However,
this solution is the most resource-light, and suited to similar-length tasks,
or where concurrent tasks typically do not exceed available daemons.
Specify url
as a character string to allow tasks to be distributed across
the network (n
is only required in this case if also providing a launch
configuration to remote
).
The host / dispatcher listens at this URL, utilising a single port, and
daemon()
processes dial in to this URL. Host / dispatcher automatically
adjusts to the number of daemons actually connected, allowing dynamic
upscaling / downscaling.
The URL should have a 'tcp://' scheme, such as 'tcp://10.75.32.70:5555'.
Switching the URL scheme to 'tls+tcp://' automatically upgrades the
connection to use TLS. The auxiliary function host_url()
may be used to
construct a valid host URL based on the computer's IP address.
IPv6 addresses are also supported and must be enclosed in square brackets
[]
to avoid confusion with the final colon separating the port. For
example, port 5555 on the IPv6 loopback address ::1 would be specified as
'tcp://[::1]:5555'.
Specifying the wildcard value zero for the port number e.g. 'tcp://[::1]:0'
will automatically assign a free ephemeral port. Use status()
to inspect
the actual assigned port at any time.
Specify remote
with a call to ssh_config()
, cluster_config()
or
remote_config()
to launch (programatically deploy) daemons on remote
machines, from where they dial back to url
. If not launching daemons,
launch_remote()
may be used to generate the shell commands for manual
deployment.
If NULL
, the "default"
compute profile is used. Providing a character
value for .compute
creates a new compute profile with the name specified.
Each compute profile retains its own daemons settings, and may be operated
independently of each other. Some usage examples follow:
local / remote daemons may be set with a host URL and specifying
.compute
as "remote"
, which creates a new compute profile. Subsequent
mirai()
calls may then be sent for local computation by not specifying the
.compute
argument, or for remote computation to connected daemons by
specifying the .compute
argument as "remote"
.
cpu / gpu some tasks may require access to different types of daemon,
such as those with GPUs. In this case, daemons()
may be called to set up
host URLs for CPU-only daemons and for those with GPUs, specifying the
.compute
argument as "cpu"
and "gpu"
respectively. By supplying the
.compute
argument to subsequent mirai()
calls, tasks may be sent to
either cpu
or gpu
daemons as appropriate.
Note: further actions such as resetting daemons via daemons(0)
should
be carried out with the desired .compute
argument specified.
Use daemons(0)
to reset daemon connections:
All connected daemons and/or dispatchers exit automatically.
mirai reverts to the default behaviour of creating a new background process for each request.
Any unresolved 'mirai' will return an 'errorValue' 19 (Connection reset) after a reset.
Daemons must be reset before calling daemons()
with revised
settings for a compute profile. Daemons may be added at any time by using
launch_local()
or launch_remote()
without needing to revise daemons
settings.
If the host session ends, all connected dispatcher and daemon processes
automatically exit as soon as their connections are dropped (unless the
daemons were started with autoexit = FALSE
).
To reset persistent daemons started with autoexit = FALSE
, use
daemons(NULL)
instead, which also sends exit signals to all connected
daemons prior to resetting.
For historical reasons, daemons()
with no arguments (other than
optionally .compute
) returns the value of status()
.
if (FALSE) { # interactive()
# Create 2 local daemons (using dispatcher)
daemons(2)
status()
# Reset to zero
daemons(0)
# Create 2 local daemons (not using dispatcher)
daemons(2, dispatcher = FALSE)
status()
# Reset to zero
daemons(0)
# Set up dispatcher accepting TLS over TCP connections
daemons(url = host_url(tls = TRUE))
status()
# Reset to zero
daemons(0)
# Set host URL for remote daemons to dial into
daemons(url = host_url(), dispatcher = FALSE)
status()
# Reset to zero
daemons(0)
# Use with() to evaluate with daemons for the duration of the expression
with(
daemons(2),
{
m1 <- mirai(Sys.getpid())
m2 <- mirai(Sys.getpid())
cat(m1[], m2[], "\n")
}
)
if (FALSE) {
# Launch daemons on remotes 'nodeone' and 'nodetwo' using SSH
# connecting back directly to the host URL over a TLS connection:
daemons(
url = host_url(tls = TRUE),
remote = ssh_config(c('ssh://nodeone', 'ssh://nodetwo'))
)
# Launch 4 daemons on the remote machine 10.75.32.90 using SSH tunnelling:
daemons(
n = 4,
url = local_url(tcp = TRUE),
remote = ssh_config('ssh://10.75.32.90', tunnel = TRUE)
)
}
}
Run the code above in your browser using DataLab