2016-09-19 62 views
2

我想并行运行一个函数,其中函数是通过字符串参数提供的。它首先从字符串解析为表达式,然后并行运行。在Julia中并行运行解析函数

的问题是,将字符串参数是不知道的所有工作进程。

我怎样才能使这项工作:

module test 

function run(f) 

@everywhere f_exp = eval(parse(f)) 

values = SharedArray(Float64,2) 
@sync @parallel for i = 1:2 
    values[i] = f_exp(i) 
end 

return mean(values) 
end 

end 

# RUN AS FOLLOWS 
# @everywhere include("c:\\projects\\evaluator\\test.jl") 
# test.run("function func(x) return 2*x end") 

我也试过@everywhere f_exp = eval(parse("@everywhere $f")),虽然我没想到它的工作...它没有。

我该如何让每个过程都知道这个论点?

@everywhere f在函数运行的第一行还没有做任何事情。

回答

1

我得到了它,如果我没有module...end周围的代码做的工作如下:

function run(f) 

@eval @everywhere f = $f 
@everywhere f_exp = eval(parse(f)) 

values = SharedArray(Float64,2) 
@sync @parallel for i = 1:2 
    values[i] = f_exp(i) 
end 

return mean(values) 
end 

如果我与周围这module...end功能,我得到类似的错误:## 9#11不定义(对工人2)

我有一个想法,它不与module...end工作的原因是@everywhere采用模块主要...我试过的东西在这个方向,但没有得到它的工作