Skip to content

Commit

Permalink
compiler: fix missing algorithm include for min/max
Browse files Browse the repository at this point in the history
  • Loading branch information
mloubout committed Jan 18, 2025
1 parent 49a284f commit d49ff6e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
5 changes: 3 additions & 2 deletions devito/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ def _lower_iet(cls, uiet, profiler=None, **kwargs):
* Finalize (e.g., symbol definitions, array casts)
"""
name = kwargs.get("name", "Kernel")
lang = cls._Target.lang()

# Wrap the IET with an EntryFunction (a special Callable representing
# the entry point of the generated library)
Expand All @@ -488,10 +489,10 @@ def _lower_iet(cls, uiet, profiler=None, **kwargs):
cls._Target.instrument(graph, profiler=profiler, **kwargs)

# Extract the necessary macros from the symbolic objects
generate_macros(graph, **kwargs)
generate_macros(graph, lang=lang, **kwargs)

# Add type specific metadata
lower_dtypes(graph, lang=cls._Target.lang(), **kwargs)
lower_dtypes(graph, lang=lang, **kwargs)

# Target-independent optimizations
minimize_symbols(graph)
Expand Down
1 change: 1 addition & 0 deletions devito/passes/iet/languages/CXX.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CXXBB(LangBB):

mapper = {
'header-memcpy': 'string.h',
'header-algorithm': 'algorithm',
'host-alloc': lambda i, j, k:
Call('posix_memalign', (i, j, k)),
'host-alloc-pin': lambda i, j, k:
Expand Down
31 changes: 17 additions & 14 deletions devito/passes/iet/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ def generate_macros(graph, **kwargs):


@iet_pass
def _generate_macros(iet, tracker=None, **kwargs):
def _generate_macros(iet, tracker=None, lang=None, **kwargs):
# Derive the Macros necessary for the FIndexeds
iet = _generate_macros_findexeds(iet, tracker=tracker, **kwargs)

headers = [i.header for i in tracker.values()]
headers = sorted((ccode(define), ccode(expr)) for define, expr in headers)

# Generate Macros from higher-level SymPy objects
headers.extend(_generate_macros_math(iet))
mheaders, includes = _generate_macros_math(iet, lang=lang)
headers.extend(mheaders)

# Remove redundancies while preserving the order
headers = filter_ordered(headers)

# Some special Symbols may represent Macros defined in standard libraries,
# so we need to include the respective includes
limits = FindApplications(ValueLimit).visit(iet)
includes = set()
if limits & (set(limits_mapper[np.int32]) | set(limits_mapper[np.int64])):
includes.add('limits.h')
elif limits & (set(limits_mapper[np.float32]) | set(limits_mapper[np.float64])):
Expand Down Expand Up @@ -194,35 +194,38 @@ def _generate_macros_findexeds(iet, sregistry=None, tracker=None, **kwargs):
return iet


def _generate_macros_math(iet):
def _generate_macros_math(iet, lang=None):
headers = []
includes = []
for i in FindApplications().visit(iet):
headers.extend(_lower_macro_math(i))
header, include = _lower_macro_math(i, lang)
headers.extend(header)
includes.extend(include)

return headers
return headers, includes


@singledispatch
def _lower_macro_math(expr):
return ()
def _lower_macro_math(expr, lang):
return (), {}


@_lower_macro_math.register(Min)
@_lower_macro_math.register(sympy.Min)
def _(expr):
def _(expr, lang):
if has_integer_args(*expr.args) and len(expr.args) == 2:
return (('MIN(a,b)', ('(((a) < (b)) ? (a) : (b))')),)
return (('MIN(a,b)', ('(((a) < (b)) ? (a) : (b))')),), {}
else:
return ()
return (), (lang.get('header-algorithm'),) or ()


@_lower_macro_math.register(Max)
@_lower_macro_math.register(sympy.Max)
def _(expr):
def _(expr, lang):
if has_integer_args(*expr.args) and len(expr.args) == 2:
return (('MAX(a,b)', ('(((a) > (b)) ? (a) : (b))')),)
return (('MAX(a,b)', ('(((a) > (b)) ? (a) : (b))')),), {}
else:
return ()
return (), (lang.get('header-algorithm'),) or ()


@iet_pass
Expand Down
15 changes: 13 additions & 2 deletions tests/test_gpu_openacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import numpy as np

from devito import (Grid, Function, TimeFunction, SparseTimeFunction, Eq, Operator,
norm, solve)
norm, solve, Max)
from conftest import skipif, assert_blocking, opts_device_tiling
from devito.data import LEFT
from devito.exceptions import InvalidOperator
from devito.ir.iet import retrieve_iteration_tree, FindNodes, Iteration
from examples.seismic import TimeAxis, RickerSource, Receiver

pytestmark = skipif(['nodevice'], whole_module=True)
pytestmark = skipif(['novice'], whole_module=True)


class TestCodeGeneration:
Expand Down Expand Up @@ -171,6 +171,17 @@ def test_multi_tile_blocking_structure(self):
assert len(iters) == len(v)
assert all(i.step == j for i, j in zip(iters, v))

def test_std_max(self):
grid = Grid(shape=(3, 3, 3))
x, y, z = grid.dimensions

u = Function(name='u', grid=grid)

op = Operator(Eq(u, Max(1.2 * x / y, 2.3 * y / x)),
platform='nvidiaX', language='openacc')

assert '<algorithm>' in str(op)


class TestOperator:

Expand Down

0 comments on commit d49ff6e

Please sign in to comment.