mstar.utils.threads

mstar.utils.threads#

Starting threads, and moving the threads a step passes through onto their CPUs.

A step passes between the worker’s main thread, its GPU thread and its plan thread several times (submit, launch, done, commit, plan ready), and each handoff blocks one thread and wakes another. On a host with deep idle states a thread woken on another core first waits for that core to leave C6 (130-175 µs on the B300 host this was measured on); woken on a core that is already running, it starts at once. The API server’s result threads (the message thread, and the Rust frontend’s bridge) hand every step’s tokens on the same way. step_thread_core (mstar.utils.cpus) pins them.

A new thread starts with its creator’s CPU mask, so a thread or pool that a pinned thread starts would share its CPU. Thread and ThreadPool start theirs on the process mask; ruff refuses a bare threading.Thread or ThreadPoolExecutor under mstar/ (banned-api in pyproject.toml).

Functions

pin_thread(cpus, process_mask)

Confine the calling thread to cpus.

Classes

Thread([group, target, name, args, kwargs, ...])

A threading.Thread that runs on the process mask, whatever its creator's and whatever its run is: a target, an override that doesn't call super().run(), a mixin's.

ThreadPool([max_workers, ...])

A ThreadPoolExecutor whose threads run on the process mask before initializer, whatever the mask of the thread that started them.

class mstar.utils.threads.Thread(group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None)[source]#

Bases: Thread

A threading.Thread that runs on the process mask, whatever its creator’s and whatever its run is: a target, an override that doesn’t call super().run(), a mixin’s. Wrapped at start so that calling run directly stays on the caller’s thread and mask.

start()[source]#

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

Return type:

None

class mstar.utils.threads.ThreadPool(max_workers=None, thread_name_prefix='', initializer=None, initargs=())[source]#

Bases: ThreadPoolExecutor

A ThreadPoolExecutor whose threads run on the process mask before initializer, whatever the mask of the thread that started them.

Parameters:
  • max_workers (int | None)

  • thread_name_prefix (str)

  • initializer (Callable[..., object] | None)

  • initargs (tuple)

mstar.utils.threads.pin_thread(cpus, process_mask)[source]#

Confine the calling thread to cpus.

A thread’s intra-op (OpenMP) pool is started by its first parallel op with the mask the thread has then, and a pool confined to one CPU runs a parallel op many times slower. So go back to the process mask first (the main thread, already confined, starts the GPU and plan threads), start the pool there with one op as wide as the pool, and only then move.

Parameters:
Return type:

None