Skip to content

Commit e69e78a

Browse files
committed
Brought the benchmarks back to life with modern CMake and GBenchmark versions
1 parent 2be98b4 commit e69e78a

File tree

6 files changed

+123
-33
lines changed

6 files changed

+123
-33
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# The full license is in the file LICENSE, distributed with this software. #
88
############################################################################
99

10-
cmake_minimum_required(VERSION 3.29)
10+
cmake_minimum_required(VERSION 3.22)
1111
project(xtensor CXX)
1212

1313
set(XTENSOR_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

benchmark/CMakeLists.txt

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# The full license is in the file LICENSE, distributed with this software. #
77
############################################################################
88

9-
cmake_minimum_required(VERSION 3.5)
9+
cmake_minimum_required(VERSION 3.22)
10+
include(FetchContent)
1011

1112
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1213
project(xtensor-benchmark)
@@ -30,11 +31,11 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"
3031
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -g -Wunused-parameter -Wextra -Wreorder")
3132

3233
if(NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
33-
CHECK_CXX_COMPILER_FLAG("-std=c++14" HAS_CPP14_FLAG)
34-
if (HAS_CPP14_FLAG)
35-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
34+
CHECK_CXX_COMPILER_FLAG("-std=c++20" HAS_CPP20_FLAG)
35+
if (HAS_CPP20_FLAG)
36+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
3637
else()
37-
message(FATAL_ERROR "Unsupported compiler -- xtensor requires C++14 support!")
38+
message(FATAL_ERROR "Unsupported compiler -- xtensor requires C++17 support!")
3839
endif()
3940
endif()
4041

@@ -74,31 +75,17 @@ endif()
7475

7576

7677
if(DOWNLOAD_GBENCHMARK OR GBENCHMARK_SRC_DIR)
77-
if(DOWNLOAD_GBENCHMARK)
78-
# Download and unpack googlebenchmark at configure time
79-
configure_file(downloadGBenchmark.cmake.in googlebenchmark-download/CMakeLists.txt)
80-
else()
81-
# Copy local source of googlebenchmark at configure time
82-
configure_file(copyGBenchmark.cmake.in googlebenchmark-download/CMakeLists.txt)
83-
endif()
84-
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
85-
RESULT_VARIABLE result
86-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-download )
87-
if(result)
88-
message(FATAL_ERROR "CMake step for googlebenchmark failed: ${result}")
89-
endif()
90-
execute_process(COMMAND ${CMAKE_COMMAND} --build .
91-
RESULT_VARIABLE result
92-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-download )
93-
if(result)
94-
message(FATAL_ERROR "Build step for googlebenchmark failed: ${result}")
95-
endif()
78+
FetchContent_Declare(googletest
79+
GIT_REPOSITORY https://github.com/google/googletest.git
80+
GIT_TAG main)
9681

97-
# Add googlebenchmark directly to our build. This defines
98-
# the gtest and gtest_main targets.
99-
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-src
100-
${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build)
82+
FetchContent_Declare(googlebenchmark
83+
GIT_REPOSITORY https://github.com/google/benchmark.git
84+
GIT_TAG main) # need main for benchmark::benchmark
10185

86+
FetchContent_MakeAvailable(
87+
googletest
88+
googlebenchmark)
10289
set(GBENCHMARK_INCLUDE_DIRS "${googlebenchmark_SOURCE_DIR}/include")
10390
set(GBENCHMARK_LIBRARIES benchmark)
10491
else()
@@ -129,9 +116,11 @@ set(XTENSOR_BENCHMARK
129116
benchmark_view_access.cpp
130117
benchmark_view_assignment.cpp
131118
benchmark_view_adapt.cpp
119+
benchmark_stl.cpp
132120
main.cpp
133121
)
134122

123+
135124
set(XTENSOR_BENCHMARK_TARGET benchmark_xtensor)
136125
add_executable(${XTENSOR_BENCHMARK_TARGET} EXCLUDE_FROM_ALL ${XTENSOR_BENCHMARK} ${XTENSOR_HEADERS})
137126
target_link_libraries(${XTENSOR_BENCHMARK_TARGET} PUBLIC xtensor ${GBENCHMARK_LIBRARIES})

benchmark/benchmark_stl.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***************************************************************************
2+
* Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
****************************************************************************/
8+
9+
#include <benchmark/benchmark.h>
10+
#include "xtensor/containers/xtensor.hpp"
11+
#include "xtensor/core/xmath.hpp"
12+
#include "xtensor/generators/xrandom.hpp"
13+
14+
namespace xt
15+
{
16+
namespace{
17+
constexpr std::array<size_t, 2> cContainerAssignShape{2000,2000};
18+
template<class Shape>
19+
auto generateRandomInt16From0To100(Shape&& x)
20+
{
21+
return xt::random::randint(x, 0, 100);
22+
}
23+
}
24+
25+
static void Xtensor_Uint16_2000x2000_DivideBy2_StdTransform(benchmark::State& aState) {
26+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
27+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
28+
29+
for (auto _ : aState) {
30+
std::transform(vInput.begin(), vInput.end(), vOutput.begin(), [](auto&& aInputValue) { return aInputValue / 2; });
31+
}
32+
}
33+
34+
static void Xtensor_Uint16_2000x2000_DivideBy2_Xtensor(benchmark::State& aState) {
35+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
36+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
37+
38+
for (auto _ : aState) {
39+
vOutput = vInput / 2;
40+
}
41+
}
42+
43+
static void Xtensor_Uint16_2000x2000_DivideBy2Double_StdTransform(benchmark::State& aState) {
44+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
45+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
46+
47+
for (auto _ : aState) {
48+
std::transform(vInput.begin(), vInput.end(), vOutput.begin(), [](auto&& aInputValue) { return aInputValue / 2.0; });
49+
}
50+
}
51+
52+
static void Xtensor_Uint16_2000x2000_DivideBy2Double_Xtensor(benchmark::State& aState) {
53+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
54+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
55+
56+
for (auto _ : aState) {
57+
vOutput = vInput / 2.0;
58+
}
59+
}
60+
61+
static void Xtensor_Uint16_2000x2000_MultiplyBy2_StdTransform(benchmark::State& aState) {
62+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
63+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
64+
65+
for (auto _ : aState) {
66+
std::transform(vInput.begin(), vInput.end(), vOutput.begin(), [](auto&& aInputValue) { return aInputValue * 2; });
67+
}
68+
}
69+
70+
static void Xtensor_Uint16_2000x2000_MultiplyBy2_Xtensor(benchmark::State& aState) {
71+
xt::xtensor<uint16_t, 2> vInput = generateRandomInt16From0To100(cContainerAssignShape);
72+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
73+
74+
for (auto _ : aState) {
75+
vOutput = vInput * 2;
76+
}
77+
}
78+
79+
static void Xtensor_Uint16_2000x2000_Maximum_StdTransform(benchmark::State& aState) {
80+
xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape);
81+
xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape);
82+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
83+
84+
for (auto _ : aState) {
85+
auto vInput2It = vInput2.begin();
86+
std::transform(vInput1.begin(), vInput1.end(), vOutput.begin(), [&vInput2It](auto&& aInput1Value) { return std::max(aInput1Value, *vInput2It++); });
87+
}
88+
}
89+
90+
static void Xtensor_Uint16_2000x2000_Maximum_Xtensor(benchmark::State& aState) {
91+
xt::xtensor<uint16_t, 2> vInput1 = generateRandomInt16From0To100(cContainerAssignShape);
92+
xt::xtensor<uint16_t, 2> vInput2 = generateRandomInt16From0To100(cContainerAssignShape);
93+
auto vOutput = xt::xtensor<uint16_t, 2>::from_shape(cContainerAssignShape);
94+
95+
for (auto _ : aState) {
96+
vOutput = xt::maximum(vInput1, vInput2);
97+
}
98+
}
99+
}
100+

