From bd7d1a438af3d0ca92b605786cbbc9cd79dc93e5 Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Wed, 10 May 2023 11:02:18 +0100 Subject: [PATCH] scalar: Added optimised integer powers and roots --- applications/test/integerPow/Make/files | 3 + applications/test/integerPow/Make/options | 0 .../test/integerPow/Test-integerPow.C | 87 +++++++++++++++++++ .../fields/Fields/scalarField/scalarField.C | 8 +- .../fields/Fields/scalarField/scalarField.H | 9 +- .../primitives/Scalar/scalar/scalar.H | 11 +++ .../primitives/Scalar/scalar/scalarI.H | 74 ++++++++++++++++ .../reaction/specieExponent/specieExponentI.H | 25 +----- 8 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 applications/test/integerPow/Make/files create mode 100644 applications/test/integerPow/Make/options create mode 100644 applications/test/integerPow/Test-integerPow.C create mode 100644 src/OpenFOAM/primitives/Scalar/scalar/scalarI.H diff --git a/applications/test/integerPow/Make/files b/applications/test/integerPow/Make/files new file mode 100644 index 0000000000..8e07339c39 --- /dev/null +++ b/applications/test/integerPow/Make/files @@ -0,0 +1,3 @@ +Test-integerPow.C + +EXE = $(FOAM_USER_APPBIN)/Test-integerPow diff --git a/applications/test/integerPow/Make/options b/applications/test/integerPow/Make/options new file mode 100644 index 0000000000..e69de29bb2 diff --git a/applications/test/integerPow/Test-integerPow.C b/applications/test/integerPow/Test-integerPow.C new file mode 100644 index 0000000000..b6d58654d6 --- /dev/null +++ b/applications/test/integerPow/Test-integerPow.C @@ -0,0 +1,87 @@ +#include "cpuTime.H" +#include "Pair.H" +#include "Random.H" + +using namespace Foam; + +//- Number of calculations to do. Need 10^8-ish to get enough time (~2 seconds) +// to meaningfully compare. +static const label n = 100000000; + +//- Range of exponents to calculate. From -E to +E. Zero will be omitted so +// that we can do roots, too. At small values (<8) integer powers win. At +// larger values (>16), scalar powers do better. +static const label E = 4; + +//- Run a given power method +template +Pair run(Pow pow) +{ + Random rndGen(0); + + cpuTime time; + + scalar y = 0; + + for (label i = 0; i < n; ++ i) + { + const scalar x = rndGen.sampleAB(rootSmall, rootGreat); + const label e = + rndGen.sample01