Skip to content

Commit

Permalink
Make using OhMyThreads: ... explicit in documentation (#90)
Browse files Browse the repository at this point in the history
* make using ... explicit in docstrings

* make using ... more clear in docs
  • Loading branch information
carstenbauer authored Mar 18, 2024
1 parent dd4b8e6 commit f3a3838
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Version 0.5.0
- ![Enhancement][badge-enhancement] `SerialScheduler` and `DynamicScheduler` now support the keyword argument `ntasks` as an alias for `nchunks`.
- ![Enhancement][badge-enhancement] Made `@tasks` use `OhMyThreads.WithTaskLocals` automatically as an optimization.
- ![Enhancement][badge-enhancement] Uses of `@local` within `@tasks` no-longer require users to declare the type of the task local value, it can be inferred automatically if a type is not provided.
- ![Enhancement][badge-enhancement] Made `using OhMyThreads: ...` more explicit in examples in the documentation and docstrings.
- ![BREAKING][badge-breaking] The `DynamicScheduler` (default) and the `StaticScheduler` now support a `chunksize` argument to specify the desired size of chunks instead of the number of chunks (`nchunks`). Note that `chunksize` and `nchunks` are mutually exclusive. (This is unlikely to break existing code but technically could because the type parameter has changed from `Bool` to `ChunkingMode`.)
- ![Breaking][badge-breaking] `DynamicScheduler` and `StaticScheduler` don't support `nchunks=0` or `chunksize=0` any longer. Instead, chunking can now be turned off via an explicit new keyword argument `chunking=false`.
- ![BREAKING][badge-breaking] Within a `@tasks` block, task-local values must from now on be defined via `@local` instead of `@init` (renamed).
Expand Down
37 changes: 37 additions & 0 deletions docs/src/translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ This page tries to give a general overview of how to translate patterns written

```julia
# Base.Threads
using Base.Threads: @threads

@threads for i in 1:10
println(i)
end
```

```julia
# OhMyThreads
using OhMyThreads: @tasks

@tasks for i in 1:10
println(i)
end

# or
using OhMyThreads: tforeach

tforeach(1:10) do i
println(i)
Expand All @@ -31,19 +36,24 @@ end

```julia
# Base.Threads
using Base.Threads: @threads

@threads :static for i in 1:10
println(i)
end
```

```julia
# OhMyThreads
using OhMyThreads: @tasks

@tasks for i in 1:10
@set scheduler=:static
println(i)
end

# or
using OhMyThreads: tforeach

tforeach(1:10; scheduler=:static) do i
println(i)
Expand All @@ -54,23 +64,35 @@ end

```julia
# Base.Threads
using Base.Threads: @spawn

@sync for i in 1:10
@spawn println(i)
end
```

```julia
# OhMyThreads
using OhMyThreads: @tasks

@tasks for i in 1:10
@set chunking=false
println(i)
end

# or
using OhMyThreads: tforeach

tforeach(1:10; chunking=false) do i
println(i)
end

# or
using OhMyThreads: @spawn

@sync for i in 1:10
@spawn println(i)
end
```

## Reduction
Expand All @@ -79,6 +101,8 @@ No built-in feature in Base.Threads.

```julia
# Base.Threads: basic manual implementation
using Base.Threads: @spawn

data = rand(10)
chunks_itr = Iterators.partition(data, length(data) ÷ nthreads())
tasks = map(chunks_itr) do chunk
Expand All @@ -89,13 +113,15 @@ reduce(+, fetch.(tasks))

```julia
# OhMyThreads
using OhMyThreads: @tasks
data = rand(10)

@tasks for x in data
@set reducer=+
end

# or
using OhMyThreads: treduce

treduce(+, data)
```
Expand All @@ -107,27 +133,32 @@ treduce(+, data)

```julia
# Base.Threads
using Base.Threads: @threads
data = rand(10)

@threads for i in 1:10
data[i] = calc(i)
end
```

```julia
# OhMyThreads
using OhMyThreads: @tasks
data = rand(10)

@tasks for i in 1:10
data[i] = calc(i)
end

# or
using OhMyThreads: tforeach

tforeach(data) do i
data[i] = calc(i)
end

# or
using OhMyThreads: tmap!

tmap!(data, data) do i # this kind of aliasing is fine
calc(i)
Expand All @@ -141,6 +172,8 @@ end

```julia
# Base.Threads
using Base.Threads: @threads

data = Vector{Float64}(undef, 10)
@threads for i in 1:10
data[i] = calc(i)
Expand All @@ -149,16 +182,20 @@ end

```julia
# OhMyThreads
using OhMyThreads: @tasks

data = @tasks for i in 1:10
@set collect=true
calc(i)
end

# or
using OhMyThreads: tmap

data = tmap(i->calc(i), 1:10)

# or
using OhMyThreads: tcollect

data = tcollect(calc(i) for i in 1:10)
```
12 changes: 12 additions & 0 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ will get undefined results.
## Example:
```
using OhMyThreads: tmapreduce
tmapreduce(√, +, [1, 2, 3, 4, 5])
```
Expand Down Expand Up @@ -60,6 +62,8 @@ will get undefined results.
## Example:
```
using OhMyThreads: treducemap
treducemap(+, √, [1, 2, 3, 4, 5])
```
Expand Down Expand Up @@ -102,6 +106,8 @@ will get undefined results.
## Example:
```
using OhMyThreads: treduce
treduce(+, [1, 2, 3, 4, 5])
```
Expand Down Expand Up @@ -143,6 +149,8 @@ end
## Example:
```
using OhMyThreads: tforeach
tforeach(1:10) do i
println(i^2)
end
Expand Down Expand Up @@ -178,6 +186,8 @@ returned container, and will generally incur fewer allocations than the version
## Example:
```
using OhMyThreads: tmap
tmap(sin, 1:10)
```
Expand Down Expand Up @@ -227,6 +237,8 @@ returned container, and will generally incur fewer allocations than the version
## Example:
```
using OhMyThreads: tcollect
tcollect(sin(i) for i in 1:10)
```
Expand Down
11 changes: 10 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ See also: [`@set`](@ref), [`@local`](@ref)
## Examples
```julia
using OhMyThreads: @tasks
```
```julia
@tasks for i in 1:3
println(i)
Expand Down Expand Up @@ -109,9 +113,14 @@ end
## Examples
```julia
using OhMyThreads: @tasks
using OhMyThreads.Tools: taskid
@tasks for i in 1:10
@set scheduler=DynamicScheduler(; nchunks=2)
@set begin
scheduler=:dynamic
ntasks=2
end
@local x = zeros(3) # TLV
x .+= 1
Expand Down
4 changes: 3 additions & 1 deletion src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Tools
using Base.Threads: nthreads

"""
nthtid(n)
Returns the thread id of the `n`th Julia thread in the `:default` threadpool.
"""
@inline function nthtid(n)
Expand All @@ -18,7 +20,7 @@ end
"""
taskid() :: UInt
Return a `UInt` identifier for the current running [Task](https://docs.julialang.org/en/v1/base/parallel/#Core.Task). This identifier will be unique so long as references to the task it came from still exist.
Return a `UInt` identifier for the current running [Task](https://docs.julialang.org/en/v1/base/parallel/#Core.Task). This identifier will be unique so long as references to the task it came from still exist.
"""
taskid() = objectid(current_task())

Expand Down

0 comments on commit f3a3838

Please sign in to comment.