diff --git a/bench/bench_norm.cpp b/bench/bench_norm.cpp new file mode 100644 index 000000000..76c8c574d --- /dev/null +++ b/bench/bench_norm.cpp @@ -0,0 +1,259 @@ +#include +#include "BenchTimer.h" +using namespace Eigen; +using namespace std; + +template +EIGEN_DONT_INLINE typename T::Scalar sqsumNorm(const T& v) +{ + return v.norm(); +} + +template +EIGEN_DONT_INLINE typename T::Scalar hypotNorm(const T& v) +{ + return v.stableNorm(); +} + +template +EIGEN_DONT_INLINE typename T::Scalar blueNorm(const T& v) +{ + return v.blueNorm(); +} + +template +EIGEN_DONT_INLINE typename T::Scalar lapackNorm(T& v) +{ + typedef typename T::Scalar Scalar; + int n = v.size(); + Scalar scale = 1; + Scalar ssq = 0; + for (int i=0;i +EIGEN_DONT_INLINE typename T::Scalar divacNorm(T& v) +{ + int n =v.size() / 2; + for (int i=0;i0) + { + for (int i=0;i +EIGEN_DONT_INLINE typename T::Scalar pblueNorm(const T& v) +{ + typedef typename T::Scalar Scalar; + + static int nmax; + static Scalar b1, b2, s1m, s2m, overfl, rbig, relerr; + int n; + + if(nmax <= 0) + { + int nbig, ibeta, it, iemin, iemax, iexp; + Scalar abig, eps; + + nbig = std::numeric_limits::max(); // largest integer + ibeta = NumTraits::Base; // base for floating-point numbers + it = NumTraits::Mantissa; // number of base-beta digits in mantissa + iemin = std::numeric_limits::min_exponent; // minimum exponent + iemax = std::numeric_limits::max_exponent; // maximum exponent + rbig = std::numeric_limits::max(); // largest floating-point number + + // Check the basic machine-dependent constants. + if(iemin > 1 - 2*it || 1+it>iemax || (it==2 && ibeta<5) + || (it<=4 && ibeta <= 3 ) || it<2) + { + ei_assert(false && "the algorithm cannot be guaranteed on this computer"); + } + iexp = -((1-iemin)/2); + b1 = bexp(ibeta, iexp); // lower boundary of midrange + iexp = (iemax + 1 - it)/2; + b2 = bexp(ibeta,iexp); // upper boundary of midrange + + iexp = (2-iemin)/2; + s1m = bexp(ibeta,iexp); // scaling factor for lower range + iexp = - ((iemax+it)/2); + s2m = bexp(ibeta,iexp); // scaling factor for upper range + + overfl = rbig*s2m; // overfow boundary for abig + eps = bexp(ibeta, 1-it); + relerr = ei_sqrt(eps); // tolerance for neglecting asml + abig = 1.0/eps - 1.0; + if (Scalar(nbig)>abig) nmax = abig; // largest safe n + else nmax = nbig; + } + + typedef typename ei_packet_traits::type Packet; + const int ps = ei_packet_traits::size; + Packet pasml = ei_pset1(Scalar(0)); + Packet pamed = ei_pset1(Scalar(0)); + Packet pabig = ei_pset1(Scalar(0)); + Packet ps2m = ei_pset1(s2m); + Packet ps1m = ei_pset1(s1m); + Packet pb2 = ei_pset1(b2); + Packet pb1 = ei_pset1(b1); + for(int j=0; j(j)); + Packet ax_s2m = ei_pmul(ax,ps2m); + Packet ax_s1m = ei_pmul(ax,ps1m); + Packet maskBig = ei_plt(pb2,ax); + Packet maskSml = ei_plt(ax,pb1); + pabig = ei_padd(pabig, ei_pand(maskBig, ei_pmul(ax_s2m,ax_s2m))); + pasml = ei_padd(pasml, ei_pand(maskSml, ei_pmul(ax_s1m,ax_s1m))); + pamed = ei_padd(pamed, ei_pandnot(ei_pmul(ax,ax),ei_pand(maskSml,maskBig))); + } + Scalar abig = ei_predux(pabig); + Scalar asml = ei_predux(pasml); + Scalar amed = ei_predux(pamed); + if(abig > Scalar(0)) + { + abig = ei_sqrt(abig); + if(abig > overfl) + { + ei_assert(false && "overflow"); + return rbig; + } + if(amed > Scalar(0)) + { + abig = abig/s2m; + amed = ei_sqrt(amed); + } + else + { + return abig/s2m; + } + + } + else if(asml > Scalar(0)) + { + if (amed > Scalar(0)) + { + abig = ei_sqrt(amed); + amed = ei_sqrt(asml) / s1m; + } + else + { + return ei_sqrt(asml)/s1m; + } + } + else + { + return ei_sqrt(amed); + } + asml = std::min(abig, amed); + abig = std::max(abig, amed); + if(asml <= abig*relerr) + return abig; + else + return abig * ei_sqrt(Scalar(1) + ei_abs2(asml/abig)); +} + +#define BENCH_PERF(NRM) { \ + Eigen::BenchTimer tf, td; tf.reset(); td.reset();\ + for (int k=0; k()); + double yd = based * ei_abs(ei_random()); + VectorXf vf = VectorXf::Ones(s) * yf; + VectorXd vd = VectorXd::Ones(s) * yd; + + std::cout << "reference\t" << ei_sqrt(double(s))*yf << "\t" << ei_sqrt(double(s))*yd << "\n"; + std::cout << "sqsumNorm\t" << sqsumNorm(vf) << "\t" << sqsumNorm(vd) << "\n"; + std::cout << "hypotNorm\t" << hypotNorm(vf) << "\t" << hypotNorm(vd) << "\n"; + std::cout << "blueNorm\t" << blueNorm(vf) << "\t" << blueNorm(vd) << "\n"; + std::cout << "pblueNorm\t" << pblueNorm(vf) << "\t" << pblueNorm(vd) << "\n"; + std::cout << "lapackNorm\t" << lapackNorm(vf) << "\t" << lapackNorm(vd) << "\n"; +} + +int main(int argc, char** argv) +{ + int tries = 5; + int iters = 100000; + double y = 1.1345743233455785456788e12 * ei_random(); + VectorXf v = VectorXf::Ones(1024) * y; + +// std::cerr << "Performance (out of cache):\n"; +// { +// int iters = 1; +// VectorXf vf = VectorXf::Ones(1024*1024*32) * y; +// VectorXd vd = VectorXd::Ones(1024*1024*32) * y; +// BENCH_PERF(sqsumNorm); +// BENCH_PERF(blueNorm); +// BENCH_PERF(pblueNorm); +// BENCH_PERF(lapackNorm); +// BENCH_PERF(hypotNorm); +// } +// +// std::cerr << "\nPerformance (in cache):\n"; +// { +// int iters = 100000; +// VectorXf vf = VectorXf::Ones(512) * y; +// VectorXd vd = VectorXd::Ones(512) * y; +// BENCH_PERF(sqsumNorm); +// BENCH_PERF(blueNorm); +// BENCH_PERF(pblueNorm); +// BENCH_PERF(lapackNorm); +// BENCH_PERF(hypotNorm); +// } + + int s = 10000; + double basef_ok = 1.1345743233455785456788e12; + double based_ok = 1.1345743233455785456788e32; + + double basef_under = 1.1345743233455785456788e-23; + double based_under = 1.1345743233455785456788e-180; + + double basef_over = 1.1345743233455785456788e+27; + double based_over = 1.1345743233455785456788e+185; + + std::cout.precision(20); + + std::cerr << "\nNo under/overflow:\n"; + check_accuracy(basef_ok, based_ok, s); + + std::cerr << "\nUnderflow:\n"; + check_accuracy(basef_under, based_under, 1); + + std::cerr << "\nOverflow:\n"; + check_accuracy(basef_over, based_over, s); +}