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

Implement Series.datetime.floor #9571

Merged
merged 66 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
051c682
create new PR
skirui-source Oct 21, 2021
d75947b
added floor_general and floor_timestamp methods
skirui-source Oct 22, 2021
ded37c1
Added initial cython bindings
mayankanand007 Oct 22, 2021
eb985d6
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Oct 22, 2021
a6ad320
Merge branch 'series.dt.floor' of https://github.com/skirui-source/cu…
mayankanand007 Oct 22, 2021
58487a9
fixed space issue
mayankanand007 Oct 22, 2021
1ee42ee
adding tests in libcudf
skirui-source Oct 22, 2021
b693dcc
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Oct 22, 2021
0f55eb4
Fixed style issues
mayankanand007 Oct 22, 2021
2440210
fixed more style issues
mayankanand007 Oct 22, 2021
9a6458e
fixed issue with black formatting
mayankanand007 Oct 22, 2021
1224539
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Oct 25, 2021
4862ce4
black formatting
mayankanand007 Oct 25, 2021
ac89a44
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Oct 25, 2021
0aac581
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Oct 25, 2021
de49660
changing tests to resemble TestCeilDatetime format
skirui-source Oct 25, 2021
9cbb75e
.
skirui-source Oct 25, 2021
df519dd
added declarations for floor methods in header file datetime.hpp
skirui-source Oct 26, 2021
0ae000f
all tests passing now
skirui-source Oct 26, 2021
4de4dda
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Oct 30, 2021
6016257
updated copyright date
skirui-source Oct 30, 2021
1bb7f34
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 1, 2021
1d9283e
added python changes
mayankanand007 Nov 1, 2021
2716084
added spacing in docs
mayankanand007 Nov 1, 2021
3849ee8
added spacing in docs
mayankanand007 Nov 1, 2021
f2b31c6
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 1, 2021
545f675
addressing reviews
mayankanand007 Nov 1, 2021
54e2272
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 2, 2021
1e6d8f0
fixed quote issues
mayankanand007 Nov 2, 2021
0431052
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 3, 2021
d9322b5
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Nov 3, 2021
7fb4e71
address libcudf reviews
skirui-source Nov 3, 2021
e818833
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Nov 4, 2021
53b7b3b
added aliases
mayankanand007 Nov 5, 2021
30dacb8
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 5, 2021
79c57d7
added aliases for ceil as well
mayankanand007 Nov 5, 2021
9ff744f
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 5, 2021
673fa03
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Nov 5, 2021
a7abc29
added tests for microsecond and nanosecond in ceil/floor
skirui-source Nov 5, 2021
f880f48
fixed typo
skirui-source Nov 5, 2021
4e756bf
Apply suggestions from code review
mayankanand007 Nov 5, 2021
8c37f3e
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 5, 2021
7d3f42d
fix ceil test
mayankanand007 Nov 5, 2021
b767b21
addressing reviews
mayankanand007 Nov 5, 2021
44ec257
fix typo in test case
mayankanand007 Nov 5, 2021
efdbf54
fixed call to series constructor
mayankanand007 Nov 8, 2021
23819af
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 8, 2021
2f0e569
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 8, 2021
3520dd8
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Nov 8, 2021
62389b3
fixed issue with Series constructor
mayankanand007 Nov 8, 2021
d4b7eee
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 8, 2021
f039f4d
Merge branch 'rapidsai:branch-21.12' into series.dt.floor
mayankanand007 Nov 10, 2021
3ad7608
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 10, 2021
a294fa2
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 11, 2021
dfd6abd
replaced thrust with std::vector in floored day,hour
skirui-source Nov 12, 2021
fffa109
Merge branch 'series.dt.floor' of github.com:skirui-source/cudf into …
skirui-source Nov 12, 2021
da94cc1
preliminary work on round_dispatcher
skirui-source Nov 12, 2021
256620f
some preliminary changes for dispatcher
mayankanand007 Nov 12, 2021
ea18bae
minor fixes..dispatcher still WIP
skirui-source Nov 15, 2021
321dc9b
Merge branch 'rapidsai:branch-22.02' into series.dt.floor
mayankanand007 Nov 16, 2021
052456a
removing round to add in follow-up PR
mayankanand007 Nov 16, 2021
8d6bd4c
Merge branch 'rapidsai:branch-22.02' into series.dt.floor
mayankanand007 Nov 16, 2021
35b19f5
fixed naming in tests
mayankanand007 Nov 16, 2021
e900ddd
Merge branch 'rapidsai:branch-22.02' into series.dt.floor
mayankanand007 Nov 16, 2021
d40ae20
Merge branch 'branch-21.12' of https://github.com/rapidsai/cudf into …
skirui-source Nov 16, 2021
8f5a8d8
Merge branch 'branch-22.02' of https://github.com/rapidsai/cudf into …
skirui-source Nov 16, 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
93 changes: 92 additions & 1 deletion cpp/include/cudf/datetime.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, 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 @@ -378,5 +378,96 @@ std::unique_ptr<column> ceil_nanosecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_day(
isVoid marked this conversation as resolved.
Show resolved Hide resolved
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_hour(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_minute(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_second(
cudf::column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_millisecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_microsecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

/**
* @brief Round down 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> floor_nanosecond(
column_view const& column,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());

} // namespace datetime
} // namespace cudf
199 changes: 162 additions & 37 deletions cpp/src/datetime/datetime_ops.cu
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ enum class datetime_component {
NANOSECOND
};

enum class rounding_kind { CEIL, FLOOR };

template <datetime_component Component>
struct extract_component_operator {
template <typename Timestamp>
Expand Down Expand Up @@ -88,32 +90,59 @@ struct extract_component_operator {
}
};

template <datetime_component COMPONENT>
struct ceil_timestamp {
// This functor takes the rounding type as runtime info and dispatches to the ceil/floor/round
// function.
template <typename DurationType>
struct RoundFunctor {
template <typename Timestamp>
CUDA_DEVICE_CALLABLE auto operator()(rounding_kind 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);
default: cudf_assert(false && "Unsupported rounding kind.");
vyasr marked this conversation as resolved.
Show resolved Hide resolved
}
__builtin_unreachable();
}
};

struct RoundingDispatcher {
rounding_kind round_kind;
datetime_component component;

vyasr marked this conversation as resolved.
Show resolved Hide resolved
RoundingDispatcher(rounding_kind round_kind, datetime_component component)
: round_kind(round_kind), component(component)
{
}

template <typename Timestamp>
CUDA_DEVICE_CALLABLE Timestamp operator()(Timestamp const ts) const
{
using namespace cuda::std::chrono;
// want to use this with D, H, T (minute), S, L (millisecond), U
switch (COMPONENT) {
switch (component) {
case datetime_component::DAY:
return time_point_cast<typename Timestamp::duration>(ceil<duration_D>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_D>{}(round_kind, ts));
case datetime_component::HOUR:
return time_point_cast<typename Timestamp::duration>(ceil<duration_h>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_h>{}(round_kind, ts));
case datetime_component::MINUTE:
return time_point_cast<typename Timestamp::duration>(ceil<duration_m>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_m>{}(round_kind, ts));
case datetime_component::SECOND:
return time_point_cast<typename Timestamp::duration>(ceil<duration_s>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_s>{}(round_kind, ts));
case datetime_component::MILLISECOND:
return time_point_cast<typename Timestamp::duration>(ceil<duration_ms>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_ms>{}(round_kind, ts));
case datetime_component::MICROSECOND:
return time_point_cast<typename Timestamp::duration>(ceil<duration_us>(ts));
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_us>{}(round_kind, ts));
case datetime_component::NANOSECOND:
return time_point_cast<typename Timestamp::duration>(ceil<duration_ns>(ts));
default: cudf_assert(false && "Unexpected resolution");
return time_point_cast<typename Timestamp::duration>(
RoundFunctor<duration_ns>{}(round_kind, ts));
default: cudf_assert(false && "Unsupported datetime rounding resolution.");
vyasr marked this conversation as resolved.
Show resolved Hide resolved
}

return {};
__builtin_unreachable();
}
};

Expand Down Expand Up @@ -196,10 +225,11 @@ struct is_leap_year_op {
};

// Specific function for applying ceil/floor date ops
template <typename TransformFunctor>
struct dispatch_ceil {
struct dispatch_round {
template <typename Timestamp>
std::enable_if_t<cudf::is_timestamp<Timestamp>(), std::unique_ptr<cudf::column>> operator()(
rounding_kind round_kind,
datetime_component component,
cudf::column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr) const
Expand All @@ -221,7 +251,7 @@ struct dispatch_ceil {
column.begin<Timestamp>(),
column.end<Timestamp>(),
output->mutable_view().begin<Timestamp>(),
TransformFunctor{});
RoundingDispatcher{round_kind, component});

return output;
}
Expand Down Expand Up @@ -384,13 +414,14 @@ std::unique_ptr<column> add_calendrical_months(column_view const& timestamp_colu
}
}

template <datetime_component Component>
std::unique_ptr<column> ceil_general(column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
std::unique_ptr<column> round_general(rounding_kind round_kind,
datetime_component component,
column_view const& column,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
return cudf::type_dispatcher(
column.type(), dispatch_ceil<detail::ceil_timestamp<Component>>{}, column, stream, mr);
column.type(), dispatch_round{}, round_kind, component, column, stream, mr);
}

std::unique_ptr<column> extract_year(column_view const& column,
Expand Down Expand Up @@ -498,53 +529,147 @@ 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::ceil_general<detail::datetime_component::DAY>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::DAY,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_hour(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::HOUR>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::HOUR,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_minute(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::MINUTE>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::MINUTE,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_second(column_view const& column, rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::SECOND>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::SECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_millisecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::MILLISECOND>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::MILLISECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_microsecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::MICROSECOND>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::MICROSECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> ceil_nanosecond(column_view const& column,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::ceil_general<detail::datetime_component::NANOSECOND>(
column, rmm::cuda_stream_default, mr);
return detail::round_general(detail::rounding_kind::CEIL,
detail::datetime_component::NANOSECOND,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::DAY,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::HOUR,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::MINUTE,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::SECOND,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::MILLISECOND,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::MICROSECOND,
column,
rmm::cuda_stream_default,
mr);
}

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,
detail::datetime_component::NANOSECOND,
column,
rmm::cuda_stream_default,
mr);
}

std::unique_ptr<column> extract_year(column_view const& column, rmm::mr::device_memory_resource* mr)
Expand Down
Loading