Skip to content

Commit

Permalink
New QuantLib benchmark for machine comparison (#1962)
Browse files Browse the repository at this point in the history
  • Loading branch information
lballabio authored May 17, 2024
2 parents 7178092 + 56a4fbc commit 14377c2
Show file tree
Hide file tree
Showing 6 changed files with 779 additions and 415 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
- name: Test
run: |
quantlib-test-suite --log_level=message
- name: Run benchmark
run: |
quantlib-benchmark --size=1
cmake-linux-with-options:
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ set(QL_INSTALL_CMAKEDIR "lib/cmake/${PACKAGE_NAME}" CACHE STRING
"Installation directory for CMake scripts")

# Options
option(QL_BUILD_BENCHMARK "Build benchmark" ON)
option(QL_BUILD_EXAMPLES "Build examples" ON)
option(QL_BUILD_TEST_SUITE "Build test suite" ON)
option(QL_BUILD_FUZZ_TEST_SUITE "Build fuzz test suite" OFF)
Expand Down Expand Up @@ -70,6 +69,7 @@ option(QL_USE_STD_OPTIONAL "Use std::optional instead of boost::optional" OFF)
option(QL_USE_STD_SHARED_PTR "Use standard smart pointers instead of Boost ones" OFF)
option(QL_USE_STD_TUPLE "Use std::tuple instead of boost::tuple" ON)
set(QL_EXTERNAL_SUBDIRECTORIES "" CACHE STRING "Optional list of external source directories to be added to the build (semicolon-separated)")
# set -lpapi here
set(QL_EXTRA_LINK_LIBRARIES "" CACHE STRING "Optional extra link libraries to add to QuantLib")

# Require C++14 or higher
Expand Down Expand Up @@ -269,7 +269,7 @@ add_subdirectory(ql)
if (QL_BUILD_EXAMPLES)
add_subdirectory(Examples)
endif()
if (QL_BUILD_TEST_SUITE OR QL_BUILD_BENCHMARK)
if (QL_BUILD_TEST_SUITE)
add_subdirectory(test-suite)
endif()

Expand Down
63 changes: 17 additions & 46 deletions test-suite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ set(QL_TEST_SOURCES
preconditions.cpp
prices.cpp
quantlibglobalfixture.cpp
quantlibtestsuite.cpp
quantooption.cpp
quotes.cpp
rangeaccrual.cpp
Expand Down Expand Up @@ -183,63 +182,35 @@ set(QL_TEST_HEADERS
utilities.hpp
)

set(QL_BENCHMARK_SOURCES
quantlibbenchmark.cpp

americanoption.cpp
asianoptions.cpp
barrieroption.cpp
basketoption.cpp
batesmodel.cpp
convertiblebonds.cpp
digitaloption.cpp
dividendoption.cpp
europeanoption.cpp
fdheston.cpp
hestonmodel.cpp
interpolations.cpp
jumpdiffusion.cpp
lowdiscrepancysequences.cpp
marketmodel_cms.cpp
marketmodel_smm.cpp
preconditions.cpp preconditions.hpp
quantooption.cpp
quantlibglobalfixture.cpp quantlibglobalfixture.hpp
riskstats.cpp
shortratemodels.cpp
utilities.cpp utilities.hpp
swaptionvolstructuresutilities.hpp
)

if (QL_BUILD_TEST_SUITE)
add_executable(ql_test_suite ${QL_TEST_SOURCES} ${QL_TEST_HEADERS})
set_target_properties(ql_test_suite PROPERTIES OUTPUT_NAME "quantlib-test-suite")
set_source_files_properties(quantlibtestsuite.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION true)
target_link_libraries(ql_test_suite PRIVATE
ql_library
${QL_THREAD_LIBRARIES})
add_library(ql_test OBJECT ${QL_TEST_SOURCES} ${QL_TEST_HEADERS})
if (NOT Boost_USE_STATIC_LIBS)
target_compile_definitions(ql_test PUBLIC BOOST_ALL_DYN_LINK)
endif()
if(MSVC AND CMAKE_UNITY_BUILD)
# for Unity builds, we need to add /bigobj
target_compile_options(ql_test_suite PRIVATE "/bigobj")
target_compile_options(ql_test PUBLIC "/bigobj")
endif()
target_link_libraries(ql_test PUBLIC ql_library ${QL_THREAD_LIBRARIES})


add_executable(ql_test_suite quantlibtestsuite.cpp)
set_source_files_properties(quantlibtestsuite.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION true)
set_target_properties(ql_test_suite PROPERTIES OUTPUT_NAME "quantlib-test-suite")
target_link_libraries(ql_test_suite PRIVATE ql_test)

if (QL_INSTALL_TEST_SUITE)
install(TARGETS ql_test_suite RUNTIME DESTINATION ${QL_INSTALL_BINDIR})
endif()
add_test(NAME quantlib_test_suite COMMAND ql_test_suite --log_level=message)
endif()

IF (QL_BUILD_BENCHMARK)
add_executable(ql_benchmark ${QL_BENCHMARK_SOURCES})

add_executable(ql_benchmark quantlibbenchmark.cpp)
set_target_properties(ql_benchmark PROPERTIES OUTPUT_NAME "quantlib-benchmark")
set_source_files_properties(quantlibbenchmark.cpp PROPERTIES SKIP_UNITY_BUILD_INCLUSION true)
target_link_libraries(ql_benchmark PRIVATE
ql_library
${QL_THREAD_LIBRARIES})
if(MSVC AND CMAKE_UNITY_BUILD)
# for Unity builds, we need to add /bigobj
target_compile_options(ql_benchmark PRIVATE "/bigobj")
endif()
target_link_libraries(ql_benchmark PRIVATE ql_test)
if (QL_INSTALL_BENCHMARK)
install(TARGETS ql_benchmark RUNTIME DESTINATION ${QL_INSTALL_BINDIR})
endif()
endif()

63 changes: 11 additions & 52 deletions test-suite/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

QL_TEST_SRCS = \
quantlibtestsuite.cpp \
americanoption.cpp \
amortizingbond.cpp \
andreasenhugevolatilityinterpl.cpp \
Expand Down Expand Up @@ -174,6 +173,7 @@ QL_TEST_SRCS = \
zabr.cpp \
zerocouponswap.cpp


QL_TEST_HDRS = \
preconditions.hpp \
quantlibglobalfixture.hpp \
Expand All @@ -183,38 +183,6 @@ QL_TEST_HDRS = \

QL_TESTS = ${QL_TEST_SRCS} ${QL_TEST_HDRS}

QL_BENCHMARK_SRCS = \
quantlibbenchmark.cpp \
americanoption.cpp \
asianoptions.cpp \
barrieroption.cpp \
doublebarrieroption.cpp \
basketoption.cpp \
batesmodel.cpp \
convertiblebonds.cpp \
digitaloption.cpp \
dividendoption.cpp \
europeanoption.cpp \
fdheston.cpp \
hestonmodel.cpp \
interpolations.cpp \
jumpdiffusion.cpp \
lowdiscrepancysequences.cpp \
marketmodel_cms.cpp \
marketmodel_smm.cpp \
preconditions.cpp \
quantlibglobalfixture.cpp \
quantooption.cpp \
riskstats.cpp \
shortratemodels.cpp \
utilities.cpp

QL_BENCHMARK_HDRS = \
quantlibglobalfixture.hpp \
preconditions.hpp \
utilities.hpp

QL_BENCHMARKS = ${QL_BENCHMARK_SRCS} ${QL_BENCHMARK_HDRS}

dist-hook:
mkdir -p $(distdir)/build
Expand All @@ -234,44 +202,35 @@ endif

if UNITY_BUILD

nodist_quantlib_test_suite_SOURCES = unity_test.cpp

unity_test.cpp: Makefile.am
unity.cpp: Makefile.am
echo "/* This file is automatically generated; do not edit. */" > $@
echo "/* Add the files to be included into Makefile.am instead. */" >> $@
echo >> $@
for i in $(QL_TEST_SRCS); do \
echo "#include \"test-suite/$$i\"" >> $@; \
done

nodist_quantlib_benchmark_SOURCES = unity_benchmark.cpp quantlibbenchmark.cpp
UNITY_SRC = $(filter-out quantlibbenchmark.cpp,$(QL_BENCHMARK_SRCS))
nodist_quantlib_test_suite_SOURCES = unity.cpp
quantlib_test_suite_SOURCES = quantlibtestsuite.cpp

unity_benchmark.cpp: Makefile.am
echo "/* This file is automatically generated; do not edit. */" > $@
echo "/* Add the files to be included into Makefile.am instead. */" >> $@
echo >> $@
for i in $(UNITY_SRC); do \
echo "#include \"test-suite/$$i\"" >> $@; \
done
nodist_quantlib_benchmark_SOURCES = unity.cpp
quantlib_benchmark_SOURCES = quantlibbenchmark.cpp

EXTRA_DIST = $(QL_TESTS) $(QL_BENCHMARKS)
EXTRA_DIST = $(QL_TESTS)

else

quantlib_test_suite_SOURCES = $(QL_TESTS)

quantlib_benchmark_SOURCES = $(QL_BENCHMARKS)

quantlib_test_suite_SOURCES = $(QL_TESTS) quantlibtestsuite.cpp
quantlib_benchmark_SOURCES = $(QL_TESTS) quantlibbenchmark.cpp
EXTRA_DIST =

endif
endif

quantlib_test_suite_LDADD = ${top_builddir}/ql/libQuantLib.la \
${PTHREAD_LIB} ${BOOST_INTERPROCESS_LIB}

quantlib_benchmark_LDADD = ${top_builddir}/ql/libQuantLib.la \
${PTHREAD_LIB}
${PTHREAD_LIB} ${BOOST_INTERPROCESS_LIB}

TESTS = quantlib-test-suite$(EXEEXT)
TESTS_ENVIRONMENT = BOOST_TEST_LOG_LEVEL=message BOOST_TEST_COLOR_OUTPUT=false
Expand Down
2 changes: 1 addition & 1 deletion test-suite/americanoption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ BOOST_AUTO_TEST_CASE(testQdEngineStandardExample) {
);
const Real calculated = americanOption.NPV() - europeanOption.NPV();

const Real tol = 1e-15;
const Real tol = 7e-15;
const Real diff = std::abs(calculated - expected[i]);

if (diff > tol) {
Expand Down
Loading

0 comments on commit 14377c2

Please sign in to comment.