Rev 62172 | Rev 65139 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/parallel/man/unix/mcparallel.Rd% Part of the R package, http://www.R-project.org% Copyright 2009-11 R Core Team% Distributed under GPL 2 or later\newcommand{\CRANpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}\name{mcparallel}\alias{mccollect}\alias{mcparallel}\title{Evaluate an \R Expression Asynchronously in a Separate Process}\description{These functions are based on forking and so are not available on Windows.\code{mcparallel} starts a parallel \R process which evaluates thegiven expression.\code{mccollect} collects results from one or more parallel processes.}\usage{mcparallel(expr, name, mc.set.seed = TRUE, silent = FALSE,mc.affinity = NULL, mc.interactive = FALSE)mccollect(jobs, wait = TRUE, timeout = 0, intermediate = FALSE)}\arguments{\item{expr}{expression to evaluate (do \emph{not} use any on-screendevices or GUI elements in this code).}\item{name}{an optional name (character vector of length one) that canbe associated with the job.}\item{mc.set.seed}{logical: see section \sQuote{Random numbers}.}\item{silent}{if set to \code{TRUE} then all output on stdout will besuppressed (stderr is not affected).}\item{mc.affinity}{either a numeric vector specifying CPUs to restrictthe child process to (1-based) or \code{NULL} to not modify the CPUaffinity}\item{mc.interactive}{logical, if \code{TRUE} or \code{FALSE} then thechild process will be set as interactive or non-interactiverespectively. If \code{NA} then the child process will inherit theinteractive flag from the parent.}\item{jobs}{list of jobs (or a single job) to collect resultsfor. Alternatively \code{jobs} can also be an integer vector ofprocess IDs. If omitted \code{collect} will wait for all currentlyexisting children.}\item{wait}{if set to \code{FALSE} it checks for any results that areavailable within \code{timeout} seconds from now, otherwise it waitsfor all specified jobs to finish.}\item{timeout}{timeout (in seconds) to check for job results -- appliesonly if \code{wait} is \code{FALSE}.}\item{intermediate}{\code{FALSE} or a function which will be called while\code{collect} waits for results. The function will be called with oneparameter which is the list of results received so far.}}\details{\code{mcparallel} evaluates the \code{expr} expression in parallel tothe current \R process. Everything is shared read-only (or in factcopy-on-write) between the parallel process and the current process,i.e. no side-effects of the expression affect the main process. Theresult of the parallel execution can be collected using\code{mccollect} function.\code{mccollect} function collects any available results from paralleljobs (or in fact any child process). If \code{wait} is \code{TRUE}then \code{collect} waits for all specified jobs to finish beforereturning a list containing the last reported result for eachjob. If \code{wait} is \code{FALSE} then \code{mccollect} merelychecks for any results available at the moment and will not wait forjobs to finish. If \code{jobs} is specified, jobs not listed therewill not be affected or acted upon.Note: If \code{expr} uses low-level multicore functions suchas \code{\link{sendMaster}} a single job can deliver resultsmultiple times and it is the responsibility of the user to interpretthem correctly. \code{mccollect} will return \code{NULL} for aterminating job that has sent its results already after which thejob is no longer available.The \code{mc.affinity} parameter can be used to try to restrictthe child process to specific CPUs. The availability and the extent ofthis feature is system-dependent (e.g., some systems will onlyconsider the CPU count, others will ignore it completely).}\value{\code{mcparallel} returns an object of the class \code{"parallelJob"}which inherits from \code{"childProcess"} (see the \sQuote{Value}section of the help for \code{\link{mcfork}}). If argument\code{name} was supplied this will have an additional component\code{name}.\code{mccollect} returns any results that are available in a list. Theresults will have the same order as the specified jobs. If there aremultiple jobs and a job has a name it will be used to name theresult, otherwise its process ID will be used. If none of thespecified children are still running, it returns \code{NULL}.}\section{Random numbers}{If \code{mc.set.seed = FALSE}, the child process has the same initialrandom number generator (RNG) state as the current \R session. If theRNG has been used (or \code{.Random.seed} was restored from a savedworkspace), the child will start drawing random numbers at the samepoint as the current session. If the RNG has not yet been used, thechild will set a seed based on the time and process ID when it firstuses the RNG: this is pretty much guaranteed to give a differentrandom-number stream from the current session and any other childprocess.The behaviour with \code{mc.set.seed = TRUE} is different only if\code{\link{RNGkind}("L'Ecuyer-CMRG")} has been selected. Then eachtime a child is forked it is given the next stream (see\code{\link{nextRNGStream}}). So if you select that generator, set aseed and call \code{\link{mc.reset.stream}} just before the first useof \code{mcparallel} the results of simulations will be reproducibleprovided the same tasks are given to the first, second, \ldots{}forked process.}\note{Package \CRANpkg{multicore} also exported functions \code{collect} and\code{parallel}. These names are easily masked (for example package\CRANpkg{lattice} also has a function \code{parallel}) and they are notsupplied in this package.}\author{Simon Urbanek and R Core.Derived from the \CRANpkg{multicore} package (but with differenthandling of the RNG stream).}\seealso{\code{\link{pvec}}, \code{\link{mclapply}}}\examples{p <- mcparallel(1:10)q <- mcparallel(1:20)# wait for both jobs to finish and collect all resultsres <- mccollect(list(p, q))p <- mcparallel(1:10)mccollect(p, wait = FALSE, 10) # will retrieve the result (since it's fast)mccollect(p, wait = FALSE) # will signal the job as terminatingmccollect(p, wait = FALSE) # there is no longer such a job\dontshow{set.seed(123, "L'Ecuyer"); mc.reset.stream()}# a naive parallel lapply can be created using mcparallel alone:jobs <- lapply(1:10, function(x) mcparallel(rnorm(x), name = x))mccollect(jobs)}\keyword{interface}