Documentation
¶
Overview ¶
Package scheduler implements the runtime execution scheduler for the Go compute backend.
It manages a persistent pool of worker goroutines and schedules tasks across three hierarchical levels of parallelism:
- Inter-Call: across concurrent calls to Executable.Execute() (highest priority).
- Intra-Op: across contiguous memory chunks within a single operation.
- Inter-Op: across independent graph branches within a single Execute() call (lowest priority).
When an operation spawns intra-op subtasks (via RunIntraOpTasks, ParallelizeChunks, or Saturate), the calling goroutine immediately participates in executing its own op's subtasks (work-helping) before waiting on the completion barrier. This guarantees forward progress and prevents deadlocks even under nested inter-op and intra-op parallelism.
See design details in https://github.com/gomlx/compute/blob/main/docs/GoBackendScheduler.md
Index ¶
- type Scheduler
- func (s *Scheduler) AcquireWholeChip() bool
- func (s *Scheduler) AdjustedMaxParallelism() int
- func (s *Scheduler) EffectiveParallelism() int
- func (s *Scheduler) EnterExecution() func()
- func (s *Scheduler) IsBusy() bool
- func (s *Scheduler) IsEnabled() bool
- func (s *Scheduler) IsUnlimited() bool
- func (s *Scheduler) IsWholeChipHeld() bool
- func (s *Scheduler) MaxParallelism() int
- func (s *Scheduler) ParallelizeChunks(totalItems, minChunkSize int, fn func(start, end int))
- func (s *Scheduler) ReleaseWholeChip(acquired bool)
- func (s *Scheduler) RunIntraOpTasks(tasks []func())
- func (s *Scheduler) Saturate(workerFn func())
- func (s *Scheduler) SaturateUpTo(maxWorkers int, workerFn func())
- func (s *Scheduler) SetMaxParallelism(maxParallelism int)
- func (s *Scheduler) SubmitInterOp(task func()) bool
- func (s *Scheduler) UnscaledParallelism() int
- func (s *Scheduler) WaitToStart(task func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler manages persistent worker goroutines and dispatches intra-op and inter-op tasks.
func New ¶
func New() *Scheduler
New returns a new Scheduler with the default parallelism set to runtime.GOMAXPROCS(0).
func (*Scheduler) AcquireWholeChip ¶ added in v0.1.26
AcquireWholeChip acquires the fair FIFO whole-chip gate for a large operation so that concurrent large ops (across inter-op branches or concurrent Execute() calls) queue in strict FIFO order without starving or thrashing shared L2/L3 caches and SME units. The caller MUST call ReleaseWholeChip(acquired) when the operation finishes.
func (*Scheduler) AdjustedMaxParallelism ¶
AdjustedMaxParallelism returns the effective parallelism for the current execution. It is an alias for EffectiveParallelism() kept for convenience across op implementations.
func (*Scheduler) EffectiveParallelism ¶
EffectiveParallelism returns the target number of workers an operation within the current Execute() call should use, dynamically scaled by the number of concurrent active executions:
max(1, totalParallelism / numActiveCalls)
func (*Scheduler) EnterExecution ¶
func (s *Scheduler) EnterExecution() func()
EnterExecution registers the start of a top-level Executable.Execute() call (inter-call tracking). It returns a function that must be deferred to unregister the call when Execute() finishes.
func (*Scheduler) IsBusy ¶ added in v0.1.30
IsBusy reports whether multiple top-level Execute() calls are active concurrently or a large operation holds the whole-chip gate.
func (*Scheduler) IsEnabled ¶
IsEnabled returns whether parallelism is enabled (maxParallelism != 0).
func (*Scheduler) IsUnlimited ¶
IsUnlimited returns whether parallelism is unlimited (maxParallelism < 0).
func (*Scheduler) IsWholeChipHeld ¶ added in v0.1.26
IsWholeChipHeld reports whether a large operation currently holds the whole-chip gate.
func (*Scheduler) MaxParallelism ¶
MaxParallelism returns the configured target for parallelism. If set to 0, parallelism is disabled. If set to -1, parallelism is unlimited.
func (*Scheduler) ParallelizeChunks ¶
ParallelizeChunks partitions [0, totalItems) into contiguous, cache-friendly ranges based on EffectiveParallelism() and minChunkSize, and executes fn(start, end) across the calling goroutine and available intra-op workers.
Each worker processes contiguous ranges of items to prevent false sharing and preserve L1/L2 cache locality. If EffectiveParallelism() <= 1 or totalItems <= minChunkSize, it calls fn(0, totalItems) directly inline with zero allocations.
func (*Scheduler) ReleaseWholeChip ¶ added in v0.1.26
ReleaseWholeChip releases the whole-chip gate previously acquired via AcquireWholeChip.
func (*Scheduler) RunIntraOpTasks ¶
func (s *Scheduler) RunIntraOpTasks(tasks []func())
RunIntraOpTasks executes the given intra-op tasks for the current operation and waits for all of them to finish.
Invariant (Deadlock-Free Work-Helping): The calling goroutine immediately participates in executing tasks from this slice itself at highest priority, while available persistent workers in the scheduler assist concurrently.
func (*Scheduler) Saturate ¶
func (s *Scheduler) Saturate(workerFn func())
Saturate fans out workerFn across the calling goroutine and up to EffectiveParallelism()-1 available scheduler workers (used by work-pulling ops like MatMul).
Each workerFn pulls work from a shared queue until no work remains and then returns.
func (*Scheduler) SaturateUpTo ¶
SaturateUpTo is like Saturate, but caps the total number of participating workers (including the calling goroutine) to maxWorkers when maxWorkers > 0.
func (*Scheduler) SetMaxParallelism ¶
SetMaxParallelism sets the target maxParallelism.
This should be configured before workers start executing.
func (*Scheduler) SubmitInterOp ¶
SubmitInterOp enqueues a coarse-grained inter-op task (such as a ready branch in executeParallel) to be executed by an available persistent worker when no higher-priority intra-op tasks are waiting. It returns true if an inter-op worker slot was claimed and the task was enqueued, or false if parallelism is disabled, the inter-op worker budget is full, intra-op tasks are already waiting, or the queue is full (so the caller can execute the branch locally or leave it in readyQueue).
func (*Scheduler) UnscaledParallelism ¶ added in v0.1.26
UnscaledParallelism returns the full target number of workers for the scheduler without dividing by numActiveCalls. This is used by large operations that acquire the fair FIFO whole-chip gate via AcquireWholeChip() so each large op utilizes all workers sequentially rather than thrashing shared L2/L3 caches concurrently.
func (*Scheduler) WaitToStart ¶
func (s *Scheduler) WaitToStart(task func())
WaitToStart runs the task using an available worker from the intra-op queue, or inline if parallelism is disabled or the queue is full.
Prefer ParallelizeChunks or RunIntraOpTasks for new intra-op code so the calling goroutine participates in work-helping automatically.