b_asic.resource_assigner

B-ASIC Resource Assigner Module.

Contains functions for joint resource assignment of processing elements and memories.

b_asic.resource_assigner.assign_processing_elements_and_memories(schedule: Schedule, *, strategy: Literal['ilp_graph_color', 'ilp_min_mux', 'cp_min_mux', 'greedy_min_mux', 'greedy_graph_color', 'left_edge'] = 'ilp_graph_color', max_mux_size: int | None = None, resources: dict[TypeName, int] | None = None, max_mems: int | None = None, memory_read_ports: int = 1, memory_write_ports: int = 1, memory_total_ports: int | None = None, memory_type: Literal['RAM', 'register'] = 'RAM', solver: LpSolver | str | SolverInterface | None = None, strat_params: dict | None = None) → tuple[list[ProcessingElement], list[Memory], ProcessCollection]

Assign PEs and memories jointly using resource assignment strategies.

Parameters:
scheduleSchedule

The schedule containing the operations and memory variables to assign resources to.

strategystr, default: “ilp_graph_color”

The strategy used when assigning resources. Valid options are:

  • “ilp_graph_color” - ILP-based optimal resource assignment.

  • “ilp_min_mux” - ILP-based optimal resource assignment with multiplexer minimization.

  • “cp_min_mux” - CPMpy-based multiplexer minimization (requires the cp extra).

  • “greedy_min_mux” - Connection-aware coloring and local search.

  • “greedy_graph_color” - Greedy graph coloring-based resource assignment.

  • “left_edge” - Left-edge coloring-based resource assignment.

max_mux_sizeint, optional

The maximum fan-in size for any enabled multiplexer target in strategies ‘ilp_min_mux’, ‘cp_min_mux’, and ‘greedy_min_mux’. Must be at least 1. The heuristic may fail to find a feasible assignment even when one exists. Memory read ports count as distinct sources, and each memory write port is a separate target. Memory access ports are optimized together with resource assignment and retained in the resulting memories.

resourcesdict[TypeName, int], optional

The maximum amount of resources to assign to, used to limit the solution space for performance gains.

max_memsint, optional

The maximum amount of memories to assign to, used to limit the solution space for performance gains.

memory_read_portsint, default 1

The number of read ports used when splitting process collection based on memory variable access. left_edge supports multiple read ports and uses capacity-aware greedy packing for multiport memories. ilp_min_mux, cp_min_mux, and greedy_min_mux also support multiple read ports; their models enforce per-cycle port capacities. Other strategies require one read and one write port.

memory_write_portsint, default 1

The number of write ports used when splitting process collection based on memory variable access. Multiple write ports are supported by left_edge, ilp_min_mux, cp_min_mux, and greedy_min_mux.

memory_total_portsint, optional

The total number of ports used when splitting process collection based on memory variable access. Defaults to the sum of the read and write ports. Strategies other than left_edge, ilp_min_mux, cp_min_mux, and greedy_min_mux require one or two total ports.

memory_type{‘RAM’, ‘register’}, default: ‘RAM’

The type of memory to assign to.

solverLpSolver or str or SolverInterface, optional

For cp_min_mux, a CPMpy solver name (default: "ortools") or a CPMpy solver instance. Use a fresh instance: the generated constraints and objective are added to it in place. Native solver settings are preserved. For example:

import cpmpy as cp

cp_solver = cp.SolverLookup.get("ortools")
cp_solver.ort_solver.parameters.num_search_workers = 4
pes, memories, direct = assign_processing_elements_and_memories(
    schedule, strategy="cp_min_mux", solver=cp_solver
)

For ILP strategies, a PuLP solver instance. To see which PuLP solvers are available:

import pulp

print(pulp.listSolvers(onlyAvailable=True))
strat_paramsdict, optional

Options passed to the selected assignment strategy. The dictionary is not modified. For example, {"restarts": 1, "seed": 42} configures greedy_min_mux and {"time_limit": 60} configures cp_min_mux.

For greedy_min_mux, the following options are supported:

  • restarts : int, default: 8 Number of search starts. Must be positive.

  • max_passes : int, default: 20 Maximum number of local search passes per start. Must be nonnegative; zero disables local search.

  • seed : int, default: 0 Random seed. Results are reproducible for a fixed process order and seed.

