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

Update groupby result_cache to allow sharing intermediate results based on column_view instead of requests. #9195

Merged
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d421d6d
add shallow_hash(column_view)
karthikeyann Sep 7, 2021
9c4a9f3
add CompoundTypes to type_lists
karthikeyann Sep 7, 2021
a3dd235
add shallow_hash tests
karthikeyann Sep 7, 2021
2365d07
add column copy test
karthikeyann Sep 7, 2021
88726a4
add shallow_equal(column_view) and tests
karthikeyann Sep 7, 2021
d52509d
update result_cache to use shallow_hash, shallow_equal
karthikeyann Sep 8, 2021
d9a8bd7
Update cpp/include/cudf/column/column_view.hpp
karthikeyann Sep 8, 2021
7e7f250
ignore data, nullmask, offset if parent size is empty
karthikeyann Sep 13, 2021
d1d5c3c
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 13, 2021
0005154
is_shallow_equal ignore children states for empty column. (not childr…
karthikeyann Sep 13, 2021
e692053
for empty column, ignore child pointers in shallow_hash
karthikeyann Sep 14, 2021
44372bc
rename is_shallow_equal to is_shallow_equivalent
karthikeyann Sep 14, 2021
ecc3a7d
use hash_combine for shallow hash
karthikeyann Sep 16, 2021
d2cd468
Apply suggestions from code review (jake)
karthikeyann Sep 16, 2021
fa40847
address review comments
karthikeyann Sep 17, 2021
f709b2a
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 17, 2021
6ac5725
update after PR #9185 updates
karthikeyann Sep 17, 2021
e863bc7
Merge branch 'branch-21.10' of github.com:rapidsai/cudf into enh-grou…
karthikeyann Sep 17, 2021
f66fdd9
Merge branch 'branch-21.10' of github.com:rapidsai/cudf into fea-shal…
karthikeyann Sep 18, 2021
e36b834
add boost license for hash_combine, move to diff header
karthikeyann Sep 18, 2021
a1ff894
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 18, 2021
1fbe3fc
Apply suggestions from code review (jake)
karthikeyann Sep 18, 2021
79ca5e5
Merge branches 'enh-groupby_cache_hashed' and 'fea-shallow_hash_colum…
karthikeyann Sep 18, 2021
fc3cc6b
include cleanup
karthikeyann Sep 18, 2021
eb2b0db
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 18, 2021
0593955
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 18, 2021
f7b6bb6
add missing include due to reorg
karthikeyann Sep 18, 2021
9f19ddf
Apply suggestions from code review (jake)
karthikeyann Sep 20, 2021
98bbc94
fix duplicate {col, agg} request extract
karthikeyann Sep 20, 2021
9581525
address review comments
karthikeyann Sep 20, 2021
dcb0668
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 20, 2021
243490b
Merge branch 'branch-21.10' of github.com:rapidsai/cudf into enh-grou…
karthikeyann Sep 20, 2021
1a5f367
Update cpp/src/column/column_view.cpp
karthikeyann Sep 21, 2021
8fa765c
Merge branch 'fea-shallow_hash_columnview' of github.com:karthikeyann…
karthikeyann Sep 22, 2021
e10ca8c
Merge branch 'branch-21.12' of github.com:rapidsai/cudf into enh-grou…
karthikeyann Sep 24, 2021
68c4213
merge fix
karthikeyann Sep 24, 2021
3abeb1c
Merge branch 'rapidsai:branch-21.12' into enh-groupby_cache_hashed
karthikeyann Sep 27, 2021
ece8279
Update cpp/include/cudf/detail/aggregation/result_cache.hpp (vyasr)
karthikeyann Oct 4, 2021
7d7eda5
Merge branch 'branch-21.12' into enh-groupby_cache_hashed
karthikeyann Oct 4, 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
32 changes: 19 additions & 13 deletions cpp/include/cudf/detail/aggregation/result_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,26 @@

#include <cudf/column/column.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/detail/hashing.hpp>
#include <cudf/types.hpp>

#include <unordered_map>

namespace cudf {
namespace detail {
struct aggregation_equality {
bool operator()(aggregation const& lhs, aggregation const& rhs) const
struct pair_column_aggregation_equal_to {
bool operator()(std::pair<column_view, aggregation const&> const& lhs,
std::pair<column_view, aggregation const&> const& rhs) const
{
return lhs.is_equal(rhs);
return is_shallow_equivalent(lhs.first, rhs.first) and lhs.second.is_equal(rhs.second);
}
};

struct aggregation_hash {
size_t operator()(aggregation const& key) const noexcept { return key.do_hash(); }
struct pair_column_aggregation_hash {
size_t operator()(std::pair<column_view, aggregation const&> const& key) const noexcept
karthikeyann marked this conversation as resolved.
Show resolved Hide resolved
{
return hash_combine(shallow_hash(key.first), key.second.do_hash());
}
};

class result_cache {
Expand All @@ -43,19 +49,19 @@ class result_cache {

result_cache(size_t num_columns) : _cache(num_columns) {}

bool has_result(size_t col_idx, aggregation const& agg) const;
bool has_result(column_view const& input, aggregation const& agg) const;

void add_result(size_t col_idx, aggregation const& agg, std::unique_ptr<column>&& col);
void add_result(column_view const& input, aggregation const& agg, std::unique_ptr<column>&& col);

column_view get_result(size_t col_idx, aggregation const& agg) const;
column_view get_result(column_view const& input, aggregation const& agg) const;

std::unique_ptr<column> release_result(size_t col_idx, aggregation const& agg);
std::unique_ptr<column> release_result(column_view const& input, aggregation const& agg);

private:
std::vector<std::unordered_map<std::reference_wrapper<aggregation const>,
std::pair<std::unique_ptr<aggregation>, std::unique_ptr<column>>,
aggregation_hash,
aggregation_equality>>
std::unordered_map<std::pair<column_view, std::reference_wrapper<aggregation const>>,
std::pair<std::unique_ptr<aggregation>, std::unique_ptr<column>>,
pair_column_aggregation_hash,
pair_column_aggregation_equal_to>
_cache;
};

Expand Down
37 changes: 17 additions & 20 deletions cpp/src/aggregation/result_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,36 @@
namespace cudf {
namespace detail {

bool result_cache::has_result(size_t col_idx, aggregation const& agg) const
bool result_cache::has_result(column_view const& input, aggregation const& agg) const
{
if (col_idx > _cache.size()) return false;

auto result_it = _cache[col_idx].find(agg);

return (result_it != _cache[col_idx].end());
return _cache.count({input, agg});
}

void result_cache::add_result(size_t col_idx, aggregation const& agg, std::unique_ptr<column>&& col)
void result_cache::add_result(column_view const& input,
aggregation const& agg,
std::unique_ptr<column>&& col)
{
// We can't guarantee that agg will outlive the cache, so we need to take ownership of a copy.
// To allow lookup by reference, make the key a reference and keep the owner in the value pair.
auto owned_agg = agg.clone();
auto const& key = *owned_agg;
auto value = std::make_pair(std::move(owned_agg), std::move(col));
_cache[col_idx].emplace(key, std::move(value));
auto owned_agg = agg.clone();
auto const& key = *owned_agg;
auto value = std::make_pair(std::move(owned_agg), std::move(col));
_cache[{input, key}] = std::move(value);
}

column_view result_cache::get_result(size_t col_idx, aggregation const& agg) const
column_view result_cache::get_result(column_view const& input, aggregation const& agg) const
{
CUDF_EXPECTS(has_result(col_idx, agg), "Result does not exist in cache");

auto result_it = _cache[col_idx].find(agg);
auto result_it = _cache.find({input, agg});
karthikeyann marked this conversation as resolved.
Show resolved Hide resolved
CUDF_EXPECTS(result_it != _cache.end(), "Result does not exist in cache");
return result_it->second.second->view();
}

std::unique_ptr<column> result_cache::release_result(size_t col_idx, aggregation const& agg)
std::unique_ptr<column> result_cache::release_result(column_view const& input,
aggregation const& agg)
{
CUDF_EXPECTS(has_result(col_idx, agg), "Result does not exist in cache");

auto result_it = _cache[col_idx].extract(agg);
return std::move(result_it.mapped().second);
auto node = _cache.extract({input, agg});
CUDF_EXPECTS(not node.empty(), "Result does not exist in cache");
return std::move(node.mapped().second);
}

} // namespace detail
Expand Down
20 changes: 18 additions & 2 deletions cpp/src/groupby/common/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <cudf/detail/aggregation/result_cache.hpp>
#include <cudf/detail/groupby.hpp>
#include <cudf/utilities/span.hpp>

#include <memory>
#include <vector>

namespace cudf {
Expand All @@ -30,10 +32,24 @@ inline std::vector<aggregation_result> extract_results(host_span<RequestType con
cudf::detail::result_cache& cache)
{
std::vector<aggregation_result> results(requests.size());

std::unordered_map<std::pair<column_view, std::reference_wrapper<aggregation const>>,
column_view,
cudf::detail::pair_column_aggregation_hash,
cudf::detail::pair_column_aggregation_equal_to>
repeated_result;
for (size_t i = 0; i < requests.size(); i++) {
for (auto&& agg : requests[i].aggregations) {
results[i].results.emplace_back(cache.release_result(i, *agg));
if (cache.has_result(requests[i].values, *agg)) {
results[i].results.emplace_back(cache.release_result(requests[i].values, *agg));
repeated_result[{requests[i].values, *agg}] = results[i].results.back()->view();
} else {
auto it = repeated_result.find({requests[i].values, *agg});
if (it != repeated_result.end()) {
results[i].results.emplace_back(std::make_unique<column>(it->second));
} else {
CUDF_FAIL("Cannot extract result from the cache");
}
karthikeyann marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return results;
Expand Down
Loading