| 682 | |
| 683 | @pytest.mark.numpy |
| 684 | def test_non_cpu_buffer(pickle_module): |
| 685 | cuda = pytest.importorskip("pyarrow.cuda", exc_type=ImportError) |
| 686 | ctx = cuda.Context(0) |
| 687 | |
| 688 | data = np.array([b'testing']) |
| 689 | cuda_buf = ctx.buffer_from_data(data) |
| 690 | arr = pa.FixedSizeBinaryArray.from_buffers(pa.binary(7), 1, [None, cuda_buf]) |
| 691 | buf_on_gpu = arr.buffers()[1] |
| 692 | |
| 693 | assert buf_on_gpu.size == cuda_buf.size |
| 694 | assert buf_on_gpu.address == cuda_buf.address |
| 695 | assert buf_on_gpu.is_cpu == cuda_buf.is_cpu |
| 696 | assert buf_on_gpu.is_mutable |
| 697 | |
| 698 | repr1 = "<pyarrow.Buffer address=" |
| 699 | repr2 = "size=7 is_cpu=False is_mutable=True>" |
| 700 | assert repr1 in repr(buf_on_gpu) |
| 701 | assert repr2 in repr(buf_on_gpu) |
| 702 | |
| 703 | buf_on_gpu_sliced = buf_on_gpu.slice(2) |
| 704 | cuda_sliced = cuda.CudaBuffer.from_buffer(buf_on_gpu_sliced) |
| 705 | assert cuda_sliced.to_pybytes() == b'sting' |
| 706 | |
| 707 | buf_on_gpu_sliced = buf_on_gpu[2:4] |
| 708 | cuda_sliced = cuda.CudaBuffer.from_buffer(buf_on_gpu_sliced) |
| 709 | assert cuda_sliced.to_pybytes() == b'st' |
| 710 | |
| 711 | # Sliced buffers with same address |
| 712 | assert buf_on_gpu_sliced.equals(cuda_buf[2:4]) |
| 713 | |
| 714 | # Buffers on different devices |
| 715 | msg_device = "Device on which the data resides differs between buffers" |
| 716 | with pytest.raises(ValueError, match=msg_device): |
| 717 | buf_on_gpu.equals(pa.py_buffer(data)) |
| 718 | |
| 719 | msg = "Implemented only for data on CPU device" |
| 720 | # Buffers with different addresses |
| 721 | arr_short = np.array([b'sting']) |
| 722 | cuda_buf_short = ctx.buffer_from_data(arr_short) |
| 723 | with pytest.raises(NotImplementedError, match=msg): |
| 724 | buf_on_gpu_sliced.equals(cuda_buf_short) |
| 725 | arr_short = pa.FixedSizeBinaryArray.from_buffers( |
| 726 | pa.binary(5), 1, [None, cuda_buf_short] |
| 727 | ) |
| 728 | buf_on_gpu_short = arr_short.buffers()[1] |
| 729 | with pytest.raises(NotImplementedError, match=msg): |
| 730 | buf_on_gpu_sliced.equals(buf_on_gpu_short) |
| 731 | |
| 732 | with pytest.raises(NotImplementedError, match=msg): |
| 733 | buf_on_gpu.hex() |
| 734 | |
| 735 | with pytest.raises(NotImplementedError, match=msg): |
| 736 | cuda_buf.hex() |
| 737 | |
| 738 | with pytest.raises(NotImplementedError, match=msg): |
| 739 | buf_on_gpu[1] |
| 740 | |
| 741 | with pytest.raises(NotImplementedError, match=msg): |