-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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-39978: [C++][Parquet] Expand BYTE_STREAM_SPLIT to support FIXED_LEN_BYTE_ARRAY, INT32 and INT64 #40094
Merged
Merged
GH-39978: [C++][Parquet] Expand BYTE_STREAM_SPLIT to support FIXED_LEN_BYTE_ARRAY, INT32 and INT64 #40094
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,14 @@ | |
|
||
#include "arrow/util/endian.h" | ||
#include "arrow/util/simd.h" | ||
#include "arrow/util/small_vector.h" | ||
#include "arrow/util/ubsan.h" | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cassert> | ||
#include <cstdint> | ||
#include <cstring> | ||
|
||
#if defined(ARROW_HAVE_NEON) || defined(ARROW_HAVE_SSE4_2) | ||
#include <xsimd/xsimd.hpp> | ||
|
@@ -38,10 +41,11 @@ namespace arrow::util::internal { | |
|
||
#if defined(ARROW_HAVE_NEON) || defined(ARROW_HAVE_SSE4_2) | ||
template <int kNumStreams> | ||
void ByteStreamSplitDecodeSimd128(const uint8_t* data, int64_t num_values, int64_t stride, | ||
uint8_t* out) { | ||
void ByteStreamSplitDecodeSimd128(const uint8_t* data, int width, int64_t num_values, | ||
int64_t stride, uint8_t* out) { | ||
using simd_batch = xsimd::make_sized_batch_t<int8_t, 16>; | ||
|
||
assert(width == kNumStreams); | ||
static_assert(kNumStreams == 4 || kNumStreams == 8, "Invalid number of streams."); | ||
constexpr int kNumStreamsLog2 = (kNumStreams == 8 ? 3 : 2); | ||
constexpr int64_t kBlockSize = sizeof(simd_batch) * kNumStreams; | ||
|
@@ -92,10 +96,11 @@ void ByteStreamSplitDecodeSimd128(const uint8_t* data, int64_t num_values, int64 | |
} | ||
|
||
template <int kNumStreams> | ||
void ByteStreamSplitEncodeSimd128(const uint8_t* raw_values, const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
void ByteStreamSplitEncodeSimd128(const uint8_t* raw_values, int width, | ||
const int64_t num_values, uint8_t* output_buffer_raw) { | ||
using simd_batch = xsimd::make_sized_batch_t<int8_t, 16>; | ||
|
||
assert(width == kNumStreams); | ||
static_assert(kNumStreams == 4 || kNumStreams == 8, "Invalid number of streams."); | ||
constexpr int kBlockSize = sizeof(simd_batch) * kNumStreams; | ||
|
||
|
@@ -215,15 +220,17 @@ void ByteStreamSplitEncodeSimd128(const uint8_t* raw_values, const int64_t num_v | |
|
||
#if defined(ARROW_HAVE_AVX2) | ||
template <int kNumStreams> | ||
void ByteStreamSplitDecodeAvx2(const uint8_t* data, int64_t num_values, int64_t stride, | ||
uint8_t* out) { | ||
void ByteStreamSplitDecodeAvx2(const uint8_t* data, int width, int64_t num_values, | ||
int64_t stride, uint8_t* out) { | ||
assert(width == kNumStreams); | ||
static_assert(kNumStreams == 4 || kNumStreams == 8, "Invalid number of streams."); | ||
constexpr int kNumStreamsLog2 = (kNumStreams == 8 ? 3 : 2); | ||
constexpr int64_t kBlockSize = sizeof(__m256i) * kNumStreams; | ||
|
||
const int64_t size = num_values * kNumStreams; | ||
if (size < kBlockSize) // Back to SSE for small size | ||
return ByteStreamSplitDecodeSimd128<kNumStreams>(data, num_values, stride, out); | ||
return ByteStreamSplitDecodeSimd128<kNumStreams>(data, width, num_values, stride, | ||
out); | ||
const int64_t num_blocks = size / kBlockSize; | ||
|
||
// First handle suffix. | ||
|
@@ -299,18 +306,19 @@ void ByteStreamSplitDecodeAvx2(const uint8_t* data, int64_t num_values, int64_t | |
} | ||
|
||
template <int kNumStreams> | ||
void ByteStreamSplitEncodeAvx2(const uint8_t* raw_values, const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
void ByteStreamSplitEncodeAvx2(const uint8_t* raw_values, int width, | ||
const int64_t num_values, uint8_t* output_buffer_raw) { | ||
assert(width == kNumStreams); | ||
static_assert(kNumStreams == 4 || kNumStreams == 8, "Invalid number of streams."); | ||
constexpr int kBlockSize = sizeof(__m256i) * kNumStreams; | ||
|
||
if constexpr (kNumStreams == 8) // Back to SSE, currently no path for double. | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, num_values, | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, width, num_values, | ||
output_buffer_raw); | ||
|
||
const int64_t size = num_values * kNumStreams; | ||
if (size < kBlockSize) // Back to SSE for small size | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, num_values, | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, width, num_values, | ||
output_buffer_raw); | ||
const int64_t num_blocks = size / kBlockSize; | ||
const __m256i* raw_values_simd = reinterpret_cast<const __m256i*>(raw_values); | ||
|
@@ -373,25 +381,26 @@ void ByteStreamSplitEncodeAvx2(const uint8_t* raw_values, const int64_t num_valu | |
|
||
#if defined(ARROW_HAVE_SIMD_SPLIT) | ||
template <int kNumStreams> | ||
void inline ByteStreamSplitDecodeSimd(const uint8_t* data, int64_t num_values, | ||
void inline ByteStreamSplitDecodeSimd(const uint8_t* data, int width, int64_t num_values, | ||
int64_t stride, uint8_t* out) { | ||
#if defined(ARROW_HAVE_AVX2) | ||
return ByteStreamSplitDecodeAvx2<kNumStreams>(data, num_values, stride, out); | ||
return ByteStreamSplitDecodeAvx2<kNumStreams>(data, width, num_values, stride, out); | ||
#elif defined(ARROW_HAVE_SSE4_2) || defined(ARROW_HAVE_NEON) | ||
return ByteStreamSplitDecodeSimd128<kNumStreams>(data, num_values, stride, out); | ||
return ByteStreamSplitDecodeSimd128<kNumStreams>(data, width, num_values, stride, out); | ||
#else | ||
#error "ByteStreamSplitDecodeSimd not implemented" | ||
#endif | ||
} | ||
|
||
template <int kNumStreams> | ||
void inline ByteStreamSplitEncodeSimd(const uint8_t* raw_values, const int64_t num_values, | ||
void inline ByteStreamSplitEncodeSimd(const uint8_t* raw_values, int width, | ||
const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
#if defined(ARROW_HAVE_AVX2) | ||
return ByteStreamSplitEncodeAvx2<kNumStreams>(raw_values, num_values, | ||
return ByteStreamSplitEncodeAvx2<kNumStreams>(raw_values, width, num_values, | ||
output_buffer_raw); | ||
#elif defined(ARROW_HAVE_SSE4_2) || defined(ARROW_HAVE_NEON) | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, num_values, | ||
return ByteStreamSplitEncodeSimd128<kNumStreams>(raw_values, width, num_values, | ||
output_buffer_raw); | ||
#else | ||
#error "ByteStreamSplitEncodeSimd not implemented" | ||
|
@@ -492,45 +501,94 @@ inline void DoMergeStreams(const uint8_t** src_streams, int width, int64_t nvalu | |
} | ||
|
||
template <int kNumStreams> | ||
void ByteStreamSplitEncodeScalar(const uint8_t* raw_values, const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
void ByteStreamSplitEncodeScalar(const uint8_t* raw_values, int width, | ||
const int64_t num_values, uint8_t* out) { | ||
assert(width == kNumStreams); | ||
std::array<uint8_t*, kNumStreams> dest_streams; | ||
for (int stream = 0; stream < kNumStreams; ++stream) { | ||
dest_streams[stream] = &output_buffer_raw[stream * num_values]; | ||
dest_streams[stream] = &out[stream * num_values]; | ||
} | ||
DoSplitStreams(raw_values, kNumStreams, num_values, dest_streams.data()); | ||
} | ||
|
||
inline void ByteStreamSplitEncodeScalarDynamic(const uint8_t* raw_values, int width, | ||
const int64_t num_values, uint8_t* out) { | ||
::arrow::internal::SmallVector<uint8_t*, 16> dest_streams; | ||
dest_streams.resize(width); | ||
for (int stream = 0; stream < width; ++stream) { | ||
dest_streams[stream] = &out[stream * num_values]; | ||
} | ||
DoSplitStreams(raw_values, width, num_values, dest_streams.data()); | ||
} | ||
|
||
template <int kNumStreams> | ||
void ByteStreamSplitDecodeScalar(const uint8_t* data, int64_t num_values, int64_t stride, | ||
uint8_t* out) { | ||
void ByteStreamSplitDecodeScalar(const uint8_t* data, int width, int64_t num_values, | ||
int64_t stride, uint8_t* out) { | ||
assert(width == kNumStreams); | ||
std::array<const uint8_t*, kNumStreams> src_streams; | ||
for (int stream = 0; stream < kNumStreams; ++stream) { | ||
src_streams[stream] = &data[stream * stride]; | ||
} | ||
DoMergeStreams(src_streams.data(), kNumStreams, num_values, out); | ||
} | ||
|
||
template <int kNumStreams> | ||
void inline ByteStreamSplitEncode(const uint8_t* raw_values, const int64_t num_values, | ||
uint8_t* output_buffer_raw) { | ||
inline void ByteStreamSplitDecodeScalarDynamic(const uint8_t* data, int width, | ||
int64_t num_values, int64_t stride, | ||
uint8_t* out) { | ||
::arrow::internal::SmallVector<const uint8_t*, 16> src_streams; | ||
src_streams.resize(width); | ||
for (int stream = 0; stream < width; ++stream) { | ||
src_streams[stream] = &data[stream * stride]; | ||
} | ||
DoMergeStreams(src_streams.data(), width, num_values, out); | ||
} | ||
|
||
inline void ByteStreamSplitEncode(const uint8_t* raw_values, int width, | ||
const int64_t num_values, uint8_t* out) { | ||
#if defined(ARROW_HAVE_SIMD_SPLIT) | ||
return ByteStreamSplitEncodeSimd<kNumStreams>(raw_values, num_values, | ||
output_buffer_raw); | ||
#define ByteStreamSplitEncodePerhapsSimd ByteStreamSplitEncodeSimd | ||
#else | ||
return ByteStreamSplitEncodeScalar<kNumStreams>(raw_values, num_values, | ||
output_buffer_raw); | ||
#define ByteStreamSplitEncodePerhapsSimd ByteStreamSplitEncodeScalar | ||
#endif | ||
switch (width) { | ||
case 1: | ||
memcpy(out, raw_values, num_values); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ok with this, but seems it's equal to PLAIN 🤔? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, yes, by definition. |
||
return; | ||
case 2: | ||
return ByteStreamSplitEncodeScalar<2>(raw_values, width, num_values, out); | ||
case 4: | ||
return ByteStreamSplitEncodePerhapsSimd<4>(raw_values, width, num_values, out); | ||
case 8: | ||
return ByteStreamSplitEncodePerhapsSimd<8>(raw_values, width, num_values, out); | ||
case 16: | ||
return ByteStreamSplitEncodeScalar<16>(raw_values, width, num_values, out); | ||
} | ||
return ByteStreamSplitEncodeScalarDynamic(raw_values, width, num_values, out); | ||
#undef ByteStreamSplitEncodePerhapsSimd | ||
} | ||
|
||
template <int kNumStreams> | ||
void inline ByteStreamSplitDecode(const uint8_t* data, int64_t num_values, int64_t stride, | ||
uint8_t* out) { | ||
inline void ByteStreamSplitDecode(const uint8_t* data, int width, int64_t num_values, | ||
int64_t stride, uint8_t* out) { | ||
#if defined(ARROW_HAVE_SIMD_SPLIT) | ||
return ByteStreamSplitDecodeSimd<kNumStreams>(data, num_values, stride, out); | ||
#define ByteStreamSplitDecodePerhapsSimd ByteStreamSplitDecodeSimd | ||
#else | ||
return ByteStreamSplitDecodeScalar<kNumStreams>(data, num_values, stride, out); | ||
#define ByteStreamSplitDecodePerhapsSimd ByteStreamSplitDecodeScalar | ||
#endif | ||
switch (width) { | ||
case 1: | ||
memcpy(out, data, num_values); | ||
return; | ||
case 2: | ||
return ByteStreamSplitDecodeScalar<2>(data, width, num_values, stride, out); | ||
case 4: | ||
return ByteStreamSplitDecodePerhapsSimd<4>(data, width, num_values, stride, out); | ||
case 8: | ||
return ByteStreamSplitDecodePerhapsSimd<8>(data, width, num_values, stride, out); | ||
case 16: | ||
return ByteStreamSplitDecodeScalar<16>(data, width, num_values, stride, out); | ||
} | ||
return ByteStreamSplitDecodeScalarDynamic(data, width, num_values, stride, out); | ||
#undef ByteStreamSplitDecodePerhapsSimd | ||
} | ||
|
||
} // 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.
Would this benifits performance?
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.
Potentially, though perhaps not a in a micro-benchmark where allocations are reused efficiently.