Examples for 'parallel::mclapply'


Parallel Versions of 'lapply' and 'mapply' using Forking

Aliases: mclapply mcmapply mcMap

Keywords: interface

### ** Examples
## No test: 
simplify2array(mclapply(rep(4, 5), rnorm))
           [,1]      [,2]        [,3]       [,4]        [,5]
[1,]  1.5436178 2.2195551 -0.59771234  0.9284825 -1.17773758
[2,] -0.3127122 0.1154679 -2.64483554 -0.3497437 -1.84956446
[3,]  0.4986590 1.7314462  0.83552972  0.3157886  0.34313444
[4,]  0.3620856 0.1221489 -0.03274486  1.1421857  0.01718078
# use the same random numbers for all values
set.seed(1)
simplify2array(mclapply(rep(4, 5), rnorm, mc.preschedule = FALSE,
                        mc.set.seed = FALSE))
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.6264538 -0.6264538 -0.6264538 -0.6264538 -0.6264538
[2,]  0.1836433  0.1836433  0.1836433  0.1836433  0.1836433
[3,] -0.8356286 -0.8356286 -0.8356286 -0.8356286 -0.8356286
[4,]  1.5952808  1.5952808  1.5952808  1.5952808  1.5952808
## Contrast this with the examples for clusterCall
library(boot)
cd4.rg <- function(data, mle) MASS::mvrnorm(nrow(data), mle$m, mle$v)
cd4.mle <- list(m = colMeans(cd4), v = var(cd4))
mc <- getOption("mc.cores", 2)
run1 <- function(...) boot(cd4, corr, R = 500, sim = "parametric",
                           ran.gen = cd4.rg, mle = cd4.mle)
## To make this reproducible:
set.seed(123, "L'Ecuyer")
res <- mclapply(seq_len(mc), run1)
cd4.boot <- do.call(c, res)
boot.ci(cd4.boot,  type = c("norm", "basic", "perc"),
        conf = 0.9, h = atanh, hinv = tanh)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL : 
boot.ci(boot.out = cd4.boot, conf = 0.9, type = c("norm", "basic", 
    "perc"), h = atanh, hinv = tanh)

Intervals : 
Level      Normal              Basic              Percentile     
90%   ( 0.4575,  0.8547 )   ( 0.4703,  0.8575 )   ( 0.4964,  0.8663 )  
Calculations on Transformed Scale;  Intervals on Original Scale
## Usage of the affinity.list parameter
A <- runif(2500000,0,100)
B <- runif(2500000,0,100)
C <- runif(5000000,0,100)
first <- function(i) head(sort(i), n = 1)

# Restict all elements of X to run on CPU 1 and 2
affL <- list(c(1,2), c(1,2), c(1,2))
mclapply(list(A, A, A), first, mc.preschedule = FALSE, affinity.list = affL)
[[1]]
[1] 0.0001111999

[[2]]
[1] 0.0001111999

[[3]]
[1] 0.0001111999
# Completion times are assumed to have a high variance
# To optimize the overall execution time elements of X are scheduled to suitable CPUs
# Assuming that the runtime for C is as long as the runtime of A plus B
# mapping: A to 1 , B to 1, C to 2
X <- list(A, B, C)
affL <- c(1, 1, 2)
mclapply(X, first, mc.preschedule = FALSE, affinity.list = affL)
[[1]]
[1] 0.0001111999

[[2]]
[1] 1.373701e-05

[[3]]
[1] 2.18628e-05
## End(No test)

[Package parallel version 4.2.3 Index]