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

Add pystencils.opencl.autoinit

parent 043e65c8
No related branches found
No related tags found
No related merge requests found
import os import os
import pytest
import tempfile
import runpy import runpy
import sys import sys
import warnings import warnings
import tempfile
import nbformat
import pytest
from nbconvert import PythonExporter
from pystencils.boundaries.createindexlistcython import * # NOQA
# Trigger config file reading / creation once - to avoid race conditions when multiple instances are creating it # Trigger config file reading / creation once - to avoid race conditions when multiple instances are creating it
# at the same time # at the same time
from pystencils.cpu import cpujit from pystencils.cpu import cpujit
...@@ -15,7 +20,6 @@ try: ...@@ -15,7 +20,6 @@ try:
pyximport.install(language_level=3) pyximport.install(language_level=3)
except ImportError: except ImportError:
pass pass
from pystencils.boundaries.createindexlistcython import * # NOQA
SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__)) SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__))
...@@ -29,7 +33,8 @@ def add_path_to_ignore(path): ...@@ -29,7 +33,8 @@ def add_path_to_ignore(path):
collect_ignore += [os.path.join(SCRIPT_FOLDER, path, f) for f in os.listdir(os.path.join(SCRIPT_FOLDER, path))] collect_ignore += [os.path.join(SCRIPT_FOLDER, path, f) for f in os.listdir(os.path.join(SCRIPT_FOLDER, path))]
collect_ignore = [os.path.join(SCRIPT_FOLDER, "doc", "conf.py")] collect_ignore = [os.path.join(SCRIPT_FOLDER, "doc", "conf.py"),
os.path.join(SCRIPT_FOLDER, "pystencils", "opencl", "opencl.autoinit")]
add_path_to_ignore('pystencils_tests/benchmark') add_path_to_ignore('pystencils_tests/benchmark')
add_path_to_ignore('_local_tmp') add_path_to_ignore('_local_tmp')
...@@ -78,8 +83,6 @@ for root, sub_dirs, files in os.walk('.'): ...@@ -78,8 +83,6 @@ for root, sub_dirs, files in os.walk('.'):
collect_ignore.append(f) collect_ignore.append(f)
import nbformat
from nbconvert import PythonExporter
class IPythonMockup: class IPythonMockup:
......
...@@ -14,7 +14,8 @@ class PyOpenClArrayHandler: ...@@ -14,7 +14,8 @@ class PyOpenClArrayHandler:
if not queue: if not queue:
from pystencils.opencl.opencljit import get_global_cl_queue from pystencils.opencl.opencljit import get_global_cl_queue
queue = get_global_cl_queue() queue = get_global_cl_queue()
assert queue, "OpenCL queue missing" assert queue, "OpenCL queue missing!\n" \
"Use `import pystencils.opencl.autoinit` if you want it to be automatically created"
self.queue = queue self.queue = queue
def zeros(self, shape, dtype=np.float64, order='C'): def zeros(self, shape, dtype=np.float64, order='C'):
......
"""
"""
from pystencils.opencl.opencljit import (
clear_global_ctx, init_globally, init_globally_with_context, make_python_function)
__all__ = ['init_globally', 'init_globally_with_context', 'clear_global_ctx', 'make_python_function']
"""
Automatically initializes OpenCL context using any device.
Use `pystencils.opencl.{init_globally_with_context,init_globally}` if you want to use a specific device.
"""
from pystencils.opencl import * # noqa
from pystencils.opencl.opencljit import * # noqa
from pystencils.opencl.opencljit import init_globally
init_globally()
...@@ -65,8 +65,10 @@ def make_python_function(kernel_function_node, opencl_queue, opencl_ctx, argumen ...@@ -65,8 +65,10 @@ def make_python_function(kernel_function_node, opencl_queue, opencl_ctx, argumen
if not opencl_queue: if not opencl_queue:
opencl_queue = _global_cl_queue opencl_queue = _global_cl_queue
assert opencl_ctx, "No valid OpenCL context" assert opencl_ctx, "No valid OpenCL context!\n" \
assert opencl_queue, "No valid OpenCL queue" "Use `import pystencils.opencl.autoinit` if you want it to be automatically created"
assert opencl_queue, "No valid OpenCL queue!\n" \
"Use `import pystencils.opencl.autoinit` if you want it to be automatically created"
if argument_dict is None: if argument_dict is None:
argument_dict = {} argument_dict = {}
......
import numpy as np import numpy as np
import pytest import pytest
import sympy as sp
import pystencils import pystencils
import sympy as sp
from pystencils.backends.cuda_backend import CudaBackend from pystencils.backends.cuda_backend import CudaBackend
from pystencils.backends.opencl_backend import OpenClBackend from pystencils.backends.opencl_backend import OpenClBackend
from pystencils.opencl.opencljit import make_python_function, init_globally, get_global_cl_queue from pystencils.opencl.opencljit import get_global_cl_queue, init_globally, make_python_function
try: try:
import pyopencl as cl import pyopencl as cl
...@@ -235,9 +235,9 @@ def test_without_cuda(): ...@@ -235,9 +235,9 @@ def test_without_cuda():
opencl_kernel(x=x, y=y, z=z) opencl_kernel(x=x, y=y, z=z)
@pytest.mark.skipif(not HAS_OPENCL, reason="Test requires pyopencl") @pytest.mark.skipif(not HAS_OPENCL, reason="Test requires pyopencl")
def test_kernel_creation(): def test_kernel_creation():
global pystencils
z, y, x = pystencils.fields("z, y, x: [20,30]") z, y, x = pystencils.fields("z, y, x: [20,30]")
assignments = pystencils.AssignmentCollection({ assignments = pystencils.AssignmentCollection({
...@@ -246,8 +246,9 @@ def test_kernel_creation(): ...@@ -246,8 +246,9 @@ def test_kernel_creation():
print(assignments) print(assignments)
pystencils.opencl.clear_global_ctx()
init_globally() import pystencils.opencl.autoinit
ast = pystencils.create_kernel(assignments, target='opencl') ast = pystencils.create_kernel(assignments, target='opencl')
print(ast.backend) print(ast.backend)
...@@ -270,4 +271,3 @@ def test_kernel_creation(): ...@@ -270,4 +271,3 @@ def test_kernel_creation():
assert opencl_kernel is not None assert opencl_kernel is not None
opencl_kernel(x=x, y=y, z=z) opencl_kernel(x=x, y=y, z=z)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment