pitrou commented on code in PR #46963: URL: https://github.com/apache/arrow/pull/46963#discussion_r2179291264
########## cpp/src/arrow/util/byte_stream_split_internal.h: ########## @@ -584,52 +446,63 @@ inline void ByteStreamSplitDecodeScalarDynamic(const uint8_t* data, int width, DoMergeStreams(src_streams.data(), width, num_values, out); } +template <int kNumStreams> +void ByteStreamSplitDecodeSimdDispatch(const uint8_t* data, int width, int64_t num_values, + int64_t stride, uint8_t* out); + +extern template void ByteStreamSplitDecodeSimdDispatch<2>(const uint8_t*, int, int64_t, Review Comment: You may need `ARROW_TEMPLATE_EXPORT` to fix the Windows linking errors (see CI builds). ########## cpp/src/arrow/util/byte_stream_split_internal.cc: ########## @@ -0,0 +1,120 @@ +// 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 "arrow/util/byte_stream_split_internal.h" +#include "arrow/util/dispatch_internal.h" + +#include <array> + +namespace arrow::util::internal { + +using ::arrow::internal::DispatchLevel; +using ::arrow::internal::DynamicDispatch; + +/************************ + * Decode dispatching * + ************************/ + +template <int kNumStreams> +struct ByteStreamSplitDecodeDynamic { + using FunctionType = decltype(&ByteStreamSplitDecodeScalar<kNumStreams>); + using Implementation = std::pair<DispatchLevel, FunctionType>; + + constexpr static auto implementations() { + return std::array{ + // Limited interest for performance on builds without a minimum SIMD level + // So we put SSE4.2 and NEON as a base level. Review Comment: The comment is incorrect. It's not that there's "limited interest in performance", it's that we always expect SSE4.2 or NEON to be available on the respective architectures (x86, ARM). ########## cpp/src/arrow/util/byte_stream_split_internal.h: ########## @@ -584,52 +446,63 @@ inline void ByteStreamSplitDecodeScalarDynamic(const uint8_t* data, int width, DoMergeStreams(src_streams.data(), width, num_values, out); } +template <int kNumStreams> +void ByteStreamSplitDecodeSimdDispatch(const uint8_t* data, int width, int64_t num_values, + int64_t stride, uint8_t* out); + +extern template void ByteStreamSplitDecodeSimdDispatch<2>(const uint8_t*, int, int64_t, + int64_t, uint8_t*); +extern template void ByteStreamSplitDecodeSimdDispatch<4>(const uint8_t*, int, int64_t, + int64_t, uint8_t*); +extern template void ByteStreamSplitDecodeSimdDispatch<8>(const uint8_t*, int, int64_t, + int64_t, uint8_t*); + +template <int kNumStreams> +void ByteStreamSplitEncodeSimdDispatch(const uint8_t* raw_values, int width, + const int64_t num_values, + uint8_t* output_buffer_raw); + +extern template void ByteStreamSplitEncodeSimdDispatch<2>(const uint8_t*, int, + const int64_t, uint8_t*); +extern template void ByteStreamSplitEncodeSimdDispatch<4>(const uint8_t*, int, + const int64_t, uint8_t*); +extern template void ByteStreamSplitEncodeSimdDispatch<8>(const uint8_t*, int, + const int64_t, uint8_t*); + inline void ByteStreamSplitEncode(const uint8_t* raw_values, int width, const int64_t num_values, uint8_t* out) { -#if defined(ARROW_HAVE_SIMD_SPLIT) -# define ByteStreamSplitEncodePerhapsSimd ByteStreamSplitEncodeSimd -#else -# define ByteStreamSplitEncodePerhapsSimd ByteStreamSplitEncodeScalar -#endif switch (width) { case 1: - memcpy(out, raw_values, num_values); + std::memcpy(out, raw_values, num_values); Review Comment: Is there a reason to prefer `std::memcpy`? My understanding is that the compiler can optimize a `memcpy` call however it wants, but I'm not sure about `std::memcpy`. ########## cpp/src/arrow/util/byte_stream_split_internal.cc: ########## @@ -0,0 +1,120 @@ +// 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 "arrow/util/byte_stream_split_internal.h" +#include "arrow/util/dispatch_internal.h" + +#include <array> + +namespace arrow::util::internal { + +using ::arrow::internal::DispatchLevel; +using ::arrow::internal::DynamicDispatch; + +/************************ + * Decode dispatching * + ************************/ + +template <int kNumStreams> +struct ByteStreamSplitDecodeDynamic { + using FunctionType = decltype(&ByteStreamSplitDecodeScalar<kNumStreams>); + using Implementation = std::pair<DispatchLevel, FunctionType>; + + constexpr static auto implementations() { + return std::array{ + // Limited interest for performance on builds without a minimum SIMD level + // So we put SSE4.2 and NEON as a base level. + std::pair{ Review Comment: Why not `Implementation` here? ########## cpp/src/arrow/util/dispatch_internal.h: ########## @@ -76,7 +76,8 @@ class DynamicDispatch { protected: // Use the Implementation with the highest DispatchLevel - void Resolve(const std::vector<Implementation>& implementations) { + template <typename Range> + void Resolve(const Range& implementations) { Review Comment: Can probably use a universal ref? ```suggestion void Resolve(Range&& implementations) { ``` ########## cpp/src/arrow/util/small_vector.h: ########## @@ -26,6 +26,7 @@ #include <new> #include <type_traits> #include <utility> +#include <vector> Review Comment: I'm curious why this is necessary. Is `std::vector` used in this header? ########## cpp/src/arrow/util/byte_stream_split_test.cc: ########## @@ -143,33 +143,42 @@ class TestByteStreamSplitSpecialized : public ::testing::Test { funcs.push_back({"scalar", &ByteStreamSplitDecodeScalar<kWidth>}); #if defined(ARROW_HAVE_SIMD_SPLIT) if constexpr (kSimdImplemented) { - funcs.push_back({"simd", &ByteStreamSplitDecodeSimd<kWidth>}); - funcs.push_back({"simd128", &ByteStreamSplitDecodeSimd128<kWidth>}); + funcs.push_back({"simd_dispatch", &ByteStreamSplitDecodeSimdDispatch<kWidth>}); +# if defined(ARROW_HAVE_NEON) + funcs.push_back({"xsimd_neon", &ByteStreamSplitDecodeSimd<xsimd::neon64, kWidth>}); +# endif +# if defined(ARROW_HAVE_SSE4_2) + funcs.push_back( + {"xsimd_sse4_2", &ByteStreamSplitDecodeSimd<xsimd::sse4_2, kWidth>}); +# endif # if defined(ARROW_HAVE_AVX2) - // The only available implementations - if constexpr (kWidth == 4 || kWidth == 8) { - funcs.push_back({"avx2", &ByteStreamSplitDecodeAvx2<kWidth>}); - } + funcs.push_back({"xsimd_avx2", &ByteStreamSplitDecodeSimd<xsimd::avx2, kWidth>}); # endif } #endif // defined(ARROW_HAVE_SIMD_SPLIT) return funcs; } - template <bool kSimdImplemented = (kWidth == 4 || kWidth == 8)> + template <bool kSimdImplemented = (kWidth == 2 || kWidth == 4 || kWidth == 8)> static std::vector<EncodeFunc> MakeEncodeFuncs() { std::vector<EncodeFunc> funcs; funcs.push_back({"reference", &ReferenceByteStreamSplitEncode}); funcs.push_back({"scalar_dynamic", &ByteStreamSplitEncodeScalarDynamic}); funcs.push_back({"scalar", &ByteStreamSplitEncodeScalar<kWidth>}); #if defined(ARROW_HAVE_SIMD_SPLIT) if constexpr (kSimdImplemented) { - funcs.push_back({"simd", &ByteStreamSplitEncodeSimd<kWidth>}); - funcs.push_back({"simd128", &ByteStreamSplitEncodeSimd128<kWidth>}); + funcs.push_back({"simd_dispatch", &ByteStreamSplitEncodeSimdDispatch<kWidth>}); +# if defined(ARROW_HAVE_NEON) + funcs.push_back({"xsimd_neon", &ByteStreamSplitEncodeSimd<xsimd::neon64, kWidth>}); +# endif +# if defined(ARROW_HAVE_SSE4_2) + funcs.push_back( + {"xsimd_sse4_2", &ByteStreamSplitEncodeSimd<xsimd::sse4_2, kWidth>}); +# endif # if defined(ARROW_HAVE_AVX2) - // The only available implementation + funcs.push_back({"xsimd_avx2", &ByteStreamSplitEncodeSimd<xsimd::sse4_2, kWidth>}); Review Comment: Do you mean `xsimd::avx2` here? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org