Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
For loop Julia-是否可以使用进度条和线程。@线程在同一for循环中?_For Loop_Parallel Processing_Progress Bar_Julia - Fatal编程技术网

For loop Julia-是否可以使用进度条和线程。@线程在同一for循环中?

For loop Julia-是否可以使用进度条和线程。@线程在同一for循环中?,for-loop,parallel-processing,progress-bar,julia,For Loop,Parallel Processing,Progress Bar,Julia,假设我有这个for循环: for i in 1:100000 1+1 # Let's do some long computations here end 这两个for循环有效: Threads.@threads for i in 1:100000 1+1 # Let's do some long computations here end using ProgressMeter @showprogress 1 "Computing..." for i in 1:

假设我有这个for循环:

for i in 1:100000
   1+1 
   # Let's do some long computations here
end
这两个for循环有效:

Threads.@threads for i in 1:100000
   1+1 
   # Let's do some long computations here
end

using ProgressMeter
@showprogress 1 "Computing..." for i in 1:100000
   1+1 
   # Let's do some long computations here
end
但以下任何一项都不起作用:

@showprogress 1 "Computing..." Threads.@threads for i in 1:100000
   1+1 
   # Let's do some long computations here
end

using ProgressMeter
Threads.@threads @showprogress 1 "Computing..." for i in 1:100000
   1+1 
   # Let's do some long computations here
end

那么,在Julia中,是否可以在for循环和进度条中实现并行性呢?

这段代码运行良好:

using ProgressMeter
N = 200
p = Progress(N);
update!(p,0)
jj = Threads.Atomic{Int}(0)

l = Threads.SpinLock()
Threads.@threads for i in 1:N
   sum(rand(10_000_000)) # some big computation
   Threads.atomic_add!(jj, 1)
   Threads.lock(l)
   update!(p, jj[])
   Threads.unlock(l)  
end

如果您不想使用锁,您也可以考虑只在第一个线程上更新(但是在这种情况下进度条运行得不太顺利):


谢谢你的回答。不幸的是,当我将它添加到for循环中时,Julia会在第一次迭代时停止,而它以前是工作的。令人惊讶的是,Julia返回shell提示符:Julia控制台崩溃。更令人惊讶的是,Julia没有留下任何错误、跟踪或消息,因此很难知道它为何崩溃。。。这是我第一次在Julia中编写代码,我总是至少有一条错误消息。很可能在多线程循环中,您有一个函数在执行时未编译,并且运行到多线程编译过程中。这总是让茱莉亚很难过。首先检查我的示例是否有效(应该)?如果可以,请尝试在单个线程中运行MultihtReaded循环中的代码一次,这将导致代码编译。然后运行主lopp。不幸的是,多线程支持是实验性的,有很多技巧可以让它工作。我总是建议改用多处理(如果可能的话)。
Threads.threadid() == 1 && update!(p, jj[])