JuliaFolds2 / OhMyThreads.jl

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

Add special region support to `@tasks` #93

Closed carstenbauer closed 7 months ago

carstenbauer commented 7 months ago

Addresses https://github.com/JuliaFolds2/OhMyThreads.jl/issues/73

(We don't implement implicit barriers at the end here, or options to turn those on or off. This should go into a new PR targeting #74)

carstenbauer commented 7 months ago

Example:

@tasks for i in 1:10
    @set ntasks = 10

    println(i, ": before")
    @section :critical begin
        println(i, ": one task at a time")
        sleep(1)
    end
    println(i, ": after")
end

@MasonProtter, how do you feel about this syntax, i.e. @section kind begin...end? Alternatively, we could name the "macro" @region but I think I like @section more.

carstenbauer commented 7 months ago
julia> @tasks for i in 1:10
           @set ntasks = 10

           println(i, ": before")
           @section :single begin
               println(i, ": only printed by a single task")
               sleep(1)
           end
           println(i, ": after")
       end
4: before
10: before
9: before
1: before
9: after
1: after
5: before
3: before
7: before
8: before
7: after
8: after
3: after
4: only printed by a single task
6: before
10: after
6: after
2: before
2: after
5: after
4: after
MasonProtter commented 7 months ago

Honestly, as someone who has never used OpenMP, I don't really find any of these names informative or obvious.

I think "region" is a bit better than section for me, but I find "single" and "critical" are both quite bad. On the other hand, there's advantages to using standardized terminology from another piece of software, and I don't anticipate myself really needing these things, so I'm not sure I have particularly strong feelings.

carstenbauer commented 7 months ago

Honestly, as someone who has never used OpenMP, I don't really find any of these names informative or obvious.

Note that critical section (or region) isn't an OpenMP term but just standard terminology. As for "single", isn't that as obvious as it gets? The section will be run by a single task only.

What would you suggest instead?

MasonProtter commented 7 months ago

Yeah if it's widespread terminology, I'm hesitant to go against it, but my reflex would be to try and give it a more descriptive name like e.g. @one_at_a_time begin ... end and @only_one_task begin ... end. This is verbose, but to be fair, it's also fewer characters than @section :critical begin ... end and @section :single begin ... end.

I don't feel particularly strongly about this, these are just my thoughts.

carstenbauer commented 7 months ago

I like more descriptive names, so that's a good point (irrespective of standard terminology). Brainstorming:

@one_task_only and @one_task_at_a_time

@only_one and @one_by_one

@single_task and @each_task_alone

@single and @each_alone

I must say that @only_one and @one_by_one is growing on me because it's more descriptive but also quite short.

carstenbauer commented 7 months ago

With the new (tentative) macro names:

julia> using OhMyThreads: @tasks

julia> @tasks for i in 1:10
           @set ntasks = 10

           println(i, ": before")

           @one_by_one begin
               println(i, ": printed by all tasks but one after another")
               sleep(0.5)
           end

           @only_one begin
               println(i, ": only printed by a single task")
               sleep(1)
           end

           println(i, ": after")
       end
1: before
10: before
4: before
2: before
9: before
5: before
3: before
6: before
8: before
7: before
1: printed by all tasks but one after another
1: only printed by a single task
10: printed by all tasks but one after another
10: after
5: printed by all tasks but one after another
1: after
5: after
3: printed by all tasks but one after another
3: after
4: printed by all tasks but one after another
4: after
8: printed by all tasks but one after another
8: after
2: printed by all tasks but one after another
2: after
6: printed by all tasks but one after another
6: after
7: printed by all tasks but one after another
7: after
9: printed by all tasks but one after another
9: after