Skip to content

Commit 7fd3e15

Browse files
committed
Merge branch 'captures-seissol63' into dev3
2 parents 7974d8a + 56ca3a3 commit 7fd3e15

7 files changed

Lines changed: 208 additions & 41 deletions

File tree

‎tests/seissol_suite.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
in `values.json.xz` and named by `{"ref": hash}`; each equation system's file
1010
holds its distinct descriptions by hash and, per configuration, which kernel
1111
is which. This reads it back into the descriptions yateto sent.
12+
13+
A family of more than one parameter is numbered in the order yateto handed
14+
its members over, as the exporter recorded them then, the last parameter
15+
fastest: over a space of shape (n0, n1) the member of parameters p is
16+
`[p0*n1 + p1]` here, where SeisSol calls it `[p0 + n0*p1]`, the first
17+
parameter fastest (`tools/bench/README.md`) -- likewise over three, as the
18+
elastic neighbor fluxes here are. `[0]` is the same member either way.
1219
"""
1320

1421
from __future__ import annotations

‎tests/test_seissol_export.py‎

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
66
Against a stand-in for SeisSol's codegen -- a generate.py that keeps the
77
arguments it is given, says which memory layout it read and hands the device
8-
exporter one kernel, and the three names of yateto the recorder patches -- so
9-
that no SeisSol is needed. The defaults have to ask what the first captures
10-
(tools/bench/data) were recorded with, and record it as they did, byte for
11-
byte: those captures stay reproducible beside the ones of a build's own
12-
settings. With SEISSOL_CODEGEN at seissol21's codegen, one of the first
13-
captures is re-exported as well, and has to be the committed file.
8+
exporter one kernel (or a family over a space of two parameters), and the
9+
names of yateto the recorder patches -- so that no SeisSol is needed. The
10+
defaults have to ask what the first captures (tools/bench/data) were recorded
11+
with; with --member-order iteration, their numbering of a family's members,
12+
they record it as those did, byte for byte: those captures stay reproducible
13+
beside the ones of a build's own settings. A family's members are named by
14+
yateto's index of them by default, which SeisSol calls them by. With
15+
SEISSOL_CODEGEN at seissol21's codegen, one of the first captures is
16+
re-exported as well, and has to be the committed file; and a family of two
17+
parameters is recorded under SeisSol's numbering.
1418
"""
1519

1620
from __future__ import annotations
@@ -59,10 +63,35 @@ def _handleTensor(self, tensor, *args, **kwargs):
5963

6064
VISITOR = '''
6165
class OptimizedKernelGenerator:
66+
def generateKernelOutline(self, nonZeroFlops, cfg, gemm_cfg, target, attrs=None):
67+
cfg() # the stand-in's kernel: it hands its description over, as a routine generator would
68+
return object()
69+
6270
def generate(self, cpp, header, name, kernelOutlines, *args, **kwargs):
6371
return None
6472
'''
6573

74+
#: A family over simpleParameterSpace(4, 2), as yateto's addFamily builds one: each member numbered
75+
#: linear(stride, p) with stride (1, 4) -- the first parameter fastest -- and its outline generated in the
76+
#: parameter space's order, the last parameter fastest (`Generator.generate`: `family.items()`).
77+
FAMILY = '''
78+
import itertools, json, os, sys
79+
import tensorforge
80+
from yateto.codegen import visitor
81+
82+
83+
def main():
84+
with open(os.environ['STUB_ARGV'], 'w') as f:
85+
json.dump(sys.argv[1:], f)
86+
exporter = tensorforge.get_routine_generator(None)(None)
87+
gen = visitor.OptimizedKernelGenerator()
88+
outlines = [None] * 8
89+
for p in itertools.product(range(4), range(2)):
90+
outlines[p[0] + 4 * p[1]] = gen.generateKernelOutline(
91+
0, lambda p=p: exporter.add_kernel({'version': 7, 'p': list(p)}), None, 'gpu')
92+
gen.generate(None, None, 'family', outlines, (1, 4))
93+
'''
94+
6695

