Skip to content

Commit be8804a

Browse files
pevnakjanfrancu
authored andcommitted
added example with pmap on juliaset
1 parent be22875 commit be8804a

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

docs/src/lecture_10/juliaset_p.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Plots, BenchmarkTools, Distributed
2+
function juliaset_pixel(z₀, c)
3+
z = z₀
4+
for i in 1:255
5+
abs2(z)> 4.0 && return (i - 1)%UInt8
6+
z = z*z + c
7+
end
8+
return UInt8(255)
9+
end
10+
11+
function juliaset_column!(img, c, n, colj, j)
12+
x = -2.0 + (j-1)*4.0/(n-1)
13+
for i in 1:n
14+
y = -2.0 + (i-1)*4.0/(n-1)
15+
@inbounds img[i, colj] = juliaset_pixel(x+im*y, c)
16+
end
17+
nothing
18+
end
19+
20+
function juliaset_range(c, n, columns)
21+
img = Array{UInt8,2}(undef, n, length(columns))
22+
for (colj, j) in enumerate(columns)
23+
juliaset_column!(img, c, n, colj, j)
24+
end
25+
img
26+
end
27+
28+
function juliaset_distributed(x, y, partitions = nworkers(), n = 1000)
29+
c = x + y*im
30+
columns = Iterators.partition(1:n, div(n, partitions))
31+
slices = pmap(cols -> juliaset_range(c, n, cols), columns)
32+
reduce(hcat, slices)
33+
end
34+
35+
@btime juliaset_distributed(-0.79, 0.15)
36+
37+
# frac = juliaset_distributed(-0.79, 0.15)
38+
# plot(heatmap(1:size(frac,1), 1:size(frac,2), frac, color=:Spectral))
39+

docs/src/lecture_10/juliaset_t.jl

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Plots, BenchmarkTools
2+
Threads.nthreads()
3+
function juliaset_pixel(z₀, c)
4+
z = z₀
5+
for i in 1:255
6+
abs2(z)> 4.0 && return (i - 1)%UInt8
7+
z = z*z + c
8+
end
9+
return UInt8(255)
10+
end
11+
12+
function juliaset_column!(img, c, n, j)
13+
x = -2.0 + (j-1)*4.0/(n-1)
14+
for i in 1:n
15+
y = -2.0 + (i-1)*4.0/(n-1)
16+
@inbounds img[i,j] = juliaset_pixel(x+im*y, c)
17+
end
18+
nothing
19+
end
20+
21+
function juliaset_single!(img, c, n)
22+
for j in 1:n
23+
juliaset_column!(img, c, n, j)
24+
end
25+
nothing
26+
end
27+
28+
function juliaset(x, y, n=1000, method = juliaset_single!, extra...)
29+
c = x + y*im
30+
img = Array{UInt8,2}(undef,n,n)
31+
method(img, c, n, extra...)
32+
return img
33+
end
34+
35+
# frac = juliaset(-0.79, 0.15)
36+
# plot(heatmap(1:size(frac,1),1:size(frac,2), frac, color=:Spectral))
37+
38+
39+
@btime juliaset(-0.79, 0.15);
40+
41+
function juliaset_static!(img, c, n)
42+
Threads.@threads for j in 1:n
43+
juliaset_column!(img, c, n, j)
44+
end
45+
nothing
46+
end
47+
48+
@btime juliaset(-0.79, 0.15, 1000, juliaset_static!);
49+
50+
51+
using Folds
52+
function juliaset_folds!(img, c, n)
53+
Folds.foreach(j -> juliaset_column!(img, c, n, j), 1:n)
54+
nothing
55+
end
56+
julia> @btime juliaset(-0.79, 0.15, 1000, juliaset_folds!);
57+
16.267 ms (25 allocations: 978.20 KiB)
58+
59+
function juliaset_folds!(img, c, n, nt)
60+
parts = collect(Iterators.partition(1:n, cld(n, nt)))
61+
Folds.foreach(parts) do ii
62+
foreach(j ->juliaset_column!(img, c, n, j), ii)
63+
end
64+
nothing
65+
end
66+
julia> @btime juliaset(-0.79, 0.15, 1000, (args...) -> juliaset_folds!(args..., 16));
67+
16.716 ms (25 allocations: 978.61 KiB)
68+
69+
70+
using FLoops, FoldsThreads
71+
function juliaset_folds!(img, c, n)
72+
@floop ThreadedEx(basesize = 2) for j in 1:n
73+
juliaset_column!(img, c, n, j)
74+
end
75+
nothing
76+
end
77+
@btime juliaset(-0.79, 0.15, 1000, juliaset_folds!);

docs/src/lecture_10/lecture.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,33 @@ Julia offers different levels of parallel programming
77

88
In this lecture, we will focus mainly on the first two, since SIMD instructions are mainly used for optimization of loops, and task switching is not a true paralelism, but allows to run a different task when one task is waiting for example for IO.
99

10+
## controller / worker model of parallel processes
11+
- the usual remote fetch call
12+
- solve monte carlo
13+
- pmap
1014

15+
- how to set up workers,
16+
+ how to load functions, modules
17+
+ julia -p 16 -L load_my_script.jl
18+
- how to send data / how to define variable on remote process
19+
20+
## Synchronization primitives
21+
- Channels and their guarantees
22+
- How to orchestrate workers by channels
23+
- how to kill the remote process with channel
24+
25+
## Sending data
26+
- Do not send `randn(1000, 1000)`
27+
- Serialization is very time consuming, an efficient converstion to something simple might be wort
28+
- Dict("a" => [1,2,3], "b" = [2,3,4,5]) -> (Array of elements, array of bounds, keys)
1129

1230
## Multi-Threadding
31+
- Locks / lock-free multi-threadding
32+
- Show the effect of different schedullers
33+
- intra-model parallelism
34+
- sucks when operating with Heap
35+
36+
## Julia sets
1337
An example adapted from [Eric Aubanel](http://www.cs.unb.ca/~aubanel/JuliaMultithreadingNotes.html).
1438

1539
For ilustration, we will use Julia set fractals, ad they can be easily paralelized. Some fractals (Julia set, Mandelbrot) are determined by properties of some complex-valued functions. Julia set counts, how many iteration is required for ``f(z)=z^2+c`` to be bigger than two in absolute value, ``|f(z)>=2``. The number of iterations can then be mapped to the pixel's color, which creates a nice visualization we know.
@@ -150,4 +174,13 @@ function juliaset_folds!(img, c, n)
150174
end
151175
julia> @btime juliaset(-0.79, 0.15, 1000, juliaset_folds!);
152176
10.421 ms (3582 allocations: 1.20 MiB)
153-
```
177+
```
178+
179+
### Materials
180+
- http://cecileane.github.io/computingtools/pages/notes1209.html
181+
- https://lucris.lub.lu.se/ws/portalfiles/portal/61129522/julia_parallel.pdf
182+
- https://www.csd.uwo.ca/~mmorenom/cs2101a_moreno/Parallel_computing_with_Julia.pdf
183+
- Threads: https://juliahighperformance.com/code/Chapter09.html
184+
- Processes: https://juliahighperformance.com/code/Chapter10.html
185+
- Alan Adelman uses FLoops in https://www.youtube.com/watch?v=dczkYlOM2sg
186+
- Examples: ?Heat equation? from https://hpc.llnl.gov/training/tutorials/introduction-parallel-computing-tutorial#Examples

0 commit comments

Comments
 (0)