From e817f13a1624b721db0ebbd83e71eb16c7a3e415 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson <oscar.gustafsson@gmail.com> Date: Fri, 17 Feb 2023 14:03:39 +0100 Subject: [PATCH] Improve repr for resources --- b_asic/process.py | 19 +++++++++++++++++++ b_asic/resources.py | 13 +++++++++++++ b_asic/schedule.py | 20 ++++++++++++++++++-- examples/threepointwinograddft.py | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/b_asic/process.py b/b_asic/process.py index 8d7e7620..df855bf0 100644 --- a/b_asic/process.py +++ b/b_asic/process.py @@ -58,6 +58,11 @@ class Process: def __str__(self) -> str: return self._name + def __repr__(self) -> str: + return ( + f"Process({self.start_time}, {self.execution_time}, {self.name})" + ) + # Static counter for default names _name_cnt = 0 @@ -142,6 +147,13 @@ class MemoryVariable(Process): def write_port(self) -> OutputPort: return self._write_port + def __repr__(self) -> str: + reads = {k: v for k, v in zip(self._read_ports, self._life_times)} + return ( + f"MemoryVariable({self.start_time}, {self.write_port}," + f" {reads!r}, {self.name!r})" + ) + class PlainMemoryVariable(Process): """ @@ -189,3 +201,10 @@ class PlainMemoryVariable(Process): @property def write_port(self) -> int: return self._write_port + + def __repr__(self) -> str: + reads = {k: v for k, v in zip(self._read_ports, self._life_times)} + return ( + f"PlainMemoryVariable({self.start_time}, {self.write_port}," + f" {reads!r}, {self.name!r})" + ) diff --git a/b_asic/resources.py b/b_asic/resources.py index d25d0493..c50007a7 100644 --- a/b_asic/resources.py +++ b/b_asic/resources.py @@ -1,3 +1,4 @@ +import io import re from typing import Dict, Iterable, List, Optional, Set, Tuple, TypeVar, Union @@ -390,3 +391,15 @@ class ProcessCollection: ) for process_collection_set in process_collection_set_list } + + def _repr_svg_(self) -> str: + """ + Generate an SVG_ of the resource collection. This is automatically displayed in e.g. + Jupyter Qt console. + """ + fig, ax = plt.subplots() + self.draw_lifetime_chart(ax, show_markers=False) + f = io.StringIO() + fig.savefig(f, format="svg") + + return f.getvalue() diff --git a/b_asic/schedule.py b/b_asic/schedule.py index bab48e36..fa526019 100644 --- a/b_asic/schedule.py +++ b/b_asic/schedule.py @@ -32,6 +32,7 @@ from b_asic.graph_component import GraphID from b_asic.operation import Operation from b_asic.port import InputPort, OutputPort from b_asic.process import MemoryVariable, Process +from b_asic.resources import ProcessCollection from b_asic.signal_flow_graph import SFG from b_asic.special_operations import Delay, Output @@ -583,8 +584,8 @@ class Schedule: ] + cast(int, source_port.latency_offset) self._remove_delays() - def _get_memory_variables_list(self) -> List['Process']: - ret: List['Process'] = [] + def _get_memory_variables_list(self) -> List['MemoryVariable']: + ret: List['MemoryVariable'] = [] for graph_id, start_time in self._start_times.items(): slacks = self._forward_slacks(graph_id) for outport, signals in slacks.items(): @@ -597,10 +598,25 @@ class Schedule: start_time + cast(int, outport.latency_offset), outport, reads, + outport.operation.graph_id, ) ) return ret + def get_memory_variables(self) -> ProcessCollection: + """ + Return a :class:`~b_asic.resources.ProcessCollection` containing all + memory variables. + + Returns + ------- + ProcessCollection + + """ + return ProcessCollection( + set(self._get_memory_variables_list()), self.schedule_time + ) + def _get_y_position( self, graph_id, operation_height=1.0, operation_gap=None ) -> float: diff --git a/examples/threepointwinograddft.py b/examples/threepointwinograddft.py index 7e1fd537..36a332f0 100644 --- a/examples/threepointwinograddft.py +++ b/examples/threepointwinograddft.py @@ -54,3 +54,5 @@ sfg.set_execution_time_of_type(Subtraction.type_name(), 1) # Generate schedule schedule = Schedule(sfg, cyclic=True) schedule.plot() + +pc = schedule.get_memory_variables() -- GitLab