Skip to content

Commit

Permalink
'shift_to_zero_indexing' some streamlining and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelSt98 authored and mlange05 committed Mar 25, 2024
1 parent e94a942 commit 12aaeb0
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions loki/transform/transform_array_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
]


def shift_to_zero_indexing(routine, ignore=()):
def shift_to_zero_indexing(routine, ignore=None):
"""
Shift all array indices to adjust to 0-based indexing conventions (eg. for C or Python)
Expand All @@ -42,8 +42,10 @@ def shift_to_zero_indexing(routine, ignore=()):
routine : :any:`Subroutine`
The subroutine in which the array dimensions should be shifted
ignore : list of str
Dimensions (or rather variables being dimensions) to be ignored
List of variable names for which, if found in the dimension expression
of an array subscript, that dimension is not shifted to zero.
"""
ignore = as_tuple(ignore)
vmap = {}
for v in FindVariables(unique=False).visit(routine.body):
if isinstance(v, sym.Array):
Expand All @@ -54,14 +56,10 @@ def shift_to_zero_indexing(routine, ignore=()):
# no shift for stop because Python ranges are [start, stop)
new_dims += [sym.RangeIndex((start, d.stop, d.step))]
else:
shift = True
for var in FindVariables().visit(d):
if var in ignore:
shift = False
if shift:
new_dims += [d - sym.Literal(1)]
else:
if ignore and any(var in ignore for var in FindVariables().visit(d)):
new_dims += [d]
else:
new_dims += [d - sym.Literal(1)]
vmap[v] = v.clone(dimensions=as_tuple(new_dims))
routine.body = SubstituteExpressions(vmap).visit(routine.body)

Expand Down

0 comments on commit 12aaeb0

Please sign in to comment.