Skip to content

Commit cd5200e

Browse files
New Feature - Pass coloring strategy via command line for he_prep (#142)
* modified assembler to enable command line controlled coloring strategy * Removing unused verbose param --------- Co-authored-by: Jose Rojas Chaves <jose.rojas.chaves@intel.com>
1 parent 21f79e8 commit cd5200e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

assembler_tools/hec-assembler-tools/assembler/stages/prep/preprocessor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def reduce_var_deps_by_var(mem_model: MemoryModel, insts_list: list, var_name: s
150150
last_pos += 1
151151

152152

153-
def assign_register_banks_to_vars(mem_model: MemoryModel, insts_list: list, use_bank0: bool) -> str:
153+
def assign_register_banks_to_vars(
154+
mem_model: MemoryModel, insts_list: list, use_bank0: bool, strategy="largest_first", interchange=False
155+
) -> str:
154156
"""
155157
Assigns register banks to variables using vertex coloring graph algorithm.
156158
@@ -171,7 +173,8 @@ def assign_register_banks_to_vars(mem_model: MemoryModel, insts_list: list, use_
171173
If `False`, bank 0 will not be assigned to any variable. Resulting ASM instructions
172174
should add corresponding `move` instructions to move variables from bank 0 to
173175
correct bank.
174-
verbose (bool, optional): If True, prints verbose output. Defaults to False.
176+
strategy (str, optional): Strategy for greedy coloring algorithm. Defaults to "largest_first".
177+
interchange (bool, optional): Whether to use interchange in greedy coloring. Defaults to False.
175178
176179
Raises:
177180
ValueError: Thrown for these cases:
@@ -193,7 +196,7 @@ def assign_register_banks_to_vars(mem_model: MemoryModel, insts_list: list, use_
193196
# Extract the dependency graph for variables
194197
dep_graph_vars, dest_names, source_names = dependency_graph_for_vars(insts_list)
195198
only_sources = source_names - dest_names # Find which variables are ever only used as sources
196-
color_dict = nx.greedy_color(dep_graph_vars) # Do coloring
199+
color_dict = nx.greedy_color(dep_graph_vars, strategy=strategy, interchange=interchange) # Do coloring
197200

198201
needs_reduction = False
199202
for var_name, bank in color_dict.items():

assembler_tools/hec-assembler-tools/he_prep.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def main(args):
6969
- output_file_name
7070
- mem_file
7171
- verbose
72+
optional:
73+
- strategy
74+
- interchange
7275
"""
7376

7477
GlobalConfig.debugVerbose = args.verbose
@@ -93,7 +96,9 @@ def main(args):
9396
num_input_instr: int = len(insts_listing) # track number of instructions in input kernel
9497
if args.verbose > 0:
9598
print("Assigning register banks to variables...")
96-
preprocessor.assign_register_banks_to_vars(hec_mem_model, insts_listing, use_bank0=False)
99+
preprocessor.assign_register_banks_to_vars(
100+
hec_mem_model, insts_listing, use_bank0=False, strategy=args.strategy, interchange=args.interchange
101+
)
97102

98103
# Determine output file name
99104
if not args.output_file_name:
@@ -195,6 +200,17 @@ def parse_args():
195200
"Increase level of verbosity by specifying flag multiple times, e.g. -vv"
196201
),
197202
)
203+
parser.add_argument(
204+
"--strategy",
205+
default="largest_first",
206+
help="Strategy for greedy coloring algorithm. Defaults to 'largest_first'.",
207+
)
208+
parser.add_argument(
209+
"--interchange",
210+
action="store_true",
211+
default=False,
212+
help="Whether to use interchange in greedy coloring. Defaults to False.",
213+
)
198214
p_args = parser.parse_args()
199215
p_args.split_on = bool(p_args.split_inst_limit != float("inf") or p_args.split_vars_limit != float("inf"))
200216
if p_args.split_on:
@@ -223,6 +239,8 @@ def parse_args():
223239
print(f"Split Inst Limit: {args.split_inst_limit}")
224240
print(f"Split Vars Limit: {args.split_vars_limit}")
225241
print(f"Split On: {args.split_on}")
242+
print(f"Graph Coloring Strategy: {args.strategy}")
243+
print(f"Graph Coloring Interchange: {args.interchange}")
226244

227245
main(args)
228246

0 commit comments

Comments
 (0)