Skip to content

Instantly share code, notes, and snippets.

@SC-One
Last active April 16, 2022 12:01
Show Gist options
  • Save SC-One/4904e0c0fb44067cd360e90511e04696 to your computer and use it in GitHub Desktop.
Save SC-One/4904e0c0fb44067cd360e90511e04696 to your computer and use it in GitHub Desktop.
Template base benchmark (google)
//https://github.com/google/benchmark/issues/1157
#include <benchmark/benchmark.h>
#include <vector>
#include <future>
#include <numeric>
#include <span>
using NumberType = float;
using ReduceType = double;
template <int P>
ReduceType sp_async(std::span<NumberType> x, std::span<NumberType> y)
{
std::vector<std::future<ReduceType>> futures(P);
std::vector<ReduceType> psums(P);
size_t N = x.size();
for (int rank = 0; rank < P; ++rank) {
size_t ibegin = N*rank/P;
size_t iend = (N+1)*rank/P;
auto xsub = std::span{x.begin()+ibegin, x.begin()+iend};
auto ysub = std::span{y.begin()+ibegin, y.begin()+iend};
futures[rank] = std::async([xsub, ysub]() {
return std::inner_product(xsub.begin(), xsub.end(), ysub.begin(), ReduceType(0), std::plus<ReduceType>(), std::multiplies<ReduceType>());
});
}
for (int rank = 0; rank < P; ++rank) {
psums[rank] = futures[rank].get();
}
return std::accumulate(psums.begin(), psums.end(), ReduceType(0));
}
template <int P>
static void BM_scalar_product_async(benchmark::State& state) {
// Perform setup here
ptrdiff_t N = state.range(0);
std::vector<NumberType> x(N);
std::vector<NumberType> y(N);
std::iota(x.begin(), x.end(), 1);
std::iota(y.begin(), y.end(), 1);
for (auto _ : state) {
benchmark::DoNotOptimize(sp_async<P>(x, y));
}
}
BENCHMARK_TEMPLATE(BM_scalar_product_async, 2)->Range(8<<3, 8<<25)->Unit(benchmark::kMillisecond)->UseRealTime();
BENCHMARK_MAIN();
//https://github.com/fvanmaele/hasc/tree/master/ex04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment