|
| 1 | +import dpnp as cupy |
| 2 | + |
| 3 | + |
| 4 | +def test_1d_contiguous(): |
| 5 | + a = cupy.zeros(12, dtype=cupy.int64) |
| 6 | + itemsize = a.itemsize |
| 7 | + a_low = a.get_array()._pointer |
| 8 | + a_high = a.get_array()._pointer + 12 * itemsize |
| 9 | + assert cupy.byte_bounds(a) == (a_low, a_high) |
| 10 | + |
| 11 | + |
| 12 | +def test_2d_contiguous(): |
| 13 | + a = cupy.zeros((4, 7), dtype=cupy.int64) |
| 14 | + itemsize = a.itemsize |
| 15 | + a_low = a.get_array()._pointer |
| 16 | + a_high = a.get_array()._pointer + 4 * 7 * itemsize |
| 17 | + assert cupy.byte_bounds(a) == (a_low, a_high) |
| 18 | + |
| 19 | + |
| 20 | +def test_1d_noncontiguous_pos_stride(): |
| 21 | + a = cupy.zeros(12, dtype=cupy.int64) |
| 22 | + itemsize = a.itemsize |
| 23 | + b = a[::2] |
| 24 | + b_low = b.get_array()._pointer |
| 25 | + b_high = b.get_array()._pointer + 11 * itemsize # a[10] |
| 26 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
| 27 | + |
| 28 | + |
| 29 | +def test_2d_noncontiguous_pos_stride(): |
| 30 | + a = cupy.zeros((4, 7), dtype=cupy.int64) |
| 31 | + b = a[::2, ::2] |
| 32 | + itemsize = b.itemsize |
| 33 | + b_low = a.get_array()._pointer |
| 34 | + b_high = b.get_array()._pointer + 3 * 7 * itemsize # a[2][6] |
| 35 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
| 36 | + |
| 37 | + |
| 38 | +def test_1d_contiguous_neg_stride(): |
| 39 | + a = cupy.zeros(12, dtype=cupy.int64) |
| 40 | + b = a[::-1] |
| 41 | + itemsize = b.itemsize |
| 42 | + b_low = b.get_array()._pointer - 11 * itemsize |
| 43 | + b_high = b.get_array()._pointer + 1 * itemsize |
| 44 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
| 45 | + |
| 46 | + |
| 47 | +def test_2d_noncontiguous_neg_stride(): |
| 48 | + a = cupy.zeros((4, 7), dtype=cupy.int64) |
| 49 | + b = a[::-2, ::-2] # strides = (-56, -8), shape = (2, 4) |
| 50 | + itemsize = b.itemsize |
| 51 | + b_low = ( |
| 52 | + b.get_array()._pointer |
| 53 | + - 2 * 7 * itemsize * (2 - 1) |
| 54 | + - 2 * itemsize * (4 - 1) |
| 55 | + ) |
| 56 | + b_high = b.get_array()._pointer + 1 * itemsize |
| 57 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
| 58 | + |
| 59 | + |
| 60 | +def test_2d_noncontiguous_posneg_stride_1(): |
| 61 | + a = cupy.zeros((4, 7), dtype=cupy.int64) |
| 62 | + b = a[::1, ::-1] # strides = (28, -4), shape=(4, 7) |
| 63 | + itemsize = b.itemsize |
| 64 | + b_low = b.get_array()._pointer - itemsize * (7 - 1) |
| 65 | + b_high = b.get_array()._pointer + 1 * itemsize + 7 * itemsize * (4 - 1) |
| 66 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
| 67 | + |
| 68 | + |
| 69 | +def test_2d_noncontiguous_posneg_stride_2(): |
| 70 | + a = cupy.zeros((4, 7), dtype=cupy.int64) |
| 71 | + b = a[::2, ::-2] # strides = (56, -8), shape=(2, 4) |
| 72 | + itemsize = b.itemsize |
| 73 | + b_low = b.get_array()._pointer - 2 * itemsize * (4 - 1) |
| 74 | + b_high = b.get_array()._pointer + 1 * itemsize + 2 * 7 * itemsize * (2 - 1) |
| 75 | + assert cupy.byte_bounds(b) == (b_low, b_high) |
0 commit comments