gpt4 book ai didi

parallel-processing - Julia :正确使用pmap

转载 作者:行者123 更新时间:2023-12-04 08:05:42 26 4
gpt4 key购买 nike

为什么这不做我认为应该做的事情:

benjamin@benjamin-VirtualBox:~$ julia -p 3
julia> @everywhere(function foom(bar::Vector{Any}, k::Integer) println(repeat(bar[2],bar[1])); return bar; end)
julia> foo={{1,"a"},{2,"b"},{3,"c"}}
julia> pmap(foom, foo, 5)
From worker 2: a
1-element Array{Any,1}:
{1,"a"}

这就是它的全部输出。我期望pmap遍历foo中的每个元组并在其上调用foom。

编辑:

当我不传递其他参数时,它可以正常工作:
julia> @everywhere(function foom(bar::Vector{Any}) println(repeat(bar[2],bar[1])); return bar; end)

julia> pmap(foom, foo)
From worker 3: bb
From worker 2: a
From worker 4: ccc
3-element Array{Any,1}:
{1,"a"}
{2,"b"}
{3,"c"}

如何将更多参数传递给pmap?

最佳答案

函数 pmap 确实接受任意数量的参数,每个参数一个集合

function pmap(f, lsts...; err_retry=true, err_stop=false)
将为每个集合向函数f发送一个参数
示例多参数函数
julia> @everywhere f(s,count)=(println("process id = $(myid()) s = $s count = $count");repeat(s,count))
pmap使用1
julia> pmap((a1,a2)->f(a1,a2),{"a","b","c"},{2,1,3})
From worker 3: process id = 3 s = b count = 1
From worker 2: process id = 2 s = a count = 2
From worker 4: process id = 4 s = c count = 3
3-element Array{Any,1}:
"aa"
"b"
"ccc"
pmap使用2
或者,可以将参数作为一个外部集合中的参数集合发送,可以将其添加到目标函数的多个参数中
julia> pmap((args)->f(args...),{{"a",2},{"b",1},{"c",3}})
From worker 2: process id = 2 s = a count = 2
From worker 3: process id = 3 s = b count = 1
From worker 4: process id = 4 s = c count = 3
3-element Array{Any,1}:
"aa"
"b"
"ccc"

关于parallel-processing - Julia :正确使用pmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com