accelmo.fitting.protocol

The parent <-> worker protocol: typed messages and the parent-side channels that carry them.

A worker receives Start once, then any number of Evaluate and Command messages, then Stop. Each Evaluate is answered with a GradReply, each Command with an Ack or a LossReply. A failure is answered with an ErrorReply, which the receiving WorkerChannels raises.

class accelmo.fitting.protocol.Start(knobs)[source]

Bases: object

The first message: the model values the worker builds its MAD-NG session with.

Parameters:

knobs (dict[str, float])

knobs: dict[str, float]
class accelmo.fitting.protocol.Evaluate(knobs, batch=0, loss_only=False, curvature=False)[source]

Bases: object

Evaluate the loss and, unless loss_only, its knob derivatives at knobs.

batch selects a tracking worker’s particle batch. loss_only asks a closed-orbit worker for the loss alone, from plain closed-orbit solves without knob derivatives (about 6x cheaper); the LM fit uses it for trial points. curvature asks a tracking worker for the Gauss-Newton matrix JᵀWJ too (the tracking LM fit); loss and gradient are the chi-square sums either way.

Parameters:
  • knobs (dict[str, float])

  • batch (int)

  • loss_only (bool)

  • curvature (bool)

knobs: dict[str, float]
batch: int = 0
loss_only: bool = False
curvature: bool = False
class accelmo.fitting.protocol.TotalChi2(knobs)[source]

Bases: object

Chi-square of every batch at knobs; answered with a LossReply.

Parameters:

knobs (dict[str, float])

knobs: dict[str, float]
class accelmo.fitting.protocol.SetKnobs(knobs)[source]

Bases: object

Load knobs into the model without evaluating.

Parameters:

knobs (dict[str, float])

knobs: dict[str, float]
class accelmo.fitting.protocol.UpdateInitCoords(coords)[source]

Bases: object

Replace the start coordinates; coords has shape (particles, 4): x, px, y, py.

Parameters:

coords (np.ndarray)

coords: ndarray
class accelmo.fitting.protocol.SetUncertaintyMode(hessians)[source]

Bases: object

The Hessians ("true", "gauss_newton") to compute on Stop; none disables it.

Parameters:

hessians (tuple[str, ...])

hessians: tuple[str, ...]
accelmo.fitting.protocol.Command = accelmo.fitting.protocol.TotalChi2 | accelmo.fitting.protocol.SetKnobs | accelmo.fitting.protocol.UpdateInitCoords | accelmo.fitting.protocol.SetUncertaintyMode

Control commands a tracking worker answers between evaluations with an Ack or, for TotalChi2, a LossReply.

class accelmo.fitting.protocol.Stop[source]

Bases: object

End the worker. A tracking worker first answers with its uncertainty part.

class accelmo.fitting.protocol.GradReply(worker_id, loss, grad, normal=None, extra=None)[source]

Bases: object

One evaluation’s result.

loss is NaN when the worker lost its particles or its closed orbit, and the caller rejects the step. normal is JᵀWJ (the chi² Gauss-Newton Hessian is twice it), sent only when curvature is asked for. extra carries worker-specific results: a calibrated worker’s block header, or the orbit a reference worker published.

Parameters:
  • worker_id (int)

  • loss (float)

  • grad (np.ndarray)

  • normal (np.ndarray | None)

  • extra (Any)

worker_id: int
loss: float
grad: ndarray
normal: ndarray | None = None
extra: Any = None
class accelmo.fitting.protocol.LossReply(worker_id, loss)[source]

Bases: object

A loss without derivatives.

Parameters:
  • worker_id (int)

  • loss (float)

worker_id: int
loss: float
class accelmo.fitting.protocol.Ack(worker_id)[source]

Bases: object

A control command was applied.

Parameters:

worker_id (int)

worker_id: int
class accelmo.fitting.protocol.UncertaintyProgress(worker_id)[source]

Bases: object

One more batch of the second-order pass is done; sent before the final reply.

Parameters:

worker_id (int)

worker_id: int
class accelmo.fitting.protocol.ErrorReply(worker_id, phase, error_type, error, traceback)[source]

Bases: object

The worker failed during phase.

Parameters:
  • worker_id (int)

  • phase (str)

  • error_type (str)

  • error (str)

  • traceback (str)

worker_id: int
phase: str
error_type: str
error: str
traceback: str
raise_error(worker=None)[source]

Raise this failure in the parent.

Return type:

NoReturn

Parameters:

worker (Process | None)

class accelmo.fitting.protocol.WorkerChannels(parent_conns, workers)[source]

Bases: object

Reusable worker communication state for fast send/receive rounds.

Parameters:
  • parent_conns (list[Connection])

  • workers (list[Process])

parent_conns
workers
send_all(message)[source]

Send one message to every worker.

Return type:

None

Parameters:

message (object)

send_to(index, message)[source]

Send one message to the worker at index.

Return type:

None

Parameters:
  • index (int)

  • message (object)

recv_all(on_progress=None)[source]

Receive one message from each worker, preserving connection order.

Return type:

list[Any]

Parameters:

on_progress (Callable[[UncertaintyProgress], None] | None)

recv_some(indices, on_progress=None)[source]

Receive one message from each listed worker, in the order of indices.

UncertaintyProgress messages are passed to on_progress and are not replies.

Return type:

list[Any]

Parameters:
ack_all(command)[source]

Send command to every worker and require an Ack from each.

Return type:

None

Parameters:

command (TotalChi2 | SetKnobs | UpdateInitCoords | SetUncertaintyMode)

accelmo.fitting.protocol.FD_SETSIZE = 1024

pymadng waits on each MAD process with select(), which rejects fd numbers >= 1024.

accelmo.fitting.protocol.FDS_PER_WORKER = 4

Descriptors a forked worker leaves open in every later worker (pipe pair, both ends).

accelmo.fitting.protocol.FD_RESERVE = 64

Descriptors the main process and each MAD-NG process need before any worker exists.

accelmo.fitting.protocol.machine_worker_limit()[source]

Most MAD-NG workers this machine can run at once, and what limits it.

CPU count is the working limit; the open-file bound only matters on a host with more cores than FD_SETSIZE allows workers.

Return type:

tuple[int, str]

accelmo.fitting.protocol.distribute(costs, n_groups)[source]

Split item indices into n_groups of near-equal total cost (longest first).

Return type:

list[list[int]]

Parameters:
  • costs (list[int])

  • n_groups (int)