Newer
Older
from __future__ import division
__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__)
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):
ctx = ctx_factory()
knl = lp.make_kernel(ctx.devices[0],
"{[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")
print knl
sr_keys = knl.substitutions.keys()
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(ctx.devices[0],
"{[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")
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(ctx.devices[0],
"{[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))
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def test_simple_side_effect(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(ctx.devices[0],
"{[i,j]: 0<=i,j<100}",
"""
a[i] = a[i] + 1
""",
[lp.GlobalArg("a", np.float32, shape=(100,))]
)
kernel_gen = lp.generate_loop_schedules(knl)
for gen_knl in kernel_gen:
print gen_knl
compiled = lp.CompiledKernel(ctx, gen_knl)
print compiled.code
def test_nonsense_reduction(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(ctx.devices[0],
"{[i]: 0<=i<100}",
"""
a[i] = sum(i, 2)
""",
[lp.GlobalArg("a", np.float32, shape=(100,))]
)
import pytest
with pytest.raises(RuntimeError):
list(lp.generate_loop_schedules(knl))
def test_owed_barriers(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(ctx.devices[0],
"{[i]: 0<=i<100}",
[
Andreas Klöckner
committed
"<float32> z[i] = a[i]"
],
[lp.GlobalArg("a", np.float32, shape=(100,))]
Andreas Klöckner
committed
knl = lp.tag_inames(knl, dict(i="l.0"))
kernel_gen = lp.generate_loop_schedules(knl)
for gen_knl in kernel_gen:
compiled = lp.CompiledKernel(ctx, gen_knl)
print compiled.code
Andreas Klöckner
committed
def test_wg_too_small(ctx_factory):
ctx = ctx_factory()
knl = lp.make_kernel(ctx.devices[0],
"{[i]: 0<=i<100}",
[
Andreas Klöckner
committed
"<float32> z[i] = a[i] {id=copy}"
Andreas Klöckner
committed
],
[lp.GlobalArg("a", np.float32, shape=(100,))],
Andreas Klöckner
committed
local_sizes={0: 16})
Andreas Klöckner
committed
knl = lp.tag_inames(knl, dict(i="l.0"))
Andreas Klöckner
committed
kernel_gen = lp.generate_loop_schedules(knl)
Andreas Klöckner
committed
import pytest
Andreas Klöckner
committed
for gen_knl in kernel_gen:
Andreas Klöckner
committed
with pytest.raises(RuntimeError):
lp.CompiledKernel(ctx, gen_knl).get_code()
Andreas Klöckner
committed
def test_join_inames(ctx_factory):
ctx = ctx_factory()
Loading
Loading full blame...