6796
def _yateto(where: Path) -> Path:
6897
for rel, text in (('yateto/__init__.py', ''), ('yateto/codegen/__init__.py', ''),
@@ -111,8 +140,9 @@ def test_the_defaults_ask_what_the_first_captures_asked(codegen, tmp_path):
111140

112141
def test_the_defaults_record_what_the_first_captures_recorded(codegen, tmp_path):
113142
"""The compact JSON, the first captures' source, and the config's keys
114-
in their order -- none of the new ones."""
115-
_, _, text = _export(codegen, tmp_path)
143+
in their order -- none of the new ones -- under the first captures'
144+
numbering of a family's members."""
145+
_, _, text = _export(codegen, tmp_path, '--member-order', 'iteration')
116146
blob = json.loads(text)
117147
assert text == json.dumps(blob, separators=(',', ':'))
118148
assert blob['source'] == ('SeisSol codegen (seissol21, master plus the damage and viscoacoustic systems) '
@@ -144,7 +174,46 @@ def test_a_builds_settings_reach_generate_py(codegen, tmp_path, premultiply, fla
144174
assert config == dict(equation='elastic', solver='linearck', order=4, precision='s', mechanisms=3,
145175
device='oneapi/pvc', host='skx', plasticity='sq', dr_quad='dunavant',
146176
mem_layout='tensorforge.xml', gemm_tools='Eigen,TensorForge',
147-
premultiply_flux=premultiply == 'on', multiple_simulations=2, workarounds=[])
177+
premultiply_flux=premultiply == 'on', member_order='seissol', multiple_simulations=2,
178+
workarounds=[])
179+
180+
181+
def test_the_default_member_order_is_recorded(codegen, tmp_path):
182+
"""Named as SeisSol names a family's members, a capture says so; under
183+
the first captures' numbering it says nothing, as they do not."""
184+
_, _, text = _export(codegen, tmp_path)
185+
assert json.loads(text)['config']['member_order'] == 'seissol'
186+
_, _, text = _export(codegen, tmp_path, '--member-order', 'iteration')
187+
assert 'member_order' not in json.loads(text)['config']
188+
189+
190+
@pytest.fixture
191+
def family_codegen(tmp_path):
192+
root = tmp_path / 'family'
193+
root.mkdir()
194+
(root / 'generate.py').write_text(FAMILY)
195+
_yateto(root)
196+
return root
197+
198+
199+
def test_a_familys_members_are_named_as_seissol_names_them(family_codegen, tmp_path):
200+
"""yateto's index of a member, k = p0 + 4*p1 over (4, 2): the family's
201+
member [1] has the parameters (1, 0), though it was handed over third."""
202+
_, _, text = _export(family_codegen, tmp_path)
203+
recorded = json.loads(text)['descriptions']
204+
assert sorted(recorded) == sorted(f'family[{k}]' for k in range(8))
205+
for k in range(8):
206+
assert recorded[f'family[{k}]']['p'] == [k % 4, k // 4], k
207+
assert json.loads(text)['config']['workarounds'] == []
208+
209+
210+
def test_the_iteration_order_numbers_them_as_handed_over(family_codegen, tmp_path):
211+
"""The committed captures' numbering: the member handed over k-th is
212+
[k], the parameters (k // 2, k % 2) over (4, 2)."""
213+
_, _, text = _export(family_codegen, tmp_path, '--member-order', 'iteration')
214+
recorded = json.loads(text)['descriptions']
215+
for k in range(8):
216+
assert recorded[f'family[{k}]']['p'] == [k // 2, k % 2], k
148217

149218

150219
def test_without_the_flag_generate_py_decides_the_premultiplication(codegen, tmp_path):
@@ -184,14 +253,39 @@ def test_a_yateto_beside_the_codegen(tmp_path):
184253
SEISSOL21 = Path(os.environ.get('SEISSOL_CODEGEN', ROOT.parent / 'seissol21' / 'codegen'))
185254

186255

187-
@pytest.mark.skipif(not (SEISSOL21 / 'generate.py').exists(), reason='no SeisSol codegen (SEISSOL_CODEGEN)')
188-
def test_a_first_capture_re_exports_byte_for_byte(tmp_path):
189-
"""elastic order 3, single precision: the committed capture is the
190-
export with --pretty."""
256+
def _first_capture(tmp_path, *extra):
191257
out = tmp_path / 'elastic-3-s-s1.json'
192258
env = dict(os.environ, SEISSOL_CODEGEN=str(SEISSOL21), PYTHONDONTWRITEBYTECODE='1')
193259
run = subprocess.run([sys.executable, '-B', str(EXPORT), 'elastic', 'linearck', '3', 's', str(out),
194-
'--mechanisms', '0', '--simcount', '1', '--pretty'],
260+
'--mechanisms', '0', '--simcount', '1', '--pretty', *extra],
195261
capture_output=True, text=True, env=env, cwd=tmp_path, timeout=1800)
196262
assert run.returncode == 0, run.stderr
263+
return out
264+
265+
266+
@pytest.mark.skipif(not (SEISSOL21 / 'generate.py').exists(), reason='no SeisSol codegen (SEISSOL_CODEGEN)')
267+
def test_a_first_capture_re_exports_byte_for_byte(tmp_path):
268+
"""elastic order 3, single precision: the committed capture is the
269+
export with --pretty, under its numbering of a family's members."""
270+
out = _first_capture(tmp_path, '--member-order', 'iteration')
197271
assert out.read_bytes() == (ROOT / 'tools' / 'bench' / 'data' / 'elastic-3-s-s1.json').read_bytes()
272+
273+
274+
@pytest.mark.skipif(not (SEISSOL21 / 'generate.py').exists(), reason='no SeisSol codegen (SEISSOL_CODEGEN)')
275+
def test_seissols_neighbor_flux_is_named_as_seissol_names_it(tmp_path):
276+
"""seissol21's neighboringFlux, a family over simpleParameterSpace(4, 4)
277+
of `lambda j, i: rDivM[i] * fPrT[j]`: SeisSol's member k = j + 4*i reads
278+
fPrT(k % 4) and rDivM(k // 4). The committed capture's member k, numbered
279+
as handed over, reads fPrT(k // 4) and rDivM(k % 4) -- the same
280+
descriptions, under another name."""
281+
def reads(blob, k):
282+
return sorted(t['name'] for t in blob['descriptions'][f'gpu_neighboringFlux[{k}]']['tensors']
283+
if t['name'].startswith(('fPrT(', 'rDivM(')))
284+
ours = json.loads(_first_capture(tmp_path).read_text())
285+
first = json.loads((ROOT / 'tools' / 'bench' / 'data' / 'elastic-3-s-s1.json').read_text())
286+
for k in range(16):
287+
assert reads(ours, k) == [f'fPrT({k % 4})', f'rDivM({k // 4})'], k
288+
assert reads(first, k) == [f'fPrT({k // 4})', f'rDivM({k % 4})'], k
289+
assert (ours['descriptions'][f'gpu_neighboringFlux[{k}]']
290+
== first['descriptions'][f'gpu_neighboringFlux[{4 * (k % 4) + k // 4}]']), k
291+
assert ours['config']['member_order'] == 'seissol'

‎tools/bench/README.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ a kernel by both.
320320

321321
The exports in `data/` are the first captures: seissol21, recorded with the
322322
exporter's first settings (`tools/seissol_export.py`'s defaults: the stars
323-
dense, the fluxes not premultiplied, the stroud rule), and kept as they are
323+
dense, the fluxes not premultiplied, the stroud rule; and a family's members
324+
numbered as handed over, `--member-order iteration`), and kept as they are
324325
so that what was measured on them stays reproducible.
325326
`data/seissol63/<backend>/<tag>.json.xz` holds the same configurations, with
326327
orders 7 and 8, as SeisSol's GPU builds generate them today: seissol63, each
@@ -330,6 +331,23 @@ tools, which stores the stars sparse). They are xz-compressed, which
330331
`suite.from_dump` reads as it reads the plain ones. sm_90a and sm_100a
331332
record the same descriptions, so `cuda` holds one set.
332333

