Skip to content

Commit 6535fe6

Browse files
committed
move disable_polyester_threads to PolyesterWeave
1 parent fe7ef9e commit 6535fe6

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BitTwiddlingConvenienceFunctions = "0.1"
2121
CPUSummary = "0.1.2 - 0.1.8, 0.1.11"
2222
IfElse = "0.1"
2323
ManualMemory = "0.1.3"
24-
PolyesterWeave = "0.1"
24+
PolyesterWeave = "0.1.7"
2525
Requires = "1"
2626
Static = "0.7"
2727
StrideArraysCore = "0.3.11"

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,25 @@ Float16[83.0, 90.0, 27.0, 65.0]
452452
453453
## Disabling Polyester threads
454454
455-
When running many repetitions of a Polyester-multithreaded function (e.g. in an embarrassingly parallel problem that repeatedly executes a small already Polyester-multithreaded function), it can be beneficial to disable Polyester (the inner multithreaded loop) and multithread only at the outer level (e.g. with `Base.Threads`). This can be done with the `disable_polyester_threads` context manager. In the expandable section below you can see example benchmarks.
455+
When running many repetitions of a Polyester-multithreaded function (e.g. in an embarrassingly parallel problem that repeatedly executes a small already Polyester-multithreaded function), it can be beneficial to disable Polyester (the inner multithreaded loop) and multithread only at the outer level (e.g. with `Base.Threads`). This can be done with the `disable_polyester_threads` context manager. In the expandable section below you can see examples with benchmarks.
456+
457+
It is best to call `disable_polyester_threads` only once, before any `@thread` uses happen, to avoid overhead. E.g. best to do it as:
458+
```julia
459+
disable_polyester_threads() do
460+
@threads for i in 1:n
461+
f()
462+
end
463+
end
464+
```
465+
instead of doing it in the following unnecessarily slow manner:
466+
```julia
467+
@threads for i in 1:n # DO NOT DO THIS
468+
disable_polyester_threads() do # IT HAS UNNECESSARY OVERHEAD
469+
f()
470+
end
471+
end
472+
```
473+
456474
457475
<details>
458476
<summary>Benchmarks of nested multi-threading with Polyester</summary>
@@ -472,7 +490,9 @@ x = rand(size(y)...);
472490
@btime sequential_thread($x,$y) # 49.373 ms (196 allocations: 18.25 KiB)
473491

474492
@btime threads_of_polyester($x,$y) # 78.828 ms (58 allocations: 4.84 KiB)
493+
# the following is a purposefully suboptimal way to disable threads
475494
@btime threads_of_polyester_inner_disable($x,$y) # 70.182 ms (47 allocations: 4.50 KiB)
495+
# the following is a good way to disable threads (the disable call happening once in the outer scope)
476496
@btime Polyester.disable_polyester_threads() do; threads_of_polyester($x,$y) end; # 71.141 ms (47 allocations: 4.50 KiB)
477497
@btime threads_of_sequential($x,$y) # 70.857 ms (46 allocations: 4.47 KiB)
478498
@btime threads_of_thread($x,$y) # 45.116 ms (219 allocations: 22.00 KiB)
@@ -491,7 +511,9 @@ x = rand(size(y)...);
491511
@btime sequential_thread($x,$y) # 9.296 ms (49002 allocations: 4.46 MiB)
492512

493513
@btime threads_of_polyester($x,$y) # 2.090 ms (42 allocations: 4.34 KiB)
514+
# the following is a purposefully suboptimal way to disable threads
494515
@btime threads_of_polyester_inner_disable($x,$y) # 1.065 ms (42 allocations: 4.34 KiB)
516+
# the following is a good way to disable threads (the disable call happening once in the outer scope)
495517
@btime Polyester.disable_polyester_threads() do; threads_of_polyester($x,$y) end; # 997.918 μs (49 allocations: 4.56 KiB)
496518
@btime threads_of_sequential($x,$y) # 1.057 ms (48 allocations: 4.53 KiB)
497519
@btime threads_of_thread($x,$y) # 4.105 ms (42059 allocations: 4.25 MiB)
@@ -541,6 +563,9 @@ function threads_of_polyester(x,y)
541563
end
542564

543565
function threads_of_polyester_inner_disable(x,y)
566+
# XXX This is a bad way to disable Polyester threads as
567+
# it causes unnecessary overhead for each @threads thread.
568+
# See the benchmarks above for a better way.
544569
@threads for j axes(x,2)
545570
Polyester.disable_polyester_threads() do
546571
inner_polyester(x,y,j)
@@ -566,4 +591,19 @@ function threads_of_sequential(x,y)
566591
end
567592
end
568593
```
594+
Benchmarks executed on:
595+
```
596+
Julia Version 1.9.0-DEV.998
597+
Commit e1739aa42a1 (2022-07-18 10:27 UTC)
598+
Platform Info:
599+
OS: Linux (x86_64-linux-gnu)
600+
CPU: 16 × AMD Ryzen 7 1700 Eight-Core Processor
601+
WORD_SIZE: 64
602+
LIBM: libopenlibm
603+
LLVM: libLLVM-14.0.5 (ORCJIT, znver1)
604+
Threads: 8 on 16 virtual cores
605+
Environment:
606+
JULIA_EDITOR = code
607+
JULIA_NUM_THREADS = 8
608+
```
569609
</details>

src/Polyester.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ using ManualMemory: Reference
88
using Static
99
using Requires
1010
using PolyesterWeave:
11-
request_threads, free_threads!, mask, UnsignedIteratorEarlyStop, assume
11+
request_threads, free_threads!, mask, UnsignedIteratorEarlyStop, assume,
12+
disable_polyester_threads
1213
using CPUSummary: num_threads, num_cores
1314

1415
export batch, @batch, num_threads, disable_polyester_threads
1516

1617

1718
include("batch.jl")
1819
include("closure.jl")
19-
include("utility.jl")
2020

2121
# y = rand(1)
2222
# x = rand(1)

src/utility.jl

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)