Julia中的并行计算-在多个内核上运行简单的for循环 [英] Parallel computing in Julia - running a simple for-loop on multiple cores
问题描述
对于初学者来说,我不得不说我对并行计算是完全陌生的(并且对计算机科学几乎一无所知),所以我对诸如工人"或过程"之类的东西的理解实际上是非常有限的.但是,我确实有一个问题,关于运行一个简单的for循环,该循环大概在并行迭代之间没有依赖性.
For starters, I have to say I'm completely new to parallel computing (and know close to nothing about computer science), so my understanding of what things like "workers" or "processes" actually are is very limited. I do however have a question about running a simple for-loop that presumably has no dependencies between the iterations in parallel.
假设我要执行以下操作:
Let's say I wanted to do the following:
for N in 1:5:20
println("The N of this iteration in $N")
end
如果我只是希望这些消息出现在屏幕上,并且出现的顺序无关紧要,那么如何在Julia 0.6中实现这一目标,并在Julia 0.7(以及1.0)中作为将来的参考呢?
If I simply wanted these messages to appear on screen and the order of appearance didn't matter, how could one achieve this in Julia 0.6, and for future reference in Julia 0.7 (and therefore 1.0)?
推荐答案
分布式处理
以例如julia -p 4
如果要使用4 cpus(或使用功能addprocs(4)
).在Julia 1.x中,按如下所示进行并行循环:
Distributed Processing
Start julia with e.g. julia -p 4
if you want to use 4 cpus (or use the function addprocs(4)
). In Julia 1.x, you make a parallel loop as following:
using Distributed
@distributed for N in 1:5:20
println("The N of this iteration in $N")
end
请注意,每个进程默认都有自己的变量. 对于任何严肃的工作,请查看手册 https://docs.julialang.org/en/v1.4/manual/parallel-computing/,尤其是有关SharedArrays的部分.
Note that every process have its own variables per default. For any serious work, have a look at the manual https://docs.julialang.org/en/v1.4/manual/parallel-computing/, in particular the section about SharedArrays.
用于分布式计算的另一个选项是函数pmap
或程序包MPI.jl
.
Another option for distributed computing are the function pmap
or the package MPI.jl
.
从Julia 1.3开始,您还可以使用wueli提到的Threads.
以例如开始朱莉娅julia -t 4
使用4个线程.另外,您可以在启动julia之前或设置环境变量JULIA_NUM_THREADS
.
例如Linux/Mac OS:
Since Julia 1.3, you can also use Threads as noted by wueli.
Start julia with e.g. julia -t 4
to use 4 threads. Alternatively you can or set the environment variable JULIA_NUM_THREADS
before starting julia.
For example Linux/Mac OS:
export JULIA_NUM_THREADS=4
在Windows中,您可以在cmd提示符下使用set JULIA_NUM_THREADS 4
.
In windows, you can use set JULIA_NUM_THREADS 4
in the cmd prompt.
然后在茱莉亚:
Threads.@threads for N = 1::20
println("N = $N (thread $(Threads.threadid()) of out $(Threads.nthreads()))")
end
在上面的示例中,假定所有CPU都可以访问共享内存(例如,"OpenMP样式"并行性),这是多核CPU的常见情况.
All CPUs are assumed to have access to shared memory in the examples above (e.g. "OpenMP style" parallelism) which is the common case for multi-core CPUs.
这篇关于Julia中的并行计算-在多个内核上运行简单的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!