Skip to content

Commit

Permalink
Added a compilation order heuristic for compiling C before Nc process…
Browse files Browse the repository at this point in the history
…es (lava-nc#275)

* DiGraphBase: collapse a cycle of length 1 correctly.

Signed-off-by: Risbud, Sumedh <[email protected]>

* Added 'C-first-Nc-second compilation heuristic for heterogeneous ProcGroups containing C and Nc Processes

Signed-off-by: Risbud, Sumedh <[email protected]>

* Bugfix to C-first-Nc-second heuristic

Signed-off-by: Risbud, Sumedh <[email protected]>
  • Loading branch information
srrisbud authored Jul 27, 2022
1 parent 2c35606 commit 53e2781
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/lava/magma/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,42 @@ def _create_subcompilers(
initialized with the Processes they will compile.
"""
subcompilers = []
c_idx = []
nc_idx = []
# Go through all required subcompiler classes...
for subcompiler_class, procs in compiler_type_to_procs.items():
for idx, (subcompiler_class, procs) in \
enumerate(compiler_type_to_procs.items()):
# ...create the subcompiler instance...
compiler = subcompiler_class(procs, self._compile_config)
# ...and add it to the list.
subcompilers.append(compiler)

# Remember the index for C and Nc subcompilers:
if isinstance(compiler, CProcCompiler):
c_idx.append(idx)
if isinstance(compiler, NcProcCompiler):
nc_idx.append(idx)

# Implement the heuristic "C-first Nc-second" whenever C and Nc
# compilers are in the list. The subcompiler.compile is called in
# their order of appearance in this list.
# ToDo: In the implementation below, we assume that there is only one
# instance of each subcompiler (Py/C/Nc) per ProcGroup. This should
# be true as long as the `compiler_type_to_procs` mapping comes from
# `self._map_subcompiler_type_to_procs`.
# 1. Confirm that there is only one instance of C and Nc subcompilers
if len(c_idx) > 1 or len(nc_idx) > 1:
raise AssertionError("More than one instance of C or Nc "
"subcompiler detected.")
# 2. If the index of C subcompiler is larger (appears later in the
# list), then swap it with Nc subcompiler.
if len(c_idx) > 0 and len(nc_idx) > 0:
if c_idx[0] > nc_idx[0]:
subcompilers[c_idx[0]], subcompilers[nc_idx[0]] = subcompilers[
nc_idx[0]], subcompilers[c_idx[0]]
# We have ensured that C subcompiler appears before Nc subcompiler in
# the list we return. As compile() is called serially on each
# subcompiler, C Processes will be compiled before Nc Processes
# within a ProcGroup.
return subcompilers

@staticmethod
Expand Down

0 comments on commit 53e2781

Please sign in to comment.