Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
Pipeline #16993 passed
...@@ -3,6 +3,7 @@ from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, ...@@ -3,6 +3,7 @@ from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set,
import sympy as sp import sympy as sp
import pystencils
from pystencils.assignment import Assignment from pystencils.assignment import Assignment
from pystencils.sympyextensions import count_operations, fast_subs, sort_assignments_topologically from pystencils.sympyextensions import count_operations, fast_subs, sort_assignments_topologically
...@@ -346,6 +347,25 @@ class AssignmentCollection: ...@@ -346,6 +347,25 @@ class AssignmentCollection:
def __iter__(self): def __iter__(self):
return self.main_assignments.__iter__() 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 @property
def main_assignments_dict(self): def main_assignments_dict(self):
return {a.lhs: a.rhs for a in self.main_assignments} return {a.lhs: a.rhs for a in self.main_assignments}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment