Skip to content

Commit

Permalink
Merge pull request #2218 from devitocodes/red-loc
Browse files Browse the repository at this point in the history
compiler: prevent temporary for local reductions
  • Loading branch information
mloubout authored Sep 28, 2023
2 parents a7d9c49 + 47bf87c commit b610ccf
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 26 deletions.
2 changes: 1 addition & 1 deletion devito/builtins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self, *functions, op=dv.mpi.MPI.SUM, dtype=None):
self.op = op

def __enter__(self):
i = dv.Dimension(name='i',)
i = dv.Dimension(name='mri',)
self.n = dv.Function(name='n', shape=(1,), dimensions=(i,),
grid=self.grid, dtype=self.dtype)
self.n.data[0] = 0
Expand Down
51 changes: 32 additions & 19 deletions devito/ir/clusters/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from devito.exceptions import InvalidOperator
from devito.ir.support import (Any, Backward, Forward, IterationSpace,
PARALLEL_IF_ATOMIC, pull_dims)
pull_dims)
from devito.ir.clusters.analysis import analyze
from devito.ir.clusters.cluster import Cluster, ClusterGroup
from devito.ir.clusters.visitors import Queue, QueueStateful, cluster_pass
Expand Down Expand Up @@ -402,7 +402,8 @@ def normalize(clusters, **kwargs):
sregistry = kwargs['sregistry']

clusters = normalize_nested_indexeds(clusters, sregistry)
clusters = normalize_reductions(clusters, sregistry, options)
clusters = normalize_reductions_dense(clusters, sregistry, options)
clusters = normalize_reductions_sparse(clusters, sregistry, options)

return clusters

Expand Down Expand Up @@ -444,34 +445,22 @@ def pull_indexeds(expr, subs, mapper, parent=None):
return cluster.rebuild(processed)


@cluster_pass(mode='all')
def normalize_reductions(cluster, sregistry, options):
@cluster_pass(mode='dense')
def normalize_reductions_dense(cluster, sregistry, options):
"""
Extract the right-hand sides of reduction Eq's in to temporaries.
"""
opt_mapify_reduce = options['mapify-reduce']

dims = [d for d, v in cluster.properties.items() if PARALLEL_IF_ATOMIC in v]
dims = [d for d in cluster.properties.dimensions
if cluster.properties.is_parallel_atomic(d)]

if not dims:
return cluster

processed = []
for e in cluster.exprs:
if e.is_Reduction and (e.lhs.is_Indexed or cluster.is_sparse):
# Transform `e` such that we reduce into a scalar (ultimately via
# atomic ops, though this part is carried out by a much later pass)
# For example, given `i = m[p_src]` (i.e., indirection array), turn:
# `u[t, i] += f(u[t, i], src, ...)`
# into
# `s = f(u[t, i], src, ...)`
# `u[t, i] += s`
name = sregistry.make_name()
v = Symbol(name=name, dtype=e.dtype)
processed.extend([e.func(v, e.rhs, operation=None),
e.func(e.lhs, v)])

elif e.is_Reduction and e.lhs.is_Symbol and opt_mapify_reduce:
if e.is_Reduction and e.lhs.is_Symbol and opt_mapify_reduce:
# Transform `e` into what is in essence an explicit map-reduce
# For example, turn:
# `s += f(u[x], v[x], ...)`
Expand All @@ -484,7 +473,31 @@ def normalize_reductions(cluster, sregistry, options):
a = Array(name=name, dtype=e.dtype, dimensions=dims)
processed.extend([Eq(a.indexify(), e.rhs),
e.func(e.lhs, a.indexify())])
else:
processed.append(e)

return cluster.rebuild(processed)


@cluster_pass(mode='sparse')
def normalize_reductions_sparse(cluster, sregistry, options):
"""
Extract the right-hand sides of reduction Eq's in to temporaries.
"""
processed = []
for e in cluster.exprs:
if e.is_Reduction and e.lhs.is_Indexed:
# Transform `e` such that we reduce into a scalar (ultimately via
# atomic ops, though this part is carried out by a much later pass)
# For example, given `i = m[p_src]` (i.e., indirection array), turn:
# `u[t, i] += f(u[t, i], src, ...)`
# into
# `s = f(u[t, i], src, ...)`
# `u[t, i] += s`
name = sregistry.make_name()
v = Symbol(name=name, dtype=e.dtype)
processed.extend([e.func(v, e.rhs, operation=None),
e.func(e.lhs, v)])
else:
processed.append(e)

Expand Down
15 changes: 13 additions & 2 deletions devito/ir/clusters/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,20 @@ def is_dense(self):
not self.is_halo_touch and
all(a.is_regular for a in self.scope.accesses))

