Skip to content

Commit ddfacda

Browse files
committed
tests: Update estimate-memory tests and consolidate boilerplate
1 parent 19b825b commit ddfacda

1 file changed

Lines changed: 40 additions & 45 deletions

File tree

tests/test_operator.py

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,11 +2067,21 @@ class TestEstimateMemory:
20672067
"""Tests for the Operator.estimate_memory() utility"""
20682068

20692069
_array_temp = "r0L0(x, y)" if "CXX" in configuration['language'] else "r0[x][y]"
2070+
_devicelangs = ('openacc',)
20702071

20712072
def parse_output(self, summary, expected):
20722073
"""Parse estimate_memory machine-readable output"""
20732074
assert (summary['host'], summary['device']) == expected
20742075

2076+
def sum_sizes(self, funcs):
2077+
return sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2078+
for func in funcs)
2079+
2080+
def make_check(self, check):
2081+
if configuration['language'] in self._devicelangs:
2082+
return (check, check)
2083+
return (check, 0)
2084+
20752085
@pytest.mark.parametrize('shape', [(11,), (101, 101), (101, 101, 101)])
20762086
@pytest.mark.parametrize('dtype', [np.int8, np.int16, np.float32,
20772087
np.float32, np.complex64])
@@ -2088,7 +2098,7 @@ def test_basic_usage(self, caplog, shape, dtype, so):
20882098

20892099
# Check output of estimate_memory
20902100
host = reduce(mul, f.shape_allocated)*np.dtype(f.dtype).itemsize
2091-
expected = (host, 0)
2101+
expected = self.make_check(host)
20922102
self.parse_output(summary, expected)
20932103

20942104
def test_multiple_objects(self, caplog):
@@ -2101,9 +2111,8 @@ def test_multiple_objects(self, caplog):
21012111
summary = op.estimate_memory()
21022112
assert "Allocating" not in caplog.text
21032113

2104-
check = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2105-
for func in (f, g))
2106-
expected = (check, 0)
2114+
check = self.sum_sizes((f, g))
2115+
expected = self.make_check(check)
21072116
self.parse_output(summary, expected)
21082117

21092118
@pytest.mark.parametrize('time', [True, False])
@@ -2121,9 +2130,8 @@ def test_sparse(self, caplog, time):
21212130
summary = op.estimate_memory()
21222131
assert "Allocating" not in caplog.text
21232132

2124-
check = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2125-
for func in (f, src, src.coordinates))
2126-
expected = (check, 0)
2133+
check = self.sum_sizes((f, src, src.coordinates))
2134+
expected = self.make_check(check)
21272135
self.parse_output(summary, expected)
21282136

21292137
@pytest.mark.parametrize('save', [None, Buffer(3), 10])
@@ -2136,7 +2144,7 @@ def test_timefunction(self, caplog, save):
21362144
summary = op.estimate_memory()
21372145
assert "Allocating" not in caplog.text
21382146
check = reduce(mul, f.shape_allocated)*np.dtype(f.dtype).itemsize
2139-
expected = (check, 0)
2147+
expected = self.make_check(check)
21402148
self.parse_output(summary, expected)
21412149

21422150
def test_mashup(self, caplog):
@@ -2158,10 +2166,8 @@ def test_mashup(self, caplog):
21582166
summary = op.estimate_memory()
21592167
assert "Allocating" not in caplog.text
21602168

2161-
check = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2162-
for func in (f, g, src0, src0.coordinates,
2163-
src1, src1.coordinates))
2164-
expected = (check, 0)
2169+
check = self.sum_sizes((f, g, src0, src0.coordinates, src1, src1.coordinates))
2170+
expected = self.make_check(check)
21652171
self.parse_output(summary, expected)
21662172

21672173
@pytest.mark.parametrize('override', [True, False])
@@ -2201,37 +2207,30 @@ def test_temp_array(self, caplog, override):
22012207
summary = op.estimate_memory(**kwargs)
22022208
assert "Allocating" not in caplog.text
22032209

2204-
check = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2205-
for func in funcs)
2210+
check = self.sum_sizes(funcs)
22062211

22072212
# Factor in the temp array
22082213
check += reduce(mul, b.shape_allocated)*np.dtype(b.dtype).itemsize
2209-
2210-
expected = (check, 0)
2214+
expected = self.make_check(check)
22112215
self.parse_output(summary, expected)
22122216

