Skip to content

Commit

Permalink
add manual broadcast loops, remove tabs, fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Apr 5, 2018
1 parent 5cc89c1 commit 6bd6b96
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 155 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
if (HAS_CPP14_FLAG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -ffast-math -march=native -std=c++14 -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast -ffast-math -march=native -std=c++14 -pthread -Wno-narrowing")
else()
message(FATAL_ERROR "Unsupported compiler -- xtensor requires C++14 support!")
endif()
Expand Down
118 changes: 94 additions & 24 deletions src/benchmark_broadcasting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "xtensor/xrandom.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xfixed.hpp"

#ifdef HAS_PYTHONIC
#include <pythonic/core.hpp>
Expand All @@ -27,37 +28,106 @@

namespace xt
{
void xtensor_broadcasting(benchmark::State& state)
{
using namespace xt;
using allocator = xsimd::aligned_allocator<double, 32>;
using tensor3 = xtensor_container<xt::uvector<double, allocator>, 3, layout_type::row_major>;
using tensor2 = xtensor_container<xt::uvector<double, allocator>, 2, layout_type::row_major>;
void xtensor_broadcasting(benchmark::State& state)
{
using namespace xt;
using allocator = xsimd::aligned_allocator<double, 32>;
using tensor3 = xtensor_container<xt::uvector<double, allocator>, 3, layout_type::row_major>;
using tensor2 = xtensor_container<xt::uvector<double, allocator>, 2, layout_type::row_major>;

tensor3 a = random::rand<double>({state.range(0), state.range(0), state.range(0)});
tensor2 b = random::rand<double>({state.range(0), state.range(0)});
tensor3 a = random::rand<double>({state.range(0), state.range(0), state.range(0)});
tensor2 b = random::rand<double>({state.range(0), state.range(0)});

for (auto _ : state)
{
tensor3 res(a + b);
benchmark::DoNotOptimize(res.raw_data());
}
}
BENCHMARK(xtensor_broadcasting)->RangeMultiplier(MULTIPLIER)->Range(RANGE);
{
tensor3 res(a + b);
benchmark::DoNotOptimize(res.raw_data());
}
}
BENCHMARK(xtensor_broadcasting)->RangeMultiplier(MULTIPLIER)->Range(RANGE);

void xarray_broadcasting(benchmark::State& state)
{
using namespace xt;
using allocator = xsimd::aligned_allocator<double, 32>;
using tensor3 = xarray_container<xt::uvector<double, allocator>, layout_type::row_major>;
using tensor2 = xarray_container<xt::uvector<double, allocator>, layout_type::row_major>;

tensor3 a = random::rand<double>({state.range(0), state.range(0), state.range(0)});
tensor2 b = random::rand<double>({state.range(0), state.range(0)});

for (auto _ : state)
{
tensor3 res(a + b);
benchmark::DoNotOptimize(res.raw_data());
}
}
BENCHMARK(xarray_broadcasting)->RangeMultiplier(MULTIPLIER)->Range(RANGE);

template <std::size_t N>
void manual_broadcast_xtensorf(benchmark::State& state)
{
auto a = xt::xtensorf<double, xt::xshape<N, N, N>>();
auto b = xt::xtensorf<double, xt::xshape<N, N>>();
for (auto _ : state)
{
auto c = xt::xtensorf<double, xt::xshape<N, N, N>>();
for (std::size_t i = 0; i < a.shape()[0]; ++i)
for (std::size_t j = 0; j < a.shape()[1]; ++j)
for (std::size_t k = 0; k < a.shape()[2]; ++k)
c(i, j, k) = a(i, j, k) + b(i, j, k);
benchmark::DoNotOptimize(c.raw_data());
}
}
BENCHMARK_TEMPLATE(manual_broadcast_xtensorf, 3);
BENCHMARK_TEMPLATE(manual_broadcast_xtensorf, 8);
BENCHMARK_TEMPLATE(manual_broadcast_xtensorf, 64);

void manual_broadcast_xtensor(benchmark::State& state)
{
auto a = xt::xtensor<double, 3>::from_shape({state.range(0), state.range(0), state.range(0)});
auto b = xt::xtensor<double, 2>::from_shape({state.range(0), state.range(0)});
for (auto _ : state)
{
xt::xtensor<double, 3> c = xt::xtensor<double, 3>::from_shape({state.range(0), state.range(0), state.range(0)});
for (std::size_t i = 0; i < a.shape()[0]; ++i)
for (std::size_t j = 0; j < a.shape()[1]; ++j)
for (std::size_t k = 0; k < a.shape()[2]; ++k)
c(i, j, k) = a(i, j, k) + b(i, j, k);
benchmark::DoNotOptimize(c.raw_data());
}
}
BENCHMARK(manual_broadcast_xtensor)->RangeMultiplier(MULTIPLIER)->Range(RANGE);

void manual_broadcast_xarray(benchmark::State& state)
{
auto a = xt::xarray<double>::from_shape({state.range(0), state.range(0), state.range(0)});
auto b = xt::xarray<double>::from_shape({state.range(0), state.range(0)});
for (auto _ : state)
{
xt::xarray<double> c = xt::xarray<double>::from_shape({state.range(0), state.range(0), state.range(0)});
for (std::size_t i = 0; i < a.shape()[0]; ++i)
for (std::size_t j = 0; j < a.shape()[1]; ++j)
for (std::size_t k = 0; k < a.shape()[2]; ++k)
c(i, j, k) = a(i, j, k) + b(i, j, k);
benchmark::DoNotOptimize(c.raw_data());
}
}
BENCHMARK(manual_broadcast_xarray)->RangeMultiplier(MULTIPLIER)->Range(RANGE);

#ifdef HAS_PYTHONIC
void pythonic_broadcasting(benchmark::State& state)
{
auto x = pythonic::numpy::random::rand(state.range(0), state.range(0), state.range(0));
auto y = pythonic::numpy::random::rand(state.range(0), state.range(0));
void pythonic_broadcasting(benchmark::State& state)
{
auto x = pythonic::numpy::random::rand(state.range(0), state.range(0), state.range(0));
auto y = pythonic::numpy::random::rand(state.range(0), state.range(0));

for (auto _ : state)
{
pythonic::types::ndarray<double, 3> z = x + y;
benchmark::DoNotOptimize(z.fbegin());
}
}
BENCHMARK(pythonic_broadcasting)->RangeMultiplier(MULTIPLIER)->Range(RANGE);
{
pythonic::types::ndarray<double, 3> z = x + y;
benchmark::DoNotOptimize(z.fbegin());
}
}
BENCHMARK(pythonic_broadcasting)->RangeMultiplier(MULTIPLIER)->Range(RANGE);
#endif

}
Expand Down
Loading

0 comments on commit 6bd6b96

Please sign in to comment.