2020-12-15 18:18:36 +00:00
|
|
|
# Constantine
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# ############################################################
|
|
|
|
#
|
|
|
|
# Benchmark blueprint
|
|
|
|
#
|
|
|
|
# ############################################################
|
|
|
|
|
|
|
|
import
|
|
|
|
# Internal
|
2022-02-27 00:49:08 +00:00
|
|
|
../constantine/platforms/abstractions,
|
2020-12-15 18:18:36 +00:00
|
|
|
# Helpers
|
2022-01-01 15:19:35 +00:00
|
|
|
../helpers/prng_unsafe,
|
2020-12-15 18:18:36 +00:00
|
|
|
./platforms,
|
|
|
|
# Standard library
|
|
|
|
std/[monotimes, times, strformat, strutils, macros]
|
|
|
|
|
2023-01-29 00:06:37 +00:00
|
|
|
export strutils, strformat, platforms, times, monotimes, macros
|
2020-12-15 18:18:36 +00:00
|
|
|
|
|
|
|
var rng*: RngState
|
|
|
|
let seed = uint32(getTime().toUnix() and (1'i64 shl 32 - 1)) # unixTime mod 2^32
|
|
|
|
rng.seed(seed)
|
|
|
|
echo "bench xoshiro512** seed: ", seed
|
|
|
|
|
|
|
|
# warmup
|
|
|
|
proc warmup*() =
|
|
|
|
# Warmup - make sure cpu is on max perf
|
|
|
|
let start = cpuTime()
|
|
|
|
var foo = 123
|
|
|
|
for i in 0 ..< 300_000_000:
|
|
|
|
foo += i*i mod 456
|
|
|
|
foo = foo mod 789
|
|
|
|
|
|
|
|
# Compiler shouldn't optimize away the results as cpuTime rely on sideeffects
|
|
|
|
let stop = cpuTime()
|
|
|
|
echo &"Warmup: {stop - start:>4.4f} s, result {foo} (displayed to avoid compiler optimizing warmup away)\n"
|
|
|
|
|
|
|
|
warmup()
|
|
|
|
|
|
|
|
when defined(gcc):
|
|
|
|
echo "\nCompiled with GCC"
|
|
|
|
elif defined(clang):
|
|
|
|
echo "\nCompiled with Clang"
|
|
|
|
elif defined(vcc):
|
|
|
|
echo "\nCompiled with MSVC"
|
|
|
|
elif defined(icc):
|
|
|
|
echo "\nCompiled with ICC"
|
|
|
|
else:
|
|
|
|
echo "\nCompiled with an unknown compiler"
|
|
|
|
|
|
|
|
echo "Optimization level => "
|
|
|
|
echo " no optimization: ", not defined(release)
|
|
|
|
echo " release: ", defined(release)
|
|
|
|
echo " danger: ", defined(danger)
|
|
|
|
echo " inline assembly: ", UseASM_X86_64
|
|
|
|
|
2023-06-04 15:41:54 +00:00
|
|
|
when (sizeof(int) == 4) or defined(CTT_32):
|
2020-12-15 18:18:36 +00:00
|
|
|
echo "⚠️ Warning: using Constantine with 32-bit limbs"
|
|
|
|
else:
|
|
|
|
echo "Using Constantine with 64-bit limbs"
|
|
|
|
|
|
|
|
when SupportsCPUName:
|
|
|
|
echo "Running on ", cpuName(), ""
|
|
|
|
|
|
|
|
when SupportsGetTicks:
|
|
|
|
echo "\n⚠️ Cycles measurements are approximate and use the CPU nominal clock: Turbo-Boost and overclocking will skew them."
|
|
|
|
echo "i.e. a 20% overclock will be about 20% off (assuming no dynamic frequency scaling)"
|
|
|
|
|
|
|
|
echo "\n=================================================================================================================\n"
|
|
|
|
|
|
|
|
proc separator*(length: int) =
|
|
|
|
echo "-".repeat(length)
|
|
|
|
|
|
|
|
proc notes*() =
|
2021-02-14 13:24:48 +00:00
|
|
|
echo ""
|
2020-12-15 18:18:36 +00:00
|
|
|
echo "Notes:"
|
2021-02-14 13:24:48 +00:00
|
|
|
echo " - All procedures are constant-time unless mentioned otherwise (unsafe or vartime)"
|
2020-12-15 18:18:36 +00:00
|
|
|
echo " - Compilers:"
|
|
|
|
echo " Compilers are severely limited on multiprecision arithmetic."
|
2021-02-14 13:24:48 +00:00
|
|
|
echo " Constantine compile-time assembler is used by default \"nimble bench_summary_bls12_381\"."
|
2020-12-15 18:18:36 +00:00
|
|
|
echo " GCC is significantly slower than Clang on multiprecision arithmetic due to catastrophic handling of carries."
|
|
|
|
echo " GCC also seems to have issues with large temporaries and register spilling."
|
2021-02-14 13:24:48 +00:00
|
|
|
echo " This is somewhat alleviated by Constantine compile-time assembler but not perfect."
|
|
|
|
echo " Bench on specific compiler with assembler: \"nimble bench_summary_bls12_381_clang\"."
|
|
|
|
echo " Bench on specific compiler with assembler: \"nimble bench_summary_bls12_381_clang_noasm\"."
|
2020-12-15 18:18:36 +00:00
|
|
|
echo " - The simplest operations might be optimized away by the compiler."
|
|
|
|
|
|
|
|
template measure*(iters: int,
|
|
|
|
startTime, stopTime: untyped,
|
|
|
|
startClk, stopClk: untyped,
|
|
|
|
body: untyped): untyped =
|
|
|
|
let startTime = getMonotime()
|
|
|
|
when SupportsGetTicks:
|
|
|
|
let startClk = getTicks()
|
|
|
|
for _ in 0 ..< iters:
|
|
|
|
body
|
|
|
|
when SupportsGetTicks:
|
|
|
|
let stopClk = getTicks()
|
|
|
|
let stopTime = getMonotime()
|
|
|
|
|
|
|
|
when not SupportsGetTicks:
|
|
|
|
let startClk = -1'i64
|
|
|
|
let stopClk = -1'i64
|