Skip to content

Commit

Permalink
Add SimdUtilBenchmark (facebookincubator#7735)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: facebookincubator#7735

Differential Revision: D51555528
  • Loading branch information
Yuhta authored and facebook-github-bot committed Nov 27, 2023
1 parent 409877a commit e60b8f6
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions velox/common/base/benchmarks/SimdUtilBenchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed 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 "velox/common/base/SimdUtil.h"

#include <folly/Benchmark.h>
#include <folly/init/Init.h>

#include <random>

namespace facebook::velox {
namespace {

#define VELOX_BENCHMARK(_type, _name, ...) \
_type _name(FOLLY_PP_STRINGIZE(_name), __VA_ARGS__)

template <typename T>
class LeadingMask {
public:
LeadingMask(const char* name, std::default_random_engine& gen) {
std::uniform_int_distribution<> dist(xsimd::batch<T>::size + 1);
for (int i = 0; i < kSize; ++i) {
inputs_[i] = dist(gen);
}
folly::addBenchmark(__FILE__, name, [this] { return run(); });
}

private:
unsigned run() {
xsimd::batch_bool<T> ans = {};
for (int i = 0; i < kSize; ++i) {
ans = ans ^ simd::leadingMask<T>(inputs_[i]);
}
folly::doNotOptimizeAway(ans);
return kSize;
}

static constexpr int kSize = 4 << 10;
int8_t inputs_[kSize];
};

} // namespace
} // namespace facebook::velox

int main(int argc, char* argv[]) {
using namespace facebook::velox;
folly::Init follyInit(&argc, &argv);
std::default_random_engine gen(std::random_device{}());
VELOX_BENCHMARK(LeadingMask<int32_t>, leadingMaskInt32, gen);
VELOX_BENCHMARK(LeadingMask<int64_t>, leadingMaskInt64, gen);
folly::runBenchmarks();
return 0;
}

0 comments on commit e60b8f6

Please sign in to comment.