scheduler

package
v0.1.38 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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:

  1. Inter-Call: across concurrent calls to Executable.Execute() (highest priority).
  2. Intra-Op: across contiguous memory chunks within a single operation.
  3. 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPhysicalParallelism added in v0.1.34

func DefaultPhysicalParallelism() int

DefaultPhysicalParallelism returns min(PhysicalCores(), runtime.GOMAXPROCS(0)), respecting container/cgroup GOMAXPROCS limits while avoiding SMT oversubscription.

func PhysicalCores added in v0.1.34

func PhysicalCores() int

PhysicalCores returns the detected number of online physical CPU cores on this machine (excluding SMT/Hyper-Threading sibling threads on Linux and macOS; falls back to runtime.NumCPU() on other platforms).

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

func (s *Scheduler) AcquireWholeChip() bool

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

func (s *Scheduler) AdjustedMaxParallelism() int

AdjustedMaxParallelism returns the effective parallelism for the current execution. It is an alias for EffectiveParallelism() kept for convenience across op implementations.

func (*Scheduler) Close added in v0.1.38

func (s *Scheduler) Close()

Close shuts down the persistent intra-op worker goroutines cleanly. Safe to call multiple times after all in-flight executions have drained.

func (*Scheduler) EffectiveParallelism

func (s *Scheduler) EffectiveParallelism() int

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 and capped by MaxIntraCallParallelism (when > 0):

min(maxIntraCallParallelism, max(1, maxInterCallParallelism / numActiveCalls))

func (*Scheduler) EnterExecution

func (s *Scheduler) EnterExecution() func()

EnterExecution registers the start of a top-level Executable.Execute() call (inter-call tracking). When maxParallelism > 0, it also throttles concurrent active executions to maxInterCallParallelism() so callers launching more concurrent requests than available cores do not oversubscribe the CPU. Note that maxIntraCallParallelism does NOT reduce this inter-call concurrency limit. It returns a function that must be deferred to unregister the call when Execute() finishes.

func (*Scheduler) IsBusy added in v0.1.30

func (s *Scheduler) IsBusy() bool

IsBusy reports whether multiple top-level Execute() calls are active concurrently or a large operation holds the whole-chip gate.

func (*Scheduler) IsEnabled

func (s *Scheduler) IsEnabled() bool

IsEnabled returns whether parallelism is enabled (maxParallelism != 0).

func (*Scheduler) IsSpinPoolEnabled added in v0.1.38

func (s *Scheduler) IsSpinPoolEnabled() bool

IsSpinPoolEnabled reports whether adaptive spin-waiting is enabled on the intra-op queue.

func (*Scheduler) IsUnlimited

func (s *Scheduler) IsUnlimited() bool

IsUnlimited returns whether parallelism is unlimited (maxParallelism < 0).

func (*Scheduler) IsWholeChipHeld added in v0.1.26

func (s *Scheduler) IsWholeChipHeld() bool

IsWholeChipHeld reports whether a large operation currently holds the whole-chip gate.

func (*Scheduler) MaxIntraCallParallelism added in v0.1.34

func (s *Scheduler) MaxIntraCallParallelism() int

MaxIntraCallParallelism returns the configured per-call (intra-op + inter-op) worker cap. A value <= 0 means no separate cap (same limit as MaxParallelism).

func (*Scheduler) MaxParallelism

func (s *Scheduler) MaxParallelism() int

MaxParallelism returns the configured target for parallelism. If set to 0, parallelism is disabled. If set to -1, parallelism is unlimited.

func (*Scheduler) ParallelizeChunks

func (s *Scheduler) ParallelizeChunks(totalItems, minChunkSize int, fn func(start, end int))

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

func (s *Scheduler) ReleaseWholeChip(acquired bool)

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

func (s *Scheduler) SaturateUpTo(maxWorkers int, workerFn func())

SaturateUpTo is like Saturate, but caps the total number of participating workers (including the calling goroutine) to maxWorkers when maxWorkers > 0.

func (*Scheduler) SetMaxIntraCallParallelism added in v0.1.34

func (s *Scheduler) SetMaxIntraCallParallelism(maxIntraCall int)

SetMaxIntraCallParallelism sets an upper bound on the number of workers used within any single Execute() call (intra-op + inter-op) without reducing inter-call concurrency in EnterExecution(). Pass 0 (the default) to use the same limit as MaxParallelism.

func (*Scheduler) SetMaxParallelism

func (s *Scheduler) SetMaxParallelism(maxParallelism int)

SetMaxParallelism sets the target maxParallelism.

This should typically be configured before workers start executing; calling it dynamically safely wakes any executions parked in EnterExecution.

func (*Scheduler) SubmitInterOp

func (s *Scheduler) SubmitInterOp(task func()) bool

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

func (s *Scheduler) UnscaledParallelism() int

UnscaledParallelism returns the full target number of workers for a single Execute() call without dividing by numActiveCalls (still respecting MaxIntraCallParallelism when > 0). This is used by large operations that acquire the fair FIFO whole-chip gate via AcquireWholeChip() so each large op utilizes its allowed 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.

func (*Scheduler) WithSpinPool added in v0.1.38

func (s *Scheduler) WithSpinPool(enabled bool) *Scheduler

WithSpinPool configures whether adaptive spin-waiting is enabled on the intra-op queue and returns s for chaining. Must be called at creation time before workers start executing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL