Skip to content

Commit

Permalink
Added templated function run_benchmark<group_type...>(lambda)t.
Browse files Browse the repository at this point in the history
It initializes samples array of given template types and runs series of
calls to lambda. Samples are traversed with strides so they do not sit
in cache.

After the benchmark the statistics are printed to std::cout.

renamed folder, moved profiler to benchmark_tools

Moved scoped profiler to benchmark_tools

Fix warnings

Doubled in place

Moved benchmarks upper
  • Loading branch information
vo-nil committed Aug 13, 2024
1 parent 8fb1f02 commit 5b7e1ff
Show file tree
Hide file tree
Showing 28 changed files with 601 additions and 469 deletions.
4 changes: 4 additions & 0 deletions libs/algebra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ cm_add_test_subdirectory(test)
if(BUILD_EXAMPLES)
add_subdirectory(example)
endif()

if(BUILD_BENCH_TESTS)
add_subdirectory(bench)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include(CMTest)

add_custom_target(algebra_runtime_bench_tests)

macro(define_runtime_algebra_test name)
macro(define_algebra_benchmark name)
set(test_name "algebra_${name}_bench_test")
add_dependencies(algebra_runtime_bench_tests ${test_name})

Expand All @@ -22,7 +22,13 @@ macro(define_runtime_algebra_test name)

${Boost_INCLUDE_DIRS})

set_target_properties(${test_name} PROPERTIES CXX_STANDARD 17
target_link_libraries(${test_name}
${CMAKE_WORKSPACE_NAME}::benchmark_tools
)

set_target_properties(${test_name}
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED TRUE)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
Expand All @@ -31,16 +37,16 @@ macro(define_runtime_algebra_test name)
target_compile_options(${test_name} PRIVATE "-fconstexpr-ops-limit=4294967295")
endif()

target_compile_definitions(${test_name} PRIVATE TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data/")
endmacro()

set(RUNTIME_TESTS_NAMES
set(BENCHMARK_NAMES
"bench_curves"
"bench_fields"
"bench_multiexp"
)
)

foreach(TEST_NAME ${RUNTIME_TESTS_NAMES})
define_runtime_algebra_test(${TEST_NAME})
foreach(BENCH_NAME ${BENCHMARK_NAMES})
define_algebra_benchmark(${BENCH_NAME})
endforeach()


162 changes: 162 additions & 0 deletions libs/algebra/bench/bench_curves.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2024 Martun Karapetyan <[email protected]>
// Copyright (c) 2024 Vasiliy Olekhov <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#define BOOST_TEST_MODULE algebra_curves_bench_test

#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/data/monomorphic.hpp>

#include <boost/test/execution_monitor.hpp>

#include <boost/mpl/list.hpp>

#include <iostream>

#include <nil/crypto3/algebra/curves/alt_bn128.hpp>
#include <nil/crypto3/algebra/curves/bls12.hpp>
#include <nil/crypto3/algebra/curves/jubjub.hpp>
#include <nil/crypto3/algebra/curves/babyjubjub.hpp>
#include <nil/crypto3/algebra/curves/mnt4.hpp>
#include <nil/crypto3/algebra/curves/mnt6.hpp>
#include <nil/crypto3/algebra/curves/secp_k1.hpp>
#include <nil/crypto3/algebra/curves/secp_r1.hpp>
#include <nil/crypto3/algebra/curves/ed25519.hpp>
#include <nil/crypto3/algebra/curves/curve25519.hpp>
#include <nil/crypto3/algebra/curves/vesta.hpp>
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include <nil/crypto3/algebra/type_traits.hpp>
#include <nil/crypto3/bench/benchmark.hpp>

using namespace nil::crypto3::algebra;
using namespace nil::crypto3::bench;

template <typename curve_type>
void benchmark_curve_operations(std::string const& curve_name)
{
using g1_type = typename curve_type::template g1_type<>;
using base_field = typename curve_type::base_field_type;
using scalar_field = typename curve_type::scalar_field_type;

run_benchmark<base_field, base_field>(
curve_name + " Fp addition",
[](typename base_field::value_type& A, typename base_field::value_type const& B) {
return A += B;
});
run_benchmark<base_field, base_field>(
curve_name + " Fp multiplication",
[](typename base_field::value_type& A, typename base_field::value_type const& B) {
return A *= B;
});
run_benchmark<base_field>(
curve_name + " Fp inverse",
[](typename base_field::value_type& A) {
return A.inversed();
});
run_benchmark<scalar_field, scalar_field>(
curve_name + " Fq addition",
[](typename scalar_field::value_type& A, typename scalar_field::value_type const& B) {
return A += B;
});
run_benchmark<scalar_field, scalar_field>(
curve_name + " Fq multiplication",
[](typename scalar_field::value_type& A, typename scalar_field::value_type const& B) {
return A *= B;
});
run_benchmark<scalar_field>(
curve_name + " Fq inverse",
[](typename scalar_field::value_type& A) {
return A.inversed();
});
run_benchmark<g1_type, g1_type>(
curve_name + " G1 addition",
[](typename g1_type::value_type& A, typename g1_type::value_type const& B) {
return A += B;
});
run_benchmark<g1_type>(
curve_name + " G1 doubling",
[](typename g1_type::value_type& A) {
A.double_inplace();// += A;
return A;
});
run_benchmark<g1_type, scalar_field>(
curve_name + " G1 scalar multiplication",
[](typename g1_type::value_type& A, typename scalar_field::value_type const& B) {
return A *= B;
});

if constexpr (has_template_g2_type<curve_type>::value) {
using g2_type = typename curve_type::template g2_type<>;
run_benchmark<g2_type, g2_type>(
curve_name + " G2 addition",
[](typename g2_type::value_type& A, typename g2_type::value_type const& B) {
return A += B;
});
run_benchmark<g2_type>(
curve_name + " G2 doubling",
[](typename g2_type::value_type& A) {
A.double_inplace();
return A;
//return A += A;
});
run_benchmark<g2_type, scalar_field>(
curve_name + " G2 scalar multiplication",
[](typename g2_type::value_type& A, typename scalar_field::value_type const& B) {
return A *= B;
});
} else {
std::cout << "Curve " << curve_name << " does not have G2, skipping benchmarks" << std::endl;
}
}

BOOST_AUTO_TEST_SUITE(curves_benchmark)

BOOST_AUTO_TEST_CASE(pallas)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::pallas>("Pallas");
}

BOOST_AUTO_TEST_CASE(vesta)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::vesta>("Vesta");
}

