[R] Issue with mcapply

Rainer M Krug Rainer at krugs.de
Thu Jun 11 10:45:31 CEST 2015


AROONALOK PYNE <aroonalok.pyne at gmail.com> writes:

> Please check this code :
>
> library(parallel)
> workerFunc <- function(n) { return(n^2) }
> a <- function(){
>   CurrentTime <- Sys.time()
>   res <- lapply(values, workerFunc)
>   TimeTaken <- Sys.time() - CurrentTime
>   print(TimeTaken)
> }
> b <- function(){
>   CurrentTime <- Sys.time()
>   numWorkers <- detectCores()
>   res <- mclapply(values, workerFunc, mc.cores = numWorkers-2)
>   TimeTaken <- Sys.time() - CurrentTime
>   print(TimeTaken)
> }
> c <- function(n){
>   values <<- 1:n
>   print("Evaluating a : ")
>   a()
>   print("Evaluating b : ")
>   b()
> }
>
> For large values of n, the code calculates a() but the hangs indefinitely
> on reaching b(). How do I correct it ?
> I am using Ubuntu 14.04 and core i7 Processor

Which version of R and what are "large values"?

I just did the following successfully:

,----
| > c(100)
| [1] "Evaluating a : "
| Time difference of 0.0002059937 secs
| [1] "Evaluating b : "
| Time difference of 0.02037406 secs
| > c(1000)
| [1] "Evaluating a : "
| Time difference of 0.001929998 secs
| [1] "Evaluating b : "
| Time difference of 0.01943302 secs
| > c(1000000)
| [1] "Evaluating a : "
| Time difference of 1.822959 secs
| [1] "Evaluating b : "
| Time difference of 1.46111 secs
| > c(10000000)
| [1] "Evaluating a : "
| Time difference of 23.55863 secs
| [1] "Evaluating b : "
| Time difference of 15.90938 secs
| >
`----

Cheers,

Rainer
-- 
Rainer M. Krug
email: Rainer<at>krugs<dot>de
PGP: 0x0F52F982
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 480 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150611/6a686214/attachment.bin>


More information about the R-help mailing list