Skip to content

Commit d2788a2

Browse files
kaushikcfdinducer
authored andcommitted
Test complex math functions with real-valued arguments
1 parent c61d5a4 commit d2788a2

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

test/test_expression.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,81 @@ def test_np_bool_handling(ctx_factory):
642642
assert out.get().item() is True
643643

644644

645+
@pytest.mark.parametrize("target", [lp.PyOpenCLTarget, lp.ExecutableCTarget])
646+
def test_complex_functions_with_real_args(ctx_factory, target):
647+
# Reported by David Ham. See <https://github.com/inducer/loopy/issues/851>
648+
t_unit = lp.make_kernel(
649+
"{[i]: 0<=i<10}",
650+
"""
651+
y1[i] = abs(c64[i])
652+
y2[i] = real(c64[i])
653+
y3[i] = imag(c64[i])
654+
y4[i] = conj(c64[i])
655+
656+
y5[i] = abs(c128[i])
657+
y6[i] = real(c128[i])
658+
y7[i] = imag(c128[i])
659+
y8[i] = conj(c128[i])
660+
661+
662+
y9[i] = abs(f32[i])
663+
y10[i] = real(f32[i])
664+
y11[i] = imag(f32[i])
665+
y12[i] = conj(f32[i])
666+
667+
y13[i] = abs(f64[i])
668+
y14[i] = real(f64[i])
669+
y15[i] = imag(f64[i])
670+
y16[i] = conj(f64[i])
671+
""",
672+
target=target())
673+
674+
t_unit = lp.add_dtypes(t_unit,
675+
{"y9,y10,y11,y12": np.complex64,
676+
"y13,y14,y15,y16": np.complex128,
677+
"c64": np.complex64,
678+
"c128": np.complex128,
679+
"f64": np.float64,
680+
"f32": np.float32})
681+
t_unit = lp.set_options(t_unit, return_dict=True)
682+
683+
from numpy.random import default_rng
684+
rng = default_rng(0)
685+
c64 = (rng.random(10, dtype=np.float32)
686+
+ np.csingle(1j)*rng.random(10, dtype=np.float32))
687+
c128 = (rng.random(10, dtype=np.float64)
688+
+ np.cdouble(1j)*rng.random(10, dtype=np.float64))
689+
f32 = rng.random(10, dtype=np.float32)
690+
f64 = rng.random(10, dtype=np.float64)
691+
692+
if target == lp.PyOpenCLTarget:
693+
cl_ctx = ctx_factory()
694+
with cl.CommandQueue(cl_ctx) as queue:
695+
evt, out = t_unit(queue, c64=c64, c128=c128, f32=f32, f64=f64)
696+
elif target == lp.ExecutableCTarget:
697+
t_unit = lp.set_options(t_unit, build_options=["-Werror"])
698+
evt, out = t_unit(c64=c64, c128=c128, f32=f32, f64=f64)
699+
else:
700+
raise NotImplementedError("unsupported target")
701+
702+
np.testing.assert_allclose(out["y1"], np.abs(c64), rtol=1e-6)
703+
np.testing.assert_allclose(out["y2"], np.real(c64), rtol=1e-6)
704+
np.testing.assert_allclose(out["y3"], np.imag(c64), rtol=1e-6)
705+
np.testing.assert_allclose(out["y4"], np.conj(c64), rtol=1e-6)
706+
np.testing.assert_allclose(out["y5"], np.abs(c128), rtol=1e-6)
707+
np.testing.assert_allclose(out["y6"], np.real(c128), rtol=1e-6)
708+
np.testing.assert_allclose(out["y7"], np.imag(c128), rtol=1e-6)
709+
np.testing.assert_allclose(out["y8"], np.conj(c128), rtol=1e-6)
710+
np.testing.assert_allclose(out["y9"], np.abs(f32), rtol=1e-6)
711+
np.testing.assert_allclose(out["y10"], np.real(f32), rtol=1e-6)
712+
np.testing.assert_allclose(out["y11"], np.imag(f32), rtol=1e-6)
713+
np.testing.assert_allclose(out["y12"], np.conj(f32), rtol=1e-6)
714+
np.testing.assert_allclose(out["y13"], np.abs(f64), rtol=1e-6)
715+
np.testing.assert_allclose(out["y14"], np.real(f64), rtol=1e-6)
716+
np.testing.assert_allclose(out["y15"], np.imag(f64), rtol=1e-6)
717+
np.testing.assert_allclose(out["y16"], np.conj(f64), rtol=1e-6)
718+
719+
645720
if __name__ == "__main__":
646721
if len(sys.argv) > 1:
647722
exec(sys.argv[1])

0 commit comments

Comments
 (0)