From 6b857cff20bef3ed3c5ab22c20eeafe5608e3556 Mon Sep 17 00:00:00 2001 From: Stephan Seitz <stephan.seitz@fau.de> Date: Wed, 7 Aug 2019 12:36:24 +0200 Subject: [PATCH] Use own implementation of _has_exclusive_writes AssignmentCollection.has_exclusive_writes was not merged in pystencils core since the check overlaps with pystencils own checks and is very specific --- src/pystencils_autodiff/autodiff.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pystencils_autodiff/autodiff.py b/src/pystencils_autodiff/autodiff.py index 984eaaf..9447df9 100644 --- a/src/pystencils_autodiff/autodiff.py +++ b/src/pystencils_autodiff/autodiff.py @@ -20,6 +20,27 @@ class DiffModes(str, Enum): TF_MAD = 'transposed-forward' +def _has_exclusive_writes(assignment_collection): + """ + Simple check for exclusive (non-overlapping) writes. + I.e. AssignmentCollection can be executed safely in parallel without caring about race conditions. + No writes on same spatial location (considering all possible shifts). + """ + + assignments = assignment_collection.main_assignments + write_field_accesses = [a.lhs for a in assignments if isinstance(a.lhs, ps.Field.Access)] + + exclusive_writes = set() + for a in write_field_accesses: + + if (a.field, a.index) in exclusive_writes: + return False + else: + exclusive_writes.add((a.field, a.index)) + + return True + + def get_jacobian_of_assignments(assignments, diff_variables): """ Calculates the Jacobian of iterable of assignments wrt. diff_variables @@ -180,7 +201,7 @@ Backward: main_assignments = [a for a in backward_assignments if isinstance(a.lhs, ps.Field.Access)] subexpressions = [a for a in backward_assignments if not isinstance(a.lhs, ps.Field.Access)] backward_assignments = ps.AssignmentCollection(main_assignments, subexpressions) - assert backward_assignments.has_exclusive_writes, "Backward assignments don't have exclusive writes." + \ + assert _has_exclusive_writes(backward_assignments), "Backward assignments don't have exclusive writes." + \ " You should consider using 'transposed-forward' mode for resolving those conflicts" self._forward_assignments = forward_assignments @@ -329,7 +350,7 @@ Backward: subexpressions = [a for a in backward_assignments if not isinstance(a.lhs, ps.Field.Access)] backward_assignments = ps.AssignmentCollection(main_assignments, subexpressions) - assert backward_assignments.has_exclusive_writes, "Backward assignments don't have exclusive writes!" + assert _has_exclusive_writes(backward_assignments), "Backward assignments don't have exclusive writes!" self._backward_assignments = backward_assignments self._backward_field_map = {**diff_read_fields, **diff_write_fields} -- GitLab