benchmark/benchmark_view_assignment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,6 @@ namespace xt
155155
// BENCHMARK(assign_create_strided_view);
156156
BENCHMARK(assign_create_view);
157157
BENCHMARK(assign_create_manual_view);
158-
BENCHMARK(data_offset);
158+
//BENCHMARK(data_offset);
159159
BENCHMARK(data_offset_view);
160160
}

benchmark/copyGBenchmark.cmake.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# The full license is in the file LICENSE, distributed with this software. #
77
############################################################################
88

9-
cmake_minimum_required(VERSION 2.8.2)
9+
cmake_minimum_required(VERSION 3.5)
1010

1111
project(googlebenchmark-download NONE)
1212

benchmark/downloadGBenchmark.cmake.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
# The full license is in the file LICENSE, distributed with this software. #
77
############################################################################
88

9-
cmake_minimum_required(VERSION 2.8.2)
9+
cmake_minimum_required(VERSION 3.5)
1010

1111
project(googlebenchmark-download NONE)
1212

1313
include(ExternalProject)
1414
ExternalProject_Add(googlebenchmark
1515
GIT_REPOSITORY https://github.com/google/benchmark.git
16-
GIT_TAG v1.3.0
16+
GIT_TAG v1.9.4
1717
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-src"
1818
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googlebenchmark-build"
1919
CONFIGURE_COMMAND ""
2020
BUILD_COMMAND ""
21+
CMAKE_ARGS "BENCHMARK_DOWNLOAD_DEPENDENCIES=TRUE"
2122
INSTALL_COMMAND ""
2223
TEST_COMMAND ""
2324
)

0 commit comments

Comments
 (0)