Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia动态调度_Julia - Fatal编程技术网

Julia动态调度

Julia动态调度,julia,Julia,我有一个用于动态调度的函数,我可以将其用于简单的数组和函数,例如,我可以将其用于以下代码: 日程安排: @everywhere function pmap(f,lst) np=nprocs() n=length(lst) results=Vector{Any}(n) i=1 nextidx()=(idx=i;i+=1;idx) @

我有一个用于动态调度的函数,我可以将其用于简单的数组和函数,例如,我可以将其用于以下代码:

日程安排:

@everywhere function pmap(f,lst)
              np=nprocs()
              n=length(lst)
              results=Vector{Any}(n)
              i=1
              nextidx()=(idx=i;i+=1;idx)
              @sync begin
              for p=1:np
                  if p != myid() || np==1
                      @sync begin
                          while true
                              idx=nextidx()
                              if idx > n
                                  break
                              end
                              results[idx]= remotecall_fetch(f,p,lst[idx])
                          end
                      end
                  end
               end
              end
              results
              end
功能:

@everywhere f(x)=x+1
f (generic function with 1 method)
@everywhere f(x,y)=x.+y

julia> x=SharedArray{Float64}(2,2)
2×2 SharedArray{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> y=SharedArray{Float64}(2,2)
2×2 SharedArray{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> x=[1 2;3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> y=[6 7;8 9]
2×2 Array{Int64,2}:
 6  7
 8  9
数组:

julia> arrays=SharedArray{Float64}(10)
10-element SharedArray{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

julia> arrays=[1 2 3 4 5 6 7 8 9 10]
1×10 Array{Int64,2}:
 1  2  3  4  5  6  7  8  9  10
结果:

@everywhere function fsum(x)
       x+1
       end

pmap(fsum,arrays)
10-element Array{Any,1}:
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
我的问题是,如果我有这个函数和数组,我应该如何使用调度函数? 功能:

@everywhere f(x,y)=x.+y

julia> x=SharedArray{Float64}(2,2)
2×2 SharedArray{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> y=SharedArray{Float64}(2,2)
2×2 SharedArray{Float64,2}:
 0.0  0.0
 0.0  0.0

julia> x=[1 2;3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

julia> y=[6 7;8 9]
2×2 Array{Int64,2}:
 6  7
 8  9
我想通过
pmap(f,x,y)
调用它,但我得到了这个错误:

ERROR: MethodError: no method matching pmap(::#f, ::Array{Int64,2}, ::Array{Int64,2})
You may have intended to import Base.pmap
Closest candidates are:
  pmap(::Any, ::Any) at REPL[1]:2

我还有另一个问题,我们如何确定我们的问题是在不同的过程中运行的?如何监控它?

pmap
splats参数,因此它可以工作:

f(x,y) = x+y; pmap(f,1:5,6:10)

您可能使用OP中的内容重新定义了
pmap
,而OP中的内容不会显示参数,因此会失败。您不需要在此处编写自己的版本:如果您只使用内置版本,它将起作用。

为什么不使用内置的
pmap
?它做的就是这样的动态调度,而且它会浪费参数。你是什么意思?我有一个函数,它有几个参数,例如
x
y
,我想通过两个参数调用
pmap
函数。例如,我告诉wand通过两个矩阵调用它
x
y
pmap(f,zip(x,y))
生成一个值元组。当我使用
pmap(f,zip(x,y))
时,我得到了一个错误:pmap(f,zip(x,y))错误:MethodError:没有方法匹配getindex(::Base.Iterators.Zip2{Array{Int64,2},Array{Int64,2},::Int64)Stacktrace:[1]宏扩展在./REPL[1]:16[inline][2]宏扩展在./task.jl:302 inline][3]宏扩展在./REPL[1] :10[inline][4]宏扩展在./task.jl:302[inline][5]pmap(::函数,::Base.Iterators.Zip2{Array{Int64,2},Array{Int64,2})在./REPL[1]:7Huh,你在用什么版本的Julia?我刚刚试过
f(x,y)=x+y;pmap(f,1:5,6:10)
并且它可以工作。你重新启动Julia了吗?根据上面的
pmap
的定义,你可能只是重写了它,并且一直在使用自己的版本,它不会进行参数的散乱。注意
zip
的完整内容是
f(x,y)=x+y;g(x)=f(x…;pmap(g,collect(zip(1:5,6:10)))
。但是这应该是不必要的,因为
pmap(f,x,y)
在标准的Julia安装上工作,没有覆盖
pmap
。但是我得到了一个错误,`Julia>@everywhere f(x,y)=x+y;pmap(f,1:5,6:10)错误:MethodError:没有与pmap匹配的方法(::::#f,::UnitRange{Int64},::UnitRange{Int64})您可能打算在REPL[10]处导入Base.pmap最接近的候选项是:pmap(::Any,::Any):2`是的,因为您重新定义了
pmap
。不要这样做。请重新启动Julia,不要运行您在OP中定义的函数。是的,它可以工作,但我的问题是如何使用我在上面编写的
pmap
函数进行动态调度?我想并行一些有很多循环的程序,并为每个进程分配任务所以我在julia的教程中看到了这个函数,我想把它用于两个矩阵。它会自动执行。
@parallel
是静态调度。
pmap
是批次的动态调度(默认为
batch\u size=1
)。请在
睡眠(rand())上试用它
你会看到它根据需要重新分配工作。是的,
pmap
意味着并行映射。如果你已经添加了进程,那么它将在它们之间并行化。你不需要做任何事情来在矩阵上使用它:
pmap(f,rand(4,4),rand(4,4))
效果很好。要进行更深入的讨论,请加入StackOverflow评论,因为StackOverflow评论不是用来聊天的。