Skip to content

Commit 11c97b8

Browse files
AntoinePrvpitrou
andauthored
GH-46962: [C++][Parquet] Generic xsimd function and dynamic dispatch for Byte Stream Split (#46963)
Thanks for opening a pull request! ### Rationale for this change Lot of linux systems ship arrow with SSE4.2, but the AVX2 instructions are quite available. For byte stream split, they are faster than SSE4.2. ### What changes are included in this PR? - Make the xsimd functions refactored in #46789 to make them arch independent. - Use dynamic dispatch to AVX2 at runtime if available (it was considered that builds without SSE4.2 or Neon at compile time were not so popular to add them to the dynamic dispatch). ### Are these changes tested? Yes, the exisiting tests already cover the code. ### Are there any user-facing changes? No * GitHub Issue: #46962 Lead-authored-by: AntoinePrv <AntoinePrv@users.noreply.github.com> Co-authored-by: Antoine Pitrou <antoine@python.org> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent f12356a commit 11c97b8

15 files changed

+533
-303
lines changed

ci/scripts/python_wheel_macos_build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ cmake \
147147
-DPARQUET_REQUIRE_ENCRYPTION=${PARQUET_REQUIRE_ENCRYPTION} \
148148
-DVCPKG_MANIFEST_MODE=OFF \
149149
-DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET} \
150+
-Dxsimd_SOURCE=BUNDLED \
150151
-G ${CMAKE_GENERATOR} \
151152
${source_dir}/cpp
152153
cmake --build . --target install

ci/scripts/python_wheel_windows_build.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ cmake ^
101101
-DPARQUET_REQUIRE_ENCRYPTION=%PARQUET_REQUIRE_ENCRYPTION% ^
102102
-DVCPKG_MANIFEST_MODE=OFF ^
103103
-DVCPKG_TARGET_TRIPLET=%VCGPK_TARGET_TRIPLET% ^
104+
-Dxsimd_SOURCE=BUNDLED ^
104105
-G "%CMAKE_GENERATOR%" ^
105106
-A "%CMAKE_PLATFORM%" ^
106107
C:\arrow\cpp || exit /B 1

ci/scripts/python_wheel_xlinux_build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ cmake \
128128
-DPARQUET_REQUIRE_ENCRYPTION=${PARQUET_REQUIRE_ENCRYPTION} \
129129
-DVCPKG_MANIFEST_MODE=OFF \
130130
-DVCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET} \
131+
-Dxsimd_SOURCE=BUNDLED \
131132
${ARROW_EXTRA_CMAKE_FLAGS} \
132133
-G ${CMAKE_GENERATOR} \
133134
/arrow/cpp

