Skip to content

Commit 1cf1196

Browse files
committed
Test CudaBuffer::CopyFromHost
Change-Id: I205b2861797c57756a38f4c695937d354d497b53
1 parent a2708f2 commit 1cf1196

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

cpp/src/arrow/gpu/cuda-test.cc

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,34 @@ constexpr int kGpuNumber = 0;
3434
class TestCudaBuffer : public ::testing::Test {};
3535

3636
TEST_F(TestCudaBuffer, Allocate) {
37-
const int device = 0;
38-
3937
const int64_t kSize = 100;
4038
std::shared_ptr<CudaBuffer> buffer;
4139

42-
ASSERT_OK(AllocateCudaBuffer(device, kSize, &buffer));
40+
ASSERT_OK(AllocateCudaBuffer(kGpuNumber, kSize, &buffer));
4341
ASSERT_EQ(kSize, buffer->size());
4442
}
4543

46-
TEST_F(TestCudaBuffer, CopyFromHost) {}
44+
void AssertCudaBufferEquals(const CudaBuffer& buffer, const uint8_t* host_data,
45+
const int64_t nbytes) {
46+
std::shared_ptr<MutableBuffer> result;
47+
ASSERT_OK(AllocateBuffer(default_memory_pool(), nbytes, &result));
48+
ASSERT_OK(buffer.CopyToHost(result->mutable_data()));
49+
ASSERT_EQ(0, std::memcmp(result->data(), host_data, nbytes));
50+
}
51+
52+
TEST_F(TestCudaBuffer, CopyFromHost) {
53+
const int64_t kSize = 1000;
54+
std::shared_ptr<CudaBuffer> device_buffer;
55+
ASSERT_OK(AllocateCudaBuffer(kGpuNumber, kSize, &device_buffer));
56+
57+
std::shared_ptr<PoolBuffer> host_buffer;
58+
ASSERT_OK(test::MakeRandomBytePoolBuffer(kSize, default_memory_pool(), &host_buffer));
59+
60+
ASSERT_OK(device_buffer->CopyFromHost(0, host_buffer->data(), 500));
61+
ASSERT_OK(device_buffer->CopyFromHost(500, host_buffer->data() + 500, kSize - 500));
62+
63+
AssertCudaBufferEquals(*device_buffer, host_buffer->data(), kSize);
64+
}
4765

4866
class TestCudaBufferWriter : public ::testing::Test {
4967
public:
@@ -83,12 +101,7 @@ class TestCudaBufferWriter : public ::testing::Test {
83101

84102
ASSERT_OK(writer_->Flush());
85103

86-
std::shared_ptr<MutableBuffer> result;
87-
ASSERT_OK(AllocateBuffer(default_memory_pool(), total_bytes, &result));
88-
89-
ASSERT_OK(device_buffer_->CopyToHost(result->mutable_data()));
90-
91-
ASSERT_EQ(0, std::memcmp(result->data(), buffer->data(), total_bytes));
104+
AssertCudaBufferEquals(*device_buffer_, buffer->data(), total_bytes);
92105
}
93106

94107
protected:
@@ -151,10 +164,7 @@ TEST_F(TestCudaBufferWriter, EdgeCases) {
151164
ASSERT_EQ(0, writer_->num_bytes_buffered());
152165

153166
// Check that everything was written
154-
std::shared_ptr<MutableBuffer> result;
155-
ASSERT_OK(AllocateBuffer(default_memory_pool(), 1000, &result));
156-
ASSERT_OK(device_buffer_->CopyToHost(result->mutable_data()));
157-
ASSERT_EQ(0, std::memcmp(result->data(), host_data, 1000));
167+
AssertCudaBufferEquals(*device_buffer_, host_data, 1000);
158168
}
159169

160170
} // namespace gpu

cpp/src/arrow/gpu/cuda_memory.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ CudaBuffer::~CudaBuffer() {
3737
}
3838
}
3939

40-
Status CudaBuffer::CopyToHost(uint8_t* out) {
40+
Status CudaBuffer::CopyToHost(uint8_t* out) const {
4141
CUDA_RETURN_NOT_OK(cudaMemcpy(out, data_, size_, cudaMemcpyDeviceToHost));
4242
return Status::OK();
4343
}
4444

4545
Status CudaBuffer::CopyFromHost(const int64_t position, const uint8_t* data,
4646
int64_t nbytes) {
47-
DCHECK_LT(nbytes, size_ - position) << "Copy would overflow buffer";
47+
DCHECK_LE(nbytes, size_ - position) << "Copy would overflow buffer";
4848
CUDA_RETURN_NOT_OK(
4949
cudaMemcpy(mutable_data_ + position, data, nbytes, cudaMemcpyHostToDevice));
5050
return Status::OK();

cpp/src/arrow/gpu/cuda_memory.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ARROW_EXPORT CudaBuffer : public MutableBuffer {
4343
/// \brief Copy memory from GPU device to CPU host
4444
/// \param[out] out a pre-allocated output buffer
4545
/// \return Status
46-
Status CopyToHost(uint8_t* out);
46+
Status CopyToHost(uint8_t* out) const;
4747

4848
/// \brief Copy memory to device at position
4949
/// \param[in] position start position to copy bytes
@@ -108,7 +108,10 @@ class ARROW_EXPORT CudaBufferWriter : public io::FixedSizeBufferWriter {
108108
/// By default writes are unbuffered
109109
Status SetBufferSize(const int64_t buffer_size);
110110

111+
/// \brief Returns size of host (CPU) buffer, 0 for unbuffered
111112
int64_t buffer_size() const { return buffer_size_; }
113+
114+
/// \brief Returns number of bytes buffered on host
112115
int64_t num_bytes_buffered() const { return buffer_position_; }
113116

114117
private:

0 commit comments

Comments
 (0)