22132217
def test_overrides(self, caplog):
2214-
# TODO: Consolidate this boilerplate
2215-
grid0 = Grid(shape=(101, 101))
2218+
def setup(size, npoint, nt, counter):
2219+
grid = Grid(shape=(size, size))
2220+
# Original fields
2221+
f = Function(name=f'f{counter}', grid=grid, space_order=4)
2222+
tf = TimeFunction(name=f'tf{counter}', grid=grid, space_order=4)
2223+
s = SparseFunction(name=f's{counter}', grid=grid, npoint=npoint)
2224+
st = SparseTimeFunction(name=f'st{counter}', grid=grid, npoint=npoint, nt=nt)
2225+
2226+
return f, tf, s, st
2227+
22162228
# Original fields
2217-
f0 = Function(name='f0', grid=grid0, space_order=4)
2218-
tf0 = TimeFunction(name='tf0', grid=grid0, space_order=4)
2219-
s0 = SparseFunction(name='s0', grid=grid0, npoint=100)
2220-
st0 = SparseTimeFunction(name='st0', grid=grid0, npoint=100, nt=10)
2221-
2222-
grid1 = Grid(shape=(201, 201)) # Bigger grid so overrides are distinct
2223-
# Replacement fields
2224-
f1 = Function(name='f1', grid=grid1, space_order=4)
2225-
tf1 = TimeFunction(name='tf1', grid=grid1, space_order=4)
2226-
s1 = SparseFunction(name='s1', grid=grid1, npoint=200)
2227-
st1 = SparseTimeFunction(name='st1', grid=grid1, npoint=200, nt=20)
2228-
2229-
grid2 = Grid(shape=(51, 51)) # Smaller grid so overrides are distinct
2230-
# Alternative replacement fields
2231-
f2 = Function(name='f2', grid=grid2, space_order=4)
2232-
tf2 = TimeFunction(name='tf2', grid=grid2, space_order=4)
2233-
s2 = SparseFunction(name='s2', grid=grid2, npoint=50)
2234-
st2 = SparseTimeFunction(name='st2', grid=grid2, npoint=50, nt=5)
2229+
f0, tf0, s0, st0 = setup(101, 100, 10, 0)
2230+
# Replacement fields with bigger grid, etc
2231+
f1, tf1, s1, st1 = setup(201, 200, 20, 1)
2232+
# Replacement fields with smaller grid, etc
2233+
f2, tf2, s2, st2 = setup(51, 50, 5, 2)
22352234

22362235
eq0 = Eq(f0, 1)
22372236
eq1 = Eq(tf0, 1)
@@ -2244,20 +2243,16 @@ def test_overrides(self, caplog):
22442243
# Apply overrides for the check
22452244
summary0 = op.estimate_memory(f0=f1, tf0=tf1, s0=s1, st0=st1)
22462245

2247-
check0 = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2248-
for func in (f1, tf1, s1, s1.coordinates, st1, st1.coordinates))
2249-
2250-
expected0 = (check0, 0)
2246+
check0 = self.sum_sizes((f1, tf1, s1, s1.coordinates, st1, st1.coordinates))
2247+
expected0 = self.make_check(check0)
22512248
self.parse_output(summary0, expected0)
22522249

22532250
# Check with a second set of overrides
22542251
summary1 = op.estimate_memory(f0=f2, tf0=tf2, s0=s2, st0=st2)
22552252
assert "Allocating" not in caplog.text
22562253

2257-
check1 = sum(reduce(mul, func.shape_allocated)*np.dtype(func.dtype).itemsize
2258-
for func in (f2, tf2, s2, s2.coordinates, st2, st2.coordinates))
2259-
2260-
expected1 = (check1, 0)
2254+
check1 = self.sum_sizes((f2, tf2, s2, s2.coordinates, st2, st2.coordinates))
2255+
expected1 = self.make_check(check1)
22612256
self.parse_output(summary1, expected1)
22622257

22632258
def test_device(self, caplog):
@@ -2269,7 +2264,7 @@ def test_device(self, caplog):
22692264

22702265
f = Function(name='f', grid=grid, space_order=2)
22712266

2272-
# Compiler is never invoked, so this should be fine
2267+
# Compiler is never invoked, so this is fine
22732268
config = {'log_level': 'DEBUG', 'language': 'openacc',
22742269
'platform': 'nvidiaX'}
22752270
with switchconfig(**config), caplog.at_level(logging.DEBUG):

0 commit comments

Comments
 (0)