Skip to content

Commit

Permalink
Combine code for sparse model functions and their index files
Browse files Browse the repository at this point in the history
Combine code for sparse model functions and their index files, i.e. generate
only a single file instead of 3 individual files for content, rowvals, and
colptrs, respectively.

Advantage: Faster import of smaller models and fewer files. For a toy model,
this reduced the build steps from 44 to 28, and reduces build time by >20%.

Disadvantage: None found, so I don't think it worth adding an option
for (not) combining those files. For larger models, there shouldn't be any impact.
The extra time for compiling the index arrays should be negligible compared
to computing the contents.

Related to #2119
  • Loading branch information
dweindl committed Aug 18, 2023
1 parent fa69dd3 commit b7e9ed6
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Set,
Tuple,
Union,
Literal
)

import numpy as np
Expand Down Expand Up @@ -2838,9 +2839,6 @@ def _generate_c_code(self) -> None:
if func_info.generate_body:
dec = log_execution_time(f"writing {func_name}.cpp", logger)
dec(self._write_function_file)(func_name)
if func_name in sparse_functions and func_info.body:
self._write_function_index(func_name, "colptrs")
self._write_function_index(func_name, "rowvals")

for name in self.model.sym_names():
# only generate for those that have nontrivial implementation,
Expand Down Expand Up @@ -3132,21 +3130,29 @@ def _write_function_file(self, function: str) -> None:
self._build_hints.add(fun["build_hint"])
lines.insert(0, fun["include"])

if function in sparse_functions:
lines.extend(self._generate_function_index(function, "colptrs"))
lines.extend(self._generate_function_index(function, "rowvals"))

# if not body is None:
filename = os.path.join(self.model_path, f"{function}.cpp")
with open(filename, "w") as fileout:
fileout.write("\n".join(lines))

def _write_function_index(self, function: str, indextype: str) -> None:
def _generate_function_index(
self, function: str, indextype: Literal["colptrs", "rowvals"]
) -> List[str]:
"""
Generate equations and write the C++ code for the function
``function``.
Generate equations and C++ code for the function ``function``.
:param function:
name of the function to be written (see ``self.functions``)
:param indextype:
type of index {'colptrs', 'rowvals'}
:returns:
The code lines for the respective function index file
"""
if indextype == "colptrs":
values = self.model.colptrs(function)
Expand Down Expand Up @@ -3233,11 +3239,7 @@ def _write_function_index(self, function: str, indextype: str) -> None:
]
)

filename = f"{function}_{indextype}.cpp"
filename = os.path.join(self.model_path, filename)

with open(filename, "w") as fileout:
fileout.write("\n".join(lines))
return lines

def _get_function_body(self, function: str, equations: sp.Matrix) -> List[str]:
"""
Expand Down

0 comments on commit b7e9ed6

Please sign in to comment.