diff --git a/pystencils/backends/cbackend.py b/pystencils/backends/cbackend.py index f8b7e6d8a2d2216df4dc627843b1a16140dbb31d..64b0b38e1dcc84ecdb4c05b994f430e8f926e0ec 100644 --- a/pystencils/backends/cbackend.py +++ b/pystencils/backends/cbackend.py @@ -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 ------------------------------------------------- diff --git a/pystencils/backends/opencl_backend.py b/pystencils/backends/opencl_backend.py new file mode 100644 index 0000000000000000000000000000000000000000..92431fe79c1ba6fd35b36d181b5f5efe5e66ef38 --- /dev/null +++ b/pystencils/backends/opencl_backend.py @@ -0,0 +1,19 @@ +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 diff --git a/pystencils_tests/test_opencl.py b/pystencils_tests/test_opencl.py new file mode 100644 index 0000000000000000000000000000000000000000..71a97ce779c194e9ef85fb58754ec2d068f31d53 --- /dev/null +++ b/pystencils_tests/test_opencl.py @@ -0,0 +1,29 @@ +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()