Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e0a29e8
working on cpp verification
mscroggs Apr 1, 2026
996ae6f
simplefem++
mscroggs Apr 1, 2026
9da3b8d
working towards jittable C++
mscroggs Apr 1, 2026
83a0471
fix paths
mscroggs Apr 1, 2026
e4c7180
trying to get cpp working
mscroggs Apr 8, 2026
1da5a7d
working on cpp interface
mscroggs Apr 9, 2026
322f9f1
it works!!
mscroggs Apr 9, 2026
cf8505f
indentataion, and a little bit of simplifying
mscroggs Apr 9, 2026
f7ec08d
ruff format
mscroggs Apr 9, 2026
71d26b4
mypy
mscroggs Apr 9, 2026
2a90d14
simplify
mscroggs Apr 9, 2026
41eadb0
pydocstyle
mscroggs Apr 10, 2026
b75d20e
move simplefempp into simplefem.py
mscroggs Apr 10, 2026
d879e10
add test that C++ jit works
mscroggs Apr 10, 2026
8f03d84
mdpsan license
mscroggs Apr 10, 2026
a445eca
Merge branch 'main' into mscroggs/cpp
mscroggs Apr 10, 2026
5763bdb
website build tools version
mscroggs Apr 10, 2026
ad98ff2
simplefempp
mscroggs Apr 10, 2026
026826b
add documentation
mscroggs Apr 14, 2026
51de709
@classmethod, ruff
mscroggs Apr 14, 2026
746f763
list[]
mscroggs Apr 14, 2026
8238e13
rename file
mscroggs Apr 15, 2026
c9d674a
Merge branch 'main' into mscroggs/cpp
mscroggs Apr 23, 2026
90dc6cc
Merge branch 'main' into mscroggs/cpp
mscroggs May 5, 2026
4530ea8
Merge branch 'main' into mscroggs/cpp
mscroggs May 18, 2026
8a2c042
Merge branch 'main' into mscroggs/cpp
mscroggs Jun 25, 2026
f4615fd
Merge branch 'main' into mscroggs/cpp
mscroggs Jul 28, 2026
8088129
ruff updates
mscroggs Jul 28, 2026
eba8cbd
move replacer to function not lambda function
mscroggs Jul 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ruff updates
  • Loading branch information
mscroggs committed Jul 28, 2026
commit 80881290d4c0eb062351e610351a36cef99d1827
2 changes: 1 addition & 1 deletion defelement/implementations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import os
from inspect import isclass

from defelement.implementations import jit
from defelement.implementations.core import (
DegreeNotImplemented,
Implementation,
NotImplementedOnReference,
VariantNotImplemented,
parse_example,
)
from defelement.implementations import jit

__all__ = [
"DegreeNotImplemented",
Expand Down
2 changes: 1 addition & 1 deletion defelement/implementations/jit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Just-in-time compilation."""

from defelement.implementations.jit.argument_types import array, ndarray, integer, double
from defelement.implementations.jit.argument_types import array, double, integer, ndarray
from defelement.implementations.jit.cpp import compile as cpp
7 changes: 3 additions & 4 deletions defelement/implementations/jit/argument_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from abc import ABC, abstractmethod
from itertools import product

import numpy as np


Expand Down Expand Up @@ -253,10 +254,8 @@ def from_raw(self, lib, ffi, obj):
"""Convert raw C object to a Python type."""
shape = [getattr(obj, f"shape{i}") for i in range(self.dimension)]
out = np.empty(shape)
i = 0
for index in product(*[range(j) for j in shape]):
out.__setitem__(index, obj.data[i])
i += 1
for data, index in zip(obj.data, product(*[range(j) for j in shape])):
out.__setitem__(index, data)
return out


Expand Down
30 changes: 19 additions & 11 deletions defelement/implementations/jit/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@
import os
import re
from collections.abc import Callable
from defelement.implementations.jit import tools
from defelement.implementations.jit.argument_types import ArgType
from webtools.tools import join

from cffi import FFI
from webtools.tools import join

from defelement.implementations.jit import tools
from defelement.implementations.jit.argument_types import ArgType

here = os.path.dirname(os.path.realpath(__file__))


def compile(
*,
function: str,
inputs: list[ArgType] = [],
outputs: list[ArgType] = [],
inputs: list[ArgType] | None = None,
outputs: list[ArgType] | None = None,
imports: str | None = None,
id: str | None = None,
packages: list[tuple[str, str]] = [],
packages: list[tuple[str, str]] | None = None,
) -> Callable:
"""Just-in-time compile a C++ function."""
folder = tools.source_dir("cpp", id)

library_name = "defelement_function"
project_name = "DefElementFunction"

if packages is None:
packages = []
if inputs is None:
inputs = []
if outputs is None:
outputs = []

true_outputs = [o for o in outputs if not o.in_out]
in_out = [o for o in outputs if o.in_out]
function_inputs = ", ".join(f"{i.function_input('cpp')}" for i in inputs + in_out)
Expand Down Expand Up @@ -81,9 +89,9 @@ def compile(
code += init_i + "\n\n"
for vars, out in [(inputs + in_out, False), (true_outputs, True)]:
for i in vars:
if i.initialise("cpp", output=out) != "":
if f"INIT {i.variable};" not in function:
function = i.initialise("cpp", output=out) + "\n" + function
init_code = i.initialise("cpp", output=out)
if init_code != "" and f"INIT {i.variable};" not in function:
function = init_code + "\n" + function
if o in in_out:
mf = o.metadata_function_signature("cpp", function_inputs_no_in_out)
if mf is not None:
Expand All @@ -108,7 +116,7 @@ def compile(
# TODO: indent
code = re.sub(
r"( *)INIT " + i.variable,
lambda matches: tools.add_indent(
lambda matches, out=out, i=i: tools.add_indent(
i.initialise("cpp", output=out), len(matches[1])
),
code,
Expand Down Expand Up @@ -149,7 +157,7 @@ def compile(
assert os.system(f"make -C {folder}/build") == 0
assert os.system(f"make -C {folder}/build install") == 0

except BaseException as e:
except BaseException as e: # noqa: BLE001
tools.save_error(folder, e)

tools.check_for_error(folder)
Expand Down
9 changes: 5 additions & 4 deletions defelement/implementations/jit/tools.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""Utility functions."""

import shutil
import traceback
from datetime import datetime
import hashlib
import os
import shutil
import traceback
from datetime import datetime, timedelta, timezone

from appdirs import user_cache_dir

try:
os.mkdir(user_cache_dir())
except FileExistsError:
pass

TODAY = datetime.now().strftime("%Y-%m-%d")
TODAY = datetime.now(tz=timezone(timedelta())).strftime("%Y-%m-%d")
DIR = user_cache_dir("defelement")
try:
os.mkdir(DIR)
Expand Down
2 changes: 1 addition & 1 deletion defelement/implementations/simplefem.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,5 @@ def verify(
id = "simplefempp"
name = "simplefem++"
url = "https://github.com/DefElement/simplefempp"
languages = ["cpp"]
languages = ("cpp",)
verification = True
1 change: 1 addition & 0 deletions test/test_jit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

from defelement.implementations import jit


Expand Down
Loading