@@ -20,11 +20,12 @@ import ..Dagger: @dagdebug, @safe_lock_spin1, @maybelog, @take_or_alloc!
2020import DataStructures: PriorityQueue, enqueue!, dequeue_pair!, peek
2121
2222import .. Dagger: ReusableCache, ReusableLinkedList, ReusableDict
23- import .. Dagger: @reusable , @reusable_dict , @reusable_vector , @reusable_tasks
23+ import .. Dagger: @reusable , @reusable_dict , @reusable_vector , @reusable_tasks , @reuse_scope , @reuse_defer_cleanup
2424
2525import TimespanLogging
2626
2727import TaskLocalValues: TaskLocalValue
28+ import ScopedValues: @with
2829
2930const OneToMany = Dict{Thunk, Set{Thunk}}
3031
@@ -530,7 +531,7 @@ struct ScheduleTaskSpec
530531 est_alloc_util:: UInt64
531532 est_occupancy:: UInt32
532533end
533- function schedule! (ctx, state, sch_options, procs= procs_to_use (ctx, sch_options))
534+ @reuse_scope function schedule! (ctx, state, sch_options, procs= procs_to_use (ctx, sch_options))
534535 lock (state. lock) do
535536 safepoint (state)
536537
@@ -541,8 +542,9 @@ function schedule!(ctx, state, sch_options, procs=procs_to_use(ctx, sch_options)
541542
542543 # Schedule tasks
543544 to_fire = @reusable_dict :schedule!_to_fire ScheduleTaskLocation Vector{ScheduleTaskSpec} ScheduleTaskLocation (OSProc (), OSProc ()) ScheduleTaskSpec[] 1024
545+ to_fire_cleanup = @reuse_defer_cleanup empty! (to_fire)
544546 failed_scheduling = @reusable_vector :schedule!_failed_scheduling Union{Thunk,Nothing} nothing 32
545-
547+ failed_scheduling_cleanup = @reuse_defer_cleanup empty! (failed_scheduling)
546548 # Select a new task and get its options
547549 task = nothing
548550 @label pop_task
@@ -611,21 +613,20 @@ function schedule!(ctx, state, sch_options, procs=procs_to_use(ctx, sch_options)
611613
612614 input_procs = @reusable_vector :schedule!_input_procs Processor OSProc () 32
613615 input_procs_cleanup = @reuse_defer_cleanup empty! (input_procs)
614- for gp in Dagger. compatible_processors (scope, procs)
615- subprocs = get_processors (gp)
616- for proc in subprocs
617- if ! (proc in input_procs)
618- push! (input_procs, proc)
619- end
616+ for proc in Dagger. compatible_processors (scope, procs)
617+ if ! (proc in input_procs)
618+ push! (input_procs, proc)
620619 end
621620 end
622621
623622 sorted_procs = @reusable_vector :schedule!_sorted_procs Processor OSProc () 32
623+ sorted_procs_cleanup = @reuse_defer_cleanup empty! (sorted_procs)
624624 resize! (sorted_procs, length (input_procs))
625625 costs = @reusable_dict :schedule!_costs Processor Float64 OSProc () 0.0 32
626+ costs_cleanup = @reuse_defer_cleanup empty! (costs)
626627 estimate_task_costs! (sorted_procs, costs, state, input_procs, task)
627- empty! (costs ) # We don't use costs here
628- empty! (input_procs )
628+ costs_cleanup ( ) # We don't use costs here
629+ input_procs_cleanup ( )
629630 scheduled = false
630631
631632 # Move our corresponding ThreadProc to be the last considered
@@ -655,27 +656,29 @@ function schedule!(ctx, state, sch_options, procs=procs_to_use(ctx, sch_options)
655656 state. worker_time_pressure[gproc. pid][proc] =
656657 get (state. worker_time_pressure[gproc. pid], proc, 0 ) +
657658 est_time_util
658- @dagdebug task :schedule " Scheduling to $gproc -> $proc "
659- empty! (sorted_procs)
659+ @dagdebug task :schedule " Scheduling to $gproc -> $proc (cost: $(costs[proc]) , pressure: $(state. worker_time_pressure[gproc. pid][proc]) )"
660+ sorted_procs_cleanup ()
661+ costs_cleanup ()
660662 @goto pop_task
661663 end
662664 end
663665 end
664666 ex = SchedulingException (" No processors available, try widening scope" )
665667 store_result! (state, task, ex; error= true )
666668 set_failed! (state, task)
667- empty! (sorted_procs)
669+ sorted_procs_cleanup ()
670+ costs_cleanup ()
668671 @goto pop_task
669672
670673 # Fire all newly-scheduled tasks
671674 @label fire_tasks
672675 for (task_loc, task_spec) in to_fire
673676 fire_tasks! (ctx, task_loc, task_spec, state)
674677 end
675- empty! (to_fire )
678+ to_fire_cleanup ( )
676679
677680 append! (state. ready, failed_scheduling)
678- empty! (failed_scheduling )
681+ failed_scheduling_cleanup ( )
679682 end
680683end
681684
@@ -801,9 +804,10 @@ struct TaskSpec
801804end
802805Base. hash (task:: TaskSpec , h:: UInt ) = hash (task. thunk_id, hash (TaskSpec, h))
803806
804- function fire_tasks! (ctx, task_loc:: ScheduleTaskLocation , task_specs:: Vector{ScheduleTaskSpec} , state)
807+ @reuse_scope function fire_tasks! (ctx, task_loc:: ScheduleTaskLocation , task_specs:: Vector{ScheduleTaskSpec} , state)
805808 gproc, proc = task_loc. gproc, task_loc. proc
806809 to_send = @reusable_vector :fire_tasks!_to_send Union{TaskSpec,Nothing} nothing 1024
810+ to_send_cleanup = @reuse_defer_cleanup empty! (to_send)
807811 for task_spec in task_specs
808812 thunk = task_spec. task
809813 push! (state. running, thunk)
@@ -859,7 +863,7 @@ function fire_tasks!(ctx, task_loc::ScheduleTaskLocation, task_specs::Vector{Sch
859863 @reusable_tasks :fire_tasks!_task_cache 32 _-> nothing " fire_tasks!" FireTaskSpec (proc, state. chan, task_spec)
860864 end
861865 end
862- empty! (to_send )
866+ to_send_cleanup ( )
863867end
864868
865869struct FireTaskSpec
@@ -1306,7 +1310,7 @@ end
13061310
13071311Executes a single task specified by `task` on `to_proc`.
13081312"""
1309- function do_task (to_proc, task:: TaskSpec )
1313+ @reuse_scope function do_task (to_proc, task:: TaskSpec )
13101314 thunk_id = task. thunk_id
13111315
13121316 ctx_vars = task. ctx_vars
@@ -1461,7 +1465,9 @@ function do_task(to_proc, task::TaskSpec)
14611465 f = Dagger. value (first (data))
14621466 @assert ! (f isa Chunk) " Failed to unwrap thunk function"
14631467 fetched_args = @reusable_vector :do_task_fetched_args Any nothing 32
1468+ fetched_args_cleanup = @reuse_defer_cleanup empty! (fetched_args)
14641469 fetched_kwargs = @reusable_vector :do_task_fetched_kwargs Pair{Symbol,Any} :NULL => nothing 32
1470+ fetched_kwargs_cleanup = @reuse_defer_cleanup empty! (fetched_kwargs)
14651471 for idx in 2 : length (data)
14661472 arg = data[idx]
14671473 if Dagger. ispositional (arg)
@@ -1535,8 +1541,8 @@ function do_task(to_proc, task::TaskSpec)
15351541 bt = catch_backtrace ()
15361542 RemoteException (myid (), CapturedException (ex, bt))
15371543 finally
1538- empty! (fetched_args )
1539- empty! (fetched_kwargs )
1544+ fetched_args_cleanup ( )
1545+ fetched_kwargs_cleanup ( )
15401546 end
15411547
15421548 threadtime = cputhreadtime () - threadtime_start
0 commit comments