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

Add pystencils.opencl.autoinit

parent 3f142a0e
Branches
No related merge requests found
import os
import pytest
import tempfile
import runpy
import sys
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
# at the same time
from pystencils.cpu import cpujit
......@@ -14,7 +19,6 @@ try:
pyximport.install(language_level=3)
except ImportError:
pass
from pystencils.boundaries.createindexlistcython import * # NOQA
SCRIPT_FOLDER = os.path.dirname(os.path.realpath(__file__))
......@@ -28,7 +32,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, "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('_local_tmp')
......@@ -77,8 +82,6 @@ for root, sub_dirs, files in os.walk('.'):
collect_ignore.append(f)
import nbformat
from nbconvert import PythonExporter
class IPythonMockup:
......
......@@ -14,7 +14,8 @@ class PyOpenClArrayHandler:
if not queue:
from pystencils.opencl.opencljit import 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
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
if not opencl_queue:
opencl_queue = _global_cl_queue
assert opencl_ctx, "No valid OpenCL context"
assert opencl_queue, "No valid OpenCL queue"
assert opencl_ctx, "No valid OpenCL context!\n" \
"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:
argument_dict = {}
......
import numpy as np
import pytest
import sympy as sp
import pystencils
import sympy as sp
from pystencils.backends.cuda_backend import CudaBackend
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:
import pyopencl as cl
......@@ -235,9 +235,9 @@ def test_without_cuda():
opencl_kernel(x=x, y=y, z=z)
@pytest.mark.skipif(not HAS_OPENCL, reason="Test requires pyopencl")
def test_kernel_creation():
global pystencils
z, y, x = pystencils.fields("z, y, x: [20,30]")
assignments = pystencils.AssignmentCollection({
......@@ -246,8 +246,9 @@ def test_kernel_creation():
print(assignments)
pystencils.opencl.clear_global_ctx()
init_globally()
import pystencils.opencl.autoinit
ast = pystencils.create_kernel(assignments, target='opencl')
print(ast.backend)
......@@ -270,4 +271,3 @@ def test_kernel_creation():
assert opencl_kernel is not None
opencl_kernel(x=x, y=y, z=z)
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