|
5 | 5 |
|
6 | 6 | Against a stand-in for SeisSol's codegen -- a generate.py that keeps the |
7 | 7 | 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. |
14 | 18 | """ |
15 | 19 |
|
16 | 20 | from __future__ import annotations |
@@ -59,10 +63,35 @@ def _handleTensor(self, tensor, *args, **kwargs): |
59 | 63 |
|
60 | 64 | VISITOR = ''' |
61 | 65 | 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 | +
|
62 | 70 | def generate(self, cpp, header, name, kernelOutlines, *args, **kwargs): |
63 | 71 | return None |
64 | 72 | ''' |
65 | 73 |
|
| 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 | + |
66 | 95 |
|
67 | 96 | def _yateto(where: Path) -> Path: |
68 | 97 | 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): |
111 | 140 |
|
112 | 141 | def test_the_defaults_record_what_the_first_captures_recorded(codegen, tmp_path): |
113 | 142 | """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') |
116 | 146 | blob = json.loads(text) |
117 | 147 | assert text == json.dumps(blob, separators=(',', ':')) |
118 | 148 | 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 |
144 | 174 | assert config == dict(equation='elastic', solver='linearck', order=4, precision='s', mechanisms=3, |
145 | 175 | device='oneapi/pvc', host='skx', plasticity='sq', dr_quad='dunavant', |
146 | 176 | 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 |
148 | 217 |
|
149 | 218 |
|
150 | 219 | 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): |
184 | 253 | SEISSOL21 = Path(os.environ.get('SEISSOL_CODEGEN', ROOT.parent / 'seissol21' / 'codegen')) |
185 | 254 |
|
186 | 255 |
|
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): |
191 | 257 | out = tmp_path / 'elastic-3-s-s1.json' |
192 | 258 | env = dict(os.environ, SEISSOL_CODEGEN=str(SEISSOL21), PYTHONDONTWRITEBYTECODE='1') |
193 | 259 | 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], |
195 | 261 | capture_output=True, text=True, env=env, cwd=tmp_path, timeout=1800) |
196 | 262 | 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') |
197 | 271 | 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' |
0 commit comments