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
pycodegen
pystencils
Commits
325ca386
Commit
325ca386
authored
2 months ago
by
Richard Angersbach
Browse files
Options
Downloads
Patches
Plain Diff
Move checks and init value determination for ReductionAssignments to add_reduction_info
parent
0c8654e3
No related branches found
No related tags found
1 merge request
!438
Reduction Support
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/pystencils/backend/kernelcreation/context.py
+28
-3
28 additions, 3 deletions
src/pystencils/backend/kernelcreation/context.py
src/pystencils/backend/kernelcreation/freeze.py
+2
-39
2 additions, 39 deletions
src/pystencils/backend/kernelcreation/freeze.py
with
30 additions
and
42 deletions
src/pystencils/backend/kernelcreation/context.py
+
28
−
3
View file @
325ca386
...
...
@@ -6,7 +6,8 @@ from itertools import chain, count
from
collections
import
namedtuple
,
defaultdict
import
re
from
..ast.expressions
import
PsExpression
from
..ast.expressions
import
PsExpression
,
PsConstantExpr
,
PsCall
from
..functions
import
NumericLimitsFunctions
,
PsMathFunction
from
...defaults
import
DEFAULTS
from
...field
import
Field
,
FieldType
from
...sympyextensions
import
ReductionOp
...
...
@@ -208,10 +209,16 @@ class KernelCreationContext:
lhs_name
:
str
,
lhs_dtype
:
PsType
,
reduction_op
:
ReductionOp
,
init_value
:
PsExpression
,
):
"""
Create ReductionInfo instance and add to its corresponding lookup table for a given symbol name.
"""
# make sure that lhs symbol never occurred before ReductionAssignment
if
self
.
find_symbol
(
lhs_name
):
raise
KernelConstraintsError
(
f
"
Left-hand side
{
lhs_name
}
of ReductionAssignment already exists in symbol table.
"
f
"
Make sure that it is only used once in a kernel
'
s ReductionAssignment.
"
)
# replace datatype of lhs symbol with pointer datatype for write-back mechanism
symb
=
self
.
get_symbol
(
lhs_name
,
lhs_dtype
)
pointer_symb
=
PsSymbol
(
lhs_name
,
PsPointerType
(
lhs_dtype
))
...
...
@@ -221,9 +228,27 @@ class KernelCreationContext:
local_symb
=
PsSymbol
(
f
"
{
lhs_name
}
_local
"
,
lhs_dtype
)
self
.
add_symbol
(
local_symb
)
# match for reduction operation and set neutral init_val
init_val
:
PsExpression
match
reduction_op
:
case
ReductionOp
.
Add
:
init_val
=
PsConstantExpr
(
PsConstant
(
0
))
case
ReductionOp
.
Sub
:
init_val
=
PsConstantExpr
(
PsConstant
(
0
))
case
ReductionOp
.
Mul
:
init_val
=
PsConstantExpr
(
PsConstant
(
1
))
case
ReductionOp
.
Min
:
init_val
=
PsCall
(
PsMathFunction
(
NumericLimitsFunctions
.
Max
),
[])
case
ReductionOp
.
Max
:
init_val
=
PsCall
(
PsMathFunction
(
NumericLimitsFunctions
.
Min
),
[])
case
_
:
raise
PsInternalCompilerError
(
f
"
Unsupported kind of reduction assignment:
{
reduction_op
}
.
"
)
# create reduction info and add to set
reduction_info
=
ReductionInfo
(
reduction_op
,
init_val
ue
,
local_symb
,
pointer_symb
reduction_op
,
init_val
,
local_symb
,
pointer_symb
)
self
.
_reduction_data
[
lhs_name
]
=
reduction_info
...
...
This diff is collapsed.
Click to expand it.
src/pystencils/backend/kernelcreation/freeze.py
+
2
−
39
View file @
325ca386
...
...
@@ -189,21 +189,12 @@ class FreezeExpressions:
def
map_ReductionAssignment
(
self
,
expr
:
ReductionAssignment
):
assert
isinstance
(
expr
.
lhs
,
TypedSymbol
)
# make sure that lhs symbol never occurred before ReductionAssignment
if
self
.
_ctx
.
find_symbol
(
expr
.
lhs
.
name
):
raise
FreezeError
(
f
"
Left-hand side
{
expr
.
lhs
}
of ReductionAssignment already exists in symbol table.
"
f
"
Make sure that it is only used once in a kernel
'
s ReductionAssignment.
"
)
lhs
=
self
.
visit
(
expr
.
lhs
)
rhs
=
self
.
visit
(
expr
.
rhs
)
assert
isinstance
(
rhs
,
PsExpression
)
assert
isinstance
(
lhs
,
PsSymbolExpr
)
reduction_op
=
expr
.
reduction_op
lhs_symbol
=
lhs
.
symbol
lhs_symbol
=
expr
.
lhs
lhs_dtype
=
lhs_symbol
.
dtype
lhs_name
=
lhs_symbol
.
name
...
...
@@ -211,27 +202,9 @@ class FreezeExpressions:
lhs_dtype
,
PsNumericType
),
"
Reduction assignments require type information of the lhs symbol.
"
# match for reduction operation and set neutral init_val
init_val
:
PsExpression
match
reduction_op
:
case
ReductionOp
.
Add
:
init_val
=
PsConstantExpr
(
PsConstant
(
0
))
case
ReductionOp
.
Sub
:
init_val
=
PsConstantExpr
(
PsConstant
(
0
))
case
ReductionOp
.
Mul
:
init_val
=
PsConstantExpr
(
PsConstant
(
1
))
case
ReductionOp
.
Min
:
init_val
=
PsCall
(
PsMathFunction
(
NumericLimitsFunctions
.
Max
),
[])
case
ReductionOp
.
Max
:
init_val
=
PsCall
(
PsMathFunction
(
NumericLimitsFunctions
.
Min
),
[])
case
_
:
raise
FreezeError
(
f
"
Unsupported kind of reduction assignment:
{
reduction_op
}
.
"
)
# get reduction info from context
reduction_info
=
self
.
_ctx
.
add_reduction_info
(
lhs_name
,
lhs_dtype
,
reduction_op
,
init_val
lhs_name
,
lhs_dtype
,
reduction_op
)
# create new lhs from newly created local lhs symbol
...
...
@@ -330,16 +303,6 @@ class FreezeExpressions:
def
map_TypedSymbol
(
self
,
expr
:
TypedSymbol
):
dtype
=
self
.
_ctx
.
resolve_dynamic_type
(
expr
.
dtype
)
# check if symbol is referenced after freezing a ReductionAssignment
if
self
.
_ctx
.
find_reduction_info
(
expr
.
name
):
# check if types do not align since a ReductionAssignment modifies
# the symbol's type to PsPointerType in the context's symbol table
if
(
symbol
:
=
self
.
_ctx
.
find_symbol
(
expr
.
name
))
and
symbol
.
dtype
!=
dtype
:
raise
FreezeError
(
f
"
Illegal access to reduction symbol
{
symbol
.
name
}
after freezing a kernel
'
s ReductionAssignment.
"
)
symb
=
self
.
_ctx
.
get_symbol
(
expr
.
name
,
dtype
)
return
PsSymbolExpr
(
symb
)
...
...
This diff is collapsed.
Click to expand it.
Frederik Hennig
@da15siwa
mentioned in commit
4e688b86
·
2 months ago
mentioned in commit
4e688b86
mentioned in commit 4e688b867339c90a6fba3ad12c383d1e59211ed9
Toggle commit list
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