334+
A family of more than one parameter is numbered otherwise in these sets
335+
than SeisSol numbers it. yateto, and SeisSol with it, calls the member of
336+
parameters p over a space of shape (n0, n1, ...) by its index with the
337+
first parameter fastest, k = p0 + n0*p1 + n0*n1*p2 ...; the first captures,
338+
the seissol63 set and the fixture store (`tests/seissol_suite.py`) number
339+
the members in the order yateto hands them over, the last parameter
340+
fastest: over (n0, n1), k' = p0*n1 + p1. So the seissol63 set's
341+
`gpu_neighboringFlux[1]` (a family over (4, 4)) is SeisSol's `[4]`; `[0]`
342+
and the diagonal agree. The same holds for `neighborFluxExt`,
343+
`projectToDR`, `nodalFlux`, `evaluateAndRotateQAtInterpolationPoints`, the
344+
Vtk projections, and the fixture store's three-parameter neighbor fluxes; a
345+
family of one parameter is numbered alike. Nothing measured or keyed on
346+
these sets reads another member of such a family than `[0]`.
347+
`tools/seissol_export.py` names members as SeisSol does, and says so in the
348+
config (`member_order`); `--member-order iteration` records the sets'
349+
numbering, byte for byte.
350+
333351
A run that predates those fields is placed by its **directory name** --
334352
`elastic-6-s-s1` as `evaluate.py` names an export, or `elastic-linearck-o8-s`
335353
as the recorded suite calls a configuration. A row is believed over its

