MCPcopy Create free account
hub / github.com/apache/arrow / test_context_from_object

Function test_context_from_object

python/pyarrow/tests/test_cuda.py:251–279  ·  view source on GitHub ↗
(size)

Source from the content-addressed store, hash-verified

249
250@pytest.mark.parametrize("size", [0, 1, 1000])
251def test_context_from_object(size):
252 ctx = global_context
253 arr, cbuf = make_random_buffer(size, target='device')
254 dtype = arr.dtype
255
256 # Creating device buffer from a CUDA host buffer
257 hbuf = cuda.new_host_buffer(size * arr.dtype.itemsize)
258 np.frombuffer(hbuf, dtype=dtype)[:] = arr
259 cbuf2 = ctx.buffer_from_object(hbuf)
260 assert cbuf2.size == cbuf.size
261 arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype)
262 np.testing.assert_equal(arr, arr2)
263
264 # Creating device buffer from a device buffer
265 cbuf2 = ctx.buffer_from_object(cbuf2)
266 assert cbuf2.size == cbuf.size
267 arr2 = np.frombuffer(cbuf2.copy_to_host(), dtype=dtype)
268 np.testing.assert_equal(arr, arr2)
269
270 # Trying to create a device buffer from a Buffer
271 with pytest.raises(pa.ArrowTypeError,
272 match=('buffer is not backed by a CudaBuffer')):
273 ctx.buffer_from_object(pa.py_buffer(b"123"))
274
275 # Trying to create a device buffer from numpy.array
276 with pytest.raises(pa.ArrowTypeError,
277 match=("cannot create device buffer view from "
278 ".* \'numpy.ndarray\'")):
279 ctx.buffer_from_object(np.array([1, 2, 3]))
280
281
282def test_foreign_buffer():

Callers

nothing calls this directly

Calls 2

make_random_bufferFunction · 0.70
arrayMethod · 0.45

Tested by

no test coverage detected