Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jonas Plewinski
pystencils
Commits
0b27d3c3
Commit
0b27d3c3
authored
Aug 13, 2019
by
Stephan Seitz
Browse files
Add OpenCLBackend
parent
69f779ea
Changes
3
Hide whitespace changes
Inline
Side-by-side
pystencils/backends/cbackend.py
View file @
0b27d3c3
...
...
@@ -58,6 +58,9 @@ def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom
elif
dialect
==
'cuda'
:
from
pystencils.backends.cuda_backend
import
CudaBackend
printer
=
CudaBackend
(
signature_only
=
signature_only
)
elif
dialect
==
'opencl'
:
from
pystencils.backends.opencl_backend
import
OpenCLBackend
printer
=
OpenCLBackend
(
signature_only
=
signature_only
)
else
:
raise
ValueError
(
"Unknown dialect: "
+
str
(
dialect
))
code
=
printer
(
ast_node
)
...
...
@@ -276,10 +279,10 @@ class CBackend:
]
destructuring_bindings
.
sort
()
# only for code aesthetics
return
"{
\n
"
+
self
.
_indent
+
\
(
"
\n
"
+
self
.
_indent
).
join
(
destructuring_bindings
)
+
\
"
\n
"
+
self
.
_indent
+
\
(
"
\n
"
+
self
.
_indent
).
join
(
self
.
_print
(
node
.
body
).
splitlines
())
+
\
"
\n
}"
(
"
\n
"
+
self
.
_indent
).
join
(
destructuring_bindings
)
+
\
"
\n
"
+
self
.
_indent
+
\
(
"
\n
"
+
self
.
_indent
).
join
(
self
.
_print
(
node
.
body
).
splitlines
())
+
\
"
\n
}"
# ------------------------------------------ Helper function & classes -------------------------------------------------
...
...
pystencils/backends/opencl_backend.py
0 → 100644
View file @
0b27d3c3
from
pystencils.backends.cuda_backend
import
CudaBackend
from
pystencils.backends.cbackend
import
generate_c
from
pystencils.astnodes
import
Node
def
generate_opencl
(
astnode
:
Node
,
signature_only
:
bool
=
False
)
->
str
:
"""Prints an abstract syntax tree node as CUDA code.
Args:
astnode: KernelFunction node to generate code for
signature_only: if True only the signature is printed
Returns:
C-like code for the ast node and its descendants
"""
return
generate_c
(
astnode
,
signature_only
,
dialect
=
'opencl'
)
class
OpenCLBackend
(
CudaBackend
):
pass
\ No newline at end of file
pystencils_tests/test_opencl.py
0 → 100644
View file @
0b27d3c3
import
sympy
as
sp
import
pystencils
from
pystencils.backends.cuda_backend
import
CudaBackend
from
pystencils.backends.opencl_backend
import
OpenCLBackend
def
test_opencl_backend
():
z
,
y
,
x
=
pystencils
.
fields
(
"z, y, x: [2d]"
)
assignments
=
pystencils
.
AssignmentCollection
({
z
[
0
,
0
]:
x
[
0
,
0
]
*
sp
.
log
(
x
[
0
,
0
]
*
y
[
0
,
0
])
})
print
(
assignments
)
ast
=
pystencils
.
create_kernel
(
assignments
,
target
=
'gpu'
)
print
(
ast
)
code
=
pystencils
.
show_code
(
ast
,
custom_backend
=
CudaBackend
())
print
(
code
)
opencl_code
=
pystencils
.
show_code
(
ast
,
custom_backend
=
OpenCLBackend
())
print
(
opencl_code
)
if
__name__
==
'__main__'
:
test_opencl_backend
()
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment