From 9da6b5b6b015cef9fa14cca7eee5b449c88a7ed9 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Thu, 11 Aug 2022 12:59:49 +0200 Subject: [PATCH] column_view: add owner This is a hack we have to remove before merging this PR. --- cpp/include/cudf/column/column_view.hpp | 23 +++++++++--- cpp/src/column/column_view.cpp | 17 +++++---- .../cudf/cudf/_lib/cpp/column/column_view.pxd | 35 ++++++++++++++++--- 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/cpp/include/cudf/column/column_view.hpp b/cpp/include/cudf/column/column_view.hpp index 217f88e67f9..f7ecb0baf89 100644 --- a/cpp/include/cudf/column/column_view.hpp +++ b/cpp/include/cudf/column/column_view.hpp @@ -22,6 +22,7 @@ #include #include +#include #include #include @@ -239,6 +240,8 @@ class column_view_base { mutable size_type _null_count{}; ///< The number of null elements size_type _offset{}; ///< Index position of the first element. ///< Enables zero-copy slicing + std::shared_ptr _owner; ///< Pointer to the owner (if any) of the device + ///< memory allocation. column_view_base() = default; ~column_view_base() = default; @@ -282,15 +285,19 @@ class column_view_base { * @param data Pointer to device memory containing the column elements * @param null_mask Optional, pointer to device memory containing the null * indicator bitmask - * @param null_count Optional, the number of null elements. + * @param null_count Optional, the number of null elements * @param offset optional, index of the first element + * @param owner optional, object to which the lifetime of the device memory + * allocation is tied. If provided, a reference to this object is kept until + * destruction */ column_view_base(data_type type, size_type size, void const* data, bitmask_type const* null_mask = nullptr, size_type null_count = UNKNOWN_NULL_COUNT, - size_type offset = 0); + size_type offset = 0, + std::shared_ptr owner = nullptr); }; class mutable_column_view_base : public column_view_base { @@ -376,6 +383,9 @@ class column_view : public detail::column_view_base { * @param offset optional, index of the first element * @param children optional, depending on the element type, child columns may * contain additional data + * @param owner optional, object to which the lifetime of the device memory + * allocation is tied. If provided, a reference to this object is kept until + * destruction */ column_view(data_type type, size_type size, @@ -383,7 +393,8 @@ class column_view : public detail::column_view_base { bitmask_type const* null_mask = nullptr, size_type null_count = UNKNOWN_NULL_COUNT, size_type offset = 0, - std::vector const& children = {}); + std::vector const& children = {}, + std::shared_ptr owner = nullptr); /** * @brief Returns the specified child @@ -531,6 +542,9 @@ class mutable_column_view : public detail::column_view_base { * @param offset optional, index of the first element * @param children optional, depending on the element type, child columns may * contain additional data + * @param owner optional, object to which the lifetime of the device memory + * allocation is tied. If provided, a reference to this object is kept until + * destruction */ mutable_column_view(data_type type, size_type size, @@ -538,7 +552,8 @@ class mutable_column_view : public detail::column_view_base { bitmask_type* null_mask = nullptr, size_type null_count = cudf::UNKNOWN_NULL_COUNT, size_type offset = 0, - std::vector const& children = {}); + std::vector const& children = {}, + std::shared_ptr owner = nullptr); /** * @brief Returns pointer to the base device memory allocation casted to diff --git a/cpp/src/column/column_view.cpp b/cpp/src/column/column_view.cpp index 2ff088a3f20..c2c72080dd6 100644 --- a/cpp/src/column/column_view.cpp +++ b/cpp/src/column/column_view.cpp @@ -36,13 +36,15 @@ column_view_base::column_view_base(data_type type, void const* data, bitmask_type const* null_mask, size_type null_count, - size_type offset) + size_type offset, + std::shared_ptr owner) : _type{type}, _size{size}, _data{data}, _null_mask{null_mask}, _null_count{null_count}, - _offset{offset} + _offset{offset}, + _owner{owner} { CUDF_EXPECTS(size >= 0, "Column size cannot be negative."); @@ -143,8 +145,10 @@ column_view::column_view(data_type type, bitmask_type const* null_mask, size_type null_count, size_type offset, - std::vector const& children) - : detail::column_view_base{type, size, data, null_mask, null_count, offset}, _children{children} + std::vector const& children, + std::shared_ptr owner) + : detail::column_view_base{type, size, data, null_mask, null_count, offset, owner}, + _children{children} { if (type.id() == type_id::EMPTY) { CUDF_EXPECTS(num_children() == 0, "EMPTY column cannot have children."); @@ -158,8 +162,9 @@ mutable_column_view::mutable_column_view(data_type type, bitmask_type* null_mask, size_type null_count, size_type offset, - std::vector const& children) - : detail::column_view_base{type, size, data, null_mask, null_count, offset}, + std::vector const& children, + std::shared_ptr owner) + : detail::column_view_base{type, size, data, null_mask, null_count, offset, owner}, mutable_children{children} { if (type.id() == type_id::EMPTY) { diff --git a/python/cudf/cudf/_lib/cpp/column/column_view.pxd b/python/cudf/cudf/_lib/cpp/column/column_view.pxd index 39c1c958531..e9256e4fe01 100644 --- a/python/cudf/cudf/_lib/cpp/column/column_view.pxd +++ b/python/cudf/cudf/_lib/cpp/column/column_view.pxd @@ -1,6 +1,7 @@ -# Copyright (c) 2020, NVIDIA CORPORATION. +# Copyright (c) 2020-2022, NVIDIA CORPORATION. from libcpp cimport bool +from libcpp.memory cimport shared_ptr from libcpp.vector cimport vector from cudf._lib.cpp.types cimport bitmask_type, data_type, size_type @@ -54,6 +55,17 @@ cdef extern from "cudf/column/column_view.hpp" namespace "cudf" nogil: vector[column_view] children ) except + + column_view( + data_type type, + size_type size, + const void* data, + const bitmask_type* null_mask, + size_type null_count, + size_type offset, + vector[column_view] children, + shared_ptr[void] owner + ) except + + const T* data[T]() except + const T* head[T]() except + const bitmask_type* null_mask() except + @@ -102,9 +114,24 @@ cdef extern from "cudf/column/column_view.hpp" namespace "cudf" nogil: ) except + mutable_column_view( - data_type type, size_type size, const void* data, - const bitmask_type* null_mask, size_type null_count, - size_type offset, vector[mutable_column_view] children + data_type type, + size_type size, + const void* data, + const bitmask_type* null_mask, + size_type null_count, + size_type offset, + vector[mutable_column_view] children + ) except + + + mutable_column_view( + data_type type, + size_type size, + const void* data, + const bitmask_type* null_mask, + size_type null_count, + size_type offset, + vector[mutable_column_view] children, + shared_ptr[void] owner ) except + T* data[T]() except +