From 7cd5a19374346843678b66400d54ed5770888a3b Mon Sep 17 00:00:00 2001 From: Mark Harris Date: Wed, 2 Feb 2022 13:25:20 +1100 Subject: [PATCH 1/3] Add device_buffer::ssize() and device_uvector::ssize() --- include/rmm/device_buffer.hpp | 12 ++++++++++-- include/rmm/device_uvector.hpp | 11 +++++++++++ tests/device_buffer_tests.cu | 1 + tests/device_uvector_tests.cpp | 3 ++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/rmm/device_buffer.hpp b/include/rmm/device_buffer.hpp index 48e1fc6b3..82ce3770d 100644 --- a/include/rmm/device_buffer.hpp +++ b/include/rmm/device_buffer.hpp @@ -315,11 +315,19 @@ class device_buffer { void* data() noexcept { return _data; } /** - * @brief Returns size in bytes that was requested for the device memory - * allocation + * @brief Returns size in bytes of the underlying device memory storage. */ [[nodiscard]] std::size_t size() const noexcept { return _size; } + /** + * @brief Returns signed size in bytes of the underlying device memory storage. + */ + [[nodiscard]] std::int64_t ssize() const noexcept + { + assert(size() < std::numeric_limits::max() && "Size overflows signed integer"); + return static_cast(size()); + } + /** * @brief Returns whether the size in bytes of the `device_buffer` is zero. * diff --git a/include/rmm/device_uvector.hpp b/include/rmm/device_uvector.hpp index ff1220ec3..85c92c4fd 100644 --- a/include/rmm/device_uvector.hpp +++ b/include/rmm/device_uvector.hpp @@ -486,6 +486,17 @@ class device_uvector { */ [[nodiscard]] std::size_t size() const noexcept { return bytes_to_elements(_storage.size()); } + /** + * @brief Returns signed number of elements in the vector. + * + * @return The number of elements. + */ + [[nodiscard]] std::int64_t ssize() const noexcept + { + assert(size() < std::numeric_limits::max() && "Size overflows signed integer"); + return static_cast(size()); + } + /** * @brief Returns true if the vector contains no elements, i.e., `size() == 0`. * diff --git a/tests/device_buffer_tests.cu b/tests/device_buffer_tests.cu index fd772cfea..832eae5ac 100644 --- a/tests/device_buffer_tests.cu +++ b/tests/device_buffer_tests.cu @@ -67,6 +67,7 @@ TYPED_TEST(DeviceBufferTest, DefaultMemoryResource) rmm::device_buffer buff(this->size, rmm::cuda_stream_view{}); EXPECT_NE(nullptr, buff.data()); EXPECT_EQ(this->size, buff.size()); + EXPECT_EQ(this->size, buff.ssize()); EXPECT_EQ(this->size, buff.capacity()); EXPECT_EQ(rmm::mr::get_current_device_resource(), buff.memory_resource()); EXPECT_EQ(rmm::cuda_stream_view{}, buff.stream()); diff --git a/tests/device_uvector_tests.cpp b/tests/device_uvector_tests.cpp index 539dd2c26..dfab2a27e 100644 --- a/tests/device_uvector_tests.cpp +++ b/tests/device_uvector_tests.cpp @@ -52,7 +52,8 @@ TYPED_TEST(TypedUVectorTest, NonZeroSizeConstructor) { auto const size{12345}; rmm::device_uvector vec(size, this->stream()); - EXPECT_EQ(vec.size(), 12345); + EXPECT_EQ(vec.size(), size); + EXPECT_EQ(vec.ssize(), size); EXPECT_NE(vec.data(), nullptr); EXPECT_EQ(vec.end(), vec.begin() + vec.size()); EXPECT_FALSE(vec.is_empty()); From 6e5530672a7e4fe94cc74432f43aaca949c1b2ca Mon Sep 17 00:00:00 2001 From: Mark Harris Date: Wed, 2 Feb 2022 13:33:55 +1100 Subject: [PATCH 2/3] copyrights --- include/rmm/device_buffer.hpp | 2 +- include/rmm/device_uvector.hpp | 2 +- tests/device_buffer_tests.cu | 2 +- tests/device_uvector_tests.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/rmm/device_buffer.hpp b/include/rmm/device_buffer.hpp index 82ce3770d..8e14246c2 100644 --- a/include/rmm/device_buffer.hpp +++ b/include/rmm/device_buffer.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2021, NVIDIA CORPORATION. + * Copyright (c) 2019-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/include/rmm/device_uvector.hpp b/include/rmm/device_uvector.hpp index 85c92c4fd..2a8b9a247 100644 --- a/include/rmm/device_uvector.hpp +++ b/include/rmm/device_uvector.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, NVIDIA CORPORATION. + * Copyright (c) 2020-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/tests/device_buffer_tests.cu b/tests/device_buffer_tests.cu index 832eae5ac..9cab76323 100644 --- a/tests/device_buffer_tests.cu +++ b/tests/device_buffer_tests.cu @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2021, NVIDIA CORPORATION. + * Copyright (c) 2019-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/tests/device_uvector_tests.cpp b/tests/device_uvector_tests.cpp index dfab2a27e..c1505bc0a 100644 --- a/tests/device_uvector_tests.cpp +++ b/tests/device_uvector_tests.cpp @@ -1,6 +1,6 @@ /* - * Copyright (c) 2020-2021, NVIDIA CORPORATION. + * Copyright (c) 2020-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From 9f439a460d72713a71069d434a38371b36e98cbc Mon Sep 17 00:00:00 2001 From: Mark Harris Date: Fri, 4 Feb 2022 10:34:00 +1100 Subject: [PATCH 3/3] docs --- include/rmm/device_buffer.hpp | 8 ++++---- include/rmm/device_uvector.hpp | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/include/rmm/device_buffer.hpp b/include/rmm/device_buffer.hpp index 8e14246c2..78f8ba184 100644 --- a/include/rmm/device_buffer.hpp +++ b/include/rmm/device_buffer.hpp @@ -285,7 +285,7 @@ class device_buffer { * Reallocates and copies on stream `stream` the contents of the device memory * allocation to reduce `capacity()` to `size()`. * - * If `size() == capacity()`, no allocations nor copies occur. + * If `size() == capacity()`, no allocations or copies occur. * * @throws rmm::bad_alloc If creating the new allocation fails * @throws rmm::cuda_error If the copy from the old to new allocation fails @@ -315,12 +315,12 @@ class device_buffer { void* data() noexcept { return _data; } /** - * @brief Returns size in bytes of the underlying device memory storage. + * @brief Returns the number of bytes. */ [[nodiscard]] std::size_t size() const noexcept { return _size; } /** - * @brief Returns signed size in bytes of the underlying device memory storage. + * @brief Returns the signed number of bytes. */ [[nodiscard]] std::int64_t ssize() const noexcept { @@ -329,7 +329,7 @@ class device_buffer { } /** - * @brief Returns whether the size in bytes of the `device_buffer` is zero. + * @brief returns the number of bytes that can be held in currently allocated storage. * * If `is_empty() == true`, the `device_buffer` may still hold an allocation * if `capacity() > 0`. diff --git a/include/rmm/device_uvector.hpp b/include/rmm/device_uvector.hpp index 2a8b9a247..57ef1149a 100644 --- a/include/rmm/device_uvector.hpp +++ b/include/rmm/device_uvector.hpp @@ -480,16 +480,12 @@ class device_uvector { [[nodiscard]] const_iterator end() const noexcept { return cend(); } /** - * @brief Returns the number of elements in the vector. - * - * @return The number of elements. + * @brief Returns the number of elements. */ [[nodiscard]] std::size_t size() const noexcept { return bytes_to_elements(_storage.size()); } /** - * @brief Returns signed number of elements in the vector. - * - * @return The number of elements. + * @brief Returns the signed number of elements. */ [[nodiscard]] std::int64_t ssize() const noexcept {