无法在 R 中并行化具有多个参数的函数

Unable to parallelize a function with multiple arguments in R

我尝试并行化一个简单的函数,该函数将两个数字相加并使用库 parallel 中的 mclapplyR 中打印结果。这是我的代码:

library(doParallel)
t = list(list(1,1),list(2,2),list(3,3))
f <- function (a,b){
    print(a + b)
}
mclapply(t,f)

但是 returns 错误 :

Warning message in mclapply(t, f):
“all scheduled cores encountered errors in user code”
[[1]]
[1] "Error in print(a + b) : argument \"b\" is missing, with no default\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in print(a + b): argument "b" is missing, with no default>

[[2]]
[1] "Error in print(a + b) : argument \"b\" is missing, with no default\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in print(a + b): argument "b" is missing, with no default>

[[3]]
[1] "Error in print(a + b) : argument \"b\" is missing, with no default\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError in print(a + b): argument "b" is missing, with no default>

有人可以告诉我我做错了什么吗?

我试图搜索如何 ​​运行 并行具有多个参数的函数,但没有找到答案。

mclapply 正在为本身就是列表的列表中的每个元素调用函数。因此,每次调用该函数时,您都会将一个列表传递给它,而不是其他任何东西。您需要解压函数中的列表:

library(doParallel)
t = list(list(1,1),list(2,2),list(3,3))
f <- function (a){
  print(a[[1]] + a[[2]])
}
mclapply(t,f)