mclappy:在多核机器上 lapply hack to 运行 不适用于特定库 (pdist) 函数

mclappy: The lapply hack to run on multi-core machines doesn't work for a particular library (pdist) function

首先:我的问题的起源是 "lapply" 的 Windows 并行处理实现的 hack - 作者:Nathan VanHoudnos

http://edustatistics.org/nathanvan/2014/07/14/implementing-mclapply-on-windows/

我的 objective 是应用距离函数 "pdist" 来计算两个矩阵之间的 "distances"。

原始代码工作正常 - 但是当我尝试使用 'pdist' 库函数时出现问题。

我还确保 'pdist' 函数名称包含在 clusterExport() 代码中。

我得到的错误是:

checkForRemoteErrors(val) 错误: 4个节点产生错误;第一个错误:C 符号名称 "Rpdist" 未加载 table

在此处转载代码:

## Load packages 
require(parallel)
require(pdist)

# Define global variables 
A = rbind(c(3,40,1),c(24,13,2), c(90,8,1));
B = rbind(c(23,4,11),c(13,913,12), c(0.9,0.8,0.1));

## Step 1: Create a cluster of child processes 
cat("\n Step 1: Create a cluster of child processes...."); 
cl <- makeCluster(4)

## Step 2: Load the necessary R package(s)
## N.B. length(cl) is the number of child processes in the cluster 
cat("\n Step 2: Load the necessary R package(s)....");
par.setup <- parLapply (cl, 1:length(cl),
    function(xx) {
        require(pdist) 
})

## Step 3: Distribute the necessary R objects 
cat("\n Step 3: Distribute the necessary R objects....");
clusterExport (cl, c('A', 'B', 'pdist'))

## Step 4: Do the computation
cat("\n Step 4: Do the multi-core computation....\n");
par.Distance <- parLapply (cl, 1:4,
    function(xx) {
       as.matrix(pdist(A, B))            
    })

## Step 5: Remember to stop the cluster!
cat("\n Step 5: Stop the clusters....\n");
stopCluster(cl)

cat("\n Output: "); print(par.Distance);
cat("\n ----------------------------- \n");

感谢您的帮助。

你的例子对我有用,所以我怀疑工作人员无法在你的机器上成功加载 pdist 包。 par.setup 的值应该是包含四个 TRUE 值的列表。如果没有,您需要解决该问题,可能是在加载 pdist.

之前对工作人员执行 .libPaths

此外,将 pdist 输出给使用 clusterExport 的工人没有意义。如果您可以成功地将 pdist 包加载到工作人员身上,则没有必要,这还不够,因为它依赖于 pdist 包中的代码,而这些代码不会被发送给工作人员clusterExport。它所做的只是将错误消息更改为您现在看到的错误消息。