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
Alexander Reinauer
pystencils
Commits
c5c66fe0
Commit
c5c66fe0
authored
1 year ago
by
Markus Holzer
Browse files
Options
Downloads
Plain Diff
Merge branch 'bauerd/print-pow' into 'master'
Print small integer powers as divisions/multiplications Closes
#72
See merge request
!357
parents
7f28040a
9115975f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pystencils/backends/cbackend.py
+21
-5
21 additions, 5 deletions
pystencils/backends/cbackend.py
pystencils_tests/test_printing.py
+51
-0
51 additions, 0 deletions
pystencils_tests/test_printing.py
with
72 additions
and
5 deletions
pystencils/backends/cbackend.py
+
21
−
5
View file @
c5c66fe0
...
@@ -443,10 +443,22 @@ class CustomSympyPrinter(CCodePrinter):
...
@@ -443,10 +443,22 @@ class CustomSympyPrinter(CCodePrinter):
def
_print_Pow
(
self
,
expr
):
def
_print_Pow
(
self
,
expr
):
"""
Don
'
t use std::pow function, for small integer exponents, write as multiplication
"""
"""
Don
'
t use std::pow function, for small integer exponents, write as multiplication
"""
if
isinstance
(
expr
.
exp
,
sp
.
Integer
)
and
(
-
8
<
expr
.
exp
<
8
):
# Ideally the printer has as little logic as possible. Therefore,
raise
ValueError
(
f
"
This expression:
{
expr
}
contains a pow function that should be simplified already with
"
# powers should be rewritten as `DivFunc`s / unevaluated `Mul`s before
f
"
a sequence of multiplications
"
)
# printing. `NodeCollection` offers a convenience function to do just
return
super
(
CustomSympyPrinter
,
self
).
_print_Pow
(
expr
)
# that. However, `cut_loops` rewrites unevaluated multiplications as
# `Pow`s again. Neither `deepcopy` nor `func(*args)` are suited to
# rebuild unevaluated expressions. Therefore, as long as we stick with
# SymPy, this is the only way to avoid printing `pow`s.
exp
=
expr
.
exp
.
expr
if
isinstance
(
expr
.
exp
,
CastFunc
)
else
expr
.
exp
one_type
=
expr
.
base
.
dtype
if
hasattr
(
expr
.
base
,
"
dtype
"
)
else
get_type_of_expression
(
expr
.
base
)
if
exp
.
is_integer
and
exp
.
is_number
and
(
0
<
exp
<=
8
):
return
f
"
(
{
self
.
_print
(
sp
.
Mul
(
*
[
expr
.
base
]
*
exp
,
evaluate
=
False
))
}
)
"
elif
exp
.
is_integer
and
exp
.
is_number
and
(
-
8
<=
exp
<
0
):
return
f
"
{
self
.
_typed_number
(
1
,
one_type
)
}
/ (
{
self
.
_print
(
sp
.
Mul
(
*
([
expr
.
base
]
*
-
exp
),
evaluate
=
False
))
}
)
"
else
:
return
super
(
CustomSympyPrinter
,
self
).
_print_Pow
(
expr
)
# TODO don't print ones in sp.Mul
# TODO don't print ones in sp.Mul
...
@@ -490,6 +502,7 @@ class CustomSympyPrinter(CCodePrinter):
...
@@ -490,6 +502,7 @@ class CustomSympyPrinter(CCodePrinter):
assert
len
(
expr
.
args
)
==
1
,
"
address_of must only have one argument
"
assert
len
(
expr
.
args
)
==
1
,
"
address_of must only have one argument
"
return
f
"
&(
{
self
.
_print
(
expr
.
args
[
0
])
}
)
"
return
f
"
&(
{
self
.
_print
(
expr
.
args
[
0
])
}
)
"
elif
isinstance
(
expr
,
CastFunc
):
elif
isinstance
(
expr
,
CastFunc
):
cast
=
"
(({data_type})({code}))
"
arg
,
data_type
=
expr
.
args
arg
,
data_type
=
expr
.
args
if
arg
.
is_Number
and
not
isinstance
(
arg
,
(
sp
.
core
.
numbers
.
Infinity
,
sp
.
core
.
numbers
.
NegativeInfinity
)):
if
arg
.
is_Number
and
not
isinstance
(
arg
,
(
sp
.
core
.
numbers
.
Infinity
,
sp
.
core
.
numbers
.
NegativeInfinity
)):
return
self
.
_typed_number
(
arg
,
data_type
)
return
self
.
_typed_number
(
arg
,
data_type
)
...
@@ -504,9 +517,12 @@ class CustomSympyPrinter(CCodePrinter):
...
@@ -504,9 +517,12 @@ class CustomSympyPrinter(CCodePrinter):
for
k
in
known
:
for
k
in
known
:
if
k
in
code
:
if
k
in
code
:
return
code
.
replace
(
k
,
f
'
{
k
}
f
'
)
return
code
.
replace
(
k
,
f
'
{
k
}
f
'
)
# Powers of small integers are printed as divisions/multiplications.
if
'
/
'
in
code
or
'
*
'
in
code
:
return
cast
.
format
(
data_type
=
data_type
,
code
=
code
)
raise
ValueError
(
f
"
{
code
}
doesn
'
t give
{
known
=
}
function back.
"
)
raise
ValueError
(
f
"
{
code
}
doesn
'
t give
{
known
=
}
function back.
"
)
else
:
else
:
return
f
"
((
{
data_type
}
)(
{
self
.
_print
(
arg
)
}
))
"
return
cast
.
format
(
data_type
=
data_type
,
code
=
self
.
_print
(
arg
)
)
elif
isinstance
(
expr
,
fast_division
):
elif
isinstance
(
expr
,
fast_division
):
raise
ValueError
(
"
fast_division is only supported for Taget.GPU
"
)
raise
ValueError
(
"
fast_division is only supported for Taget.GPU
"
)
elif
isinstance
(
expr
,
fast_sqrt
):
elif
isinstance
(
expr
,
fast_sqrt
):
...
...
This diff is collapsed.
Click to expand it.
pystencils_tests/test_printing.py
+
51
−
0
View file @
c5c66fe0
import
pytest
import
pytest
import
re
import
sympy
as
sp
import
sympy
as
sp
import
pystencils
import
pystencils
...
@@ -51,3 +52,53 @@ def test_print_subtraction(dtype, target):
...
@@ -51,3 +52,53 @@ def test_print_subtraction(dtype, target):
code
=
pystencils
.
get_code_str
(
ast
)
code
=
pystencils
.
get_code_str
(
ast
)
assert
"
-1.0
"
not
in
code
assert
"
-1.0
"
not
in
code
def
test_print_small_integer_pow
():
printer
=
pystencils
.
backends
.
cbackend
.
CBackend
()
x
=
sp
.
Symbol
(
"
x
"
)
y
=
sp
.
Symbol
(
"
y
"
)
n
=
pystencils
.
TypedSymbol
(
"
n
"
,
"
int
"
)
t
=
pystencils
.
TypedSymbol
(
"
t
"
,
"
float32
"
)
s
=
pystencils
.
TypedSymbol
(
"
s
"
,
"
float32
"
)
equs
=
[
pystencils
.
astnodes
.
SympyAssignment
(
y
,
1
/
x
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
*
x
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
1
/
(
x
*
x
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
8
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
(
-
8
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
9
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
(
-
9
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
n
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
sp
.
Pow
(
4
,
4
,
evaluate
=
False
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
0.25
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
x
**
y
),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
pystencils
.
typing
.
cast_functions
.
CastFunc
(
1
/
x
,
"
float32
"
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
pystencils
.
typing
.
cast_functions
.
CastFunc
(
x
*
x
,
"
float32
"
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
(
t
+
s
)
**
(
-
8
)),
pystencils
.
astnodes
.
SympyAssignment
(
y
,
(
t
+
s
)
**
(
-
9
)),
]
typed
=
pystencils
.
typing
.
transformations
.
add_types
(
equs
,
pystencils
.
CreateKernelConfig
())
regexes
=
[
r
"
1\.0\s*/\s*\(?\s*x\s*\)?
"
,
r
"
x\s*\*\s*x
"
,
r
"
1\.0\s*/\s*\(\s*x\s*\*x\s*\)
"
,
r
"
x(\s*\*\s*x){7}
"
,
r
"
1\.0\s*/\s*\(\s*x(\s*\*\s*x){7}\s*\)
"
,
r
"
pow\(\s*x\s*,\s*9(\.0)?\s*\)
"
,
r
"
pow\(\s*x\s*,\s*-9(\.0)?\s*\)
"
,
r
"
pow\(\s*x\s*,\s*\(?\s*\(\s*double\s*\)\s*\(\s*n\s*\)\s*\)?\s*\)
"
,
r
"
\(\s*int[a-zA-Z0-9_]*\s*\)\s*\(+\s*4(\s*\*\s*4){3}\s*\)+
"
,
r
"
pow\(\s*x\s*,\s*0\.25\s*\)
"
,
r
"
pow\(\s*x\s*,\s*y\s*\)
"
,
r
"
\(\s*float\s*\)[ ()]*1\.0\s*/\s*\(?\s*x\s*\)?
"
,
r
"
\(\s*float\s*\)[ ()]*x\s*\*\s*x
"
,
r
"
\(\s*float\s*\)\s*\(\s*1\.0f\s*/\s*\(\s*\(\s*s\s*\+\s*t\s*\)(\s*\*\s*\(\s*s\s*\+\s*t\s*\)){7}\s*\)
"
,
r
"
powf\(\s*s\s*\+\s*t\s*,\s*-9\.0f\s*\)
"
,
]
for
r
,
e
in
zip
(
regexes
,
typed
):
assert
re
.
search
(
r
,
printer
(
e
))
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