BOOST_AUTO_TEST_CASE(bls12_381)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::bls12<381>>("BLS12-381");
}

BOOST_AUTO_TEST_CASE(mnt4_298)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::mnt4<298>>("MNT4-298");
}

BOOST_AUTO_TEST_CASE(mnt6_298)
{
benchmark_curve_operations<nil::crypto3::algebra::curves::mnt6<298>>("MNT6-298");
}


BOOST_AUTO_TEST_SUITE_END()
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void run_perf_test(std::string const& field_name) {

std::ofstream f(filename, std::ofstream::out);
f << "# " << typeid(Field).name() << std::endl;
f << "sum,mul,sqr,inv" << std::endl;
f << "sum,mul,sub,sqr,inv" << std::endl;

for(size_t i = 0; i < plus_results.size(); ++i) {
f << std::fixed << std::setprecision(3) << plus_results[i].count() << ","
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion libs/algebra/example/short_weierstrass_coordinates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ void coordinates_examples() {
std::cout << "c2 value: " << (c2) << std::endl;
std::cout << "c1 + c2 value: " << (c1 + c2) << std::endl;
std::cout << "c1 - c2 value: " << (c1 - c2) << std::endl;
std::cout << "Doubled c1 value: " << (c1.doubled()) << std::endl;
c1.double_inplace();
std::cout << "Doubled c1 value: " << c1 << std::endl;
}

int main() {
Expand Down
2 changes: 2 additions & 0 deletions libs/algebra/include/nil/crypto3/algebra/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ namespace nil {
BOOST_TTI_HAS_TYPE(g2_type)
BOOST_TTI_HAS_TYPE(gt_type)

BOOST_TTI_HAS_TEMPLATE(g2_type)

BOOST_TTI_HAS_TYPE(group_type)

BOOST_TTI_HAS_STATIC_MEMBER_DATA(value_bits)
Expand Down
4 changes: 0 additions & 4 deletions libs/algebra/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ endforeach()
foreach(TEST_NAME ${COMPILE_TIME_TESTS_NAMES})
define_compile_time_algebra_test(${TEST_NAME})
endforeach()

if(BUILD_BENCH_TESTS)
cm_add_test_subdirectory(bench_test)
endif()
Loading

0 comments on commit 5b7e1ff

Please sign in to comment.