pitrou commented on code in PR #50030:
URL: https://github.com/apache/arrow/pull/50030#discussion_r3332764920


##########
cpp/src/parquet/bloom_filter_block_inc.h:
##########
@@ -0,0 +1,38 @@
+// 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.
+#pragma once
+
+#include <cstdint>
+
+// Used to make sure ODR rule isn't violated.
+#ifndef PARQUET_IMPL_NAMESPACE
+#  error "PARQUET_IMPL_NAMESPACE must be defined"
+#endif

Review Comment:
   There's no reason for ODR rule to be violated as this include is only 
compiled in once (AVX2 dynamic dispatch doesn't involve this file).



##########
cpp/src/parquet/bloom_filter_avx2.cc:
##########
@@ -0,0 +1,47 @@
+// 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 <xsimd/xsimd.hpp>
+
+#include "parquet/bloom_filter_avx2_internal.h"
+
+namespace parquet::internal {
+
+// Parquet SBBF probe (256-bit block, 8 SALT-derived bits per probe). Unrelated
+// to cpp/src/arrow/acero/bloom_filter_avx2.cc -- Acero's blocked bloom filter
+// is a different algorithm (64-bit block, ~4 bits per probe, in-memory only)
+// and the two kernels are not interchangeable. See the Parquet spec for the
+// SBBF on-disk layout this kernel must match.
+//
+// Spelled in xsimd rather than reusing the autovectorized body in
+// bloom_filter_block_inc.h: only clang lowers that body to a single vptest;
+// gcc and MSVC emit a longer horizontal vpor reduction.
+bool FindHashBlockAvx2(const uint32_t* block, const uint32_t* salt, uint32_t 
key) {
+  using batch = xsimd::batch<uint32_t>;
+  const batch mask =
+      batch(uint32_t{1})
+      << xsimd::bitwise_rshift<27>(batch(key) * batch::load_unaligned(salt));

Review Comment:
   Any reason for not using full operator syntax?
   
   ```suggestion
     const batch mask =
         batch(uint32_t{1})
         << ((batch(key) * batch::load_unaligned(salt)) >> 27);
   ```



##########
cpp/src/parquet/bloom_filter_block_inc.h:
##########


Review Comment:
   Let's call this file `bloom_filter_block_impl_internal.h` or something? The 
`_internal` suffix is important to avoid having this file installed.



##########
cpp/src/parquet/bloom_filter_test.cc:
##########
@@ -434,5 +445,86 @@ TYPED_TEST(TestBatchBloomFilter, Basic) {
   AssertBufferEqual(*buffer, *batch_insert_buffer);
 }
 
+// Guards against silent drift between the baseline and AVX2 probe bodies --
+// DynamicDispatch only runs one of them per host.
+#if defined(ARROW_HAVE_RUNTIME_AVX2)
+namespace {
+
+// 8-lane block matches BlockSplitBloomFilter's hard-coded shape; declared
+// locally so the test doesn't depend on the class's private constants.
+constexpr int kProbeBlockLanes = 8;
+
+// Test-only SALT (matches the Parquet SBBF spec values used in
+// bloom_filter.h). Kernel-vs-kernel agreement holds for any SALT, so this
+// duplication is a contained test-side convenience, not a spec mirror.
+alignas(32) constexpr uint32_t kProbeTestSalt[kProbeBlockLanes] = {
+    0x47b6137bU, 0x44974d91U, 0x8824ad5bU, 0xa2b7289dU,
+    0x705495c7U, 0x2df1424bU, 0x9efc4947U, 0x5c6bfb31U,
+};
+
+inline void InsertIntoBlock(uint32_t* block, uint32_t key) {
+  for (int i = 0; i < kProbeBlockLanes; ++i) {
+    block[i] |= uint32_t{1} << ((key * kProbeTestSalt[i]) >> 27);
+  }
+}
+
+void AssertKernelsAgree(const uint32_t* block, uint32_t key) {
+  const bool standard = internal::standard::FindHashBlockImpl(block, 
kProbeTestSalt, key);
+  const bool avx2 = internal::FindHashBlockAvx2(block, kProbeTestSalt, key);
+  ASSERT_EQ(standard, avx2) << "dispatch targets diverged for key=0x" << 
std::hex << key;
+}
+
+}  // namespace
+
+class BloomFilterProbeKernel : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    if (!::arrow::internal::CpuInfo::GetInstance()->IsSupported(
+            ::arrow::internal::CpuInfo::AVX2)) {
+      GTEST_SKIP() << "AVX2 not available at runtime";
+    }
+  }
+};
+
+// Random-block fuzz: exercises the full bit lattice, catches reduction /
+// operand-order bugs that don't depend on realistic fill density.
+TEST_F(BloomFilterProbeKernel, AgreeOnRandomBlocks) {
+  std::mt19937_64 rng(0xC0FFEE);
+  constexpr int kNumTrials = 20000;
+  for (int trial = 0; trial < kNumTrials; ++trial) {
+    alignas(32) uint32_t block[kProbeBlockLanes];

Review Comment:
   Same comment wrt `alignas`



##########
cpp/src/parquet/bloom_filter_test.cc:
##########
@@ -434,5 +445,86 @@ TYPED_TEST(TestBatchBloomFilter, Basic) {
   AssertBufferEqual(*buffer, *batch_insert_buffer);
 }
 
+// Guards against silent drift between the baseline and AVX2 probe bodies --
+// DynamicDispatch only runs one of them per host.
+#if defined(ARROW_HAVE_RUNTIME_AVX2)
+namespace {
+
+// 8-lane block matches BlockSplitBloomFilter's hard-coded shape; declared
+// locally so the test doesn't depend on the class's private constants.
+constexpr int kProbeBlockLanes = 8;

Review Comment:
   This is `kBitsSetPerBlock`, right? Any reason for using a different 
terminology?



##########
cpp/src/parquet/bloom_filter_test.cc:
##########
@@ -434,5 +445,86 @@ TYPED_TEST(TestBatchBloomFilter, Basic) {
   AssertBufferEqual(*buffer, *batch_insert_buffer);
 }
 
+// Guards against silent drift between the baseline and AVX2 probe bodies --
+// DynamicDispatch only runs one of them per host.
+#if defined(ARROW_HAVE_RUNTIME_AVX2)
+namespace {
+
+// 8-lane block matches BlockSplitBloomFilter's hard-coded shape; declared
+// locally so the test doesn't depend on the class's private constants.
+constexpr int kProbeBlockLanes = 8;
+
+// Test-only SALT (matches the Parquet SBBF spec values used in
+// bloom_filter.h). Kernel-vs-kernel agreement holds for any SALT, so this
+// duplication is a contained test-side convenience, not a spec mirror.
+alignas(32) constexpr uint32_t kProbeTestSalt[kProbeBlockLanes] = {

Review Comment:
   We use `load_unaligned` so the `alignas` shouldn't be required, right?



##########
cpp/src/parquet/bloom_filter.cc:
##########
@@ -34,6 +35,35 @@
 #include "parquet/thrift_internal.h"
 #include "parquet/xxhasher.h"
 
+#if defined(ARROW_HAVE_AVX2) || defined(ARROW_HAVE_RUNTIME_AVX2)
+#  include "parquet/bloom_filter_avx2_internal.h"
+#endif
+
+#define PARQUET_IMPL_NAMESPACE standard
+#include "parquet/bloom_filter_block_inc.h"
+#undef PARQUET_IMPL_NAMESPACE
+
+#include "arrow/util/dispatch_internal.h"
+
+namespace parquet::internal {
+namespace {
+
+using ::arrow::internal::DynamicDispatch;
+
+struct FindHashBlockDynamicFunction {
+  using FunctionType = decltype(&standard::FindHashBlockImpl);
+
+  static constexpr auto targets() {
+    return std::array{
+        ARROW_DISPATCH_TARGET_NONE(&standard::FindHashBlockImpl)  //
+        ARROW_DISPATCH_TARGET_AVX2(&FindHashBlockAvx2)            //
+    };
+  }
+};
+
+}  // namespace
+}  // namespace parquet::internal
+
 namespace parquet {

Review Comment:
   It seems you can at least partially collapse and simplify those namespace 
declarations.



##########
cpp/src/parquet/bloom_filter_test.cc:
##########
@@ -434,5 +445,86 @@ TYPED_TEST(TestBatchBloomFilter, Basic) {
   AssertBufferEqual(*buffer, *batch_insert_buffer);
 }
 
+// Guards against silent drift between the baseline and AVX2 probe bodies --
+// DynamicDispatch only runs one of them per host.
+#if defined(ARROW_HAVE_RUNTIME_AVX2)
+namespace {
+
+// 8-lane block matches BlockSplitBloomFilter's hard-coded shape; declared
+// locally so the test doesn't depend on the class's private constants.
+constexpr int kProbeBlockLanes = 8;
+
+// Test-only SALT (matches the Parquet SBBF spec values used in
+// bloom_filter.h). Kernel-vs-kernel agreement holds for any SALT, so this
+// duplication is a contained test-side convenience, not a spec mirror.
+alignas(32) constexpr uint32_t kProbeTestSalt[kProbeBlockLanes] = {
+    0x47b6137bU, 0x44974d91U, 0x8824ad5bU, 0xa2b7289dU,
+    0x705495c7U, 0x2df1424bU, 0x9efc4947U, 0x5c6bfb31U,
+};
+
+inline void InsertIntoBlock(uint32_t* block, uint32_t key) {
+  for (int i = 0; i < kProbeBlockLanes; ++i) {
+    block[i] |= uint32_t{1} << ((key * kProbeTestSalt[i]) >> 27);
+  }
+}
+
+void AssertKernelsAgree(const uint32_t* block, uint32_t key) {
+  const bool standard = internal::standard::FindHashBlockImpl(block, 
kProbeTestSalt, key);
+  const bool avx2 = internal::FindHashBlockAvx2(block, kProbeTestSalt, key);
+  ASSERT_EQ(standard, avx2) << "dispatch targets diverged for key=0x" << 
std::hex << key;
+}
+
+}  // namespace
+
+class BloomFilterProbeKernel : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    if (!::arrow::internal::CpuInfo::GetInstance()->IsSupported(
+            ::arrow::internal::CpuInfo::AVX2)) {
+      GTEST_SKIP() << "AVX2 not available at runtime";
+    }
+  }
+};
+
+// Random-block fuzz: exercises the full bit lattice, catches reduction /
+// operand-order bugs that don't depend on realistic fill density.
+TEST_F(BloomFilterProbeKernel, AgreeOnRandomBlocks) {
+  std::mt19937_64 rng(0xC0FFEE);
+  constexpr int kNumTrials = 20000;
+  for (int trial = 0; trial < kNumTrials; ++trial) {
+    alignas(32) uint32_t block[kProbeBlockLanes];
+    for (uint32_t& word : block) {
+      word = static_cast<uint32_t>(rng());
+    }
+    AssertKernelsAgree(block, static_cast<uint32_t>(rng()));
+  }
+}
+
+// Production-fill fuzz: blocks populated by the same SALT-derived insert the
+// writer uses, then probed with both inserted keys (must match) and fresh
+// keys (mostly miss). Catches bugs that only surface on real fill density.
+TEST_F(BloomFilterProbeKernel, AgreeOnPopulatedBlocks) {
+  std::mt19937_64 rng(0xBABECAFE);
+  constexpr int kNumBlocks = 200;
+  constexpr int kKeysPerBlock = 6;  // ~k inserts per 256-bit block, realistic 
FPP.
+  for (int b = 0; b < kNumBlocks; ++b) {
+    alignas(32) uint32_t block[kProbeBlockLanes] = {0};

Review Comment:
   (and same)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to