|
| 1 | +""" |
| 2 | +Test problems in nested calls. |
| 3 | +Usually due to invalid type conversion between function boundaries. |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | +from numba import cuda |
| 8 | +from numba.core import types |
| 9 | +from numba.cuda.testing import CUDATestCase |
| 10 | +from numba.extending import overload |
| 11 | +import unittest |
| 12 | +import numpy as np |
| 13 | + |
| 14 | + |
| 15 | +def generated_inner(out, x, y=5, z=6): |
| 16 | + # Provide implementation for the simulation. |
| 17 | + if isinstance(x, complex): |
| 18 | + out[0], out[1] = x + y, z |
| 19 | + else: |
| 20 | + out[0], out[1] = x - y, z |
| 21 | + |
| 22 | + |
| 23 | +@overload(generated_inner) |
| 24 | +def ol_generated_inner(out, x, y=5, z=6): |
| 25 | + if isinstance(x, types.Complex): |
| 26 | + def impl(out, x, y=5, z=6): |
| 27 | + out[0], out[1] = x + y, z |
| 28 | + else: |
| 29 | + def impl(out, x, y=5, z=6): |
| 30 | + out[0], out[1] = x - y, z |
| 31 | + return impl |
| 32 | + |
| 33 | + |
| 34 | +def call_generated(a, b, out): |
| 35 | + generated_inner(out, a, z=b) |
| 36 | + |
| 37 | + |
| 38 | +class TestNestedCall(CUDATestCase): |
| 39 | + def test_call_generated(self): |
| 40 | + """ |
| 41 | + Test a nested function call to a generated jit function. |
| 42 | + """ |
| 43 | + cfunc = cuda.jit(call_generated) |
| 44 | + |
| 45 | + out = np.empty(2, dtype=np.int64) |
| 46 | + cfunc[1,1](1, 2, out) |
| 47 | + self.assertPreciseEqual(tuple(out), (-4, 2)) |
| 48 | + |
| 49 | + out = np.empty(2, dtype=np.complex64) |
| 50 | + cfunc[1,1](1j, 2, out) |
| 51 | + self.assertPreciseEqual(tuple(map(complex,out)), (5 + 1j, 2 + 0j)) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + unittest.main() |
0 commit comments