Skip to content

Add alloc_size member function to cudf::column and cudf::table #18639

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

Merged
merged 7 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 12 additions & 1 deletion cpp/include/cudf/column/column.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -279,6 +279,17 @@ class column {
*/
contents release() noexcept;

/**
* @brief Returns the total device allocation size of the column in bytes
*
* This includes the size of the data buffer, null mask, and any child columns.
* It also includes any padding bytes and is not guaranteed to be the same as
* the size()*sizeof(T) for the column's logical type.
*
* @return The total allocation size in bytes
*/
[[nodiscard]] std::size_t alloc_size() const;

/**
* @brief Creates an immutable, non-owning view of the column's data and
* children.
Expand Down
11 changes: 10 additions & 1 deletion cpp/include/cudf/table/table.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -92,6 +92,15 @@ class table {
*/
[[nodiscard]] size_type num_rows() const noexcept { return _num_rows; }

/**
* @brief Returns the total device allocation size of the table's columns in bytes
*
* @see cudf::column::alloc_size
*
* @return The total allocation size in bytes
*/
[[nodiscard]] std::size_t alloc_size() const;

/**
* @brief Returns an immutable, non-owning `table_view` of the contents of
*this `table`.
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/column/column.cu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,6 +85,15 @@ column::contents column::release() noexcept
std::move(_children)};
}

std::size_t column::alloc_size() const
{
return _data.size() + _null_mask.size() +
std::accumulate(
_children.begin(), _children.end(), 0, [](auto const& sum, auto const& child) {
return sum + child->alloc_size();
});
}

// Create immutable view
column_view column::view() const
{
Expand Down
11 changes: 10 additions & 1 deletion cpp/src/table/table.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@

#include <rmm/cuda_stream_view.hpp>

#include <numeric>

namespace cudf {

// Copy the columns from another table
Expand Down Expand Up @@ -61,6 +63,13 @@ table::table(table_view view, rmm::cuda_stream_view stream, rmm::device_async_re
}
}

std::size_t table::alloc_size() const
{
return std::accumulate(_columns.begin(), _columns.end(), 0, [](auto const& sum, auto const& c) {
return sum + c->alloc_size();
});
}

// Create immutable view
table_view table::view() const
{
Expand Down
28 changes: 27 additions & 1 deletion cpp/tests/table/table_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION.
* Copyright (c) 2019-2025, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -148,4 +148,30 @@ TEST_F(TableTest, CreateFromViewVectorEmptyTables)
EXPECT_EQ(final_view.num_columns(), 0);
}

TEST_F(TableTest, AllocSize)
{
column_wrapper<int32_t> col1{{1, 2, 3, 4}};
column_wrapper<int16_t> col2{{1, 2, 3, 4}};

CVector cols;
cols.push_back(col1.release());
cols.push_back(col2.release());

Table t(std::move(cols));
EXPECT_EQ(t.alloc_size(), 24);
}

TEST_F(TableTest, AllocSizeWithNulls)
{
column_wrapper<int32_t> col1{{1, 2, 3, 4}, {1, 0, 1, 0}};
column_wrapper<int16_t> col2{{1, 2, 3, 4}, {1, 0, 1, 0}};

CVector cols;
cols.push_back(col1.release());
cols.push_back(col2.release());

Table t(std::move(cols));
EXPECT_EQ(t.alloc_size(), 152); // bitmask has padding
}

CUDF_TEST_PROGRAM_MAIN()