Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anirudh Jonnalagadda
pystencils
Commits
443527ae
Commit
443527ae
authored
5 years ago
by
Stephan Seitz
Committed by
Martin Bauer
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add ConditionalFieldAccess (Field.Access after out-of-bounds check)
parent
b338d183
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pystencils/astnodes.py
+25
-0
25 additions, 0 deletions
pystencils/astnodes.py
pystencils/backends/cbackend.py
+3
-0
3 additions, 0 deletions
pystencils/backends/cbackend.py
pystencils_tests/test_conditional_field_access.py
+69
-0
69 additions, 0 deletions
pystencils_tests/test_conditional_field_access.py
with
97 additions
and
0 deletions
pystencils/astnodes.py
+
25
−
0
View file @
443527ae
...
...
@@ -744,3 +744,28 @@ class EmptyLine(Node):
def
__repr__
(
self
):
return
self
.
__str__
()
class
ConditionalFieldAccess
(
sp
.
Function
):
"""
:class:`pystencils.Field.Access` that is only executed if a certain condition is met.
Can be used, for instance, for out-of-bound checks.
"""
def
__new__
(
cls
,
field_access
,
outofbounds_condition
,
outofbounds_value
=
0
):
return
sp
.
Function
.
__new__
(
cls
,
field_access
,
outofbounds_condition
,
sp
.
S
(
outofbounds_value
))
@property
def
access
(
self
):
return
self
.
args
[
0
]
@property
def
outofbounds_condition
(
self
):
return
self
.
args
[
1
]
@property
def
outofbounds_value
(
self
):
return
self
.
args
[
2
]
def
__getnewargs__
(
self
):
return
self
.
access
,
self
.
outofbounds_condition
,
self
.
outofbounds_value
This diff is collapsed.
Click to expand it.
pystencils/backends/cbackend.py
+
3
−
0
View file @
443527ae
...
...
@@ -426,6 +426,9 @@ class CustomSympyPrinter(CCodePrinter):
)
return
code
def
_print_ConditionalFieldAccess
(
self
,
node
):
return
self
.
_print
(
sp
.
Piecewise
((
node
.
outofbounds_value
,
node
.
outofbounds_condition
),
(
node
.
access
,
True
)))
_print_Max
=
C89CodePrinter
.
_print_Max
_print_Min
=
C89CodePrinter
.
_print_Min
...
...
This diff is collapsed.
Click to expand it.
pystencils_tests/test_conditional_field_access.py
0 → 100644
+
69
−
0
View file @
443527ae
# -*- coding: utf-8 -*-
#
# Copyright © 2019 Stephan Seitz <stephan.seitz@fau.de>
#
# Distributed under terms of the GPLv3 license.
"""
"""
import
itertools
import
numpy
as
np
import
pytest
import
sympy
as
sp
import
pystencils
as
ps
from
pystencils
import
Field
,
x_vector
from
pystencils.astnodes
import
ConditionalFieldAccess
from
pystencils.simp
import
sympy_cse
def
add_fixed_constant_boundary_handling
(
assignments
,
with_cse
):
common_shape
=
next
(
iter
(
set
().
union
(
itertools
.
chain
.
from_iterable
(
[
a
.
atoms
(
Field
.
Access
)
for
a
in
assignments
]
)))).
field
.
spatial_shape
ndim
=
len
(
common_shape
)
def
is_out_of_bound
(
access
,
shape
):
return
sp
.
Or
(
*
[
sp
.
Or
(
a
<
0
,
a
>=
s
)
for
a
,
s
in
zip
(
access
,
shape
)])
safe_assignments
=
[
ps
.
Assignment
(
assignment
.
lhs
,
assignment
.
rhs
.
subs
({
a
:
ConditionalFieldAccess
(
a
,
is_out_of_bound
(
sp
.
Matrix
(
a
.
offsets
)
+
x_vector
(
ndim
),
common_shape
))
for
a
in
assignment
.
rhs
.
atoms
(
Field
.
Access
)
if
not
a
.
is_absolute_access
}))
for
assignment
in
assignments
.
all_assignments
]
subs
=
[{
a
:
ConditionalFieldAccess
(
a
,
is_out_of_bound
(
sp
.
Matrix
(
a
.
offsets
)
+
x_vector
(
ndim
),
common_shape
))
for
a
in
assignment
.
rhs
.
atoms
(
Field
.
Access
)
if
not
a
.
is_absolute_access
}
for
assignment
in
assignments
.
all_assignments
]
print
(
subs
)
if
with_cse
:
safe_assignments
=
sympy_cse
(
ps
.
AssignmentCollection
(
safe_assignments
))
return
safe_assignments
else
:
return
ps
.
AssignmentCollection
(
safe_assignments
)
@pytest.mark.parametrize
(
'
with_cse
'
,
(
False
,
'
with_cse
'
))
def
test_boundary_check
(
with_cse
):
f
,
g
=
ps
.
fields
(
"
f, g : [2D]
"
)
stencil
=
ps
.
Assignment
(
g
[
0
,
0
],
(
f
[
1
,
0
]
+
f
[
-
1
,
0
]
+
f
[
0
,
1
]
+
f
[
0
,
-
1
])
/
4
)
f_arr
=
np
.
random
.
rand
(
1000
,
1000
)
g_arr
=
np
.
zeros_like
(
f_arr
)
# kernel(f=f_arr, g=g_arr)
assignments
=
add_fixed_constant_boundary_handling
(
ps
.
AssignmentCollection
([
stencil
]),
with_cse
)
print
(
assignments
)
kernel_checked
=
ps
.
create_kernel
(
assignments
,
ghost_layers
=
0
).
compile
()
print
(
ps
.
show_code
(
kernel_checked
))
# No SEGFAULT, please!!
kernel_checked
(
f
=
f_arr
,
g
=
g_arr
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment