Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

continue within @tasks #95

Open
Moelf opened this issue Mar 21, 2024 · 9 comments
Open

continue within @tasks #95

Moelf opened this issue Mar 21, 2024 · 9 comments
Labels
documentation Improvements or additions to documentation macro-api

Comments

@Moelf
Copy link

Moelf commented Mar 21, 2024

I think this should be fine, but I guess the @tasks transformation made the Julia parsers unhappy:

function main(tree)
    btag_distribution = Hist1D(; bins = -2:100:2)
    @tasks for evt in tree
        @set scheduler=:static
        if !(evt.trigPassed_HLT_g35_tight_3j25_pf_ftf_PhysicsTLA_L1EM22VHI)
            continue
        end
        btag_score = D_b(evt)
        push!(btag_distribution, btag_score)
    end
end
@carstenbauer
Copy link
Member

@tasks doesn't produce a for loop but maps to the higher order function API, tforeach in your case, which doesn't support continue or break. As for continue, we can maybe support it explicitly (in some cases), e.g. replace it by return nothing. break on the other hand doesn't make sense conceptually (I think). After all, the "loop" runs in parallel so it would lead to non deterministic behavior.

@Moelf
Copy link
Author

Moelf commented Mar 21, 2024

Yeah continue works for @threads so it would be nice to have support for that

@MasonProtter
Copy link
Member

MasonProtter commented Mar 21, 2024

return inside of @tasks (or tmapreduce) does what continue does in a loop.

julia> @tasks for i  1:10
           if isodd(i)
               return
           end
           println(i)
       end
8
2
6
4
10

@carstenbauer
Copy link
Member

I guess the question is whether we should replace continuereturn automatically inside of @tasks.

@MasonProtter
Copy link
Member

I'd lean towards no, because I don't want to be responsible for correctly parsing and understanding arbitrary user code. e.g. if they write

@tasks for x in v
    for i in 1:x
        if cond
            continue
        end
    end
end

then we'd actually want to leave that use of continue alone, lest we break their code in confusing and hard to debug ways.


break is supported in Transducers.jl based stuff via their early termination interface. We probably could support it too, but it'd be a little tricky. It conceptually does make sense in some circumstances. For example, if you had a parallelized any function or a parallelized all function you'd want to break out of those as soon as you find a true or false respectively.

The way Transducers does it is if one task signals that it encountered the early termination signal, then it basically just abandons the other tasks and never fetches from them, signalling to them that they shouldn't spawn more tasks in the future, and then eagerly returns the final result.

@carstenbauer carstenbauer added the documentation Improvements or additions to documentation label Mar 21, 2024
@carstenbauer
Copy link
Member

I've added a documentation label because we should at least mention this (continue in particular) somewhere in the docs/docstrings.

@carstenbauer carstenbauer changed the title break or continue outside loop continue within @tasks Mar 21, 2024
@carstenbauer
Copy link
Member

I've created a new issue for early termination support (e.g. break within @tasks) because the original example here is about continue.

@carstenbauer
Copy link
Member

carstenbauer commented Mar 21, 2024

Another potential way to support continue:

julia> @tasks for i in 1:4
           for i in i # trivial loop to support continue (could be auto inserted)
               if i > 2
                   continue
               end
               println(i)
           end
       end
2
1

julia> tforeach(1:4) do i
           for i in i # trivial loop to support continue (could be auto inserted)
               if i > 2
                   continue
               end
               println(i)
           end
       end
2
1

@Moelf
Copy link
Author

Moelf commented May 27, 2024

can we insert for i in i automatically? it's quite confusing for users to look at a loop with a bunch of returns and try to tell them they are not return just continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation macro-api
Projects
None yet
Development

No branches or pull requests

3 participants