Skip to content

Commit

Permalink
translation page for docs
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbauer committed Feb 2, 2024
1 parent f6d17f5 commit e0a4cff
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ makedocs(;
doctest = false,
pages = [
"OhMyThreads" => "index.md",
# "Getting Started" => "examples/getting_started.md",
"Translation" => "translation.md",
"Examples" => [
"Parallel Monte Carlo" => "examples/mc/mc.md",
"Julia Set" => "examples/juliaset/juliaset.md",
Expand Down
119 changes: 119 additions & 0 deletions docs/src/translation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Translation

## Basic

### `@threads`

```julia
# Base.Threads
@threads for i in 1:10
println(i)
end
```

```julia
# OhMyThreads
tforeach(1:10) do i
println(i)
end
```

#### `:static` scheduling

```julia
# Base.Threads
@threads :static for i in 1:10
println(i)
end
```

```julia
# OhMyThreads
tforeach(1:10; schedule=:static) do i
println(i)
end
```

### `@spawn`

```julia
# Base.Threads
@sync for i in 1:10
@spawn println(i)
end
```

```julia
# OhMyThreads
tforeach(1:10; nchunks=10) do i
println(i)
end
```

## Reduction

No built-in feature in Base.Threads.

```julia
# Base.Threads: basic manual implementation
data = rand(10)
chunks_itr = Iterators.partition(data, length(data) ÷ nthreads())
tasks = map(chunks_itr) do chunk
@spawn reduce(+, chunk)
end
reduce(+, fetch.(tasks))
```

```julia
# OhMyThreads
data = rand(10)
treduce(+, data)
```

## Mutation

TODO: Remark why one has to be careful here (e.g. false sharing).

```julia
# Base.Threads
data = rand(10)
@threads for i in 1:10
data[i] = calc(i)
end
```

```julia
# OhMyThreads: Variant 1
data = rand(10)
tforeach(data) do i
data[i] = calc(i)
end
```

```julia
# OhMyThreads: Variant 2
data = rand(10)
tmap!(data, data) do i # TODO: comment on aliasing
calc(i)
end
```

## Parallel initialization

```julia
# Base.Threads
data = Vector{Float64}(undef, 10)
@threads for i in 1:10
data[i] = calc(i)
end
```

```julia
# OhMyThreads: Variant 1
data = tmap(i->calc(i), 1:10)
```

```julia
# OhMyThreads: Variant 2
data = tcollect(calc(i) for i in 1:10)
```

0 comments on commit e0a4cff

Please sign in to comment.