How and where parallelization occurs is determined by mirai::daemons()
.
This is a function from the mirai package that sets up daemons
(persistent background processes that receive parallel computations) on your
local machine or across the network.
Daemons must be set prior to performing any parallel map operation, otherwise
in_parallel()
will fall back to sequential processing. To ensure that maps
are always performed in parallel, put mirai::require_daemons()
before the
map.
It is usual to set daemons once per session. You can leave them running on
your local machine as they consume almost no resources whilst waiting to
receive tasks. The following sets up 6 daemons locally:
mirai::daemons(6)
Function arguments:
n
: the number of daemons to launch on your local machine, e.g.
mirai::daemons(6)
. As a rule of thumb, for maximum efficiency this should
be (at most) one less than the number of cores on your machine, leaving one
core for the main R process.
url
and remote
: used to set up and launch daemons for distributed
computing over the network. See mirai::daemons()
documentation for more
details.
Resetting daemons:
Daemons persist for the duration of your session. To reset and tear down any
existing daemons:
mirai::daemons(0)
All daemons automatically terminate when your session ends. You do not need
to explicitly terminate daemons in this instance, although it is still good
practice to do so.
Note: it should always be for the user to set daemons. If you are using
parallel map within a package, do not make any mirai::daemons()
calls
within the package, as it should always be up to the user how they wish to
set up parallel processing e.g. using local or remote daemons. This also
helps prevent inadvertently spawning too many daemons if functions are used
recursively within each other.