‎tools/mutation_check.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9362,7 +9362,8 @@ def make():
93629362
" FormChoice((False, 3, 3, 364, False), (Datatype.F32,), TILE_ARCHS, 'tile',\n", 1)),
93639363
]),
93649364
# tools/seissol_export.py: the first captures' defaults, byte for byte,
9365-
# and a build's own settings passed and recorded.
9365+
# a build's own settings passed and recorded, and a family's members
9366+
# named by yateto's index of them, as SeisSol calls them.
93669367
'export': ('tests/test_seissol_export.py', [
93679368
('another host by default',
93689369
sub(EXPORT, "host = option('--host', 'hsw')\n", "host = option('--host', 'skx')\n", 1)),
@@ -9388,6 +9389,18 @@ def make():
93889389
sub(EXPORT, " sys.path.insert(0, os.path.abspath(yateto_dir))\n", " pass\n", 1)),
93899390
('a value out of its choices taken',
93909391
sub(EXPORT, " if choices is not None and value not in choices:\n", " if False:\n", 1)),
9392+
('members numbered as handed over by default',
9393+
sub(EXPORT, "member_order = option('--member-order', 'seissol', ",
9394+
"member_order = option('--member-order', 'iteration', ", 1)),
9395+
('a member named by its place in the hand-over',
9396+
sub(EXPORT, " names = [f'{name}[{g}]' for g in groups]\n",
9397+
" names = [f'{name}[{i}]' for i, g in enumerate(groups)]\n", 1)),
9398+
('the outline a description came in not remembered',
9399+
sub(EXPORT, " outline_of[id(description)] = outline\n", " pass\n", 1)),
9400+
('the new numbering not recorded',
9401+
sub(EXPORT, " config['member_order'] = 'seissol'\n", " pass\n", 1)),
9402+
('the numbering recorded for the old order too',
9403+
sub(EXPORT, "if member_order == 'seissol':\n # The committed", "if True:\n # The committed", 1)),
93919404
]),
93929405

93939406
# The PVC table (`Options.pvc_table`): the key, who gets a row, the IGC

‎tools/seissol_export.py‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
Runs seissol21/codegen/generate.py (its yateto: codegen/yateto -> submodules/yateto) with TensorForge as
99
the device code generator, and swaps TensorForge's routine generator for one that records every kernel
1010
description and generates nothing. Kernels are named as yateto names them (`namespace::kernel`,
11-
`family[group]` for a family member). The CPU half is generated with the gemm tools asked (none unless
11+
`family[group]` for a family member, the group yateto's index of it, which SeisSol calls it by: over a parameter
12+
space of shape (n0, n1, ...) the member of parameters p is p0 + n0*p1 + n0*n1*p2 ...). --member-order iteration
13+
numbers a family's members in the order yateto hands them over instead -- the parameter space's, its last
14+
parameter fastest -- as the committed captures were recorded (tools/bench/data, its seissol63 set, the fixture
15+
store): for a family of more than one parameter that is another numbering. The CPU half is generated with the gemm tools asked (none unless
1216
--gemm-tools says otherwise). The recorder states of itself what TensorForge's does (`DEVICE_LAYOUTS`,
1317
`device_layout_rule`), so that a yateto which asks gets the layouts TensorForge's kernels read, not the
1418
host's. --rule builds under the device layout rule TensorForge states (generate.py's --device_layout_rule;
@@ -61,6 +65,7 @@ def option(name, default=None, choices=None):
6165
source = option('--source', SOURCE)
6266
pretty = '--pretty' in sys.argv
6367
rule = '--rule' in sys.argv
68+
member_order = option('--member-order', 'seissol', ('seissol', 'iteration'))
6469

6570
sys.path.insert(0, str(TF))
6671
if yateto_dir is not None:
@@ -95,6 +100,9 @@ def _tolerant_handle_tensor(self, tensor, *args, **kwargs):
95100
_factory.ExportFactory._handleTensor = _tolerant_handle_tensor
96101

97102
pending, recorded = [], {}
103+
#: The outline each pending description was handed over in (`generateKernelOutline`): a family member is its
104+
#: outline's place in the family's list of outlines, yateto's group index of it.
105+
outline_of = {}
98106

99107

