(c, dtype, size)
| 83 | @pytest.mark.parametrize("dtype", dtypes, ids=dtypes) |
| 84 | @pytest.mark.parametrize("size", [0, 1, 8, 1000]) |
| 85 | def test_from_object(c, dtype, size): |
| 86 | ctx, nb_ctx = context_choices[c] |
| 87 | arr, cbuf = make_random_buffer(size, target='device', dtype=dtype, ctx=ctx) |
| 88 | |
| 89 | # Creating device buffer from numba DeviceNDArray: |
| 90 | darr = nb_cuda.to_device(arr) |
| 91 | cbuf2 = ctx.buffer_from_object(darr) |
| 92 | assert cbuf2.size == cbuf.size |
| 93 | arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype) |
| 94 | np.testing.assert_equal(arr, arr2) |
| 95 | |
| 96 | # Creating device buffer from a slice of numba DeviceNDArray: |
| 97 | if size >= 8: |
| 98 | # 1-D arrays |
| 99 | for s in [slice(size//4, None, None), |
| 100 | slice(size//4, -(size//4), None)]: |
| 101 | cbuf2 = ctx.buffer_from_object(darr[s]) |
| 102 | arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype) |
| 103 | np.testing.assert_equal(arr[s], arr2) |
| 104 | |
| 105 | # cannot test negative strides due to numba bug, see its issue 3705 |
| 106 | if 0: |
| 107 | rdarr = darr[::-1] |
| 108 | cbuf2 = ctx.buffer_from_object(rdarr) |
| 109 | assert cbuf2.size == cbuf.size |
| 110 | arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype) |
| 111 | np.testing.assert_equal(arr, arr2) |
| 112 | |
| 113 | with pytest.raises(ValueError, |
| 114 | match=('array data is non-contiguous')): |
| 115 | ctx.buffer_from_object(darr[::2]) |
| 116 | |
| 117 | # a rectangular 2-D array |
| 118 | s1 = size//4 |
| 119 | s2 = size//s1 |
| 120 | assert s1 * s2 == size |
| 121 | cbuf2 = ctx.buffer_from_object(darr.reshape(s1, s2)) |
| 122 | assert cbuf2.size == cbuf.size |
| 123 | arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype) |
| 124 | np.testing.assert_equal(arr, arr2) |
| 125 | |
| 126 | with pytest.raises(ValueError, |
| 127 | match=('array data is non-contiguous')): |
| 128 | ctx.buffer_from_object(darr.reshape(s1, s2)[:, ::2]) |
| 129 | |
| 130 | # a 3-D array |
| 131 | s1 = 4 |
| 132 | s2 = size//8 |
| 133 | s3 = size//(s1*s2) |
| 134 | assert s1 * s2 * s3 == size |
| 135 | cbuf2 = ctx.buffer_from_object(darr.reshape(s1, s2, s3)) |
| 136 | assert cbuf2.size == cbuf.size |
| 137 | arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype) |
| 138 | np.testing.assert_equal(arr, arr2) |
| 139 | |
| 140 | with pytest.raises(ValueError, |
| 141 | match=('array data is non-contiguous')): |
| 142 | ctx.buffer_from_object(darr.reshape(s1, s2, s3)[::2]) |
nothing calls this directly
no test coverage detected