我试图运行看起来像这样的东西:
y = @parallel (min) for i in collection
f(i)
end
其中f(i)
是一个基本上是while
循环的函数,它计算完成条件所需的迭代次数。开始时,终止条件之一是预定的迭代次数n
。但是,如果f(i)
的回报率低于n
,那么理想情况下,我希望将n
替换为f(i)
的值(例如,因为我正在寻找最小值f(i)
1}},如果f(j)
为m
,我希望所有其他循环停止检查它们是否达到m
次迭代)。
我对并行计算很陌生,所以我可能会误解documentation,但我认为我应该可以这样做:
x = Channel{Int64}(1)
put!(x,n)
y = @parallel (min) for i in collection
f(i,x)
end
close(x)
我已修改f
以获取Channel
参数,现在它看起来像这样:
@everywhere function f(item,chan)
going = true
count = 0
while (going)
going = false
# perform some operations
if (count < fetch(chan) && !conditions_met())
# conditions_met checks the other termination conditions
going = true
count += 1
end
end
count += 1
if (count < fetch(chan))
take!(chan)
put!(chan,count)
end
return count
end
如果我将第一个count < fetch(chan)
替换为count < n
并删除其他if
阻止/ Channel
代码,则脚本运行正常。但是,由于n
将比最小f(i)
大几个数量级,如果我可以像我所描述的那样做,它会显着加快计算速度。这是我应该做的事情,如果是这样,我是否正确接近这个?
现在我遇到以下错误(运行4次触发):
ERROR (unhandled task failure): On worker 3:
cannot resize array with shared data
in shift! at array.jl:501
in take! at channels.jl:54
in f at /home/michael/Documents/julia/script.jl:98
[inlined code] from /home/michael/Documents/julia/script.jl:126
in anonymous at no file:0
in anonymous at multi.jl:913
in run_work_thunk at multi.jl:651
[inlined code] from multi.jl:913
in anonymous at task.jl:63
in remotecall_fetch at multi.jl:737
in remotecall_fetch at multi.jl:740
in anonymous at multi.jl:1519
ERROR: LoadError: On worker 2:
cannot resize array with shared data
in shift! at array.jl:501
in take! at channels.jl:54
in f at /home/michael/Documents/julia/script.jl:98
[inlined code] from /home/michael/Documents/julia/script.jl:126
in anonymous at no file:0
in anonymous at multi.jl:913
in run_work_thunk at multi.jl:651
[inlined code] from multi.jl:913
in anonymous at task.jl:63
in preduce at multi.jl:1523
[inlined code] from multi.jl:1532
in anonymous at expr.jl:113
[inlined code] from /home/michael/Documents/julia/script.jl:125
in anonymous at no file:0
while loading /home/michael/Documents/julia/script.jl, in expression starting on line 121
ERROR (unhandled task failure): On worker 4:
cannot resize array with shared data
in shift! at array.jl:501
in take! at channels.jl:54
in f at /home/michael/Documents/julia/script.jl:98
[inlined code] from /home/michael/Documents/julia/script.jl:126
in anonymous at no file:0
in anonymous at multi.jl:913
in run_work_thunk at multi.jl:651
[inlined code] from multi.jl:913
in anonymous at task.jl:63
in remotecall_fetch at multi.jl:737
in remotecall_fetch at multi.jl:740
in anonymous at multi.jl:1519
ERROR (unhandled task failure): On worker 5:
cannot resize array with shared data
in shift! at array.jl:501
in take! at channels.jl:54
in f at /home/michael/Documents/julia/script.jl:98
[inlined code] from /home/michael/Documents/julia/script.jl:126
in anonymous at no file:0
in anonymous at multi.jl:913
in run_work_thunk at multi.jl:651
[inlined code] from multi.jl:913
in anonymous at task.jl:63
in remotecall_fetch at multi.jl:737
in remotecall_fetch at multi.jl:740
in anonymous at multi.jl:1519
其中,行98
是函数定义中的take!(chan)
语句,而行126
中的行f(i,x)
是并行for
循环中的 public class arrayTest {
public static void main(String[] args){
Integer[] list1 = {12, 43, 56, 72};
Double[] list2 = {12.5, 45.6, 62.4, 65.8};
String[] list3 = {"1", "2", "3"};
printArray(list1);
printArray(list2);
printArray(list3);
}
public static void printArray(Object[] list){
Object o = list;
System.out.println(o);
}
。
答案 0 :(得分:2)
Channel
为异步通信实现类似CSP的语义,但它们没有跨并行进程共享的自动机制。您出于此目的需要使用RemoteRef
:http://docs.julialang.org/en/release-0.4/manual/parallel-computing/#remoterefs-and-abstractchannels