Verwenden von parLapply von parallel: Zugriff auf Variablen im parallelen Code nicht möglich
Ich habe kürzlich einen Computer mit mehreren Kernen bekommen und lerne paralleles Rechnen. Ich bin ziemlich kompetent mitlapply
und wurde gesagtparLapply
funktioniert sehr ähnlich. Ich betreibe es aber nicht richtig. Ich muss anscheinend alles explizit in dieparLapply
damit es funktioniert (das sind zu verwendende Funktionen, Variablen usw.). Mitlapply
Es liest aus der übergeordneten Umgebung undparLapply
scheint das nicht zu tun. In meinem nachstehenden Beispiel könnte ich also alles zum Laufen bringen, indem ich alle Informationen hineinstelleparLapply
aber wenn ich dies innerhalb einer benutzerdefinierten Funktion verwende, kann ich nicht explizit setzentext.var
InnenparLapply
.
library(parallel)
text.var <- rep("I like cake and ice cream so much!", 20)
ntv <- length(text.var)
gc.rate <- 10
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
lapply(seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
#doesn't work
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i) {
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
#does work but have to specify all the stuff inside parLapply
cl <- makeCluster(mc <- getOption("cl.cores", 4))
parLapply(cl, seq_len(ntv), function(i) {
######stuff I have to put inside parLapply##########
text.var <- rep("I like cake and ice cream so much!", 20)
ntv <- length(text.var)
gc.rate <- 10
pos <- function(i) {
paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
}
######stuff I have to put inside parLapply##########
x <- pos(text.var[i])
if (i%%gc.rate==0) gc()
return(x)
}
)
Wie kann ich bestehen?text.var
, ntv
, gc.rate
, undpos
zuparLapply
ohne sie explizit hinein zu setzen? (Ich vermute, Sie übergeben sie irgendwie als Liste)
PS Windows 7-Maschine, so muss ich verwendenparLapply
Meiner Ansicht nach