Skip to content
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

Support round operation on datetime64[ns] datatypes #9820

Merged
merged 31 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f13178b
added series.dt.floor
mayankanand007 Dec 2, 2021
693a6e9
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 2, 2021
789ace3
added datetimeindex.round
mayankanand007 Dec 2, 2021
7bf420a
Merge branch 'branch-22.02' of https://github.com/mayankanand007/cudf…
mayankanand007 Dec 2, 2021
96d22ba
move round impl. to IndexedFrame
mayankanand007 Dec 2, 2021
fadb90b
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 2, 2021
98ba3b4
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 3, 2021
00fea68
fixed typo in test
mayankanand007 Dec 3, 2021
b2c3475
fixed typo in rst file
mayankanand007 Dec 3, 2021
2ed624e
added doxygen docstrings
mayankanand007 Dec 3, 2021
e4f19d9
apply suggestions related to typos in test names
mayankanand007 Dec 3, 2021
91697ec
fixed style
mayankanand007 Dec 4, 2021
43c5d32
fixed style issue
mayankanand007 Dec 4, 2021
d2f9e64
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 6, 2021
d663784
addressing PR reviews
mayankanand007 Dec 6, 2021
bafd411
Merge branch 'branch-22.02' of https://github.com/mayankanand007/cudf…
mayankanand007 Dec 6, 2021
3c30fbd
updated function docstring with formatting
mayankanand007 Dec 6, 2021
69b447e
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 6, 2021
35b7e6b
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 7, 2021
7c6c135
changed field to freq
mayankanand007 Dec 7, 2021
4424080
Merge branch 'branch-22.02' into branch-22.02
mayankanand007 Dec 7, 2021
183cc5f
Apply suggestions from code review
mayankanand007 Dec 7, 2021
b771e80
updated docstring example in DatetimeIndex.round
mayankanand007 Dec 7, 2021
431b24f
Merge branch 'branch-22.02' of https://github.com/mayankanand007/cudf…
mayankanand007 Dec 7, 2021
b99f471
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 8, 2021
e85d565
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 9, 2021
09f71e2
changing dtype to auto for timestamps in test
mayankanand007 Dec 9, 2021
435b8f5
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 9, 2021
f2b68b2
Merge branch 'rapidsai:branch-22.02' into branch-22.02
mayankanand007 Dec 10, 2021
4d0fc98
style fixes
mayankanand007 Dec 10, 2021
8c49f73
changed rounding_kind to rounding_function
mayankanand007 Dec 10, 2021
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
91 changes: 91 additions & 0 deletions cpp/include/cudf/datetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,96 @@ std::unique_ptr<column> floor_nanosecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest day
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<cudf::column> round_day(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest hour
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<cudf::column> round_hour(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest minute
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<cudf::column> round_minute(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest second
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<cudf::column> round_second(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest millisecond
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<column> round_millisecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest microsecond
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<column> round_microsecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round to the nearest nanosecond
*
* @param column cudf::column_view of the input datetime values
* @param mr Device memory resource used to allocate device memory of the returned column.
*
* @throw cudf::logic_error if input column datatype is not TIMESTAMP
* @return cudf::column of the same datetime resolution as the input column
*/
std::unique_ptr<column> round_nanosecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

} // namespace datetime
} // namespace cudf
124 changes: 101 additions & 23 deletions cpp/src/datetime/datetime_ops.cu
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ enum class datetime_component {
NANOSECOND
};

enum class rounding_kind { CEIL, FLOOR };
enum class rounding_function {
CEIL, ///< Rounds up to the next integer multiple of the provided frequency
FLOOR, ///< Rounds down to the next integer multiple of the provided frequency
ROUND ///< Rounds to the nearest integer multiple of the provided frequency
bdice marked this conversation as resolved.
Show resolved Hide resolved
};

template <datetime_component Component>
struct extract_component_operator {
Expand Down Expand Up @@ -95,22 +99,23 @@ struct extract_component_operator {
template <typename DurationType>
struct RoundFunctor {
template <typename Timestamp>
CUDA_DEVICE_CALLABLE auto operator()(rounding_kind round_kind, Timestamp dt)
CUDA_DEVICE_CALLABLE auto operator()(rounding_function round_kind, Timestamp dt)
{
switch (round_kind) {
case rounding_kind::CEIL: return cuda::std::chrono::ceil<DurationType>(dt);
case rounding_kind::FLOOR: return cuda::std::chrono::floor<DurationType>(dt);
case rounding_function::CEIL: return cuda::std::chrono::ceil<DurationType>(dt);
case rounding_function::FLOOR: return cuda::std::chrono::floor<DurationType>(dt);
case rounding_function::ROUND: return cuda::std::chrono::round<DurationType>(dt);
default: cudf_assert(false && "Unsupported rounding kind.");
}
__builtin_unreachable();
}
};

struct RoundingDispatcher {
rounding_kind round_kind;
rounding_function round_kind;
datetime_component component;

RoundingDispatcher(rounding_kind round_kind, datetime_component component)
RoundingDispatcher(rounding_function round_kind, datetime_component component)
: round_kind(round_kind), component(component)
{
}
Expand Down Expand Up @@ -224,11 +229,11 @@ struct is_leap_year_op {
}
};

// Specific function for applying ceil/floor date ops
// Specific function for applying ceil/floor/round date ops
struct dispatch_round {
template <typename Timestamp>
std::enable_if_t<cudf::is_timestamp<Timestamp>(), std::unique_ptr<cudf::column>> operator()(
rounding_kind round_kind,
rounding_function round_kind,
datetime_component component,
cudf::column_view const& column,
rmm::cuda_stream_view stream,
Expand Down Expand Up @@ -414,7 +419,7 @@ std::unique_ptr<column> add_calendrical_months(column_view const& timestamp_colu
}
}

std::unique_ptr<column> round_general(rounding_kind round_kind,
std::unique_ptr<column> round_general(rounding_function round_kind,
datetime_component component,
column_view const& column,
rmm::cuda_stream_view stream,
Expand Down Expand Up @@ -529,7 +534,7 @@ std::unique_ptr<column> extract_quarter(column_view const& column,
std::unique_ptr<column> ceil_day(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::DAY,
column,
rmm::cuda_stream_default,
Expand All @@ -539,7 +544,7 @@ std::unique_ptr<column> ceil_day(column_view const& column, rmm::mr::device_memo
std::unique_ptr<column> ceil_hour(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::HOUR,
column,
rmm::cuda_stream_default,
Expand All @@ -549,7 +554,7 @@ std::unique_ptr<column> ceil_hour(column_view const& column, rmm::mr::device_mem
std::unique_ptr<column> ceil_minute(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::MINUTE,
column,
rmm::cuda_stream_default,
Expand All @@ -559,7 +564,7 @@ std::unique_ptr<column> ceil_minute(column_view const& column, rmm::mr::device_m
std::unique_ptr<column> ceil_second(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::SECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -570,7 +575,7 @@ std::unique_ptr<column> ceil_millisecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::MILLISECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -581,7 +586,7 @@ std::unique_ptr<column> ceil_microsecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::MICROSECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -592,7 +597,7 @@ std::unique_ptr<column> ceil_nanosecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::CEIL,
return detail::round_general(detail::rounding_function::CEIL,
detail::datetime_component::NANOSECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -602,7 +607,7 @@ std::unique_ptr<column> ceil_nanosecond(column_view const& column,
std::unique_ptr<column> floor_day(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::DAY,
column,
rmm::cuda_stream_default,
Expand All @@ -612,7 +617,7 @@ std::unique_ptr<column> floor_day(column_view const& column, rmm::mr::device_mem
std::unique_ptr<column> floor_hour(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::HOUR,
column,
rmm::cuda_stream_default,
Expand All @@ -622,7 +627,7 @@ std::unique_ptr<column> floor_hour(column_view const& column, rmm::mr::device_me
std::unique_ptr<column> floor_minute(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::MINUTE,
column,
rmm::cuda_stream_default,
Expand All @@ -632,7 +637,7 @@ std::unique_ptr<column> floor_minute(column_view const& column, rmm::mr::device_
std::unique_ptr<column> floor_second(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::SECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -643,7 +648,7 @@ std::unique_ptr<column> floor_millisecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::MILLISECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -654,7 +659,7 @@ std::unique_ptr<column> floor_microsecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::MICROSECOND,
column,
rmm::cuda_stream_default,
Expand All @@ -665,7 +670,80 @@ std::unique_ptr<column> floor_nanosecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_kind::FLOOR,
return detail::round_general(detail::rounding_function::FLOOR,
detail::datetime_component::NANOSECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_day(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::DAY,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_hour(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::HOUR,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_minute(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::MINUTE,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_second(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::SECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_millisecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::MILLISECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_microsecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::MICROSECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> round_nanosecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::round_general(detail::rounding_function::ROUND,
detail::datetime_component::NANOSECOND,
column,
rmm::cuda_stream_default,
Expand Down
Loading