Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
25 changes: 25 additions & 0 deletions cpp/include/cudf/io/parquet_io_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <rmm/device_buffer.hpp>
#include <rmm/resource_ref.hpp>

#include <functional>
#include <future>
#include <tuple>
#include <vector>
Expand Down Expand Up @@ -44,6 +45,17 @@ using cudf::io::text::byte_range_info;
std::unique_ptr<cudf::io::datasource::buffer> fetch_footer_to_host(
cudf::io::datasource& datasource);

/**
* @brief Fetches host buffers of Parquet footer bytes from multiple input data sources
*
* @ingroup io_utils
*
Comment thread
mhaseeb123 marked this conversation as resolved.
Resolved
* @param datasources Input data sources
* @return Vector of host buffers containing footer bytes, one per datasource
*/
std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_footers_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

/**
* @brief Fetches a host buffer of Parquet page index from the input data source
*
Expand All @@ -56,6 +68,19 @@ std::unique_ptr<cudf::io::datasource::buffer> fetch_footer_to_host(
std::unique_ptr<cudf::io::datasource::buffer> fetch_page_index_to_host(
cudf::io::datasource& datasource, byte_range_info const page_index_bytes);

/**
* @brief Fetches host buffers of Parquet page index bytes from multiple input data sources
*
* @ingroup io_utils
*
Comment thread
mhaseeb123 marked this conversation as resolved.
Resolved
* @param datasources Input datasources
* @param page_index_bytes_per_source Byte ranges of page index, one per datasource
* @return Vector of host buffers containing page index bytes, one per datasource
*/
std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_page_indexes_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources,
cudf::host_span<byte_range_info const> page_index_bytes_per_source);

