Skip to content
Snippets Groups Projects
Commit 0b27d3c3 authored by Stephan Seitz's avatar Stephan Seitz
Browse files

Add OpenCLBackend

parent 69f779ea
No related merge requests found
...@@ -58,6 +58,9 @@ def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom ...@@ -58,6 +58,9 @@ def generate_c(ast_node: Node, signature_only: bool = False, dialect='c', custom
elif dialect == 'cuda': elif dialect == 'cuda':
from pystencils.backends.cuda_backend import CudaBackend from pystencils.backends.cuda_backend import CudaBackend
printer = CudaBackend(signature_only=signature_only) printer = CudaBackend(signature_only=signature_only)
elif dialect == 'opencl':
from pystencils.backends.opencl_backend import OpenCLBackend
printer = OpenCLBackend(signature_only=signature_only)
else: else:
raise ValueError("Unknown dialect: " + str(dialect)) raise ValueError("Unknown dialect: " + str(dialect))
code = printer(ast_node) code = printer(ast_node)
...@@ -276,10 +279,10 @@ class CBackend: ...@@ -276,10 +279,10 @@ class CBackend:
] ]
destructuring_bindings.sort() # only for code aesthetics destructuring_bindings.sort() # only for code aesthetics
return "{\n" + self._indent + \ return "{\n" + self._indent + \
("\n" + self._indent).join(destructuring_bindings) + \ ("\n" + self._indent).join(destructuring_bindings) + \
"\n" + self._indent + \ "\n" + self._indent + \
("\n" + self._indent).join(self._print(node.body).splitlines()) + \ ("\n" + self._indent).join(self._print(node.body).splitlines()) + \
"\n}" "\n}"
# ------------------------------------------ Helper function & classes ------------------------------------------------- # ------------------------------------------ Helper function & classes -------------------------------------------------
......
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
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()
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment