JuliaFolds2 / OhMyThreads.jl

Simple multithreading in julia
https://juliafolds2.github.io/OhMyThreads.jl/
MIT License
139 stars 9 forks source link

Docs: Mutation examples incorrect/misleading #110

Closed judober closed 4 months ago

judober commented 4 months ago

I played with some examples of the tranlation guide. The ones under Mutation seem incorrect.

Here the code in question:

# 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)
end
  1. tforeach does not work. It errors as i (comming from data) cannot be used to index data. The only working case would be to have data = collect(1:10). I guess it would be best to change the example to be more similar to the @tasks case:
    
    using OhMyThreads: tforeach

tforeach(1:10) do i data[i] = calc(i) end


2. the example with `tmap` works but is misleading as a novice use (like I) would assume `i` to be identical between the examples. But here it is the data and not the index. Also, the output would in general be different (depending on `calc`).
I propose to change it to

using OhMyThreads: tmap!

tmap!(data, 1:10) do i # this kind of aliasing is fine calc(i) end



It might be wiser to replace `1:10` with `eachindex(data)` but this applies to `@tasks` and `@threads` as well.
carstenbauer commented 4 months ago

@judober Hey, thanks for opening an issue. You're absolutely right and I agree with your proposed changes. Mind making a PR?