/**
* @brief Fetches a list of byte ranges from a datasource into device buffers
*
Expand Down
150 changes: 134 additions & 16 deletions cpp/src/io/parquet/io_utils/parquet_io_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "io/comp/common.hpp"
#include "io/parquet/parquet_common.hpp"

#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/utilities/cuda_memcpy.hpp>
#include <cudf/detail/utilities/host_worker_pool.hpp>
#include <cudf/detail/utilities/integer_utils.hpp>
Expand All @@ -21,6 +22,8 @@
#include <cuda/iterator>
#include <cuda/std/tuple>

#include <functional>
#include <mutex>
Comment thread
mhaseeb123 marked this conversation as resolved.
Resolved
#include <numeric>
#include <tuple>

Expand All @@ -31,29 +34,144 @@

namespace cudf::io::parquet {

namespace detail {

auto constexpr parallel_threshold = 16;

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_footers_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources)
{
// Helper to fetch footer from a datasource

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper (fetches one footer) dispatched for all sources in a loop or via thread pool if more than parallel_threshold

auto const fetch_footer = [](cudf::io::datasource& datasource) {
constexpr auto header_len = sizeof(file_header_s);
constexpr auto ender_len = sizeof(file_ender_s);
size_t const len = datasource.size();

auto header_buffer = datasource.host_read(0, header_len);
auto const header = reinterpret_cast<file_header_s const*>(header_buffer->data());
auto ender_buffer = datasource.host_read(len - ender_len, ender_len);
auto const ender = reinterpret_cast<file_ender_s const*>(ender_buffer->data());
CUDF_EXPECTS(len > header_len + ender_len, "Incorrect data source");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
CUDF_EXPECTS(header->magic == parquet_magic, "Corrupted header");
CUDF_EXPECTS(ender->magic == parquet_magic, "Corrupted footer");
CUDF_EXPECTS(ender->footer_len != 0 && ender->footer_len <= (len - header_len - ender_len),
"Incorrect footer length");

return datasource.host_read(len - ender->footer_len - ender_len, ender->footer_len);
};

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> footer_buffers;
footer_buffers.reserve(datasources.size());
auto const num_sources = datasources.size();

if (num_sources < parallel_threshold) {
// Read footers sequentially to avoid task dispatch overhead
std::transform(datasources.begin(),
datasources.end(),
std::back_inserter(footer_buffers),
[&](auto const& datasource_ref) { return fetch_footer(datasource_ref.get()); });
} else {
// Read footers in parallel
std::vector<std::future<std::unique_ptr<cudf::io::datasource::buffer>>> tasks;
tasks.reserve(datasources.size());
for (auto const& datasource_ref : datasources) {
tasks.emplace_back(cudf::detail::host_worker_pool().submit_task(
[&datasource = datasource_ref.get(), &fetch_footer]() {
return fetch_footer(datasource);
}));
}
std::transform(tasks.begin(), tasks.end(), std::back_inserter(footer_buffers), [](auto& task) {
return task.get();
});
}
return footer_buffers;
}

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_page_indexes_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to start complying with the recommendations in #22588?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. Waiting for this reader to be feature complete (few PRs related to this) before I pull the trigger on the entire thing.

cudf::host_span<cudf::io::text::byte_range_info const> page_index_bytes_per_source)
{
CUDF_EXPECTS(datasources.size() == page_index_bytes_per_source.size(),
"Encountered mismatch in number of datasources and page index byte ranges");

// Helper to fetch page index bytes from a datasource
auto const fetch_page_index = [](cudf::io::datasource& datasource,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper (fetches one page index bytes) dispatched for all sources in a loop or via thread pool if more than parallel_threshold

cudf::io::text::byte_range_info const& page_index_bytes) {
return datasource.host_read(page_index_bytes.offset(), page_index_bytes.size());
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> page_index_buffers;
page_index_buffers.reserve(datasources.size());

auto const num_sources = datasources.size();
auto iter = cuda::make_zip_iterator(datasources.begin(), page_index_bytes_per_source.begin());

if (num_sources < parallel_threshold) {
// Read page indexes sequentially to avoid task dispatch overhead
std::transform(
iter, iter + datasources.size(), std::back_inserter(page_index_buffers), [&](auto const& t) {
return fetch_page_index(cuda::std::get<0>(t).get(), cuda::std::get<1>(t));
});
} else {
// Read page indexes in parallel
std::vector<std::future<std::unique_ptr<cudf::io::datasource::buffer>>> tasks;
tasks.reserve(datasources.size());
std::for_each(iter, iter + datasources.size(), [&](auto const& tuple) {
auto const& datasource = cuda::std::get<0>(tuple);
auto const& page_index_bytes = cuda::std::get<1>(tuple);
tasks.emplace_back(cudf::detail::host_worker_pool().submit_task(
[&datasource = datasource.get(), page_index_bytes, &fetch_page_index]() {
return fetch_page_index(datasource, page_index_bytes);
}));
});
std::transform(
tasks.begin(), tasks.end(), std::back_inserter(page_index_buffers), [](auto& task) {
return task.get();
});
}
return page_index_buffers;
}

} // namespace detail

std::unique_ptr<cudf::io::datasource::buffer> fetch_footer_to_host(cudf::io::datasource& datasource)
{
constexpr auto header_len = sizeof(file_header_s);
constexpr auto ender_len = sizeof(file_ender_s);
size_t const len = datasource.size();

auto header_buffer = datasource.host_read(0, header_len);
auto const header = reinterpret_cast<file_header_s const*>(header_buffer->data());
auto ender_buffer = datasource.host_read(len - ender_len, ender_len);
auto const ender = reinterpret_cast<file_ender_s const*>(ender_buffer->data());
CUDF_EXPECTS(len > header_len + ender_len, "Incorrect data source");
CUDF_EXPECTS(header->magic == detail::parquet_magic, "Corrupted header");
CUDF_EXPECTS(ender->magic == detail::parquet_magic, "Corrupted footer");
CUDF_EXPECTS(ender->footer_len != 0 && ender->footer_len <= (len - header_len - ender_len),
"Incorrect footer length");

return datasource.host_read(len - ender->footer_len - ender_len, ender->footer_len);
CUDF_FUNC_RANGE();

// Wrap the input into an array and delegate to the detail multi-source API
std::array<std::reference_wrapper<cudf::io::datasource>, 1> datasources{std::ref(datasource)};
auto footer_buffers = detail::fetch_footers_to_host({datasources.data(), datasources.size()});
return std::move(footer_buffers.front());
}

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_footers_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources)
{
CUDF_FUNC_RANGE();
return detail::fetch_footers_to_host(datasources);
}

std::unique_ptr<cudf::io::datasource::buffer> fetch_page_index_to_host(
cudf::io::datasource& datasource, cudf::io::text::byte_range_info const page_index_bytes)
{
return datasource.host_read(page_index_bytes.offset(), page_index_bytes.size());
CUDF_FUNC_RANGE();

// Wrap the inputs into arrays and delegate to the detail multi-source API
std::array<std::reference_wrapper<cudf::io::datasource>, 1> datasources{std::ref(datasource)};
std::array<cudf::io::text::byte_range_info, 1> page_index_bytes_per_source{page_index_bytes};

auto page_index_buffers = detail::fetch_page_indexes_to_host(
{datasources.data(), datasources.size()},
{page_index_bytes_per_source.data(), page_index_bytes_per_source.size()});
return std::move(page_index_buffers.front());
}

std::vector<std::unique_ptr<cudf::io::datasource::buffer>> fetch_page_indexes_to_host(
cudf::host_span<std::reference_wrapper<cudf::io::datasource> const> datasources,
cudf::host_span<cudf::io::text::byte_range_info const> page_index_bytes_per_source)
{
CUDF_FUNC_RANGE();
return detail::fetch_page_indexes_to_host(datasources, page_index_bytes_per_source);
}

std::tuple<std::vector<rmm::device_buffer>,
Expand Down
51 changes: 51 additions & 0 deletions cpp/tests/io/experimental/hybrid_scan_filters_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@

#include <src/io/parquet/parquet_gpu.hpp>

#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <memory>
#include <vector>

namespace {

Expand Down Expand Up @@ -157,6 +161,53 @@ TEST_F(HybridScanFiltersTest, Metadata)
EXPECT_EQ(reader->total_rows_in_row_groups(input_row_group_indices), 2 * rows_per_row_group);
}

TEST_F(HybridScanFiltersTest, MultiSourceFooterAndPageIndex)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These APIs will also be used in tests for PRs implementing #22583 (nothing uses them right now)

{
srand(0xdede);
using T = uint32_t;

auto const file_buffer = std::get<1>(create_parquet_with_stats<T, 1>());

auto constexpr num_sources = 3;
auto const datasource = cudf::io::datasource::create(cudf::host_span<std::byte const>(
reinterpret_cast<std::byte const*>(file_buffer.data()), file_buffer.size()));
std::vector<std::reference_wrapper<cudf::io::datasource>> datasources(num_sources,
std::ref(*datasource));

// Fetch all footers at once
auto const footer_buffers =
cudf::io::parquet::fetch_footers_to_host({datasources.data(), datasources.size()});
ASSERT_EQ(footer_buffers.size(), num_sources);

// Fetch all page indexes at once
auto const reader = std::make_unique<cudf::io::parquet::experimental::hybrid_scan_reader>(
cudf::host_span<uint8_t const>{static_cast<uint8_t const*>(footer_buffers.front()->data()),
footer_buffers.front()->size()},
cudf::io::parquet_reader_options::builder().build());
std::vector<cudf::io::parquet::byte_range_info> page_index_byte_ranges(
num_sources, reader->page_index_byte_range());
auto const page_index_buffers = cudf::io::parquet::fetch_page_indexes_to_host(
{datasources.data(), datasources.size()},
{page_index_byte_ranges.data(), page_index_byte_ranges.size()});
ASSERT_EQ(page_index_buffers.size(), num_sources);

// Footer and page index from multi-source and single-source APIs should match
auto const single_footer = cudf::io::parquet::fetch_footer_to_host(*datasource);
auto const single_page_index =
cudf::io::parquet::fetch_page_index_to_host(*datasource, reader->page_index_byte_range());

auto const iter = cuda::make_zip_iterator(footer_buffers.begin(), page_index_buffers.begin());
std::for_each(iter, iter + num_sources, [&](auto const& pair) {
auto const& [footer_buffer, page_index_buffer] = pair;
ASSERT_EQ(footer_buffer->size(), single_footer->size());
EXPECT_EQ(std::memcmp(footer_buffer->data(), single_footer->data(), single_footer->size()), 0);
ASSERT_EQ(page_index_buffer->size(), single_page_index->size());
EXPECT_EQ(
std::memcmp(page_index_buffer->data(), single_page_index->data(), single_page_index->size()),
0);
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

TEST_F(HybridScanFiltersTest, ExternalMetadata)
{
srand(0xcaffe);
Expand Down
Loading