parRapply and parCapply return a list in corner cases

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

parRapply and parCapply return a list in corner cases

Simone Giannerini
According to ?parCapply:

parRapply and parCapply always return a vector.

This appears not to be the case in the following minimal reproducible example:

>     library(parallel)
>     nslaves <- 2
>     cl      <- makeCluster(nslaves)
>     X       <- matrix(2,nrow=3,ncol=4)
>     X       <- rbind(c(1,1,0,1),X)
>     tv <- parCapply(cl,X,FUN=function(x){
+         ind <- x[1]
+         y   <- x[-1]
+         if(ind==1){
+             r1 <- sum(y)
+         }else{
+             r1 <- logical(0)
+         }
+         return(unlist(as.numeric(c(ind,r1))))
+     })
> tv
[[1]]
[1] 1

[[2]]
[1] 6

[[3]]
[1] 1

[[4]]
[1] 6

[[5]]
[1] 0

[[6]]
[1] 1 6

> class(tv)
[1] "list"
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
[5] LC_TIME=Italian_Italy.1252

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets
methods   base

loaded via a namespace (and not attached):
[1] compiler_4.0.0

--
_________________________________________________________________

PHILOSOPHICAL TRANSACTIONS OF THE ROYAL SOCIETY A
Theme issue ‘DNA as information’
edited by Julyan H.E. Cartwright, Simone Giannerini and Diego L. González
_________________________________________________________________

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098262  Fax: +39 051 232153
https://www.unibo.it/sitoweb/simone.giannerini/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: parRapply and parCapply return a list in corner cases

Simone Giannerini

For the record: I filed a bug report here

https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17807

and this is a more polished minimal example

   library(parallel)
   nslaves <- 2
   cl      <- makeCluster(nslaves)
  X       <- matrix(c(1,0,1),1,3)
    res <- parCapply(cl,X,FUN=function(x){
        y <- x[1]
        if(y==1){
            out <- y
        }else{
            out <- double(0)
        }
        return(out)
    })

> res
[[1]]
[1] 1

[[2]]
numeric(0)

[[3]]
[1] 1

Simone


On Mon, May 18, 2020 at 5:23 PM Simone Giannerini <[hidden email]> wrote:

>
> According to ?parCapply:
>
> parRapply and parCapply always return a vector.
>
> This appears not to be the case in the following minimal reproducible example:
>
> >     library(parallel)
> >     nslaves <- 2
> >     cl      <- makeCluster(nslaves)
> >     X       <- matrix(2,nrow=3,ncol=4)
> >     X       <- rbind(c(1,1,0,1),X)
> >     tv <- parCapply(cl,X,FUN=function(x){
> +         ind <- x[1]
> +         y   <- x[-1]
> +         if(ind==1){
> +             r1 <- sum(y)
> +         }else{
> +             r1 <- logical(0)
> +         }
> +         return(unlist(as.numeric(c(ind,r1))))
> +     })
> > tv
> [[1]]
> [1] 1
>
> [[2]]
> [1] 6
>
> [[3]]
> [1] 1
>
> [[4]]
> [1] 6
>
> [[5]]
> [1] 0
>
> [[6]]
> [1] 1 6
>
> > class(tv)
> [1] "list"
> > sessionInfo()
> R version 4.0.0 (2020-04-24)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 18363)
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252
> [3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
> [5] LC_TIME=Italian_Italy.1252
>
> attached base packages:
> [1] parallel  stats     graphics  grDevices utils     datasets
> methods   base
>
> loaded via a namespace (and not attached):
> [1] compiler_4.0.0
>
> --
> _________________________________________________________________
>
> PHILOSOPHICAL TRANSACTIONS OF THE ROYAL SOCIETY A
> Theme issue ‘DNA as information’
> edited by Julyan H.E. Cartwright, Simone Giannerini and Diego L. González
> _________________________________________________________________
>
> Simone Giannerini
> Dipartimento di Scienze Statistiche "Paolo Fortunati"
> Universita' di Bologna
> Via delle belle arti 41 - 40126  Bologna,  ITALY
> Tel: +39 051 2098262  Fax: +39 051 232153
> https://www.unibo.it/sitoweb/simone.giannerini/
> ______________________________________________________



--
___________________________________________________

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098262  Fax: +39 051 232153
https://simonegiannerini.net/

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel