【发布时间】:2012-06-21 23:12:34
【问题描述】:
我对 R 有点陌生,所以请原谅这里的新手...
我正在用 R 编写代码,以在脚本中加载 1000 个保存的数据帧(文件),该脚本对每个文件中的数据运行函数并将结果值存储在向量中。我必须用不同的功能一遍又一遍地做这件事,目前这需要很长时间。
我正在尝试使用多核 mclapply 并行化该过程,但不幸的是,2 到 8 个内核之间的任何东西似乎都比仅在一个内核上运行它需要更长的时间。
由于磁盘 I/O 限制,这个想法从根本上来说是不合理的吗?多核,甚至 R,不是正确的解决方案吗?用 Python 之类的方法打开文件,然后在内容上运行 R 函数会比 R 更好吗?
对此的任何指导或想法将不胜感激 -
为清楚起见添加了代码:
library(multicore)
project.path = "/pathtodata/"
#This function reads the file location and name, then loads it and runs a simple statistic
running_station_stats <- function(nsrdb_stations)
{
varname <- "column_name"
load(file = paste(project.path, "data/",data_set_list[1], sep = ""))
tempobj <- as.data.frame(coredata(get(data_set_list[2])))
mean(tempobj[[varname]],na.rm=TRUE)
}
options(cores = 2)
#This file has a list of R data files data_set_list[1] and the names they were created with data_set_list[2]
load(file = paste(project.path, "data/data_set_list.RData", sep = ""))
thelist <- list()
thelist[[1]] <- data_set_list[1:50,]
thelist[[2]] <- data_set_list[51:100,]
thelist[[3]] <- data_set_list[101:150,]
thelist[[4]] <- data_set_list[151:200,]
#All three of these are about the same speed to run regardless of the num of cores
system.time(
{
apply(nsrdb_stations[which(nsrdb_stations$org_stations==TRUE),][1:200,],1,running_station_stats)
})
system.time(
lapply(thelist, apply, 1, running_station_stats)
)
system.time(
mclapply(thelist, apply, 1, running_station_stats)
)
【问题讨论】:
-
除非您向我们展示您的代码在做什么,否则无法知道,其他人只能猜测。您可以测试自己是文件访问是限制问题还是其他问题。一旦你将问题简化为它的组成部分,你就会开始自己回答这个问题。
-
如果数据已经在数据帧中,那么通过另一种语言编组 R 原生格式可能只会减慢速度。但正如 mdsumner 所说,我们只能根据您所展示的内容进行猜测。
-
mdsumner - 如果这个简化的代码示例中的某些内容让您作为表演犬跳出来,请告诉我。如果我发现可以加快处理速度的内容,我会在此处发布。