summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@gmail.com>2013-01-28 13:10:23 -0500
committerErik Schnetter <schnetter@gmail.com>2013-01-28 13:10:23 -0500
commit2eef18ff8d255eee985863071d845dd74b0120bf (patch)
tree1d4ef5f8d333d1880a4349b46cb6ef9097a0e747
parent40a4fab8302ec02515cc5b7328b328eec48ab19a (diff)
downloadvecmathlib-2eef18ff8d255eee985863071d845dd74b0120bf.zip
vecmathlib-2eef18ff8d255eee985863071d845dd74b0120bf.tar.gz
Add benchmarks
-rw-r--r--CMakeLists.txt2
-rw-r--r--bench.cc179
-rw-r--r--build.ninja36
-rw-r--r--rules.ninja28
4 files changed, 224 insertions, 21 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7e322d4..9964fc7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,5 +7,7 @@ project (vecmathlib)
add_executable (example example.cc)
add_executable (test test.cc)
+add_executable (bench bench.cc)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -std=gnu++11 -march=native -Ofast")
+# use flag "-DNDEBUG" for benchmarks
diff --git a/bench.cc b/bench.cc
new file mode 100644
index 0000000..658e8ec
--- /dev/null
+++ b/bench.cc
@@ -0,0 +1,179 @@
+// -*-C++-*-
+
+#include "vecmathlib.h"
+
+#include <cmath>
+#include <cstdlib>
+#include <iostream>
+
+#include <sys/time.h>
+
+using namespace std;
+
+
+
+typedef unsigned long long ticks;
+inline ticks getticks()
+{
+ ticks a, d;
+ asm volatile("rdtsc" : "=a" (a), "=d" (d));
+ return a | (d << 32);
+}
+inline double elapsed(ticks t1, ticks t0)
+{
+ return t1-t0;
+}
+
+double get_sys_time()
+{
+ timeval tp;
+ gettimeofday(&tp, NULL);
+ return tp.tv_sec + 1.0e-6 * tp.tv_usec;
+}
+
+double measure_tick()
+{
+ ticks const rstart = getticks();
+ double const wstart = get_sys_time();
+ while (get_sys_time() - wstart < 0.1) {
+ // do nothing, just wait
+ }
+ ticks const rend = getticks();
+ double const wend = get_sys_time();
+ assert(wend-wstart >= 0.09);
+ return (wend - wstart) / elapsed(rend, rstart);
+}
+
+
+
+double global_result = 0.0;
+template<typename realvec_t>
+void save_result(realvec_t result)
+{
+ for (int i=0; i<realvec_t::size; ++i) {
+ global_result += result[i];
+ }
+}
+
+
+
+template<typename T> inline T identity(T x) { return x; }
+
+#define DECLARE_FUNCTOR(func) \
+ template<typename T> \
+ struct functor_##func { \
+ T operator()(T x) { return func(x); } \
+ }
+
+DECLARE_FUNCTOR(identity);
+DECLARE_FUNCTOR(sqrt);
+DECLARE_FUNCTOR(exp);
+DECLARE_FUNCTOR(log);
+DECLARE_FUNCTOR(sin);
+DECLARE_FUNCTOR(cos);
+DECLARE_FUNCTOR(atan);
+
+
+
+template<typename realvec_t, typename func_t>
+double bench_func()
+{
+ realvec_t x0, dx;
+ for (int i=0; i<realvec_t::size; ++i) {
+ x0.set_elt(i, 1.0f + float(i));
+ dx.set_elt(i, 1.0e-6f);
+ }
+ realvec_t x, y;
+ ticks t0, t1;
+ double const cycles_per_tick = 1.0; // measure_tick();
+ int const numiters = 10000000;
+
+ func_t func;
+ t0 = getticks();
+ x = y = x0;
+ for (int n=0; n<numiters; ++n) {
+ y += func(x);
+ x += dx;
+ }
+ t1 = getticks();
+ save_result(y);
+
+ return cycles_per_tick * elapsed(t1,t0) * realvec_t::size / numiters;
+}
+
+template<typename realvec_t>
+void bench()
+{
+ cout << "identity(" << realvec_t::name << "):" << flush;
+ double const cycles_identity =
+ bench_func<realvec_t, functor_identity<realvec_t>>();
+ cout << " " << cycles_identity << " cycles\n";
+
+ cout << "sqrt(" << realvec_t::name << "):" << flush;
+ double const cycles_sqrt =
+ bench_func<realvec_t, functor_sqrt<realvec_t>>();
+ cout << " " << cycles_sqrt - cycles_identity << " cycles\n";
+
+ cout << "exp(" << realvec_t::name << "):" << flush;
+ double const cycles_exp =
+ bench_func<realvec_t, functor_exp<realvec_t>>();
+ cout << " " << cycles_exp - cycles_identity << " cycles\n";
+
+ cout << "log(" << realvec_t::name << "):" << flush;
+ double const cycles_log =
+ bench_func<realvec_t, functor_log<realvec_t>>();
+ cout << " " << cycles_log - cycles_identity << " cycles\n";
+
+ cout << "sin(" << realvec_t::name << "):" << flush;
+ double const cycles_sin =
+ bench_func<realvec_t, functor_sin<realvec_t>>();
+ cout << " " << cycles_sin - cycles_identity << " cycles\n";
+
+ cout << "cos(" << realvec_t::name << "):" << flush;
+ double const cycles_cos =
+ bench_func<realvec_t, functor_cos<realvec_t>>();
+ cout << " " << cycles_cos - cycles_identity << " cycles\n";
+
+ cout << "atan(" << realvec_t::name << "):" << flush;
+ double const cycles_atan =
+ bench_func<realvec_t, functor_atan<realvec_t>>();
+ cout << " " << cycles_atan - cycles_identity << " cycles\n";
+}
+
+
+
+int main(int argc, char** argv)
+{
+ using namespace vecmathlib;
+
+ cout << "Benchmarking math functions:\n"
+ << "\n";
+
+#ifdef VECMATHLIB_HAVE_VEC_FLOAT_1
+ bench<realvec<float,1>>();
+#endif
+#ifdef VECMATHLIB_HAVE_VEC_FLOAT_4
+ bench<realvec<float,4>>();
+#endif
+#ifdef VECMATHLIB_HAVE_VEC_FLOAT_8
+ bench<realvec<float,8>>();
+#endif
+
+ cout << "\n";
+
+#ifdef VECMATHLIB_HAVE_VEC_DOUBLE_1
+ bench<realvec<double,1>>();
+#endif
+#ifdef VECMATHLIB_HAVE_VEC_DOUBLE_2
+ bench<realvec<double,2>>();
+#endif
+#ifdef VECMATHLIB_HAVE_VEC_DOUBLE_4
+ bench<realvec<double,4>>();
+#endif
+
+ cout << "\n"
+ << "Outputting global result to prevent optimisation: "
+ << global_result << "\n";
+
+ return 0;
+}
diff --git a/build.ninja b/build.ninja
index 4b83920..600a66f 100644
--- a/build.ninja
+++ b/build.ninja
@@ -23,6 +23,26 @@
include rules.ninja
+# =============================================================================
+# Object build statements for EXECUTABLE target bench
+
+build CMakeFiles/bench.dir/bench.cc.o: CXX_COMPILER bench.cc
+ DEP_FILE = CMakeFiles/bench.dir/bench.cc.o.d
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
+ OBJECT_DIR = CMakeFiles/bench.dir
+
+# =============================================================================
+# Link build statements for EXECUTABLE target bench
+
+
+#############################################
+# Link the executable bench
+
+build bench: CXX_EXECUTABLE_LINKER CMakeFiles/bench.dir/bench.cc.o
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
+ POST_BUILD = :
+ PRE_LINK = :
+ TARGET_PDB = bench.dbg
#############################################
# Utility command for edit_cache
@@ -36,7 +56,8 @@ build edit_cache: phony CMakeFiles/edit_cache.util
build CMakeFiles/example.dir/example.cc.o: CXX_COMPILER example.cc
DEP_FILE = CMakeFiles/example.dir/example.cc.o.d
- FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
+ OBJECT_DIR = CMakeFiles/example.dir
# =============================================================================
# Link build statements for EXECUTABLE target example
@@ -46,7 +67,7 @@ build CMakeFiles/example.dir/example.cc.o: CXX_COMPILER example.cc
# Link the executable example
build example: CXX_EXECUTABLE_LINKER CMakeFiles/example.dir/example.cc.o
- FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
POST_BUILD = :
PRE_LINK = :
TARGET_PDB = example.dbg
@@ -63,7 +84,8 @@ build rebuild_cache: phony CMakeFiles/rebuild_cache.util
build CMakeFiles/test.dir/test.cc.o: CXX_COMPILER test.cc
DEP_FILE = CMakeFiles/test.dir/test.cc.o.d
- FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
+ OBJECT_DIR = CMakeFiles/test.dir
# =============================================================================
# Link build statements for EXECUTABLE target test
@@ -73,7 +95,7 @@ build CMakeFiles/test.dir/test.cc.o: CXX_COMPILER test.cc
# Link the executable test
build test: CXX_EXECUTABLE_LINKER CMakeFiles/test.dir/test.cc.o
- FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast
+ FLAGS = -Wall -g -std=gnu++11 -march=native -Ofast -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
POST_BUILD = :
PRE_LINK = :
TARGET_PDB = test.dbg
@@ -87,7 +109,7 @@ build test: CXX_EXECUTABLE_LINKER CMakeFiles/test.dir/test.cc.o
#############################################
# The main all target.
-build all: phony example test
+build all: phony bench example test
#############################################
# Make the all target the default.
@@ -97,12 +119,12 @@ default all
#############################################
# Re-run CMake if any of its inputs changed.
-build build.ninja: RERUN_CMAKE | /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeCCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeCXXCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeSystem.cmake /Users/eschnett/src/cc/vecmathlib/CMakeLists.txt /opt/local/share/cmake-2.8/Modules/CMakeCCompiler.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeCCompilerABI.c /opt/local/share/cmake-2.8/Modules/CMakeCInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCXXCompiler.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeCXXCompilerABI.cpp /opt/local/share/cmake-2.8/Modules/CMakeCXXInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeClDeps.cmake /opt/local/share/cmake-2.8/Modules/CMakeCommonLanguageInclude.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCXXCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCompilerABI.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCompilerId.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeFindBinUtils.cmake /opt/local/share/cmake-2.8/Modules/CMakeGenericSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeNinjaFindMake.cmake /opt/local/share/cmake-2.8/Modules/CMakeParseImplicitLinkInfo.cmake /opt/local/share/cmake-2.8/Modules/CMakeSystem.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeSystemSpecificInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCompilerCommon.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin.cmake /opt/local/share/cmake-2.8/Modules/Platform/UnixPaths.cmake CMakeCache.txt
+build build.ninja: RERUN_CMAKE | /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeCCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeCXXCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeSystem.cmake /Users/eschnett/src/cc/vecmathlib/CMakeLists.txt /opt/local/share/cmake-2.8/Modules/CMakeCInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCXXInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCommonLanguageInclude.cmake /opt/local/share/cmake-2.8/Modules/CMakeGenericSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeSystemSpecificInformation.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin.cmake /opt/local/share/cmake-2.8/Modules/Platform/UnixPaths.cmake CMakeCache.txt
#############################################
# A missing CMake input file is not an error.
-build /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeCCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeCXXCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/CMakeSystem.cmake /Users/eschnett/src/cc/vecmathlib/CMakeLists.txt /opt/local/share/cmake-2.8/Modules/CMakeCCompiler.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeCCompilerABI.c /opt/local/share/cmake-2.8/Modules/CMakeCInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCXXCompiler.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeCXXCompilerABI.cpp /opt/local/share/cmake-2.8/Modules/CMakeCXXInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeClDeps.cmake /opt/local/share/cmake-2.8/Modules/CMakeCommonLanguageInclude.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCXXCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCompilerABI.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineCompilerId.cmake /opt/local/share/cmake-2.8/Modules/CMakeDetermineSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeFindBinUtils.cmake /opt/local/share/cmake-2.8/Modules/CMakeGenericSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeNinjaFindMake.cmake /opt/local/share/cmake-2.8/Modules/CMakeParseImplicitLinkInfo.cmake /opt/local/share/cmake-2.8/Modules/CMakeSystem.cmake.in /opt/local/share/cmake-2.8/Modules/CMakeSystemSpecificInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCXXCompiler.cmake /opt/local/share/cmake-2.8/Modules/CMakeTestCompilerCommon.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin.cmake /opt/local/share/cmake-2.8/Modules/Platform/UnixPaths.cmake CMakeCache.txt: phony
+build /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeCCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeCXXCompiler.cmake /Users/eschnett/src/cc/vecmathlib/CMakeFiles/2.8.10/CMakeSystem.cmake /Users/eschnett/src/cc/vecmathlib/CMakeLists.txt /opt/local/share/cmake-2.8/Modules/CMakeCInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCXXInformation.cmake /opt/local/share/cmake-2.8/Modules/CMakeCommonLanguageInclude.cmake /opt/local/share/cmake-2.8/Modules/CMakeGenericSystem.cmake /opt/local/share/cmake-2.8/Modules/CMakeSystemSpecificInformation.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Compiler/GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-C.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU-CXX.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin-GNU.cmake /opt/local/share/cmake-2.8/Modules/Platform/Darwin.cmake /opt/local/share/cmake-2.8/Modules/Platform/UnixPaths.cmake CMakeCache.txt: phony
#############################################
# Clean all the built files.
diff --git a/rules.ninja b/rules.ninja
index 11318d2..4f9ef9f 100644
--- a/rules.ninja
+++ b/rules.ninja
@@ -11,15 +11,6 @@
# =============================================================================
#############################################
-# Rule for running custom commands.
-
-rule CUSTOM_COMMAND
- command = $COMMAND
- description = $DESC
- restat = 1
-
-
-#############################################
# Rule for compiling CXX files.
rule CXX_COMPILER
@@ -32,18 +23,27 @@ rule CXX_COMPILER
# Rule for linking CXX executable.
rule CXX_EXECUTABLE_LINKER
- command = $PRE_LINK && /opt/local/bin/c++ $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS $in -o $out $LINK_LIBRARIES && $POST_BUILD
+ command = $PRE_LINK && /opt/local/bin/c++ $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS $in -o $out $LINK_PATH $LINK_LIBRARIES && $POST_BUILD
description = Linking CXX executable $out
#############################################
# Rule for linking CXX executable.
-rule CXX_EXECUTABLE_LINKER_RSPFILE
- command = $PRE_LINK && /opt/local/bin/c++ $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS @$out.rsp -o $out && $POST_BUILD
+rule CXX_EXECUTABLE_LINKER_RSP_FILE
+ command = $PRE_LINK && /opt/local/bin/c++ $FLAGS -Wl,-search_paths_first -Wl,-headerpad_max_install_names $LINK_FLAGS @$RSP_FILE -o $out && $POST_BUILD
description = Linking CXX executable $out
- rspfile = $out.rsp
- rspfile_content = $in $LINK_LIBRARIES
+ rspfile = $RSP_FILE
+ rspfile_content = $in $LINK_PATH $LINK_LIBRARIES
+
+
+#############################################
+# Rule for running custom commands.
+
+rule CUSTOM_COMMAND
+ command = $COMMAND
+ description = $DESC
+ restat = 1
#############################################
OpenPOWER on IntegriCloud