中的并行计算——在多核上运行一个简单的

中的并行计算——在多核上运行一个简单的

本文介绍了Julia 中的并行计算——在多核上运行一个简单的 for 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

对于初学者,我不得不说我对并行计算完全陌生(对计算机科学几乎一无所知),所以我对工人"或进程"等事物的实际理解非常有限.但是,我确实有一个关于运行一个简单的 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.

假设我想做以下事情:

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 开头,例如julia -p 4 如果你想使用 4 个 CPU(或使用函数 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 所述的线程.用例如开始朱莉娅julia -t 4 使用 4 个线程.或者,您可以在启动 julia 之前设置环境变量 JULIA_NUM_THREADS.例如 Linux/Mac 操作系统:

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 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:29