scheduler

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 3 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

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) 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) 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:

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) IsEnabled

func (s *Scheduler) IsEnabled() bool

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

func (*Scheduler) IsUnlimited

func (s *Scheduler) IsUnlimited() bool

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

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) 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. Once any workerFn returns (indicating the work queue is exhausted), Saturate stops recruiting additional workers and waits for all already-running workerFn instances to finish before returning.

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) SetMaxParallelism

func (s *Scheduler) SetMaxParallelism(maxParallelism int)

SetMaxParallelism sets the target maxParallelism.

This should be configured before workers start executing.

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) 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.

Jump to

Keyboard shortcuts

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