并行化每个修改函数父环境的独立函数调用
Parallellize Independent Function Calls that Each Modify Function's Parent Environment
我想找到一种方法来并行化重复的独立函数调用,其中每次调用都会修改函数的父环境。该函数的每次执行都是独立的,但是,由于各种原因,我无法考虑不依赖于修改函数的父环境的任何其他实现。请参阅下面的简化示例。有没有办法将父环境的副本传递给每个节点?我在 linux 系统上 运行 这个。
create_fun <- function(){
helper <- function(x, params) {x+params}
helper2 <- function(z) {z+helper(z)}
master <- function(y, a){
parent <- parent.env(environment())
formals(parent[['helper']])$params <- a
helper2(y)}
return(master)
}
# function to be called repeatedly
master <- create_fun()
# data to be iterated over
x <- expand.grid(1:100, 1:5)
# vector where output should be stored
results <- vector("numeric", nrow(x))
# task I'd like to parallelize
for(i in 1:nrow(x)){
results[i] <- master(x[i,1], x[i, 2])
}
函数确实维护对其父环境的引用。可以看看master
环境的内容(create_fun
创建的环境)
ls (environment(master) )
# [1] "helper" "helper2" "master"
使用%dopar%
你可以做到
## Globals
master <- create_fun()
x <- expand.grid(1:100, 1:5)
## Previous results
for(i in 1:nrow(x)){
results[i] <- master(x[i,1], x[i, 2])
}
library(parallel)
library(doParallel)
cl <- makePSOCKcluster(4)
registerDoParallel(cl)
## parallel
res <- foreach(i=1:nrow(x), .combine = c) %dopar% {
master(x[i,1], x[i,2])
}
all.equal(res, results)
# TRUE
我想找到一种方法来并行化重复的独立函数调用,其中每次调用都会修改函数的父环境。该函数的每次执行都是独立的,但是,由于各种原因,我无法考虑不依赖于修改函数的父环境的任何其他实现。请参阅下面的简化示例。有没有办法将父环境的副本传递给每个节点?我在 linux 系统上 运行 这个。
create_fun <- function(){
helper <- function(x, params) {x+params}
helper2 <- function(z) {z+helper(z)}
master <- function(y, a){
parent <- parent.env(environment())
formals(parent[['helper']])$params <- a
helper2(y)}
return(master)
}
# function to be called repeatedly
master <- create_fun()
# data to be iterated over
x <- expand.grid(1:100, 1:5)
# vector where output should be stored
results <- vector("numeric", nrow(x))
# task I'd like to parallelize
for(i in 1:nrow(x)){
results[i] <- master(x[i,1], x[i, 2])
}
函数确实维护对其父环境的引用。可以看看master
环境的内容(create_fun
创建的环境)
ls (environment(master) )
# [1] "helper" "helper2" "master"
使用%dopar%
你可以做到
## Globals
master <- create_fun()
x <- expand.grid(1:100, 1:5)
## Previous results
for(i in 1:nrow(x)){
results[i] <- master(x[i,1], x[i, 2])
}
library(parallel)
library(doParallel)
cl <- makePSOCKcluster(4)
registerDoParallel(cl)
## parallel
res <- foreach(i=1:nrow(x), .combine = c) %dopar% {
master(x[i,1], x[i,2])
}
all.equal(res, results)
# TRUE