Skip to content

Commit

Permalink
Merge pull request #2810 from stfc/martin_fileinfo_cache
Browse files Browse the repository at this point in the history
Add file caching features to FileInfo
  • Loading branch information
arporter authored Jan 10, 2025
2 parents 4b3662e + 5e00ccd commit b051810
Show file tree
Hide file tree
Showing 17 changed files with 1,312 additions and 220 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ src/*.egg-info
.venv
cov.xml
.coverage.*
*.psycache
__pycache__
3 changes: 3 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

9) PR #2842 for #2841. Update all copyright date ranges for 2025.

10) PR #2810. Adds caching of the fparser2 parse tree to FileInfo. Is
disabled by default.

release 3.0.0 6th of December 2024

1) PR #2477 for #2463. Add support for Fortran Namelist statements.
Expand Down
64 changes: 64 additions & 0 deletions doc/developer_guide/module_manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

.. _module_manager:



Module Manager
##############

Expand Down Expand Up @@ -135,3 +137,65 @@ which prints the filenames of all modules used in ``tl_testkern_mod``:
Module: constants_mod constants_mod.f90
Module: fs_continuity_mod fs_continuity_mod.f90
Module: kernel_mod kernel_mod.f90



FileInfo
========

FileInfo is a class that is used to store information about Fortran files.

This information can include:

- The source code itself
- The fparser tree information
- The PSyIR tree information

All this information is gathered in this single class since this also
allows for caching of it, see next section



Caching
=======

The `ModuleManager` and `FileInfo` support a caching of the
fparser tree representation of a source code.
(Support for PSyIR is planned)

This caching has to be **explicitly enabled** in the constructor
of `ModuleManager`.


.. testcode ::
mod_manager = ModuleManager.get(use_caching=True)
Most of the time in the PSyIR generation is currently spent in the
fparser tree generation. Consequently, this leads to significant
speed-ups in the process of reading and parsing the source code
of modules.

The default cache file is named the same way as the source file,
but replaces the file extension with `.psycache`. E.g., a cache file
for the source file `foo.f90` will be called `foo.psycache`.

The caching algorithm to obtain the fparser tree OR PSyIR is briefly described as follows:

- If fparser tree / PSyIR was read before: RETURN fparser tree or PSyIR
- If source code is not yet read:

- Read the content of the file
- Create the source's checksum.
- Read cache file if it exists:

- If the checksum of the cache is the same as the one of the source:

- load the fparser tree / PSyIR from the cache file and RETURN fparser tree or PSyIR
- Create the fparser tree / PSyIR from the source code
- Save cache file IF it was not loaded before:

- Update cache information
- Store to cache file
- RETURN fparser tree or PSyIR
4 changes: 4 additions & 0 deletions doc/user_guide/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ locations searched is now:
2. ``<python-base-dir>/share/psyclone/``
3. ``${HOME}/.local/share/psyclone/``

As a last resort, the location
``<psyclone-src-base>/config/``
is searched in case PSyclone was installed in editable mode.

Note that for developers a slightly different configuration handling
is implemented, see :ref:`dev_guide:dev_configuration` for details.

Expand Down
Binary file modified psyclone.pdf
Binary file not shown.
10 changes: 10 additions & 0 deletions src/psyclone/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ def find_file():
- ${HOME}/.local/share/psyclone/
- <system-install-prefix>/share/psyclone/
- <psyclone-installation-base>/share/psyclone/
- <psyclone-src-base>/config/
:returns: the fully-qualified path to the configuration file
:rtype: str
Expand Down Expand Up @@ -449,9 +450,18 @@ def find_file():
if not within_virtual_env():
# 4. <python-installation-base>/share/psyclone/
_file_paths.append(share_dir)

# 5. <psyclone-installation-base>/share/psyclone/
_file_paths.extend(pkg_share_dir)

# 6. <psyclone-src-base>/config/
# Search for configuration file relative to this source file
dev_dir_tmp = os.path.dirname(__file__)
dev_dir_tmp = os.path.split(dev_dir_tmp)[0] # Go down one level
dev_dir_tmp = os.path.split(dev_dir_tmp)[0] # Go down another level
dev_path = os.path.join(dev_dir_tmp, "config")
_file_paths.append(dev_path)

for cfile in [os.path.join(cdir, _FILE_NAME) for cdir in _file_paths]:
if os.path.isfile(cfile):
return cfile
Expand Down
4 changes: 3 additions & 1 deletion src/psyclone/parse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# Author: J. Henrichs, Bureau of Meteorology
# Modified: M. Schreiber, Univ. Grenoble Alpes / Inria / Lab. Jean-Kuntzmann

'''This directory contains classes related to parsing Fortran.
'''

from psyclone.parse.file_info import FileInfo
from psyclone.parse.file_info import FileInfo, FileInfoFParserError
from psyclone.parse.module_info import ModuleInfo, ModuleInfoError
from psyclone.parse.module_manager import ModuleManager


# For AutoAPI documentation generation.
__all__ = [
'FileInfo',
'FileInfoFParserError',
'ModuleInfo',
'ModuleInfoError',
'ModuleManager'
Expand Down
Loading

0 comments on commit b051810

Please sign in to comment.