Skip to content

Commit 1182356

Browse files
committed
Merge branch 'master' of github.com:JuliaTeachingCTU/Scientific-Programming-in-Julia
2 parents b75eeb8 + 71b8ba8 commit 1182356

File tree

10 files changed

+1649
-15
lines changed

10 files changed

+1649
-15
lines changed

docs/make.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ lecture_09 = [
9393
]
9494

9595
lecture_10 = [
96+
"Lecture" => "./lecture_10/lecture.md"
97+
"Lab" => "./lecture_10/lab.md"
98+
"Homework" => "./lecture_10/hw.md"
9699
]
97100

98101
lecture_11 = [

docs/src/lecture_09/lecture.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,11 @@ Let's now use the IRTools to insert the timing statements into the code for `foo
491491
using IRTools: xcall, insert!, insertafter!
492492

493493
ir = @code_ir foo(1.0, 1.0)
494-
for b in IRTools.blocks(ir)
495-
for (v, ex) in b
496-
if timable(ex.expr)
497-
fname = exportname(ex.expr)
498-
insert!(b, v, xcall(LoggingProfiler, :record_start, fname))
499-
insertafter!(b, v, xcall(LoggingProfiler, :record_end, fname))
500-
end
494+
for (v, ex) in ir
495+
if timable(ex.expr)
496+
fname = exportname(ex.expr)
497+
insert!(ir, v, xcall(LoggingProfiler, :record_start, fname))
498+
insertafter!(ir, v, xcall(LoggingProfiler, :record_end, fname))
501499
end
502500
end
503501

@@ -531,13 +529,11 @@ profile_fun(f::Core.Builtin, args...) = f(args...)
531529

532530
@dynamo function profile_fun(f, args...)
533531
ir = IRTools.Inner.IR(f, args...)
534-
for b in IRTools.blocks(ir)
535-
for (v, ex) in b
536-
if timable(ex.expr)
537-
fname = exportname(ex.expr)
538-
insert!(b, v, xcall(Main, :record_start, fname))
539-
insertafter!(b, v, xcall(Main, :record_end, fname))
540-
end
532+
for (v, ex) in ir
533+
if timable(ex.expr)
534+
fname = exportname(ex.expr)
535+
insert!(ir, v, xcall(LoggingProfiler, :record_start, fname))
536+
insertafter!(ir, v, xcall(LoggingProfiler, :record_end, fname))
541537
end
542538
end
543539
for (x, st) in ir
171 KB
Loading

docs/src/lecture_10/hw.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [Homework 9: Something parallel](@id hw09)
2+
Will be published somewhere during the weekend.

docs/src/lecture_10/juliaset_p.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Pkg
2+
Pkg.activate(@__DIR__)
3+
using Plots
4+
using BenchmarkTools
5+
using Distributed
6+
using SharedArrays
7+
8+
function juliaset_pixel(z₀, c)
9+
z = z₀
10+
for i in 1:255
11+
abs2(z)> 4.0 && return (i - 1)%UInt8
12+
z = z*z + c
13+
end
14+
return UInt8(255)
15+
end
16+
17+
function juliaset_column!(img, c, n, colj, j)
18+
x = -2.0 + (j-1)*4.0/(n-1)
19+
for i in 1:n
20+
y = -2.0 + (i-1)*4.0/(n-1)
21+
@inbounds img[i, colj] = juliaset_pixel(x+im*y, c)
22+
end
23+
nothing
24+
end
25+
26+
function juliaset_columns(c, n, columns)
27+
img = Array{UInt8,2}(undef, n, length(columns))
28+
for (colj, j) in enumerate(columns)
29+
juliaset_column!(img, c, n, colj, j)
30+
end
31+
img
32+
end
33+
34+
function juliaset_distributed(x, y, partitions = nworkers(), n = 1000)
35+
c = x + y*im
36+
columns = Iterators.partition(1:n, div(n, partitions))
37+
slices = pmap(cols -> juliaset_columns(c, n, cols), columns)
38+
reduce(hcat, slices)
39+
end
40+
41+
# @btime juliaset_distributed(-0.79, 0.15)
42+
43+
# frac = juliaset_distributed(-0.79, 0.15)
44+
# plot(heatmap(1:size(frac,1), 1:size(frac,2), frac, color=:Spectral))
45+
46+
47+
####
48+
# Let's work out the shared array approach
49+
####
50+
function juliaset_column!(img, c, n, j)
51+
x = -2.0 + (j-1)*4.0/(n-1)
52+
for i in 1:n
53+
y = -2.0 + (i-1)*4.0/(n-1)
54+
@inbounds img[i, j] = juliaset_pixel(x+im*y, c)
55+
end
56+
nothing
57+
end
58+
59+
function juliaset_range!(img, c, n, columns)
60+
for j in columns
61+
juliaset_column!(img, c, n, j)
62+
end
63+
nothing
64+
end
65+
66+
function juliaset_shared(x, y, partitions = nworkers(), n = 1000)
67+
c = x + y*im
68+
columns = Iterators.partition(1:n, div(n, partitions))
69+
img = SharedArray{UInt8,2}((n, n))
70+
slices = pmap(cols -> juliaset_range!(img, c, n, cols), columns)
71+
img
72+
end
73+
74+
# juliaset_shared(-0.79, 0.15)
75+
# juliaset_shared(-0.79, 0.15, 16)

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!);

0 commit comments

Comments
 (0)