From be153a466f4832892c24b0d4e265cc8cc526f92d Mon Sep 17 00:00:00 2001 From: Simon Bjurek <simbj106@student.liu.se> Date: Thu, 13 Feb 2025 16:13:11 +0100 Subject: [PATCH 1/3] added SlackTime MaxFanOut and a Hybrid Scheduler, also started on example with ldlt matrix inverse. --- b_asic/core_operations.py | 31 ++++++++ b_asic/core_schedulers.py | 80 ++++++++++++++++++++- b_asic/gui_utils/about_window.py | 2 +- b_asic/scheduler.py | 9 +++ docs_sphinx/conf.py | 2 +- examples/ldlt_matrix_inverse.py | 120 +++++++++++++++++++++++++++++++ 6 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 examples/ldlt_matrix_inverse.py diff --git a/b_asic/core_operations.py b/b_asic/core_operations.py index 3a1474cc..0a321f90 100644 --- a/b_asic/core_operations.py +++ b/b_asic/core_operations.py @@ -77,6 +77,37 @@ class Constant(AbstractOperation): def __str__(self) -> str: return f"{self.value}" + def get_plot_coordinates( + self, + ) -> tuple[tuple[tuple[float, float], ...], tuple[tuple[float, float], ...]]: + # Doc-string inherited + return ( + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ) + + def get_input_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return tuple() + + def get_output_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return ((0, 0.5),) + class Addition(AbstractOperation): """ diff --git a/b_asic/core_schedulers.py b/b_asic/core_schedulers.py index 05bcc3f4..1b5b4f18 100644 --- a/b_asic/core_schedulers.py +++ b/b_asic/core_schedulers.py @@ -119,6 +119,84 @@ class EarliestDeadlineScheduler(ListScheduler): @staticmethod def _get_sorted_operations(schedule: "Schedule") -> list["GraphID"]: - schedule_copy = copy.deepcopy(schedule) + schedule_copy = copy.copy(schedule) ALAPScheduler().apply_scheduling(schedule_copy) + + deadlines = {} + for op_id, start_time in schedule_copy.start_times.items(): + deadlines[op_id] = start_time + schedule.sfg.find_by_id(op_id).latency + + return sorted(deadlines, key=deadlines.get) + + +class LeastSlackTimeScheduler(ListScheduler): + """Scheduler that implements the least slack time first algorithm.""" + + @staticmethod + def _get_sorted_operations(schedule: "Schedule") -> list["GraphID"]: + schedule_copy = copy.copy(schedule) + ALAPScheduler().apply_scheduling(schedule_copy) + return sorted(schedule_copy.start_times, key=schedule_copy.start_times.get) + + +class MaxFanOutScheduler(ListScheduler): + """Scheduler that implements the maximum fan-out algorithm.""" + + @staticmethod + def _get_sorted_operations(schedule: "Schedule") -> list["GraphID"]: + schedule_copy = copy.copy(schedule) + ALAPScheduler().apply_scheduling(schedule_copy) + + fan_outs = {} + for op_id, start_time in schedule_copy.start_times.items(): + fan_outs[op_id] = len(schedule.sfg.find_by_id(op_id).output_signals) + + return sorted(fan_outs, key=fan_outs.get, reverse=True) + + +class HybridScheduler(ListScheduler): + """Scheduler that implements a hybrid algorithm. Will receive a new name once finalized.""" + + @staticmethod + def _get_sorted_operations(schedule: "Schedule") -> list["GraphID"]: + # sort least-slack and then resort ties according to max-fan-out + schedule_copy = copy.copy(schedule) + ALAPScheduler().apply_scheduling(schedule_copy) + + sorted_items = sorted( + schedule_copy.start_times.items(), key=lambda item: item[1] + ) + + fan_out_sorted_items = [] + + last_value = sorted_items[0][0] + current_group = [] + for key, value in sorted_items: + + if value != last_value: + # the group is completed, sort it internally + sorted_group = sorted( + current_group, + key=lambda pair: len( + schedule.sfg.find_by_id(pair[0]).output_signals + ), + reverse=True, + ) + fan_out_sorted_items += sorted_group + current_group = [] + + current_group.append((key, value)) + + last_value = value + + sorted_group = sorted( + current_group, + key=lambda pair: len(schedule.sfg.find_by_id(pair[0]).output_signals), + reverse=True, + ) + fan_out_sorted_items += sorted_group + + sorted_op_list = [pair[0] for pair in fan_out_sorted_items] + + return sorted_op_list diff --git a/b_asic/gui_utils/about_window.py b/b_asic/gui_utils/about_window.py index d702a459..08bc42e2 100644 --- a/b_asic/gui_utils/about_window.py +++ b/b_asic/gui_utils/about_window.py @@ -59,7 +59,7 @@ class AboutWindow(QDialog): " href=\"https://gitlab.liu.se/da/B-ASIC/-/blob/master/LICENSE\">" "MIT-license</a>" " and any extension to the program should follow that same" - f" license.\n\n*Version: {__version__}*\n\nCopyright 2020-2023," + f" license.\n\n*Version: {__version__}*\n\nCopyright 2020-2025," " Oscar Gustafsson et al." ) label1.setTextFormat(Qt.MarkdownText) diff --git a/b_asic/scheduler.py b/b_asic/scheduler.py index cc472945..22052b60 100644 --- a/b_asic/scheduler.py +++ b/b_asic/scheduler.py @@ -45,6 +45,15 @@ class Scheduler(ABC): class ListScheduler(Scheduler, ABC): def __init__(self, max_resources: Optional[dict[TypeName, int]] = None) -> None: + if max_resources: + if not isinstance(max_resources, dict): + raise ValueError("max_resources must be a dictionary.") + for key, value in max_resources.items(): + if not isinstance(key, str): + raise ValueError("max_resources key must be a valid type_name.") + if not isinstance(value, int): + raise ValueError("max_resources value must be an integer.") + if max_resources: self._max_resources = max_resources else: diff --git a/docs_sphinx/conf.py b/docs_sphinx/conf.py index b56dcfbe..ed0e0e06 100644 --- a/docs_sphinx/conf.py +++ b/docs_sphinx/conf.py @@ -9,7 +9,7 @@ import shutil project = 'B-ASIC' -copyright = '2020-2023, Oscar Gustafsson et al' +copyright = '2020-2025, Oscar Gustafsson et al' author = 'Oscar Gustafsson et al' html_logo = "../logos/logo_tiny.png" diff --git a/examples/ldlt_matrix_inverse.py b/examples/ldlt_matrix_inverse.py new file mode 100644 index 00000000..b859fec2 --- /dev/null +++ b/examples/ldlt_matrix_inverse.py @@ -0,0 +1,120 @@ +""" +========================================= +LDLT Matrix Inversion Algorithm +========================================= + +""" + +from b_asic.architecture import Architecture, Memory, ProcessingElement +from b_asic.core_operations import MADS, Constant, Reciprocal +from b_asic.core_schedulers import ( + ALAPScheduler, + ASAPScheduler, + EarliestDeadlineScheduler, + HybridScheduler, + LeastSlackTimeScheduler, + MaxFanOutScheduler, +) +from b_asic.schedule import Schedule +from b_asic.sfg_generators import ldlt_matrix_inverse + +sfg = ldlt_matrix_inverse(N=3, is_complex=False) + +# %% +# The SFG is +sfg + +# %% +# Set latencies and execution times. +sfg.set_latency_of_type(Constant.type_name(), 0) +sfg.set_latency_of_type(MADS.type_name(), 3) +sfg.set_latency_of_type(Reciprocal.type_name(), 2) +sfg.set_execution_time_of_type(Constant.type_name(), 0) +sfg.set_execution_time_of_type(MADS.type_name(), 1) +sfg.set_execution_time_of_type(Reciprocal.type_name(), 1) + +# %% +# Create an ASAP schedule. +schedule = Schedule(sfg, scheduler=ASAPScheduler()) +print("Scheduling time:", schedule.schedule_time) +# schedule.show() + +# %% +# Create an ALAP schedule. +schedule = Schedule(sfg, scheduler=ALAPScheduler()) +print("Scheduling time:", schedule.schedule_time) +# schedule.show() + +# %% +# Create an EarliestDeadline schedule that satisfies the resource constraints. +resources = {MADS.type_name(): 1, Reciprocal.type_name(): 1} +schedule = Schedule(sfg, scheduler=EarliestDeadlineScheduler(resources)) +print("Scheduling time:", schedule.schedule_time) +# schedule.show() + +# %% +# Create a LeastSlackTime schedule that satisfies the resource constraints. +schedule = Schedule(sfg, scheduler=LeastSlackTimeScheduler(resources)) +print("Scheduling time:", schedule.schedule_time) +# schedule.show() + +# %% +# Create a MaxFanOutScheduler schedule that satisfies the resource constraints. +schedule = Schedule(sfg, scheduler=MaxFanOutScheduler(resources)) +print("Scheduling time:", schedule.schedule_time) +# schedule.show() + +# %% +# Create a HybridScheduler schedule that satisfies the resource constraints. +schedule = Schedule(sfg, scheduler=HybridScheduler(resources)) +print("Scheduling time:", schedule.schedule_time) +# schedule.edit() + +# %% +operations = schedule.get_operations() +mads = operations.get_by_type_name("mads") +mads.show(title="MADS executions") +reciprocals = operations.get_by_type_name("rec") +reciprocals.show(title="Reciprocal executions") +consts = operations.get_by_type_name("c") +consts.show(title="Const executions") +inputs = operations.get_by_type_name("in") +inputs.show(title="Input executions") +outputs = operations.get_by_type_name("out") +outputs.show(title="Output executions") + +mads_pe = ProcessingElement(mads, entity_name="mad") +reciprocal_pe = ProcessingElement(reciprocals, entity_name="rec") + +const_pe = ProcessingElement(consts, entity_name="c") + +pe_in = ProcessingElement(inputs, entity_name='input') +pe_out = ProcessingElement(outputs, entity_name='output') + +mem_vars = schedule.get_memory_variables() +mem_vars.show(title="All memory variables") +direct, mem_vars = mem_vars.split_on_length() +mem_vars.show(title="Non-zero time memory variables") +mem_vars_set = mem_vars.split_on_ports(read_ports=1, write_ports=1, total_ports=2) + +# %% +memories = [] +for i, mem in enumerate(mem_vars_set): + memory = Memory(mem, memory_type="RAM", entity_name=f"memory{i}") + memories.append(memory) + mem.show(title=f"{memory.entity_name}") + memory.assign("left_edge") + memory.show_content(title=f"Assigned {memory.entity_name}") + +direct.show(title="Direct interconnects") + +# %% +arch = Architecture( + {mads_pe, reciprocal_pe, const_pe, pe_in, pe_out}, + memories, + direct_interconnects=direct, +) + +# %% +arch +# schedule.edit() -- GitLab From 0f8905f2bb36d89b478f21941e383e32f58a8027 Mon Sep 17 00:00:00 2001 From: Simon Bjurek <simbj106@student.liu.se> Date: Mon, 17 Feb 2025 08:25:42 +0100 Subject: [PATCH 2/3] rendering of dont cares now working and example is completed. --- b_asic/core_operations.py | 62 +++++++++++++++++++++++++++++++++ b_asic/scheduler.py | 8 ++++- examples/ldlt_matrix_inverse.py | 37 ++++++++++---------- 3 files changed, 88 insertions(+), 19 deletions(-) diff --git a/b_asic/core_operations.py b/b_asic/core_operations.py index 0a321f90..175c59d1 100644 --- a/b_asic/core_operations.py +++ b/b_asic/core_operations.py @@ -1696,6 +1696,37 @@ class DontCare(AbstractOperation): def __str__(self) -> str: return "dontcare" + def get_plot_coordinates( + self, + ) -> tuple[tuple[tuple[float, float], ...], tuple[tuple[float, float], ...]]: + # Doc-string inherited + return ( + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ) + + def get_input_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return tuple() + + def get_output_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return ((0, 0.5),) + class Sink(AbstractOperation): r""" @@ -1740,3 +1771,34 @@ class Sink(AbstractOperation): def __str__(self) -> str: return "sink" + + def get_plot_coordinates( + self, + ) -> tuple[tuple[tuple[float, float], ...], tuple[tuple[float, float], ...]]: + # Doc-string inherited + return ( + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ( + (-0.5, 0), + (-0.5, 1), + (-0.25, 1), + (0, 0.5), + (-0.25, 0), + (-0.5, 0), + ), + ) + + def get_input_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return tuple() + + def get_output_coordinates(self) -> tuple[tuple[float, float], ...]: + # doc-string inherited + return ((0, 0.5),) diff --git a/b_asic/scheduler.py b/b_asic/scheduler.py index 22052b60..4f1a58f2 100644 --- a/b_asic/scheduler.py +++ b/b_asic/scheduler.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod from typing import TYPE_CHECKING, Optional, cast +from b_asic.core_operations import DontCare from b_asic.port import OutputPort from b_asic.special_operations import Delay, Input, Output from b_asic.types import TypeName @@ -123,11 +124,16 @@ class ListScheduler(Scheduler, ABC): self._handle_outputs(schedule) schedule.remove_delays() - # move all inputs and outputs ALAP now that operations have moved + # move all inputs ALAP now that operations have moved for input_op in schedule.sfg.find_by_type_name(Input.type_name()): input_op = cast(Input, input_op) schedule.move_operation_alap(input_op.graph_id) + # move all dont cares ALAP + for dc_op in schedule.sfg.find_by_type_name(DontCare.type_name()): + dc_op = cast(DontCare, dc_op) + schedule.move_operation_alap(dc_op.graph_id) + @staticmethod def _candidate_is_schedulable( start_times: dict["GraphID"], diff --git a/examples/ldlt_matrix_inverse.py b/examples/ldlt_matrix_inverse.py index b859fec2..3921afe4 100644 --- a/examples/ldlt_matrix_inverse.py +++ b/examples/ldlt_matrix_inverse.py @@ -6,7 +6,7 @@ LDLT Matrix Inversion Algorithm """ from b_asic.architecture import Architecture, Memory, ProcessingElement -from b_asic.core_operations import MADS, Constant, Reciprocal +from b_asic.core_operations import MADS, DontCare, Reciprocal from b_asic.core_schedulers import ( ALAPScheduler, ASAPScheduler, @@ -17,8 +17,9 @@ from b_asic.core_schedulers import ( ) from b_asic.schedule import Schedule from b_asic.sfg_generators import ldlt_matrix_inverse +from b_asic.special_operations import Input, Output -sfg = ldlt_matrix_inverse(N=3, is_complex=False) +sfg = ldlt_matrix_inverse(N=3) # %% # The SFG is @@ -26,10 +27,10 @@ sfg # %% # Set latencies and execution times. -sfg.set_latency_of_type(Constant.type_name(), 0) +sfg.set_latency_of_type(DontCare.type_name(), 0) # REMOVE!!! sfg.set_latency_of_type(MADS.type_name(), 3) sfg.set_latency_of_type(Reciprocal.type_name(), 2) -sfg.set_execution_time_of_type(Constant.type_name(), 0) +sfg.set_execution_time_of_type(DontCare.type_name(), 0) # REMOVE!!! sfg.set_execution_time_of_type(MADS.type_name(), 1) sfg.set_execution_time_of_type(Reciprocal.type_name(), 1) @@ -37,56 +38,56 @@ sfg.set_execution_time_of_type(Reciprocal.type_name(), 1) # Create an ASAP schedule. schedule = Schedule(sfg, scheduler=ASAPScheduler()) print("Scheduling time:", schedule.schedule_time) -# schedule.show() +schedule.show() # %% # Create an ALAP schedule. schedule = Schedule(sfg, scheduler=ALAPScheduler()) print("Scheduling time:", schedule.schedule_time) -# schedule.show() +schedule.show() # %% # Create an EarliestDeadline schedule that satisfies the resource constraints. resources = {MADS.type_name(): 1, Reciprocal.type_name(): 1} schedule = Schedule(sfg, scheduler=EarliestDeadlineScheduler(resources)) print("Scheduling time:", schedule.schedule_time) -# schedule.show() +schedule.show() # %% # Create a LeastSlackTime schedule that satisfies the resource constraints. schedule = Schedule(sfg, scheduler=LeastSlackTimeScheduler(resources)) print("Scheduling time:", schedule.schedule_time) -# schedule.show() +schedule.show() # %% # Create a MaxFanOutScheduler schedule that satisfies the resource constraints. schedule = Schedule(sfg, scheduler=MaxFanOutScheduler(resources)) print("Scheduling time:", schedule.schedule_time) -# schedule.show() +schedule.show() # %% # Create a HybridScheduler schedule that satisfies the resource constraints. schedule = Schedule(sfg, scheduler=HybridScheduler(resources)) print("Scheduling time:", schedule.schedule_time) -# schedule.edit() +schedule.show() # %% operations = schedule.get_operations() -mads = operations.get_by_type_name("mads") +mads = operations.get_by_type_name(MADS.type_name()) mads.show(title="MADS executions") -reciprocals = operations.get_by_type_name("rec") +reciprocals = operations.get_by_type_name(Reciprocal.type_name()) reciprocals.show(title="Reciprocal executions") -consts = operations.get_by_type_name("c") -consts.show(title="Const executions") -inputs = operations.get_by_type_name("in") +dont_cares = operations.get_by_type_name(DontCare.type_name()) +dont_cares.show(title="Dont-care executions") +inputs = operations.get_by_type_name(Input.type_name()) inputs.show(title="Input executions") -outputs = operations.get_by_type_name("out") +outputs = operations.get_by_type_name(Output.type_name()) outputs.show(title="Output executions") mads_pe = ProcessingElement(mads, entity_name="mad") reciprocal_pe = ProcessingElement(reciprocals, entity_name="rec") -const_pe = ProcessingElement(consts, entity_name="c") +dont_care_pe = ProcessingElement(dont_cares, entity_name="dc") pe_in = ProcessingElement(inputs, entity_name='input') pe_out = ProcessingElement(outputs, entity_name='output') @@ -110,7 +111,7 @@ direct.show(title="Direct interconnects") # %% arch = Architecture( - {mads_pe, reciprocal_pe, const_pe, pe_in, pe_out}, + {mads_pe, reciprocal_pe, dont_care_pe, pe_in, pe_out}, memories, direct_interconnects=direct, ) -- GitLab From 57770cf089bd3eb41212c1f4b517b3c35f03a392 Mon Sep 17 00:00:00 2001 From: Simon Bjurek <simbj106@student.liu.se> Date: Mon, 17 Feb 2025 08:43:49 +0100 Subject: [PATCH 3/3] updated so that execution time does not have to be set for dont cares --- b_asic/core_operations.py | 24 ++++++------------------ examples/ldlt_matrix_inverse.py | 2 -- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/b_asic/core_operations.py b/b_asic/core_operations.py index 175c59d1..55421296 100644 --- a/b_asic/core_operations.py +++ b/b_asic/core_operations.py @@ -1677,6 +1677,7 @@ class DontCare(AbstractOperation): output_count=1, name=name, latency_offsets={"out0": 0}, + execution_time=0, ) @classmethod @@ -1753,6 +1754,7 @@ class Sink(AbstractOperation): output_count=0, name=name, latency_offsets={"in0": 0}, + execution_time=0, ) @classmethod @@ -1777,28 +1779,14 @@ class Sink(AbstractOperation): ) -> tuple[tuple[tuple[float, float], ...], tuple[tuple[float, float], ...]]: # Doc-string inherited return ( - ( - (-0.5, 0), - (-0.5, 1), - (-0.25, 1), - (0, 0.5), - (-0.25, 0), - (-0.5, 0), - ), - ( - (-0.5, 0), - (-0.5, 1), - (-0.25, 1), - (0, 0.5), - (-0.25, 0), - (-0.5, 0), - ), + ((0, 0), (0, 1), (0.25, 1), (0.5, 0.5), (0.25, 0), (0, 0)), + ((0, 0), (0, 1), (0.25, 1), (0.5, 0.5), (0.25, 0), (0, 0)), ) def get_input_coordinates(self) -> tuple[tuple[float, float], ...]: # doc-string inherited - return tuple() + return ((0, 0.5),) def get_output_coordinates(self) -> tuple[tuple[float, float], ...]: # doc-string inherited - return ((0, 0.5),) + return tuple() diff --git a/examples/ldlt_matrix_inverse.py b/examples/ldlt_matrix_inverse.py index 3921afe4..845bd14b 100644 --- a/examples/ldlt_matrix_inverse.py +++ b/examples/ldlt_matrix_inverse.py @@ -27,10 +27,8 @@ sfg # %% # Set latencies and execution times. -sfg.set_latency_of_type(DontCare.type_name(), 0) # REMOVE!!! sfg.set_latency_of_type(MADS.type_name(), 3) sfg.set_latency_of_type(Reciprocal.type_name(), 2) -sfg.set_execution_time_of_type(DontCare.type_name(), 0) # REMOVE!!! sfg.set_execution_time_of_type(MADS.type_name(), 1) sfg.set_execution_time_of_type(Reciprocal.type_name(), 1) -- GitLab