Skip to content

Commit 9d21e0d

Browse files
committed
misc: Remove memoized_func -> functools.cache
1 parent 444a442 commit 9d21e0d

3 files changed

Lines changed: 21 additions & 61 deletions

File tree

devito/arch/archinfo.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Collection of utilities to detect properties of the underlying architecture."""
22

3-
from functools import cached_property
3+
from functools import cache, cached_property
44
from subprocess import PIPE, Popen, DEVNULL, run
55
from pathlib import Path
66
import ctypes
@@ -14,7 +14,7 @@
1414
import psutil
1515

1616
from devito.logger import warning
17-
from devito.tools import as_tuple, all_equal, memoized_func
17+
from devito.tools import as_tuple, all_equal
1818

1919
__all__ = ['platform_registry', 'get_cpu_info', 'get_gpu_info', 'get_nvidia_cc',
2020
'get_cuda_path', 'get_hip_path', 'check_cuda_runtime', 'get_m1_llvm_path',
@@ -41,7 +41,7 @@
4141
'PVC', 'INTELGPUMAX', 'MAX1100', 'MAX1550']
4242

4343

44-
@memoized_func
44+
@cache
4545
def get_cpu_info():
4646
"""Attempt CPU info autodetection."""
4747

@@ -163,7 +163,7 @@ def get_cpu_brand():
163163
return cpu_info
164164

165165

166-
@memoized_func
166+
@cache
167167
def get_gpu_info():
168168
"""Attempt GPU info autodetection."""
169169

@@ -488,7 +488,7 @@ def parse_product_arch():
488488
return None
489489

490490

491-
@memoized_func
491+
@cache
492492
def get_nvidia_cc():
493493
libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll')
494494
for libname in libnames:
@@ -511,7 +511,7 @@ def get_nvidia_cc():
511511
return 10*cc_major.value + cc_minor.value
512512

513513

514-
@memoized_func
514+
@cache
515515
def get_cuda_path():
516516
# *** First try: via commonly used environment variables
517517
for i in ['CUDA_HOME', 'CUDA_ROOT']:
@@ -531,7 +531,7 @@ def get_cuda_path():
531531
return None
532532

533533

534-
@memoized_func
534+
@cache
535535
def get_advisor_path():
536536
"""
537537
Detect if Intel Advisor is installed on the machine and return
@@ -552,7 +552,7 @@ def get_advisor_path():
552552
return path
553553

554554

555-
@memoized_func
555+
@cache
556556
def get_hip_path():
557557
# *** First try: via commonly used environment variables
558558
for i in ['HIP_HOME']:
@@ -563,7 +563,7 @@ def get_hip_path():
563563
return None
564564

565565

566-
@memoized_func
566+
@cache
567567
def get_m1_llvm_path(language):
568568
# Check if Apple's llvm is installed (installable via Homebrew), which supports
569569
# OpenMP.
@@ -595,7 +595,7 @@ def get_m1_llvm_path(language):
595595
return None
596596

597597

598-
@memoized_func
598+
@cache
599599
def check_cuda_runtime():
600600
libnames = ('libcudart.so', 'libcudart.dylib', 'cudart.dll')
601601
for libname in libnames:
@@ -623,7 +623,7 @@ def check_cuda_runtime():
623623
warning("Unable to check compatibility of NVidia driver and runtime")
624624

625625

626-
@memoized_func
626+
@cache
627627
def lscpu():
628628
try:
629629
p1 = Popen(['lscpu'], stdout=PIPE, stderr=PIPE)
@@ -645,7 +645,7 @@ def lscpu():
645645
return {}
646646

647647

648-
@memoized_func
648+
@cache
649649
def get_platform():
650650
"""Attempt Platform autodetection."""
651651

@@ -1111,7 +1111,7 @@ def march(cls):
11111111
return fallback
11121112

11131113

1114-
@memoized_func
1114+
@cache
11151115
def node_max_mem_trans_nbytes(platform):
11161116
"""
11171117
Return the maximum memory transaction size in bytes for the underlying

devito/arch/compiler.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from functools import partial
1+
from functools import cache, partial
22
from hashlib import sha1
33
from os import environ, path, makedirs
44
from packaging.version import Version
@@ -19,13 +19,12 @@
1919
from devito.exceptions import CompilationError
2020
from devito.logger import debug, warning
2121
from devito.parameters import configuration
22-
from devito.tools import (as_list, change_directory, filter_ordered,
23-
memoized_func, make_tempdir)
22+
from devito.tools import as_list, change_directory, filter_ordered, make_tempdir
2423

2524
__all__ = ['sniff_mpi_distro', 'compiler_registry']
2625

2726

28-
@memoized_func
27+
@cache
2928
def sniff_compiler_version(cc, allow_fail=False):
3029
"""
3130
Detect the compiler version.
@@ -99,7 +98,7 @@ def sniff_compiler_version(cc, allow_fail=False):
9998
return ver
10099

101100

102-
@memoized_func
101+
@cache
103102
def sniff_mpi_distro(mpiexec):
104103
"""
105104
Detect the MPI version.
@@ -117,7 +116,7 @@ def sniff_mpi_distro(mpiexec):
117116
return 'unknown'
118117

119118

120-
@memoized_func
119+
@cache
121120
def sniff_mpi_flags(mpicc='mpicc'):
122121
mpi_distro = sniff_mpi_distro('mpiexec')
123122
if mpi_distro != 'OpenMPI':
@@ -131,7 +130,7 @@ def sniff_mpi_flags(mpicc='mpicc'):
131130
return compile_flags.split(), link_flags.split()
132131

133132

134-
@memoized_func
133+
@cache
135134
def call_capture_output(cmd):
136135
"""
137136
Memoize calls to codepy's `call_capture_output` to avoid leaking memory due

devito/tools/memoization.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,14 @@
33
from itertools import tee
44
from typing import TypeVar
55

6-
__all__ = ['memoized_func', 'memoized_meth', 'memoized_generator', 'CacheInstances']
7-
8-
9-
class memoized_func:
10-
"""
11-
Decorator. Caches a function's return value each time it is called.
12-
If called later with the same arguments, the cached value is returned
13-
(not reevaluated). This decorator may also be used on class methods,
14-
but it will cache at the class level; to cache at the instance level,
15-
use ``memoized_meth``.
16-
17-
Adapted from: ::
18-
19-
https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
20-
"""
21-
22-
def __init__(self, func):
23-
self.func = func
24-
self.cache = {}
25-
26-
def __call__(self, *args, **kw):
27-
if not isinstance(args, Hashable):
28-
# Uncacheable, a list, for instance.
29-
# Better to not cache than blow up.
30-
return self.func(*args, **kw)
31-
key = (self.func, args, frozenset(kw.items()))
32-
if key in self.cache:
33-
return self.cache[key]
34-
else:
35-
value = self.func(*args, **kw)
36-
self.cache[key] = value
37-
return value
38-
39-
def __repr__(self):
40-
"""Return the function's docstring."""
41-
return self.func.__doc__
42-
43-
def __get__(self, obj, objtype):
44-
"""Support instance methods."""
45-
return partial(self.__call__, obj)
6+
__all__ = ['memoized_meth', 'memoized_generator', 'CacheInstances']
467

478

489
class memoized_meth:
4910
"""
5011
Decorator. Cache the return value of a class method.
5112
52-
Unlike ``memoized_func``, the return value of a given method invocation
13+
Unlike ``functools.cache``, the return value of a given method invocation
5314
will be cached on the instance whose method was invoked. All arguments
5415
passed to a method decorated with memoize must be hashable.
5516

0 commit comments

Comments
 (0)