`map()` 的并行版本

Parallel version of `map()`

我有一个接受多个参数的函数。

my_function <- function(list_of_vectors, list_of_scalars){
 ....
return(list)
}

我想在我的函数上使用 map(),但使用会使用多核的并行调用。 my_function() 在计算上非常昂贵,我需要调用它来创建超过 1000 个点的输出。 (list_of_vectors 是 1000 个向量的列表,list_of_scalars 是 1000 个标量的列表)

是否有 mcmap() 等效或任何其他公式?我查看了其他主题,但 none 解决了我的问题。

您可以使用 furrr 包中的 future_map() 作为 drop-in 替代品。

这是一个非常灵活的功能;它如何分配你的计算将取决于之前对 future::plan() 的调用(furrr 建立在 future 包之上),例如

future::plan(multicore, workers = 4)
future_map(...)

到 运行 你在同一台机器上的 4 个核心上的工作。

您可以使用 map() function from Parallel 库,如下所示:

const p = new Parallel([0, 1, 2, 3, 4, 5, 6]);
const log = function () { console.log(arguments); };

// One gotcha: anonymous functions cannot be serialzed
// If you want to do recursion, make sure the function
// is named appropriately
function fib(n) {
  return n < 2 ? 1 : fib(n - 1) + fib(n - 2);
};
    
p.map(fib).then(log)

// Logs the first 7 Fibonnaci numbers, woot!