-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
GH-38542: [C++][Parquet] Faster scalar BYTE_STREAM_SPLIT #38529
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9a96ab9
[C++][Parquet] Faster scalar BYTE_STREAM_SPLIT
pitrou 063e5ed
Make byte_stream_split.h internal
pitrou 659b27a
Use safe unaligned loads and stores
pitrou 761de29
Nit: using int64_t not size_t
pitrou 965e8e2
Add tests for both SIMD and scalar implementations
pitrou 1b25f35
Remove outdated comment
pitrou File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstddef> | ||
#include <functional> | ||
#include <iostream> | ||
#include <memory> | ||
#include <random> | ||
#include <string> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "arrow/testing/gtest_util.h" | ||
#include "arrow/testing/util.h" | ||
#include "arrow/util/byte_stream_split_internal.h" | ||
|
||
namespace arrow::util::internal { | ||
|
||
using ByteStreamSplitTypes = ::testing::Types<float, double>; | ||
|
||
template <typename Func> | ||
struct NamedFunc { | ||
std::string name; | ||
Func func; | ||
|
||
friend std::ostream& operator<<(std::ostream& os, const NamedFunc& func) { | ||
os << func.name; | ||
return os; | ||
} | ||
}; | ||
|
||
// A simplistic reference implementation for validation | ||
void RefererenceByteStreamSplitEncode(const uint8_t* src, int width, | ||
const int64_t num_values, uint8_t* dest) { | ||
for (int64_t i = 0; i < num_values; ++i) { | ||
for (int stream = 0; stream < width; ++stream) { | ||
dest[stream * num_values + i] = *src++; | ||
} | ||
} | ||
} | ||
|
||
template <typename T> | ||
class TestByteStreamSplitSpecialized : public ::testing::Test { | ||
public: | ||
using EncodeFunc = NamedFunc<std::function<decltype(ByteStreamSplitEncode<T>)>>; | ||
using DecodeFunc = NamedFunc<std::function<decltype(ByteStreamSplitDecode<T>)>>; | ||
|
||
static constexpr int kWidth = static_cast<int>(sizeof(T)); | ||
|
||
void SetUp() override { | ||
encode_funcs_.push_back({"reference", &ReferenceEncode}); | ||
encode_funcs_.push_back({"scalar", &ByteStreamSplitEncodeScalar<T>}); | ||
decode_funcs_.push_back({"scalar", &ByteStreamSplitDecodeScalar<T>}); | ||
#if defined(ARROW_HAVE_SIMD_SPLIT) | ||
encode_funcs_.push_back({"simd", &ByteStreamSplitEncodeSimd<T>}); | ||
decode_funcs_.push_back({"simd", &ByteStreamSplitDecodeSimd<T>}); | ||
#endif | ||
} | ||
|
||
void TestRoundtrip(int64_t num_values) { | ||
// Test one-shot roundtrip among all encode/decode function combinations | ||
ARROW_SCOPED_TRACE("num_values = ", num_values); | ||
const auto input = MakeRandomInput(num_values); | ||
std::vector<uint8_t> encoded(num_values * kWidth); | ||
std::vector<T> decoded(num_values); | ||
|
||
for (const auto& encode_func : encode_funcs_) { | ||
ARROW_SCOPED_TRACE("encode_func = ", encode_func); | ||
encoded.assign(encoded.size(), 0); | ||
encode_func.func(reinterpret_cast<const uint8_t*>(input.data()), num_values, | ||
encoded.data()); | ||
for (const auto& decode_func : decode_funcs_) { | ||
ARROW_SCOPED_TRACE("decode_func = ", decode_func); | ||
decoded.assign(decoded.size(), T{}); | ||
decode_func.func(encoded.data(), num_values, /*stride=*/num_values, | ||
decoded.data()); | ||
ASSERT_EQ(decoded, input); | ||
} | ||
} | ||
} | ||
|
||
void TestPiecewiseDecode(int64_t num_values) { | ||
// Test chunked decoding against the reference encode function | ||
ARROW_SCOPED_TRACE("num_values = ", num_values); | ||
const auto input = MakeRandomInput(num_values); | ||
std::vector<uint8_t> encoded(num_values * kWidth); | ||
ReferenceEncode(reinterpret_cast<const uint8_t*>(input.data()), num_values, | ||
encoded.data()); | ||
std::vector<T> decoded(num_values); | ||
|
||
std::default_random_engine gen(seed_++); | ||
std::uniform_int_distribution<int64_t> chunk_size_dist(1, 123); | ||
|
||
for (const auto& decode_func : decode_funcs_) { | ||
ARROW_SCOPED_TRACE("decode_func = ", decode_func); | ||
decoded.assign(decoded.size(), T{}); | ||
|
||
int64_t offset = 0; | ||
while (offset < num_values) { | ||
auto chunk_size = std::min<int64_t>(num_values - offset, chunk_size_dist(gen)); | ||
decode_func.func(encoded.data() + offset, chunk_size, /*stride=*/num_values, | ||
decoded.data() + offset); | ||
offset += chunk_size; | ||
} | ||
ASSERT_EQ(offset, num_values); | ||
ASSERT_EQ(decoded, input); | ||
} | ||
} | ||
|
||
protected: | ||
static void ReferenceEncode(const uint8_t* raw_values, const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
RefererenceByteStreamSplitEncode(raw_values, kWidth, num_values, output_buffer_raw); | ||
} | ||
|
||
static std::vector<T> MakeRandomInput(int64_t num_values) { | ||
std::vector<T> input(num_values); | ||
random_bytes(kWidth * num_values, seed_++, reinterpret_cast<uint8_t*>(input.data())); | ||
// Avoid NaNs to ease comparison | ||
for (auto& value : input) { | ||
if (std::isnan(value)) { | ||
value = nan_replacement_++; | ||
} | ||
} | ||
return input; | ||
} | ||
|
||
std::vector<EncodeFunc> encode_funcs_; | ||
std::vector<DecodeFunc> decode_funcs_; | ||
|
||
static inline uint32_t seed_ = 42; | ||
static inline T nan_replacement_ = 0; | ||
}; | ||
|
||
TYPED_TEST_SUITE(TestByteStreamSplitSpecialized, ByteStreamSplitTypes); | ||
|
||
TYPED_TEST(TestByteStreamSplitSpecialized, RoundtripSmall) { | ||
for (int64_t num_values : {1, 5, 7, 12, 19, 31, 32}) { | ||
this->TestRoundtrip(num_values); | ||
} | ||
} | ||
|
||
TYPED_TEST(TestByteStreamSplitSpecialized, RoundtripMidsized) { | ||
for (int64_t num_values : {126, 127, 128, 129, 133, 200}) { | ||
this->TestRoundtrip(num_values); | ||
} | ||
} | ||
|
||
TYPED_TEST(TestByteStreamSplitSpecialized, PiecewiseDecode) { | ||
this->TestPiecewiseDecode(/*num_values=*/500); | ||
} | ||
|
||
} // namespace arrow::util::internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: do we need
DCHECK
thewidth
is expected?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
width
can only be 4/8 now right. After Fp16 is introduced, we might supports 2/4/8, other value should be rejected?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this function should support any non-zero
width
, so I don't think it is necessary to reject anything here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmmm here it only supports FLOAT/DOUBLE?
https://github.com/apache/parquet-format/blob/master/Encodings.md#byte-stream-split-byte_stream_split--9
But I think it's also ok when
width > 0
. It's ok to me if you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, Parquet only supports FLOAT/DOUBLE, but there is no reason for this function to be restricted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree not to restrict the width unless required. This is now a common util not specific to parquet.