ci/vcpkg/vcpkg.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"re2",
2020
"snappy",
2121
"utf8proc",
22-
"xsimd",
2322
"zlib",
2423
"zstd",
2524
{

cpp/cmake_modules/SetupCxxFlags.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ endif()
4949
if(ARROW_CPU_FLAG STREQUAL "x86")
5050
# x86/amd64 compiler flags, msvc/gcc/clang
5151
if(MSVC)
52-
set(ARROW_SSE4_2_FLAG "")
52+
set(ARROW_SSE4_2_FLAG "/arch:SSE4.2")
53+
# These definitions are needed for xsimd to consider the corresponding instruction
54+
# sets available, but they are not set by MSVC (unlike other compilers).
55+
# See https://github.com/AcademySoftwareFoundation/OpenImageIO/issues/4265
56+
add_definitions(-D__SSE2__ -D__SSE4_1__ -D__SSE4_2__)
5357
set(ARROW_AVX2_FLAG "/arch:AVX2")
5458
# MSVC has no specific flag for BMI2, it seems to be enabled with AVX2
5559
set(ARROW_BMI2_FLAG "/arch:AVX2")

cpp/src/arrow/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ set(ARROW_UTIL_SRCS
491491
util/bitmap_ops.cc
492492
util/bpacking.cc
493493
util/byte_size.cc
494+
util/byte_stream_split_internal.cc
494495
util/cancel.cc
495496
util/compression.cc
496497
util/counting_semaphore.cc
@@ -530,6 +531,8 @@ set(ARROW_UTIL_SRCS
530531
util/utf8.cc
531532
util/value_parsing.cc)
532533

534+
append_runtime_avx2_src(ARROW_UTIL_SRCS util/byte_stream_split_internal_avx2.cc)
535+
533536
append_runtime_avx2_src(ARROW_UTIL_SRCS util/bpacking_avx2.cc)
534537
append_runtime_avx512_src(ARROW_UTIL_SRCS util/bpacking_avx512.cc)
535538
if(ARROW_HAVE_NEON)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#include "arrow/util/byte_stream_split_internal.h"
19+
#include "arrow/util/dispatch_internal.h"
20+
21+
#include <array>
22+
23+
namespace arrow::util::internal {
24+
25+
using ::arrow::internal::DispatchLevel;
26+
using ::arrow::internal::DynamicDispatch;
27+
28+
/************************
29+
* Decode dispatching *
30+
************************/
31+
32+
template <int kNumStreams>
33+
struct ByteStreamSplitDecodeDynamic {
34+
using FunctionType = decltype(&ByteStreamSplitDecodeScalar<kNumStreams>);
35+
using Implementation = std::pair<DispatchLevel, FunctionType>;
36+
37+
constexpr static auto implementations() {
38+
return std::array {
39+
Implementation {
40+
DispatchLevel::NONE,
41+
#if defined(ARROW_HAVE_NEON)
42+
// We always expect Neon to be available on Arm64
43+
&ByteStreamSplitDecodeSimd<xsimd::neon64, kNumStreams>,
44+
#elif defined(ARROW_HAVE_SSE4_2)
45+
// We always expect SSE4.2 to be available on x86_64
46+
&ByteStreamSplitDecodeSimd<xsimd::sse4_2, kNumStreams>,
47+
#else
48+
&ByteStreamSplitDecodeScalar<kNumStreams>,
49+
#endif
50+
}
51+
,
52+
#if defined(ARROW_HAVE_RUNTIME_AVX2)
53+
Implementation{
54+
DispatchLevel::AVX2,
55+
&ByteStreamSplitDecodeSimd<xsimd::avx2, kNumStreams>,
56+
},
57+
#endif
58+
};
59+
}
60+
};
61+
62+
template <int kNumStreams>
63+
void ByteStreamSplitDecodeSimdDispatch(const uint8_t* data, int width, int64_t num_values,
64+
int64_t stride, uint8_t* out) {
65+
static const DynamicDispatch<ByteStreamSplitDecodeDynamic<kNumStreams>> dispatch;
66+
return dispatch.func(data, width, num_values, stride, out);
67+
}
68+
69+
template void ByteStreamSplitDecodeSimdDispatch<2>(const uint8_t*, int, int64_t, int64_t,
70+
uint8_t*);
71+
template void ByteStreamSplitDecodeSimdDispatch<4>(const uint8_t*, int, int64_t, int64_t,
72+
uint8_t*);
73+
template void ByteStreamSplitDecodeSimdDispatch<8>(const uint8_t*, int, int64_t, int64_t,
74+
uint8_t*);
75+
76+
/************************
77+
* Encode dispatching *
78+
************************/
79+
80+
template <int kNumStreams>
81+
struct ByteStreamSplitEncodeDynamic {
82+
using FunctionType = decltype(&ByteStreamSplitEncodeScalar<kNumStreams>);
83+
using Implementation = std::pair<DispatchLevel, FunctionType>;
84+
85+
constexpr static auto implementations() {
86+
return std::array {
87+
Implementation {
88+
DispatchLevel::NONE,
89+
#if defined(ARROW_HAVE_NEON)
90+
// We always expect Neon to be available on Arm64
91+
&ByteStreamSplitEncodeSimd<xsimd::neon64, kNumStreams>,
92+
#elif defined(ARROW_HAVE_SSE4_2)
93+
// We always expect SSE4.2 to be available on x86_64
94+
&ByteStreamSplitEncodeSimd<xsimd::sse4_2, kNumStreams>,
95+
#else
96+
&ByteStreamSplitEncodeScalar<kNumStreams>,
97+
#endif
98+
}
99+
,
100+
#if defined(ARROW_HAVE_RUNTIME_AVX2)
101+
Implementation{DispatchLevel::AVX2, &ByteStreamSplitEncodeAvx2<kNumStreams>},
102+
#endif
103+
};
104+
}
105+
};
106+
107+
template <int kNumStreams>
108+
void ByteStreamSplitEncodeSimdDispatch(const uint8_t* raw_values, int width,
109+
const int64_t num_values,
110+
uint8_t* output_buffer_raw) {
111+
static const DynamicDispatch<ByteStreamSplitEncodeDynamic<kNumStreams>> dispatch;
112+
return dispatch.func(raw_values, width, num_values, output_buffer_raw);
113+
}
114+
115+
template void ByteStreamSplitEncodeSimdDispatch<2>(const uint8_t*, int, const int64_t,
116+
uint8_t*);
117+
template void ByteStreamSplitEncodeSimdDispatch<4>(const uint8_t*, int, const int64_t,
118+
uint8_t*);
119+
template void ByteStreamSplitEncodeSimdDispatch<8>(const uint8_t*, int, const int64_t,
120+
uint8_t*);
121+
122+
} // namespace arrow::util::internal

0 commit comments

Comments
 (0)