-
Notifications
You must be signed in to change notification settings - Fork 128
/
generate_kernels.py
executable file
·49 lines (41 loc) · 1.68 KB
/
generate_kernels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import glob
import yaml
from jinja2 import Template
def compile_kernels(fns):
template_dir = "templates"
output_dir = os.path.join("src", "george")
with open(os.path.join(template_dir, "parser.h")) as f:
PARSER_TEMPLATE = Template(f.read())
with open(os.path.join(template_dir, "kernels.h")) as f:
CPP_TEMPLATE = Template(f.read())
with open(os.path.join(template_dir, "kernels.py")) as f:
PYTHON_TEMPLATE = Template(f.read())
specs = []
for i, fn in enumerate(fns):
with open(fn, "r") as f:
spec = yaml.load(f.read(), Loader=yaml.FullLoader)
print("Found kernel '{0}'".format(spec["name"]))
spec["index"] = i
spec["reparams"] = spec.get("reparams", {})
specs.append(spec)
print("Found {0} kernel specifications".format(len(specs)))
fn = os.path.join(output_dir, "include", "george", "parser.h")
with open(fn, "w") as f:
print("Saving parser to '{0}'".format(fn))
f.write(PARSER_TEMPLATE.render(specs=specs))
fn = os.path.join(output_dir, "include", "george", "kernels.h")
with open(fn, "w") as f:
print("Saving C++ kernels to '{0}'".format(fn))
f.write(CPP_TEMPLATE.render(specs=specs))
fn = os.path.join(output_dir, "kernels.py")
with open(fn, "w") as f:
print("Saving Python kernels to '{0}'".format(fn))
f.write(PYTHON_TEMPLATE.render(specs=specs))
if __name__ == "__main__":
# If the kernel specifications are included (development mode) re-compile
# them first.
kernel_specs = glob.glob(os.path.join("kernels", "*.yml"))
compile_kernels(kernel_specs)