我正在尝试在不同的进程中产生和计算表达式。表达式包含分布式数组的局部部分,这似乎会产生问题。例如,
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))不出所料,
Out [ ]: 15但是,如果我尝试用分布式数组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我感觉问题出在localpart(),它在计算表达式时无法识别。我说的对吗?有没有办法绕过这个问题?
谢谢
发布于 2015-11-15 01:55:19
这里是在2处产生的quote函数,而不是计算本身。这就像是对spawnat宏的滥用。
看看这个:
addprocs(2)
foo = @spawnat 2 quote
myid()
end
eval(fetch(foo)) # => 1并计算分布式数组上的sum:(与@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)) # => truehttps://stackoverflow.com/questions/28959341
复制相似问题