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
Model registry
Operate
Environments
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
Stephan Seitz
pystencils
Compare revisions
master to ConditionalFieldAccess
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
seitz/pystencils
Select target project
No results found
ConditionalFieldAccess
Select Git revision
Swap
Target
pycodegen/pystencils
Select target project
anirudh.jonnalagadda/pystencils
hyteg/pystencils
jbadwaik/pystencils
jngrad/pystencils
itischler/pystencils
ob28imeq/pystencils
hoenig/pystencils
Bindgen/pystencils
hammer/pystencils
da15siwa/pystencils
holzer/pystencils
alexander.reinauer/pystencils
ec93ujoh/pystencils
Harke/pystencils
seitz/pystencils
pycodegen/pystencils
16 results
master
Select Git revision
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (2)
Add ConditionalFieldAccess (Field.Access after out-of-bounds check)
· 8dfe844c
Stephan Seitz
authored
5 years ago
8dfe844c
Merge branch 'master' into 'ConditionalFieldAccess'
· 800eaf6c
Martin Bauer
authored
5 years ago
# Conflicts: # pystencils/data_types.py
800eaf6c
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
View file @
800eaf6c
...
@@ -744,3 +744,28 @@ class EmptyLine(Node):
...
@@ -744,3 +744,28 @@ class EmptyLine(Node):
def
__repr__
(
self
):
def
__repr__
(
self
):
return
self
.
__str__
()
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
View file @
800eaf6c
...
@@ -426,6 +426,9 @@ class CustomSympyPrinter(CCodePrinter):
...
@@ -426,6 +426,9 @@ class CustomSympyPrinter(CCodePrinter):
)
)
return
code
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_Max
=
C89CodePrinter
.
_print_Max
_print_Min
=
C89CodePrinter
.
_print_Min
_print_Min
=
C89CodePrinter
.
_print_Min
...
...
This diff is collapsed.
Click to expand it.
pystencils_tests/test_conditional_field_access.py
0 → 100644
View file @
800eaf6c
# -*- 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.