First, both mclapply
and parLapply
are in the parallel
package, although mclapply
doesn't actually run in parallel on Windows. parLapply
runs in parallel on all supported platforms, but isn't always as efficient as mclapply
. The doParallel
package is used with the foreach
package, and acts as an adapter to the parallel
package.
To write a package that works on both Windows and non-Windows, you have a variety of reasonable options:
- Just use
parLapply
since it works everywhere - Use
parLapply
on Windows andmclapply
elsewhere - Use
doParallel
withforeach
The doParallel
package is convenient because it makes use of mclapply
on non-Windows platforms. For example:
library(doParallel)
registerDoParallel()
foreach(i=1:10, .options.snow=list(preschedule=TRUE)) %dopar% {
Sys.sleep(2)
}
This uses mclapply
on Linux and Mac OS X, but will automatically create a PSOCK cluster object behind the scenes on Windows. The use of preschedule=TRUE
(added in doParallel
1.0.3) will cause doParallel
to preschedule the tasks using clusterApply
internally, much like parLapply
.
Note that if you explicitly create and register a cluster object, then mclapply
will not be used, regardless of the platform. It will work fine, but may not be as efficient. To use mclapply
, you must call registerDoParallel
with a numeric argument, or no argument at all.
You can look at the source code for the boot
package for an example of how to use either mclapply
or parLapply
depending on your platform.