跟进一些data.table
并行性(1) (2) (3)我试图弄清楚。这种语法有什么问题?
library(data.table)
set.seed(1234)
dt <- data.table(id= factor(sample(1L:10000L, size= 1e6, replace= TRUE)),
val= rnorm(n= 1e6), key="id")
foo <- function(l) sum(l)
dt2 <- dt[, foo(.SD), by= "id"]
library(parallel)
cl <- makeCluster(detectCores())
dt3 <- clusterApply(cl, x= parallel:::splitRows(dt, detectCores()),
fun=lapply, FUN= function(x,foo) {
x[, foo(data.table:::".SD"), by= "id"]
}, foo= foo)
stopCluster(cl)
# note that library(parallel) is annoying and you often have to do this type ("::", ":::") of exporting to the parallel package
checkForRemoteErrors(val) 中的错误:4 个节点产生错误;第一个错误:维数不正确
cl <- makeCluster(detectCores())
dt3 <- clusterApply(cl, x= parallel:::splitRows(dt, detectCores()),
fun=lapply, FUN= function(x,foo) {
x <- data.table::data.table(x)
x[, foo(data.table:::".SD"), by= "id"]
}, foo= foo)
stopCluster(cl)
checkForRemoteErrors(val) 中的错误:4 个节点产生错误;第一个错误:找不到对象“id”
我已经玩了很多语法。这两个似乎是我能得到的最接近的。显然有些事情仍然不对劲。
我真正的问题是类似的结构,但有更多的行,我正在使用一台具有 24 个内核/48 个逻辑处理器的机器。所以看着我的电脑使用大约 4% 的计算能力(仅使用 1 个核心)真的很烦人