100108
class Recorder(ExportGenerator):
@@ -105,11 +113,19 @@ def generate(self, cpp, cache):
105113
cpp('// recorded for TensorForge, not generated')
106114

107115

108-
def name_pending(name):
109-
"""Called where yateto names what the outlines just built."""
116+
def name_pending(name, outlines=None):
117+
"""Called where yateto names what the outlines just built: a family's members by yateto's group index of
118+
each, which SeisSol calls them by (`member_order` seissol), or in the order they were handed over."""
110119
if not pending:
111120
return
112121
names = [name] if len(pending) == 1 else [f'{name}[{i}]' for i in range(len(pending))]
122+
if len(pending) > 1 and member_order == 'seissol' and outlines is not None:
123+
group = {id(outline): g for g, outline in enumerate(outlines) if outline is not None}
124+
groups = [group.get(id(outline_of.get(id(d)))) for d in pending]
125+
if None in groups or len(set(groups)) != len(groups):
126+
workarounds.add(f'{name}: its members not one an outline, numbered as handed over')
127+
else:
128+
names = [f'{name}[{g}]' for g in groups]
113129
for n, d in zip(names, pending):
114130
key, k = n, 2
115131
while key in recorded:
@@ -119,13 +135,23 @@ def name_pending(name):
119135

120136

121137
original_generate = visitor.OptimizedKernelGenerator.generate
138+
original_outline = visitor.OptimizedKernelGenerator.generateKernelOutline
139+
140+
141+
def recording_outline(self, *args, **kwargs):
142+
before = len(pending)
143+
outline = original_outline(self, *args, **kwargs)
144+
for description in pending[before:]:
145+
outline_of[id(description)] = outline
146+
return outline
122147

123148

124149
def named_generate(self, cpp, header, name, kernelOutlines, *args, **kwargs):
125-
name_pending(name)
150+
name_pending(name, kernelOutlines)
126151
return original_generate(self, cpp, header, name, kernelOutlines, *args, **kwargs)
127152

128153

154+
visitor.OptimizedKernelGenerator.generateKernelOutline = recording_outline
129155
visitor.OptimizedKernelGenerator.generate = named_generate
130156

131157
# yateto asks what is registered, before it optimizes a kernel, whether the kernels read the device layouts
@@ -180,6 +206,9 @@ def recorder(arch, attrs=None):
180206
config['gemm_tools'] = gemm_tools
181207
if premultiply is not None:
182208
config['premultiply_flux'] = premultiply == 'on'
209+
if member_order == 'seissol':
210+
# The committed captures (iteration) do not carry it: a capture that does names members as SeisSol does.
211+
config['member_order'] = 'seissol'
183212
config.update(multiple_simulations=simcount, workarounds=sorted(workarounds), **stated)
184213
blob = {'source': source + ('; the device layout rule on' if rule else ''),
185214
'config': config,

‎tools/seissol_export63.sh‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# CMake passes them to generate.py (cmake/process_users_input.cmake: PREMULTIPLY_FLUX on for NVIDIA and AMD
99
# devices, DR_QUAD_RULE dunavant, PLASTICITY_METHOD sq, MEMORY_LAYOUT auto; TensorForge appended to the gemm
1010
# tools, so tensorforge.xml). SEISSOL63 names the checkout (or a copy of its codegen and submodules/yateto).
11+
# --member-order iteration: the set numbers a family's members as they were recorded, in the order yateto hands
12+
# them over (tools/bench/README.md), not as SeisSol does.
1113

1214
SEISSOL63=${SEISSOL63:?set SEISSOL63 to a seissol63 checkout}
1315
OUTDIR=bench/data/seissol63
@@ -24,7 +26,8 @@ export_one() { # backend equation solver order precision mechanisms simcount
2426
SEISSOL_CODEGEN=$SEISSOL63/codegen python seissol_export.py $eq $solver $order $precision $out \
2527
--mechanisms $mechanisms --simcount $simcount "${device[@]}" --plasticity sq --dr-quad dunavant \
2628
--mem-layout auto --gemm-tools Eigen,TensorForge --yateto $SEISSOL63/submodules/yateto \
27-
--source "$SOURCE_BASE with the $backend build's settings" --pretty && xz -T1 -9 -f $out
29+
--source "$SOURCE_BASE with the $backend build's settings" --member-order iteration --pretty &&
30+
xz -T1 -9 -f $out
2831
}
2932

3033
for backend in cuda oneapi; do

0 commit comments

Comments
 (0)