Skip to content

Commit

Permalink
make using ... more clear in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbauer committed Mar 18, 2024
1 parent aeef73e commit cbd2337
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 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)
```

0 comments on commit cbd2337

Please sign in to comment.