Newer
Older
from __future__ import division
from __future__ import absolute_import
import six
from six.moves import range
__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 numpy as np
import loopy as lp
import pyopencl as cl
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'
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)
""",
[
lp.GlobalArg("a", np.float32, shape=("n",)),
lp.ValueArg("n", np.int32),
])
from loopy.subst import expand_subst
knl = expand_subst(knl, "g$two < h$two")
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),
],
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_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_assume(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i]: 0<=i<n}",
"a[i] = a[i] + 1",
[lp.GlobalArg("a", np.float32, shape="n"), "..."])
knl = lp.split_iname(knl, "i", 16)
knl = lp.set_loop_priority(knl, "i_outer,i_inner")
knl = lp.assume(knl, "n mod 16 = 0")
knl = lp.assume(knl, "n > 10")
knl = lp.preprocess_kernel(knl, ctx.devices[0])
kernel_gen = lp.generate_loop_schedules(knl)
for gen_knl in kernel_gen:
assert "if" not in compiled.get_code()
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_nonsense_reduction(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i]: 0<=i<100}",
"""
a[i] = sum(i, 2)
""",
[lp.GlobalArg("a", np.float32, shape=(100,))]
)
import pytest
with pytest.raises(RuntimeError):
knl = lp.preprocess_kernel(knl, ctx.devices[0])
def test_owed_barriers(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(
"{[i]: 0<=i<100}",
[
Andreas Klöckner
committed
"<float32> z[i] = a[i]"
],
[lp.GlobalArg("a", np.float32, shape=(100,))]
Loading
Loading full blame...