R的并行化低于下限

时间:2019-02-11 16:56:06

标签: r parallel-processing

我在<xsl:copy-of select="Name/*[not(self::Name6)]" /> 上运行R,R可以看到4个内核和8个超线程单元:

macOS

以下脚本的功能是休眠0.1秒,调用10次,并在1到12范围内的多个内核上并行化这些调用:

> library(parallel)
> detectCores(logical=TRUE)
[1] 8
> detectCores(logical=FALSE)
[1] 4

结果是:

long_function <- function(x) {
  Sys.sleep(0.1)
  x
}

for (no_cores in 1:12) {

  start <- Sys.time()
  x <- mclapply(1:10, long_function, mc.cores = no_cores)
  end <- Sys.time()

  print(paste0("Parallelised over ", no_cores, " cores, time = ", end - start))
}

花费的时间减少到1/8 = 0.125秒的下限以下。

无论是否使用[1] "Parallelised over 1 cores, time = 1.00716304779053" [1] "Parallelised over 2 cores, time = 0.553637027740479" [1] "Parallelised over 3 cores, time = 0.447566032409668" [1] "Parallelised over 4 cores, time = 0.338685035705566" [1] "Parallelised over 5 cores, time = 0.234640121459961" [1] "Parallelised over 6 cores, time = 0.223365068435669" [1] "Parallelised over 7 cores, time = 0.226859092712402" [1] "Parallelised over 8 cores, time = 0.227418899536133" [1] "Parallelised over 9 cores, time = 0.226329803466797" [1] "Parallelised over 10 cores, time = 0.116447925567627" [1] "Parallelised over 11 cores, time = 0.116322040557861" [1] "Parallelised over 12 cores, time = 0.12298583984375" 预先计划作业,结果都是相同的,并且在具有4个vCPU的Azure上的Ubuntu上,结果相似,运行时间低于1/4 = 0.25秒的下限。

怎么可能?

0 个答案:

没有答案