Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3d54c33
operator: Honor explicit arguments over replacement defaults
FabioLuporini Oct 21, 2026
354ea15
operator: Restore native argument override handling
FabioLuporini Oct 24, 2026
e298ef3
symbolics: Rebuild cached ctypes pointer classes during serialization
Oct 23, 2026
dd54133
symbolics: Expose integer RoundUp results to alignment proofs
Oct 23, 2026
06086d5
compiler: Unify arithmetic modes and JIT identity
Nov 4, 2026
ecf260a
compiler: Derive allocation alignments before data placement
Nov 5, 2026
73bf36d
compiler: Honor weak keys in fusion topological sorting
Nov 5, 2026
edafef4
types: Reconstruct Pointer from base dtype and stars
Nov 11, 2026
9143b61
compiler: Use runtime Grid for halo exchange peers
Nov 11, 2026
13b5b29
compiler: Handle opaque dtypes in integer-argument queries
Nov 11, 2026
21b5c18
compiler: Honor arithmetic precision during code generation
Nov 12, 2026
519f3b1
symbolics: Add ordered arithmetic printing nodes
Nov 20, 2026
6f1e3d3
compiler: Expose cast alignment and callable types
Nov 20, 2026
6fd3906
symbolics: Preserve order in unevaluated arithmetic
Nov 24, 2026
dd1f866
pickling: Declare ignored state with __rignored__
Nov 24, 2026
acefc1d
types: Support NumPy base dtypes in Pointer
Nov 24, 2026
66a0b22
symbolics: Handle opaque dtypes in RoundUp
Nov 24, 2026
56a6f13
tests: Simplify arithmetic mode and compiler ABI coverage
Nov 24, 2026
01fb435
docs: Refresh performance overview notebook output
Nov 27, 2026
07a2e2c
compiler: Tweak _print_Mul
FabioLuporini Nov 30, 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
11 changes: 7 additions & 4 deletions devito/core/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ class BasicOperator(Operator):
See `examples/userapi/08_staggered_interp.ipynb` for a worked example.
"""

HALF_ARITH = False
ARITHM_MODE = 'default'
"""
Whether an Operator working in half precision carries out the arithmetic there
too, rounding its literals and its FD weights to half. Off by default.
Whether to use default arithmetic or FP16 arithmetic for half-precision fields.
"""

@classmethod
Expand Down Expand Up @@ -237,13 +236,17 @@ def _normalize_sym_kwargs(cls, **kwargs):
"""
so = dict(kwargs.get('sym_options', {}))
out = {'interp-mode': so.pop('interp-mode', cls.INTERP_MODE),
'half-arith': so.pop('half-arith', cls.HALF_ARITH)}
'arithm-mode': so.pop('arithm-mode', cls.ARITHM_MODE)}

if so:
raise InvalidOperator(
f'Unrecognized symbolic options: [{", ".join(list(so))}]'
)

mode = out['arithm-mode']
if not isinstance(mode, str) or mode not in ('default', 'fp16'):
raise InvalidOperator("`arithm-mode` must be 'default' or 'fp16'")

return out

@classmethod
Expand Down
42 changes: 32 additions & 10 deletions devito/ir/cgen/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,9 @@ def _print_Mod(self, expr):
return '%'.join(args)

def _print_Mul(self, expr):
"""Print reciprocal factors in one denominator, e.g. `a/((b*c)*d)`."""
args, neg = [], False
# SymPy's CodePrinter extracts negative coefficients with the standalone
# _keep_coeff helper, which can rebuild a plain SymPy Mul. Since this
# is not a method, UnevalMul cannot override it. Extract signs before
# delegating to preserve the product's class without patching SymPy or
# copying its multiplication printer.
# Extract signs without letting SymPy's `_keep_coeff` evaluate the product
for a in expr.args:
if a.is_Number and a.is_negative:
a = -a
Expand All @@ -302,15 +299,40 @@ def _print_Mul(self, expr):
args.append(a)
args = args or [S.One]

if len(args) > 1:
term = super()._print_Mul(expr.func(*args, evaluate=False))
else:
product = expr.func(*args, evaluate=False) if len(args) > 1 else None
if product is None:
term = self.parenthesize(args[0], precedence(expr))
elif product in getattr(self, 'math_macros', ()):
# Let SymPy print named constants such as `M_PI_2`
term = super()._print_Mul(product)
else:
prec = precedence(product)
if self.order in ('old', 'none'):
args = sympy.Mul.make_args(product)
else:
args = product.as_ordered_factors()

