2

从我的问题的性质可以看出,我是 r 编程的新手。我正在尝试利用 train 函数的并行计算能力。

library(parallel)
#detects number of cores available to use for parallel package
nCores <- detectCores(logical = FALSE)
cat(nCores, " cores detected.")  

# detect threads with parallel()
nThreads<- detectCores(logical = TRUE)
cat(nThreads, " threads detected.")

# Create doSNOW compute cluster (try 64)
# One can increase up to 128 nodes
# Each node requires 44 Mbyte RAM under WINDOWS.
cluster <- makeCluster(128, type = "SOCK")
class(cluster);

我需要有人帮我解释这段代码。最初的第一个参数makeCluster()had nthreads 但在运行之后

nCores <- detectCores(logical = FALSE)

我了解到我有 4 个线程可用。我根据指南中提供的消息更改了值。这能让我一次同时运行 128 次 train 函数迭代吗?如果是这样,首先获取我的计算机拥有的线程和内核数量有什么意义?

4

1 回答 1

2

您要做的是首先检测您拥有的核心数量。

nCores <- detectCores() - 1

大多数情况下,人们会加负 1 以确保您还有一个核心可以做其他事情。

cluster <- makeCluster(nCores)

这将设置您希望代码运行的集群数量。有几种并行方法(doParallel、parApply、parLapply、foreach、..)。根据您选择的并行方法,将在您创建的一个特定集群上运行一种方法。

我在我的代码中使用的小例子

  no_cores <- detectCores() - 1
  cluster <- makeCluster(no_cores)
  result <- parLapply(cluster, docs$text, preProcessChunk)
  stopCluster(cluster)

我也看到你使用袜子。不确定“type=SOCK”是否有效。我总是使用“type=PSOCK”。FORK 也存在,但这取决于您使用的操作系统。

FORK: "to divide in branches and go separate ways"
Systems: Unix/Mac (not Windows)
Environment: Link all

PSOCK: Parallel Socket Cluster
Systems: All (including Windows)
Environment: Empty
于 2019-04-26T09:03:50.530 回答