Skip to content

Commit b35e7dd

Browse files
GH-43507: [C++] Use ViewOrCopyTo instead of CopyTo when pretty printing non-CPU data (#43508)
### Rationale for this change When ensuring the data we are pretty-printing is on the CPU, we can use `ViewOrCopyTo` instead of `CopyTo`, in case the data can be viewed as CPU data without a copy. ### Are these changes tested? Yes (I added a test that uses CUDA host memory, which should be a case where it can be viewed and doesn't need to be copied, but of course the test it not actually ensuring we avoid the copy, just that the printing works) ### Are there any user-facing changes? No * GitHub Issue: #43507 Authored-by: Joris Van den Bossche <[email protected]> Signed-off-by: Sutou Kouhei <[email protected]>
1 parent 0b9f06c commit b35e7dd

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎cpp/src/arrow/pretty_print.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,8 @@ class ArrayPrinter : public PrettyPrinter {
394394
if (array.device_type() != DeviceAllocationType::kCPU) {
395395
// GH-43055: ideally we only copy start/end slices from non-CPU memory
396396
// based on the window size that is being printed
397-
ARROW_ASSIGN_OR_RAISE(auto array_cpu, array.CopyTo(default_cpu_memory_manager()));
397+
ARROW_ASSIGN_OR_RAISE(auto array_cpu,
398+
array.ViewOrCopyTo(default_cpu_memory_manager()));
398399
RETURN_NOT_OK(VisitArrayInline(*array_cpu, this));
399400
} else {
400401
RETURN_NOT_OK(VisitArrayInline(array, this));

‎python/pyarrow/tests/test_cuda.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,17 @@ def test_print_array():
973973
assert str(carr) == str(arr)
974974

975975

976+
@pytest.mark.parametrize("size", [10, 100])
977+
def test_print_array_host(size):
978+
buf = cuda.new_host_buffer(size*8)
979+
np_arr = np.frombuffer(buf, dtype=np.int64)
980+
np_arr[:] = range(size)
981+
982+
arr = pa.array(range(size), pa.int64())
983+
carr = pa.Array.from_buffers(pa.int64(), size, [None, buf])
984+
assert str(carr) == str(arr)
985+
986+
976987
def make_chunked_array(n_elements_per_chunk, n_chunks):
977988
arrs = []
978989
carrs = []

0 commit comments

Comments
 (0)