Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-41664: [C++][Python] PrettyPrint non-cpu data by copying to default CPU device #42010

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cpp/src/arrow/pretty_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,12 @@ class ArrayPrinter : public PrettyPrinter {
}

Status Print(const Array& array) {
RETURN_NOT_OK(VisitArrayInline(array, this));
if (array.device_type() != DeviceAllocationType::kCPU) {
ARROW_ASSIGN_OR_RAISE(auto array_cpu, array.CopyTo(default_cpu_memory_manager()));
RETURN_NOT_OK(VisitArrayInline(*array_cpu, this));
} else {
RETURN_NOT_OK(VisitArrayInline(array, this));
}
Flush();
return Status::OK();
}
Expand Down
54 changes: 54 additions & 0 deletions python/pyarrow/tests/test_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,57 @@ def test_device_interface_batch_array():
with pytest.raises(ValueError,
match="ArrowSchema describes non-struct type"):
pa.RecordBatch._import_from_c_device(ptr_array, ptr_schema)


def test_print_array():
batch = make_recordbatch(10)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arr = batch["f0"]
carr = cbatch["f0"]
assert str(carr) == str(arr)

batch = make_recordbatch(100)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arr = batch["f0"]
carr = cbatch["f0"]
assert str(carr) == str(arr)


def make_chunked_array(n_elements_per_chunk, n_chunks):
arrs = []
carrs = []
for _ in range(n_chunks):
batch = make_recordbatch(n_elements_per_chunk)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
arrs.append(batch["f0"])
carrs.append(cbatch["f0"])

return pa.chunked_array(arrs), pa.chunked_array(carrs)
Comment on lines +965 to +975
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still a bit verbose, but can be simplified once the "copy to device" functionality is exposed (should maybe do that first now)



def test_print_chunked_array():
arr, carr = make_chunked_array(10, 3)
assert str(carr) == str(arr)

arr, carr = make_chunked_array(100, 20)
assert str(carr) == str(arr)


def test_print_record_batch():
batch = make_recordbatch(10)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
assert str(cbatch) == str(batch)

batch = make_recordbatch(100)
cbuf = cuda.serialize_record_batch(batch, global_context)
cbatch = cuda.read_record_batch(cbuf, batch.schema)
assert str(cbatch) == str(batch)


def test_print_table():
_, table, _, ctable = make_table_cuda()
assert str(ctable) == str(table)
Loading