Newer
Older
from __future__ import division, absolute_import, print_function
__copyright__ = "Copyright (C) 2012 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import six
from six.moves import range
import numpy as np
import loopy as lp
import pyopencl as cl
Andreas Klöckner
committed
import pyopencl.clmath # noqa
logger = logging.getLogger(__name__)
try:
import faulthandler
except ImportError:
pass
else:
faulthandler.enable()
from pyopencl.tools import pytest_generate_tests_for_pyopencl \
as pytest_generate_tests
__all__ = [
"pytest_generate_tests",
"cl" # 'cl.create_some_context'
from loopy.version import LOOPY_USE_LANGUAGE_VERSION_2018_1 # noqa
Andreas Klöckner
committed
def test_globals_decl_once_with_multi_subprogram(ctx_factory):
ctx = ctx_factory()
queue = cl.CommandQueue(ctx)
a = np.random.randn(16)
cnst = np.random.randn(16)
knl = lp.make_kernel(
"{[i, ii]: 0<=i, ii<n}",
"""
out[i] = a[i]+cnst[i]{id=first}
out[ii] = 2*out[ii]+cnst[ii]{id=second}
""",
[lp.TemporaryVariable(
'cnst', shape=('n'), initializer=cnst,
scope=lp.temp_var_scope.GLOBAL,
Andreas Klöckner
committed
read_only=True), '...'])
knl = lp.fix_parameters(knl, n=16)
knl = lp.add_barrier(knl, "id:first", "id:second")
knl = lp.split_iname(knl, "i", 2, outer_tag="g.0", inner_tag="l.0")
knl = lp.split_iname(knl, "ii", 2, outer_tag="g.0", inner_tag="l.0")
evt, (out,) = knl(queue, a=a)
assert np.linalg.norm(out-((2*(a+cnst)+cnst))) <= 1e-15
Andreas Klöckner
committed
def test_complicated_subst(ctx_factory):
Andreas Klöckner
committed
knl = lp.make_kernel(
Andreas Klöckner
committed
"{[i]: 0<=i<n}",
"""
f(x) := x*a[x]
g(x) := 12 + f(x)
h(x) := 1 + g(x) + 20*g$two(x)
a[i] = h$one(i) * h$two(i)
Andreas Klöckner
committed
""")
Andreas Klöckner
committed
knl = lp.expand_subst(knl, "... > id:h and tag:two > id:g and tag:two")
Andreas Klöckner
committed
Andreas Klöckner
committed
Andreas Klöckner
committed
for letter, how_many in [
("f", 1),
("g", 1),
("h", 2)
]:
substs_with_letter = sum(1 for k in sr_keys if k.startswith(letter))
assert substs_with_letter == how_many
def test_type_inference_no_artificial_doubles(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i]: 0<=i<n}",
"""
<> bb = a[i] - b[i]
c[i] = bb
""",
[
lp.GlobalArg("a", np.float32, shape=("n",)),
lp.GlobalArg("b", np.float32, shape=("n",)),
lp.GlobalArg("c", np.float32, shape=("n",)),
lp.ValueArg("n", np.int32),
],
Andreas Klöckner
committed
assumptions="n>=1")
knl = lp.preprocess_kernel(knl, ctx.devices[0])
for k in lp.generate_loop_schedules(knl):
code = lp.generate_code(k)
assert "double" not in code
def test_type_inference_with_type_dependencies():
knl = lp.make_kernel(
"{[i]: i=0}",
"""
<>a = 99
a = a + 1
<>b = 0
<>c = 1
b = b + c + 1.0
c = b + c
<>d = b + 2 + 1j
""",
Andreas Klöckner
committed
"...")
knl = lp.infer_unknown_types(knl)
from loopy.types import to_loopy_type
assert knl.temporary_variables["a"].dtype == to_loopy_type(np.int32)
assert knl.temporary_variables["b"].dtype == to_loopy_type(np.float32)
assert knl.temporary_variables["c"].dtype == to_loopy_type(np.float32)
assert knl.temporary_variables["d"].dtype == to_loopy_type(np.complex128)
def test_sized_and_complex_literals(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i]: 0<=i<n}",
"""
<> aa = 5jf
<> bb = 5j
a[i] = imag(aa)
b[i] = imag(bb)
c[i] = 5f
""",
[
lp.GlobalArg("a", np.float32, shape=("n",)),
lp.GlobalArg("b", np.float32, shape=("n",)),
lp.GlobalArg("c", np.float32, shape=("n",)),
lp.ValueArg("n", np.int32),
],
assumptions="n>=1")
lp.auto_test_vs_ref(knl, ctx, knl, parameters=dict(n=5))
def test_simple_side_effect(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i,j]: 0<=i,j<100}",
"""
a[i] = a[i] + 1
""",
[lp.GlobalArg("a", np.float32, shape=(100,))]
)
knl = lp.preprocess_kernel(knl, ctx.devices[0])
kernel_gen = lp.generate_loop_schedules(knl)
for gen_knl in kernel_gen:
compiled = lp.CompiledKernel(ctx, gen_knl)
def test_owed_barriers(ctx_factory):
ctx = ctx_factory()
Loading
Loading full blame...