LoginSignup
6
12

More than 3 years have passed since last update.

Juliaで超単純にマルチプロセス

Last updated at Posted at 2019-08-15

最低限系備忘録

  • Julia速いと言われるけど、CPU1コアしか使っていないのを見ると損している気分。なんとかしたい。

  • Rubyの parallelmap みたいに、お手軽並列実行をやる。

前準備

実行時にプログレスバーをみたいので、ProgressMeter.jl をインストールしておく。

Pkg.add("ProgressMeter")

addprocess で使用するコア数を指定する。

using Distributed
Distributed.addprocs(15)

子プロセスに持っていくパッケージに全部 @everywhere をつける。たとえば

@everywhere using DataFrames
@everywhere using Statistics
@everywhere using CSV
@everywhere using ProgressMeter
using Colors
using Glob
...

子プロセスに持っていくfunctionにも全部 @everywhere をつける。

@everywhere function f(x,y)
    x + y
end

並列実行

pmap もしくは progress_pmap を使う。Rubyから来た人なので、こういう書き方があるのは助かる。

result = progress_pmap(cats) do cat
  ...
end

htop を開いてjuliaが本当にマルチコアで動いているか確認。

6
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
12