@property
@cached_property
def is_sparse(self):
return not self.is_dense
"""
A cluster is sparse if it represent a sparse operation i.e if both
* The cluster contains sparse functions
* The cluster uses dense functions
If only the first case is true, the cluster only contains operation on the sparse
function itself without indirection and therefore only contains dense operations.
"""
return (any(f.is_SparseFunction for f in self.functions) and
len([f for f in self.functions
if (f.is_Function and not f.is_SparseFunction)]) > 0)

@property
def is_halo_touch(self):
Expand Down
8 changes: 8 additions & 0 deletions devito/ir/equations/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def apply(self, func):
kwargs['conditionals'] = {k: func(v) for k, v in self.conditionals.items()}
return self.func(*args, **kwargs)

def __repr__(self):
if not self.is_Reduction:
return super().__repr__()
elif self.operation is OpInc:
return '%s += %s' % (self.lhs, self.rhs)
else:
return '%s = %s(%s, %s)' % (self.lhs, self.operation, self.lhs, self.rhs)

# Pickling support
__reduce_ex__ = Pickable.__reduce_ex__

Expand Down
7 changes: 7 additions & 0 deletions devito/ir/support/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class Properties(frozendict):
A mapper {Dimension -> {properties}}.
"""

@property
def dimensions(self):
return tuple(self)

def add(self, dims, properties=None):
m = dict(self)
for d in as_tuple(dims):
Expand Down Expand Up @@ -205,6 +209,9 @@ def is_parallel(self, dims):
return any(len(self[d] & {PARALLEL, PARALLEL_INDEP}) > 0
for d in as_tuple(dims))

def is_parallel_atomic(self, dims):
return any(len(self[d] & {PARALLEL_IF_ATOMIC}) > 0 for d in as_tuple(dims))

def is_parallel_relaxed(self, dims):
return any(len(self[d] & PARALLELS) > 0 for d in as_tuple(dims))

Expand Down
4 changes: 2 additions & 2 deletions devito/types/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ def guard(self, expr=None):
condition=condition, indirect=True)

if expr is None:
out = self.indexify().xreplace({self._sparse_dim: cd})
out = self.indexify()._subs(self._sparse_dim, cd)
else:
functions = {f for f in retrieve_function_carriers(expr)
if f.is_SparseFunction}
out = indexify(expr).xreplace({f._sparse_dim: cd for f in functions})
out = indexify(expr).subs({f._sparse_dim: cd for f in functions})

return out, temps

Expand Down
26 changes: 25 additions & 1 deletion tests/test_dle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
configuration, dimensions, info, cos)
from devito.exceptions import InvalidArgument
from devito.ir.iet import (Iteration, FindNodes, IsPerfectIteration,
retrieve_iteration_tree)
retrieve_iteration_tree, Expression)
from devito.passes.iet.languages.openmp import Ompizer, OmpRegion
from devito.tools import as_tuple
from devito.types import Scalar
Expand Down Expand Up @@ -765,6 +765,30 @@ def test_array_sum_reduction(self, so, dim):

assert np.allclose(f.data, 18)

def test_reduction_local(self):
grid = Grid((11, 11))
d = Dimension("i")
n = Function(name="n", dimensions=(d,), shape=(1,))
u = Function(name="u", grid=grid)
u.data.fill(1.)

op = Operator(Inc(n[0], u))
op()

cond = FindNodes(Expression).visit(op)
iterations = FindNodes(Iteration).visit(op)
# Should not creat any temporary for the reduction
assert len(cond) == 1
if configuration['language'] == 'C':
pass
elif Ompizer._support_array_reduction(configuration['compiler']):
assert "reduction(+:n[0])" in iterations[0].pragmas[0].value
else:
# E.g. old GCC's
assert "atomic update" in str(iterations[-1])

assert n.data[0] == 11*11

def test_array_max_reduction(self):
"""
Test generation of OpenMP sum-reduction clauses involving Function's.
Expand Down
9 changes: 8 additions & 1 deletion tests/test_dse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2669,13 +2669,20 @@ def test_sparse_const(self):

u = TimeFunction(name="u", grid=grid)
src = PrecomputedSparseTimeFunction(name="src", grid=grid, npoint=1, nt=11,
r=2, interpolation_coeffs=np.ones((1, 3, 2)))
r=2, interpolation_coeffs=np.ones((1, 3, 2)),
gridpoints=[[5, 5, 5]])
u.data.fill(1.)

op = Operator(src.interpolate(u))

cond = FindNodes(Conditional).visit(op)
assert len(cond) == 1
assert len(cond[0].args['then_body'][0].exprs) == 1
assert all(e.is_scalar for e in cond[0].args['then_body'][0].exprs)

op()
assert np.all(src.data == 8)


class TestIsoAcoustic(object):

Expand Down

0 comments on commit b610ccf

Please sign in to comment.