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
2b6589b8
Commit
2b6589b8
authored
5 months ago
by
Richard Angersbach
Browse files
Options
Downloads
Patches
Plain Diff
Introduce masks for warp reductions and fix errors when shuffling warp results
parent
c7b564be
No related branches found
No related tags found
1 merge request
!438
Reduction Support
Pipeline
#74295
failed
5 months ago
Stage: Code Quality
Stage: Unit Tests
Stage: legacy_test
Stage: docs
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pystencils/backend/platforms/cuda.py
+23
-11
23 additions, 11 deletions
src/pystencils/backend/platforms/cuda.py
with
23 additions
and
11 deletions
src/pystencils/backend/platforms/cuda.py
+
23
−
11
View file @
2b6589b8
...
...
@@ -5,6 +5,8 @@ import operator
from
abc
import
ABC
,
abstractmethod
from
functools
import
reduce
from
pystencils.types
import
PsBoolType
from
..ast
import
PsAstNode
from
..constants
import
PsConstant
from
...compound_op_mapping
import
compound_op_to_expr
...
...
@@ -310,11 +312,9 @@ class CudaPlatform(GenericGpu):
if
not
isinstance
(
stype
,
PsIeeeFloatType
)
or
stype
.
width
not
in
(
32
,
64
):
NotImplementedError
(
"
atomic operations are only available for float32/64 datatypes
"
)
def
gen_shuffle_instr
(
offset
:
int
):
return
PsCall
(
CFunction
(
"
__shfl_xor_sync
"
,
[
UInt
(
32
),
stype
,
SInt
(
32
)],
stype
),
[
PsLiteralExpr
(
PsLiteral
(
"
0xffffffff
"
,
UInt
(
32
))),
symbol_expr
,
PsConstantExpr
(
PsConstant
(
offset
,
SInt
(
32
)))])
# set up mask symbol for active threads in warp
mask
=
PsSymbol
(
"
__shfl_mask
"
,
UInt
(
32
))
self
.
_ctx
.
add_symbol
(
mask
)
# workaround for subtractions -> use additions for reducing intermediate results
# similar to OpenMP reductions: local copies (negative sign) are added at the end
...
...
@@ -325,8 +325,13 @@ class CudaPlatform(GenericGpu):
actual_op
=
op
# perform local warp reductions
num_shuffles
=
math
.
frexp
(
warp_size
)[
1
]
-
1
shuffles
=
[
PsAssignment
(
symbol_expr
,
compound_op_to_expr
(
actual_op
,
symbol_expr
,
gen_shuffle_instr
(
i
)))
def
gen_shuffle_instr
(
offset
:
int
):
return
PsCall
(
CFunction
(
"
__shfl_xor_sync
"
,
[
UInt
(
32
),
stype
,
SInt
(
32
)],
stype
),
[
PsSymbolExpr
(
mask
),
symbol_expr
,
PsConstantExpr
(
PsConstant
(
offset
,
SInt
(
32
)))])
num_shuffles
=
math
.
frexp
(
warp_size
)[
1
]
shuffles
=
[
PsAssignment
(
symbol_expr
,
compound_op_to_expr
(
actual_op
,
symbol_expr
,
gen_shuffle_instr
(
pow
(
2
,
i
-
1
))))
for
i
in
reversed
(
range
(
1
,
num_shuffles
))]
# find first thread in warp
...
...
@@ -343,14 +348,21 @@ class CudaPlatform(GenericGpu):
PsConstantExpr
(
PsConstant
(
0
,
SInt
(
32
))))
cond
=
PsAnd
(
is_valid_thread
,
first_thread_in_warp
)
if
is_valid_thread
else
first_thread_in_warp
full_mask
=
PsLiteralExpr
(
PsLiteral
(
"
0xffffffff
"
,
UInt
(
32
)))
ballot_instr
=
PsCall
(
CFunction
(
"
__ballot_sync
"
,
[
UInt
(
32
),
SInt
(
32
)],
SInt
(
32
)),
[
full_mask
,
is_valid_thread
])
decl_mask
=
PsDeclaration
(
PsSymbolExpr
(
mask
),
ballot_instr
if
is_valid_thread
else
full_mask
)
# use atomic operation on first thread of warp to sync
call
.
function
=
CFunction
(
f
"
atomic
{
actual_op
.
name
}
"
,
[
ptrtype
,
stype
],
PsCustomType
(
"
void
"
))
call
.
args
=
(
ptr_expr
,
symbol_expr
)
# assemble warp reduction
return
PsBlock
(
shuffles
+
[
PsConditional
(
cond
,
PsBlock
([
PsStatement
(
call
)]))])
return
PsConditional
(
is_valid_thread
if
is_valid_thread
else
PsConstantExpr
(
PsLiteral
(
"
true
"
,
PsBoolType
)),
PsBlock
(
[
decl_mask
]
+
shuffles
+
[
PsConditional
(
cond
,
PsBlock
([
PsStatement
(
call
)]))]))
# Internals
...
...
@@ -359,7 +371,7 @@ class CudaPlatform(GenericGpu):
def
_get_condition_for_translation
(
self
,
ispace
:
IterationSpace
):
if
not
self
.
_omit_range_check
:
if
self
.
_omit_range_check
:
return
None
match
ispace
:
...
...
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