Skip to content

Commit 6945d16

Browse files
authored
Merge branch 'main' into better_loop_around_nest_map
2 parents 72334e8 + 73098bb commit 6945d16

4 files changed

Lines changed: 63 additions & 3 deletions

File tree

loopy/target/pyopencl.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def with_types(self, arg_id_to_dtype, callables_table):
5151
for id in arg_id_to_dtype:
5252
# since all the below functions are single arg.
5353
if not -1 <= id <= 0:
54-
raise LoopyError("%s can only take one argument." % name)
54+
raise LoopyError(f"{name} can only take one argument")
5555

5656
if 0 not in arg_id_to_dtype or arg_id_to_dtype[0] is None:
5757
# the types provided aren't mature enough to specialize the
@@ -69,14 +69,23 @@ def with_types(self, arg_id_to_dtype, callables_table):
6969
elif dtype.numpy_dtype == np.complex128:
7070
tpname = "cdouble"
7171
else:
72-
raise LoopyTypeError("unexpected complex type '%s'" % dtype)
72+
raise LoopyTypeError(f"unexpected complex type '{dtype}'")
7373

7474
return (
7575
self.copy(name_in_target=f"{tpname}_{name}",
7676
arg_id_to_dtype={0: dtype, -1: NumpyType(
7777
np.dtype(dtype.numpy_dtype.type(0).real))}),
7878
callables_table)
7979

80+
if name in ["real", "imag"]:
81+
if not dtype.is_complex():
82+
tpname = dtype.numpy_dtype.type.__name__
83+
return (
84+
self.copy(
85+
name_in_target=f"lpy_{name}_{tpname}",
86+
arg_id_to_dtype={0: dtype, -1: dtype}),
87+
callables_table)
88+
8089
if name in ["sqrt", "exp", "log",
8190
"sin", "cos", "tan",
8291
"sinh", "cosh", "tanh",
@@ -110,6 +119,23 @@ def with_types(self, arg_id_to_dtype, callables_table):
110119
self.copy(arg_id_to_dtype=arg_id_to_dtype),
111120
callables_table)
112121

122+
def generate_preambles(self, target):
123+
name = self.name_in_target
124+
if name.startswith("lpy_real") or name.startswith("lpy_imag"):
125+
if name.startswith("lpy_real"):
126+
ret = "x"
127+
else:
128+
ret = "0"
129+
130+
dtype = self.arg_id_to_dtype[-1]
131+
ctype = target.dtype_to_typename(dtype)
132+
133+
yield(f"40_{name}", f"""
134+
static inline {ctype} {name}({ctype} x) {{
135+
return {ret};
136+
}}
137+
""")
138+
113139

114140
def get_pyopencl_callables():
115141
pyopencl_ids = ["sqrt", "exp", "log", "sin", "cos", "tan", "sinh", "cosh",

loopy/target/pyopencl_execution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def handle_alloc(self, gen, arg, kernel_arg, strify, skip_arg_checks):
101101

102102
if not skip_arg_checks:
103103
for i in range(num_axes):
104-
gen("assert _lpy_ustrides_%d > 0, "
104+
gen("assert _lpy_ustrides_%d >= 0, "
105105
"\"'%s' has negative stride in axis %d\""
106106
% (i, arg.name, i))
107107

test/test_expression.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,25 @@ def test_complex_support(ctx_factory, target):
548548
(0.5*n*(n-1) - 0.5*n*(n-1)*1j) ** 2)
549549

550550

551+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
552+
def test_real_with_real_argument(ctx_factory, dtype):
553+
ctx = ctx_factory()
554+
queue = cl.CommandQueue(ctx)
555+
556+
knl = lp.make_kernel(
557+
"{[i]: 0 <= i < nresult}",
558+
"result[i] = real(ary[i])",
559+
)
560+
561+
rng = np.random.default_rng()
562+
ary = cl.array.to_device(queue, rng.random(128).astype(dtype))
563+
564+
_, (result,) = knl(queue, ary=ary)
565+
566+
assert result.dtype == ary.dtype
567+
np.testing.assert_allclose(result.get(), np.real(ary.get()))
568+
569+
551570
def test_bool_type_context(ctx_factory):
552571
# Checks if a boolean type context is correctly handled in codegen phase.
553572
# See https://github.com/inducer/loopy/pull/258

test/test_loopy.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,6 +3216,21 @@ def test_get_return_from_kernel_mapping():
32163216
assert ret_from_knl_idx[9] == 10
32173217

32183218

3219+
def test_zero_stride_array(ctx_factory):
3220+
ctx = ctx_factory()
3221+
cq = cl.CommandQueue(ctx)
3222+
3223+
knl = lp.make_kernel(
3224+
["{[i]: 0<=i<10}",
3225+
"{[j]: 1=0}"],
3226+
"""
3227+
y[i, j] = 1
3228+
""", [lp.GlobalArg("y", shape=(10, 0))])
3229+
3230+
evt, (out,) = knl(cq)
3231+
assert out.shape == (10, 0)
3232+
3233+
32193234
if __name__ == "__main__":
32203235
if len(sys.argv) > 1:
32213236
exec(sys.argv[1])

0 commit comments

Comments
 (0)