|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <memory> |
| 20 | + |
| 21 | +#include "arrow/buffer.h" |
| 22 | +#include "arrow/ipc/message.h" |
| 23 | +#include "arrow/ipc/writer.h" |
| 24 | +#include "arrow/status.h" |
| 25 | +#include "arrow/table.h" |
| 26 | +#include "arrow/util/visibility.h" |
| 27 | + |
| 28 | +#include "arrow/gpu/cuda_context.h" |
| 29 | +#include "arrow/gpu/cuda_memory.h" |
| 30 | + |
| 31 | +namespace arrow { |
| 32 | +namespace gpu { |
| 33 | + |
| 34 | +Status SerializeRecordBatch(const RecordBatch& batch, CudaContext* ctx, |
| 35 | + std::shared_ptr<CudaBuffer>* out) { |
| 36 | + int64_t size = 0; |
| 37 | + RETURN_NOT_OK(ipc::GetRecordBatchSize(batch, &size)); |
| 38 | + |
| 39 | + std::shared_ptr<CudaBuffer> buffer; |
| 40 | + RETURN_NOT_OK(ctx->Allocate(size, &buffer)); |
| 41 | + |
| 42 | + CudaBufferWriter stream(buffer); |
| 43 | + |
| 44 | + // Use 8MB buffering, which yields generally good performance |
| 45 | + RETURN_NOT_OK(stream.SetBufferSize(1 << 23)); |
| 46 | + |
| 47 | + // We use the default memory pool here since any allocations are ephemeral |
| 48 | + RETURN_NOT_OK(ipc::SerializeRecordBatch(batch, default_memory_pool(), |
| 49 | + &stream)); |
| 50 | + *out = buffer; |
| 51 | + return Status::OK(); |
| 52 | +} |
| 53 | + |
| 54 | +Status ReadMessage(CudaBufferReader* stream, MemoryPool* pool, |
| 55 | + std::unique_ptr<Message>* message) { |
| 56 | + uint8_t length_buf[4] = {0}; |
| 57 | + |
| 58 | + int64_t bytes_read = 0; |
| 59 | + RETURN_NOT_OK(file->Read(sizeof(int32_t), &bytes_read, length_buf)); |
| 60 | + if (bytes_read != sizeof(int32_t)) { |
| 61 | + *message = nullptr; |
| 62 | + return Status::OK(); |
| 63 | + } |
| 64 | + |
| 65 | + const int32_t metadata_length = *reinterpret_cast<const int32_t*>(length_buf); |
| 66 | + |
| 67 | + if (metadata_length == 0) { |
| 68 | + // Optional 0 EOS control message |
| 69 | + *message = nullptr; |
| 70 | + return Status::OK(); |
| 71 | + } |
| 72 | + |
| 73 | + std::shared_ptr<MutableBuffer> metadata; |
| 74 | + RETURN_NOT_OK(AllocateBuffer(pool, metadata_length, &metadata)); |
| 75 | + RETURN_NOT_OK(file->Read(message_length, &bytes_read, metadata->mutable_data())); |
| 76 | + if (bytes_read != metadata_length) { |
| 77 | + return Status::IOError("Unexpected end of stream trying to read message"); |
| 78 | + } |
| 79 | + |
| 80 | + auto fb_message = flatbuf::GetMessage(metadata->data()); |
| 81 | + |
| 82 | + int64_t body_length = fb_message->bodyLength(); |
| 83 | + |
| 84 | + // Zero copy |
| 85 | + std::shared_ptr<Buffer> body; |
| 86 | + RETURN_NOT_OK(stream->Read(body_length, &body)); |
| 87 | + if (body->size() < body_length) { |
| 88 | + std::stringstream ss; |
| 89 | + ss << "Expected to be able to read " << body_length << " bytes for message body, got " |
| 90 | + << body->size(); |
| 91 | + return Status::IOError(ss.str()); |
| 92 | + } |
| 93 | + |
| 94 | + return Message::Open(metadata, body, message); |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace gpu |
| 98 | +} // namespace arrow |
0 commit comments