For greedy_graph_color, options are forwarded to networkx.coloring.greedy_color() for both PEs and memories:

  • coloring_strategy : str or callable, default: “saturation_largest_first” Overrides NetworkX’s coloring strategy for both PEs and memories. This is separate from the assignment strategy argument.

  • interchange : bool, default: False Enable color interchange. Requires a compatible coloring_strategy; the default "saturation_largest_first" does not support it.

  • backend : str, optional NetworkX backend to use. Additional backend-specific keyword arguments are forwarded to that backend.

For cp_min_mux, dictionary entries are passed to the CP solver’s solve() method, e.g. {"time_limit": 60} (seconds) and, for OR-Tools, {"num_search_workers": 4, "log_search_progress": False}. With a solver name, OR-Tools search logging defaults to True; supplied options override this default. With an instance, no logging default is imposed.

The ilp_graph_color, ilp_min_mux, and left_edge strategies have no additional algorithm options. Configure ILP solvers through the solver instance.

Returns:
processing_elementslist[ProcessingElement]

The assigned processing elements.

memorieslist[Memory]

The assigned memories.

directProcessCollection

Zero-time memory variables used for direct interconnects.

The cp_min_mux strategy uses CPMpy with OR-Tools by default. Install the optional dependencies with pip install 'b-asic[cp]'. For example:

pes, memories, direct = assign_processing_elements_and_memories(
    schedule,
    strategy="cp_min_mux",
    memory_read_ports=1,
    memory_write_ports=1,
    memory_total_ports=2,
    max_mux_size=4,
    solver="ortools",
    strat_params={"time_limit": 60, "log_search_progress": False},
)

Pass strategy-specific options through strat_params. For example, strat_params={"restarts": 4, "seed": 42} configures greedy_min_mux; strat_params={"coloring_strategy": "largest_first", "interchange": True} configures greedy_graph_color. The supplied dictionary is not modified.

For multiport memories, use strategy="left_edge" with the desired memory_read_ports, memory_write_ports, and memory_total_ports. The assigner obtains a feasible memory-count upper bound using greedy_port_packing and reuses that assignment for the memories. Processing elements still use left-edge allocation. Per-cycle reads, writes, and their sum must fit the corresponding port limits. This is a heuristic upper bound, not a proof of the minimum memory count.

ilp_min_mux, cp_min_mux, and greedy_min_mux also support multiple read and write ports. Their models enforce per-cycle read, write, and combined access capacities, including accesses that wrap around the schedule. Missing multiport memory bounds and ILP warm starts use greedy_port_packing. Single-port models retain their exclusion constraints and symmetry breaking; access groups can only be fixed to distinct memories when their capacity is one. All three mux strategies count connections between individual physical ports. Different read ports of the same memory are distinct inputs to a destination multiplexer. Each memory write port has its own multiplexer and its own max_mux_size limit. PE output and input indices and memory-to-memory relay connections are included as well.

The ILP and CP models jointly choose each variable’s memory and the physical port for every read and write access. Simultaneous distinct accesses cannot use the same port of the same memory. Consumers reading the same variable at the same delay share one broadcast read. The selected ports are retained in the assigned collections and exposed through Memory.port_assignment, so architecture connections, drawings, and multiplexer counts use the solution selected by the model. Each key is (variable, delay), with None denoting the write access.

greedy_min_mux retains saturation coloring for single-port exclusions. For multiport memories it combines unit-capacity exclusion groups with per-cycle load counters, prioritizing variables that use busy cycles. Initial placement, recoloring, and swaps all respect the read, write, and combined limits. Swaps check the net load change in both memories together. For each candidate resource assignment it also improves port allocation by moving accesses to unused ports and swapping simultaneous accesses between ports. These moves minimize fan-in violations before connection count. The heuristic can fail within the supplied resource bounds even when a feasible assignment exists; more restarts or an exact strategy may help.

The other joint assignment strategies require one read port, one write port, and one or two total ports. Missing bounds use graph coloring for single-read/single-write memories. Supplied bounds skip those estimates, except that ilp_min_mux retains heuristic colorings for warm starts. left_edge computes its assignments directly without preliminary bounds, and greedy_graph_color reuses its final colorings as any missing bounds.

All three mux minimization strategies count physical connections, including inputs with fan-in one, within the supplied resource budgets, with memory_total_ports=1 enforcing a shared port. If resource budgets are omitted, greedy coloring or port packing supplies them; these budgets can restrict the best achievable mux cost.