Commit fb7e51ab authored by Stephan Seitz's avatar Stephan Seitz
Browse files

Add AssignmentCollection.has_exclusive_writes

An assumption of pystencils is that output stencils never overlap.
This allows massive parallelization without race conditions or atomics.

When I use my autodiff transformations I use this condition to check
whether the assumption still hold for the backward assignments.
parent ddb86435
Pipeline #16993 passed with stage
in 5 minutes and 15 seconds
......@@ -3,6 +3,7 @@ from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set,
import sympy as sp
import pystencils
from pystencils.assignment import Assignment
from pystencils.sympyextensions import count_operations, fast_subs, sort_assignments_topologically
......@@ -346,6 +347,25 @@ class AssignmentCollection:
def __iter__(self):
return self.main_assignments.__iter__()
def has_exclusive_writes(self):
"""
Simple check for exclusive (non-overlapping) writes.
I.e. AssignmentCollection can be executed safely in parallel without caring about race conditions.
"""
assignments = self.main_assignments
write_field_accesses = [a.lhs for a in assignments if isinstance(a.lhs, pystencils.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
@property
def main_assignments_dict(self):
return {a.lhs: a.rhs for a in self.main_assignments}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment