Polyester.jl icon indicating copy to clipboard operation
Polyester.jl copied to clipboard

Interrupting Julia disables `@batch` for this session

Open efaulhaber opened this issue 4 years ago • 10 comments

MWE:

using Polyester

function foo()
    @batch for i in 1:4
        println(Threads.threadid())
        sleep(1)
    end
end

When running this in a new REPL (Julia started with two threads, julia -t 2), the output of foo() is, as expected:

julia> foo2()
1
2
1
2

I get the same output when running this multiple times. Now, when I interrupt Julia before the function is done (that's what the sleep is for), @batch only uses one thread until I restart Julia:

julia> foo2()
1
2
ERROR: InterruptException:
Stacktrace:
  [1] poptask(W::Base.InvasiveLinkedListSynchronized{Task})
    @ Base .\task.jl:760
  [2] wait
    @ .\task.jl:768 [inlined]
  [3] wait(c::Base.GenericCondition{Base.Threads.SpinLock})
    @ Base .\condition.jl:106
  [4] _trywait(t::Timer)
    @ Base .\asyncevent.jl:111
  [5] wait
    @ .\asyncevent.jl:129 [inlined]
  [6] sleep(sec::Int64)
    @ Base .\asyncevent.jl:214
  [7] macro expansion
    @ C:\Users\Erik\git\Trixi.jl\foo.jl:18 [inlined]
  [8] #7
    @ C:\Users\Erik\.julia\packages\Polyester\0DPCU\src\closure.jl:168 [inlined]
  [9] macro expansion
    @ C:\Users\Erik\.julia\packages\Polyester\0DPCU\src\batch.jl:79 [inlined]
 [10] _batch_no_reserve
    @ C:\Users\Erik\.julia\packages\Polyester\0DPCU\src\batch.jl:53 [inlined]
 [11] batch
    @ C:\Users\Erik\.julia\packages\Polyester\0DPCU\src\batch.jl:195 [inlined]
 [12] macro expansion
    @ C:\Users\Erik\.julia\packages\Polyester\0DPCU\src\closure.jl:164 [inlined]
 [13] foo2()
    @ Main C:\Users\Erik\git\Trixi.jl\foo.jl:16
 [14] top-level scope
    @ REPL[2]:1

julia> 2
julia> foo2()
1
1
1
1

julia> foo2()
1
1
1
1

efaulhaber avatar Jun 29 '21 21:06 efaulhaber

You can reset the state via

using Polyester, ThreadingUtilities
Polyester.reset_workers!()
ThreadingUtilities.reinitialize_tasks!()

chriselrod avatar Jun 30 '21 03:06 chriselrod

Yes, that works! I guess that's not supposed to happen, right? One shouldn't need to reset the state manually.

efaulhaber avatar Jun 30 '21 14:06 efaulhaber

Was reset_workers! removed in the recent 0.5.1 update? I still need to reset the state manually from time to time but can't seem to access it anymore.

jlchan avatar Sep 15 '21 21:09 jlchan

It is now in PolyesterWeave.jl

chriselrod avatar Sep 15 '21 23:09 chriselrod

Thanks!

jlchan avatar Sep 15 '21 23:09 jlchan

Just so its visible for others, this is what I'm using with PolyesterWeave 0.1 and ThreadingUtilities 0.4.6:

PolyesterWeave.reset_workers!()
for i = 1:Threads.nthreads()-1; ThreadingUtilities.initialize_task(i); end

jlchan avatar Sep 23 '21 04:09 jlchan