Julia:在表达式中使用分布式数组

时间:2022-08-06 13:54:16

I am trying to spawn and evaluate expressions over different processes. The expressions contain local parts of distributed arrays, and this seems to create problems. For example,

我试图在不同的进程上生成和计算表达式。表达式包含分布式数组的本地部分,这似乎会产生问题。例如,

addprocs(2)
x = [i for i = 1:10]
foo = @spawnat 2 quote
  out = x[1]
  for i = 2:5
    out += x[i]
  end
  out 
end
eval(fetch(foo))

gives, as expected,

按预期给出

Out [ ]: 15

However, if I try to replace the vector x with a distributed array dx and use only the local chunk in the expression, I get the following error.

但是,如果我尝试用分布式数组dx替换向量x并仅使用表达式中的本地块,则会出现以下错误。

# Construct a distributed array dx = [1,2,3,4,5,6,7,8,9,10] # 
dx  = DArray(I->[i for i in I[1]], (10, ))    
dfoo = @spawnat 2 quote
  out = localpart(dx)[1]
  for i = 2:5
    out += localpart(dx)[i]
  end
  out 
end
eval(fetch(dfoo))


Out []: ERROR: BoundsError()
  while loading In[9], in expression starting on line 9

   in getindex at array.jl:246
   in anonymous at In[9]:2

I got the feeling that the problem is the localpart() which is not recognized when the expression is evaluated. Am I right? Is there a way around this issue?

我觉得问题是localpart()在评估表达式时无法识别。我对吗?有没有解决这个问题的方法?

Thank you

谢谢

1 个解决方案

#1


1  

Here its the quote function that spawns at 2, not the evaluation itself. its like a misusage of spawnat macro.

这里的引用函数是2,而不是评估本身。它就像spawnat宏的错误。

look at this:

看这个:

addprocs(2)
foo = @spawnat 2 quote
  myid()
end
eval(fetch(foo)) # => 1

And to calculate sum over distributed array: (there is nothing to do with @spawnat)

并计算分布式数组的总和:(与@spawnat无关)

# Construct a distributed array dx = [1,2,3,4,5,6,7,8,9,10] # 
dx  = DArray(I->[i for i in I[1]], (10, ))    
dfoo = @spawnat 2 quote
  sum(localpart(dx))
end
eval(fetch(dfoo))==sum(localpart(dx)) # => true

#1


1  

Here its the quote function that spawns at 2, not the evaluation itself. its like a misusage of spawnat macro.

这里的引用函数是2,而不是评估本身。它就像spawnat宏的错误。

look at this:

看这个:

addprocs(2)
foo = @spawnat 2 quote
  myid()
end
eval(fetch(foo)) # => 1

And to calculate sum over distributed array: (there is nothing to do with @spawnat)

并计算分布式数组的总和:(与@spawnat无关)

# Construct a distributed array dx = [1,2,3,4,5,6,7,8,9,10] # 
dx  = DArray(I->[i for i in I[1]], (10, ))    
dfoo = @spawnat 2 quote
  sum(localpart(dx))
end
eval(fetch(dfoo))==sum(localpart(dx)) # => true

相关文章