numerator, denominator = [], []
for a in args:
if a.is_Pow and a.is_commutative and \
a.exp.is_Rational and a.exp.is_negative:
base, exp = a.args
if exp != -1:
base = sympy.Pow(base, -exp, evaluate=False)
denominator.append(self.parenthesize(base, prec))
else:
numerator.append(self.parenthesize(a, prec))

term = '*'.join(numerator) or self._print(S.One)
if len(denominator) == 1:
term += f'/{denominator[0]}'
elif denominator:
term += f"/({'*'.join(denominator)})"
Comment on lines +305 to +330

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of extra complexity without any clear explanation of what it's doing


if neg:
return term[1:] if term.startswith('-') else f'-{term}'
else:
return term

return term

def _print_fmath_func(self, name, expr):
args = ",".join([self._print(i) for i in expr.args])
Expand Down
8 changes: 4 additions & 4 deletions devito/ir/iet/efunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ def make_efunc(name, iet, region=None, retval='void', prefix='static',
# Callable machinery


def make_callable(name, iet, retval='void', prefix='static'):
def make_callable(name, iet, retval='void', prefix='static', cls=Callable):
"""
Utility function to create a Callable from an IET.
Create a Callable (or subclass `cls`) from an IET.
"""
parameters = derive_parameters(iet, ordering='canonical')
return Callable(name, iet, retval, parameters=parameters, prefix=prefix)
return cls(name, iet, retval, parameters=parameters, prefix=prefix)


# EntryFunction machinery
Expand Down Expand Up @@ -190,7 +190,7 @@ def __init__(self, name, body, parameters):
class DeviceFunction(Callable):

"""
A Callable executed asynchronously on a device.
A Callable executed on a device, including kernels and inline helpers.
"""

def __init__(self, name, body, retval='void', parameters=None,
Expand Down
15 changes: 12 additions & 3 deletions devito/ir/iet/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any, Generic, TypeVar

import cgen as c
import numpy as np
from sympy import IndexedBase
from sympy.core.function import Application

Expand All @@ -24,12 +25,12 @@
from devito.ir.support.space import Backward
from devito.ir.support.utils import IMask
from devito.symbolics import (
FieldFromComposite, FieldFromPointer, ListInitializer, uxreplace
FieldFromComposite, FieldFromPointer, ListInitializer, sympy_dtype, uxreplace
)
from devito.symbolics.extended_dtypes import NoDeclStruct
from devito.tools import (
GenericVisitor, as_tuple, c_restrict_void_p, filter_ordered, filter_sorted, flatten,
is_external_ctype, natural_sort_key
GenericVisitor, as_tuple, c_restrict_void_p, dtypes_vector_mapper, filter_ordered,
filter_sorted, flatten, is_external_ctype, natural_sort_key
)
from devito.tools.cgen import make_struct
from devito.types import (
Expand Down Expand Up @@ -262,8 +263,16 @@ def __init__(self, *args, printer=None, **kwargs):
def ccode(self, expr, dtype=None, exact_prec=None):
defaults = self.printer._default_settings
dtype = defaults['dtype'] if dtype is None else dtype

if exact_prec is None:
exact_prec = defaults['exact_prec']

base = dtypes_vector_mapper.get_base_dtype(dtype, dtype)
if exact_prec and base == np.float16:
# E.g., `half_out = 0.9998*half_in + float_in` needs FP32
# literals despite its FP16 destination
dtype = sympy_dtype(expr, base=base)

return get_printer(self.printer, dtype, exact_prec).doprint(expr, None)

@property
Expand Down
6 changes: 5 additions & 1 deletion devito/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,10 @@ def arguments(self, **kwargs):

# Code generation and JIT compilation

def _signature_items(self):
# Retain literal precision and the complete toolchain ABI in JIT identity
return (str(self.ccode), repr(self._compiler.abi_id()))

@cached_property
def _soname(self):
"""A unique name for the shared object resulting from JIT compilation."""
Expand All @@ -836,7 +840,7 @@ def _soname(self):
def _printer(self):
# A Target may offer a second printer e.g. for Operators
# using half-precision arithmetic
if self._sym_options.get('half-arith'):
if self._sym_options.get('arithm-mode') == 'fp16':
with suppress(AttributeError):
return self._Target.HalfArithPrinter
return self._Target.Printer
Expand Down
5 changes: 5 additions & 0 deletions devito/passes/clusters/cse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
DefFunction, IndexedPointer, Reserved, estimate_cost, q_leaf, q_terminal, search
)
from devito.symbolics.manipulation import _uxreplace
from devito.symbolics.unevaluation import Add as UnevalAdd
from devito.symbolics.unevaluation import Mul as UnevalMul
from devito.tools import DAG, as_list, as_tuple, extract_dtype, frozendict
from devito.types import Eq, Symbol, Temp

Expand Down Expand Up @@ -463,9 +465,12 @@ def _(expr):
return mapper


@_catch.register(UnevalAdd)
@_catch.register(UnevalMul)
@_catch.register(Pow)
@_catch.register(Function)
def _(expr):
# Reuse frozen arithmetic only as complete subexpressions
mapper = _catch(expr.args)

mapper[Candidate(expr)].append(expr)
Expand Down
4 changes: 3 additions & 1 deletion devito/passes/clusters/factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from devito.symbolics import (
BasicWrapperMixin, estimate_cost, q_routine, retrieve_symbols, reuse_if_untouched
)
from devito.symbolics.unevaluation import Add as UnevalAdd
from devito.symbolics.unevaluation import Mul as UnevalMul
from devito.tools import ReducerMap
from devito.types.object import AbstractObject

Expand Down Expand Up @@ -209,7 +211,7 @@ def _collect_nested(expr, strategy):
elif expr.is_Pow:
return expr, {'pows': expr}
elif (expr.is_Symbol or expr.is_Indexed or not expr.args or
isinstance(expr, (BasicWrapperMixin, AbstractObject))):
isinstance(expr, (BasicWrapperMixin, AbstractObject, UnevalAdd, UnevalMul))):
return expr, {}
elif expr.is_Add:
return strategies[strategy](expr, strategy), {}
Expand Down
11 changes: 8 additions & 3 deletions devito/passes/clusters/fusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,14 @@ def _toposort(self, cgroups, prefix):

# Are there any ClusterGroups that could potentially be topologically
# reordered? If not, do not waste time
counter = Counter(self._key(cg).strict for cg in cgroups)
if len(counter.most_common()) == 1 or \
not any(v > 1 for it, v in counter.most_common()):
keys = [self._key(cg) for cg in cgroups]
if len({k.full for k in keys}) == 1:
return ClusterGroup(cgroups, prefix)

# Same strict keys can still hide different scheduling preferences.
# E.g., `r0, shared0, r1, shared1` may reorder to fuse `r0, r1`.
counter = Counter(k.strict for k in keys)
if not any(v > 1 for v in counter.values()):
return ClusterGroup(cgroups, prefix)

dag = self._build_dag(cgroups, prefix)
Expand Down
2 changes: 1 addition & 1 deletion devito/passes/clusters/unevaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@cluster_pass(mode='all')
def unevaluate(cluster):
def unevaluate(cluster, **kwargs):
exprs = [_unevaluate(e) for e in cluster.exprs]

return cluster.rebuild(exprs=exprs)
38 changes: 27 additions & 11 deletions devito/passes/iet/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def _alloc_host_array_on_high_bw_mem(self, site, obj, storage, *args):

storage.update(obj, site, allocs=tuple(allocs), frees=free, efuncs=efuncs)

def _alloc_local_array_on_high_bw_mem(self, site, obj, storage, *args):
def _alloc_local_array_on_high_bw_mem(self, site, obj, storage, *args, **kwargs):
"""
Allocate a local Array in the host high bandwidth memory.
"""
Expand Down Expand Up @@ -471,7 +471,7 @@ def _inject_definitions(self, iet, storage):
return processed, flatten(efuncs)

@iet_pass
def place_definitions(self, iet, globs=None, **kwargs):
def place_definitions(self, iet, globs=None, alignments=None, **kwargs):
"""
Create a new IET where all symbols have been declared, allocated, and
deallocated in one or more memory spaces.
Expand Down Expand Up @@ -507,7 +507,9 @@ def place_definitions(self, iet, globs=None, **kwargs):
if i.is_transient:
self._alloc_bundle_struct_on_high_bw_mem(iet, i, storage)
elif i._mem_local:
self._alloc_local_array_on_high_bw_mem(iet, i, storage)
self._alloc_local_array_on_high_bw_mem(
iet, i, storage, alignments=alignments
)
elif i._mem_mapped:
self._alloc_mapped_array_on_high_bw_mem(iet, i, storage)
elif i._mem_stack:
Expand All @@ -518,7 +520,9 @@ def place_definitions(self, iet, globs=None, **kwargs):
if i._mem_host:
self._alloc_host_array_on_high_bw_mem(iet, i, storage)
elif i._mem_local:
self._alloc_local_array_on_high_bw_mem(iet, i, storage)
self._alloc_local_array_on_high_bw_mem(
iet, i, storage, alignments=alignments
)
else:
self._alloc_mapped_array_on_high_bw_mem(iet, i, storage)
elif i._mem_stack:
Expand Down Expand Up @@ -579,11 +583,22 @@ def place_casts(self, iet, **kwargs):

return iet, {}

def derive_alignments(self, graph):
"""
Choose allocation padding from alignment requirements across `graph`.

Backends may inspect all kernels and helpers before deciding how many
elements to insert before each field's logical allocation origin.
An empty mapper retains the backend's ordinary allocation behavior.
"""
return {}

def process(self, graph):
"""
Apply the `place_definitions` and `place_casts` passes.
"""
self.place_definitions(graph, globs=set())
alignments = self.derive_alignments(graph)
self.place_definitions(graph, globs=set(), alignments=alignments)
self.place_casts(graph)


Expand All @@ -596,7 +611,7 @@ def __init__(self, options=None, **kwargs):

super().__init__(options=options, **kwargs)

def _alloc_local_array_on_high_bw_mem(self, site, obj, storage):
def _alloc_local_array_on_high_bw_mem(self, site, obj, storage, **kwargs):
"""
Allocate a local Array in the device high bandwidth memory.
"""
Expand Down Expand Up @@ -673,7 +688,7 @@ def _map_function_on_high_bw_mem(self, site, obj, storage, devicerm,
storage.update(obj, site, maps=mmap, unmaps=unmap, efuncs=efuncs)

@iet_pass
def place_transfers(self, iet, data_movs=None, ctx=None, **kwargs):
def place_transfers(self, iet, data_movs=None, alignments=None, **kwargs):
"""
Create a new IET with host-device data transfers. This requires mapping
symbols to the suitable memory spaces.
Expand All @@ -695,14 +710,14 @@ def place_transfers(self, iet, data_movs=None, ctx=None, **kwargs):
self._map_array_on_high_bw_mem(iet, i, storage)
else:
self._map_function_on_high_bw_mem(
iet, i, storage, devicerm, ctx=ctx
iet, i, storage, devicerm, alignments=alignments
)
for i in filter_sorted(reads - writes):
if i.is_Array:
self._map_array_on_high_bw_mem(iet, i, storage)
else:
self._map_function_on_high_bw_mem(
iet, i, storage, devicerm, read_only=True, ctx=ctx
iet, i, storage, devicerm, read_only=True, alignments=alignments
)

iet, efuncs = self._inject_definitions(iet, storage)
Expand Down Expand Up @@ -735,8 +750,9 @@ def process(self, graph):
"""
Apply the `place_transfers`, `place_definitions` and `place_casts` passes.
"""
self.place_transfers(graph, data_movs=graph.data_movs)
self.place_definitions(graph, globs=set())
alignments = self.derive_alignments(graph)
self.place_transfers(graph, data_movs=graph.data_movs, alignments=alignments)
self.place_definitions(graph, globs=set(), alignments=alignments)
self.place_devptr(graph)
self.place_bundling(graph, writes_input=graph.writes_input)
self.place_casts(graph)
Expand Down
8 changes: 4 additions & 4 deletions devito/passes/iet/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@iet_pass
def _complex_includes(iet: Callable, langbb: type[LangBB], compiler: Compiler,
sregistry: SymbolRegistry) -> tuple[Callable, dict]:
sregistry: SymbolRegistry, **kwargs) -> tuple[Callable, dict]:
"""
Includes complex arithmetic headers for the given language, if needed.
"""
Expand Down Expand Up @@ -48,9 +48,9 @@ def lower_dtypes(graph: Callable,
compiler: Compiler = None,
sregistry: SymbolRegistry = None, **kwargs) -> tuple[Callable, dict]:
"""
Lowers float16 scalar types to pointers since we can't directly pass their
value. Also includes headers for complex arithmetic if needed.
Lower dtype-specific parameters and arithmetic, and include required headers.
"""

for dtype_pass in dtype_passes:
dtype_pass(graph, langbb=langbb, compiler=compiler, sregistry=sregistry)
dtype_pass(graph, langbb=langbb, compiler=compiler, sregistry=sregistry,
**kwargs)
Loading
Loading