vfhe.util.kernels¶
Compiles your C kernels into a vfhe engine: build, compile, directory.
The engine it builds carries the prebuilt engine’s module name,
_vfhe_native_<engine>, in a directory build puts first on
sys.path; python -m vfhe.util.kernels prints that directory for
PYTHONPATH. vfhe.util.bindings then imports it in place of the prebuilt
one. An extension stays loaded for the life of the process, so this runs before
the first import of vfhe’s C; build raises once another engine is loaded.
>>> from vfhe.util import kernels
>>> kernels.build(["mine.c"], ["mine.h"])
>>> from vfhe.util.bindings import lib
>>> lib.my_kernel(...)
When generating the C itself needs vfhe loaded, compile still works: it
writes the engine and touches nothing in this process. Run the code
that uses the kernel in a child process, with that directory first on
PYTHONPATH:
>>> out = kernels.compile(["generated.c"], ["generated.h"])
>>> path = os.pathsep.join([str(out), os.environ.get("PYTHONPATH", "")])
>>> subprocess.run(
... [sys.executable, "worker.py"], env={**os.environ, "PYTHONPATH": path}
... )
- vfhe.util.kernels.build(sources: Paths = (), declarations: Paths = (), *, engine: str | None = None, cc: str | None = None) pathlib.Path¶
Compiles the engine if needed, puts its directory first on
sys.pathand returns it.Returns at once when this build is already loaded. Raises
RuntimeErrorwhen another engine is loaded.
- vfhe.util.kernels.compile(sources: Paths = (), declarations: Paths = (), *, engine: str | None = None, cc: str | None = None) pathlib.Path¶
Compiles the engine into its
directoryand returns it.Recompiles only what changed; delete the directory to start over. Leaves this process untouched. Needs meson and ninja on
PATH(pip install vfhe[kernels]). RaisesRuntimeErrorwhen they or vfhe’s C sources are missing, and when the build fails.
- vfhe.util.kernels.directory(sources: Paths = (), declarations: Paths = (), *, engine: str | None = None, cc: str | None = None) pathlib.Path¶
Returns the cache directory of this build,
extensions/<engine>-<hash>.The hash names what identifies the build: vfhe’s version, the engine, the compiler, Python, and the paths of your files. Their contents are ninja’s business, so an edit recompiles what it reached, in place.