From 2843264bd859404c09d8415b438ffb8b53c0ed02 Mon Sep 17 00:00:00 2001 From: Peter Boyle Date: Mon, 18 May 2015 12:09:25 +0100 Subject: [PATCH] Remez tested --- lib/algorithms/approx/Remez.cc | 6 +- lib/algorithms/approx/bigfloat.h | 164 +----------------------- lib/algorithms/approx/bigfloat_double.h | 162 +++++++++++++++++++++++ lib/parallelIO/GridNerscIO.h | 2 +- scripts/configure-commands | 28 ++-- tests/Grid_remez.cc | 103 +++++++++++++++ tests/Makefile.am | 5 +- 7 files changed, 290 insertions(+), 180 deletions(-) create mode 100644 lib/algorithms/approx/bigfloat_double.h create mode 100644 tests/Grid_remez.cc diff --git a/lib/algorithms/approx/Remez.cc b/lib/algorithms/approx/Remez.cc index a787e766..4ec2270d 100755 --- a/lib/algorithms/approx/Remez.cc +++ b/lib/algorithms/approx/Remez.cc @@ -116,7 +116,7 @@ double AlgRemez::generateApprox(int num_degree, int den_degree, int a_len, double *a_param, int *a_pow) { std::cout<<"Degree of the approximation is ("<= tolerance); search(step); diff --git a/lib/algorithms/approx/bigfloat.h b/lib/algorithms/approx/bigfloat.h index 7f59ea33..aee71d03 100755 --- a/lib/algorithms/approx/bigfloat.h +++ b/lib/algorithms/approx/bigfloat.h @@ -10,7 +10,7 @@ #ifndef INCLUDED_BIGFLOAT_H #define INCLUDED_BIGFLOAT_H -#ifdef HAVE_GMP + #include #include #include @@ -202,167 +202,5 @@ public: friend int sgn(const bigfloat&); }; -#else - -typedef double mfloat; -class bigfloat { -private: - - mfloat x; - -public: - - bigfloat() { } - bigfloat(const bigfloat& y) { x=y.x; } - bigfloat(const unsigned long u) { x=u; } - bigfloat(const long i) { x=i; } - bigfloat(const int i) { x=i;} - bigfloat(const float d) { x=d;} - bigfloat(const double d) { x=d;} - bigfloat(const char *str) { x=std::stod(std::string(str));} - ~bigfloat(void) { } - operator double (void) const { return (double)x; } - static void setDefaultPrecision(unsigned long dprec) { - } - - void setPrecision(unsigned long dprec) { - } - - unsigned long getPrecision(void) const { return 64; } - unsigned long getDefaultPrecision(void) const { return 64; } - - bigfloat& operator=(const bigfloat& y) { x=y.x; return *this; } - bigfloat& operator=(const unsigned long y) { x=y; return *this; } - bigfloat& operator=(const signed long y) { x=y; return *this; } - bigfloat& operator=(const float y) { x=y; return *this; } - bigfloat& operator=(const double y) { x=y; return *this; } - - size_t write(void); - size_t read(void); - - /* Arithmetic Functions */ - - bigfloat& operator+=(const bigfloat& y) { return *this = *this + y; } - bigfloat& operator-=(const bigfloat& y) { return *this = *this - y; } - bigfloat& operator*=(const bigfloat& y) { return *this = *this * y; } - bigfloat& operator/=(const bigfloat& y) { return *this = *this / y; } - - friend bigfloat operator+(const bigfloat& x, const bigfloat& y) { - bigfloat a; - a.x=x.x+y.x; - return a; - } - - friend bigfloat operator+(const bigfloat& x, const unsigned long y) { - bigfloat a; - a.x=x.x+y; - return a; - } - - friend bigfloat operator-(const bigfloat& x, const bigfloat& y) { - bigfloat a; - a.x=x.x-y.x; - return a; - } - - friend bigfloat operator-(const unsigned long x, const bigfloat& y) { - bigfloat bx(x); - return bx-y; - } - - friend bigfloat operator-(const bigfloat& x, const unsigned long y) { - bigfloat by(y); - return x-by; - } - - friend bigfloat operator-(const bigfloat& x) { - bigfloat a; - a.x=-x.x; - return a; - } - - friend bigfloat operator*(const bigfloat& x, const bigfloat& y) { - bigfloat a; - a.x=x.x*y.x; - return a; - } - - friend bigfloat operator*(const bigfloat& x, const unsigned long y) { - bigfloat a; - a.x=x.x*y; - return a; - } - - friend bigfloat operator/(const bigfloat& x, const bigfloat& y){ - bigfloat a; - a.x=x.x/y.x; - return a; - } - - friend bigfloat operator/(const unsigned long x, const bigfloat& y){ - bigfloat bx(x); - return bx/y; - } - - friend bigfloat operator/(const bigfloat& x, const unsigned long y){ - bigfloat by(y); - return x/by; - } - - friend bigfloat sqrt_bf(const bigfloat& x){ - bigfloat a; - a.x= sqrt(x.x); - return a; - } - - friend bigfloat sqrt_bf(const unsigned long x){ - bigfloat a(x); - return sqrt_bf(a); - } - - friend bigfloat abs_bf(const bigfloat& x){ - bigfloat a; - a.x=abs(x.x); - return a; - } - - friend bigfloat pow_bf(const bigfloat& a, long power) { - bigfloat b; - b.x=pow(a.x,power); - return b; - } - - friend bigfloat pow_bf(const bigfloat& a, bigfloat &power) { - bigfloat b; - b.x=pow(a.x,power.x); - return b; - } - - friend bigfloat exp_bf(const bigfloat& a) { - bigfloat b; - b.x=exp(a.x); - return b; - } - - /* Comparison Functions */ - friend int operator>(const bigfloat& x, const bigfloat& y) { - return x.x>y.x; - } - - friend int operator<(const bigfloat& x, const bigfloat& y) { - return x.x=0 ) return 1; - else return 0; - } - - /* Miscellaneous Functions */ - - // friend bigfloat& random(void); -}; - -#endif #endif diff --git a/lib/algorithms/approx/bigfloat_double.h b/lib/algorithms/approx/bigfloat_double.h new file mode 100644 index 00000000..d928b4a2 --- /dev/null +++ b/lib/algorithms/approx/bigfloat_double.h @@ -0,0 +1,162 @@ +#include + +typedef double mfloat; +class bigfloat { +private: + + mfloat x; + +public: + + bigfloat() { } + bigfloat(const bigfloat& y) { x=y.x; } + bigfloat(const unsigned long u) { x=u; } + bigfloat(const long i) { x=i; } + bigfloat(const int i) { x=i;} + bigfloat(const float d) { x=d;} + bigfloat(const double d) { x=d;} + bigfloat(const char *str) { x=std::stod(std::string(str));} + ~bigfloat(void) { } + operator double (void) const { return (double)x; } + static void setDefaultPrecision(unsigned long dprec) { + } + + void setPrecision(unsigned long dprec) { + } + + unsigned long getPrecision(void) const { return 64; } + unsigned long getDefaultPrecision(void) const { return 64; } + + bigfloat& operator=(const bigfloat& y) { x=y.x; return *this; } + bigfloat& operator=(const unsigned long y) { x=y; return *this; } + bigfloat& operator=(const signed long y) { x=y; return *this; } + bigfloat& operator=(const float y) { x=y; return *this; } + bigfloat& operator=(const double y) { x=y; return *this; } + + size_t write(void); + size_t read(void); + + /* Arithmetic Functions */ + + bigfloat& operator+=(const bigfloat& y) { return *this = *this + y; } + bigfloat& operator-=(const bigfloat& y) { return *this = *this - y; } + bigfloat& operator*=(const bigfloat& y) { return *this = *this * y; } + bigfloat& operator/=(const bigfloat& y) { return *this = *this / y; } + + friend bigfloat operator+(const bigfloat& x, const bigfloat& y) { + bigfloat a; + a.x=x.x+y.x; + return a; + } + + friend bigfloat operator+(const bigfloat& x, const unsigned long y) { + bigfloat a; + a.x=x.x+y; + return a; + } + + friend bigfloat operator-(const bigfloat& x, const bigfloat& y) { + bigfloat a; + a.x=x.x-y.x; + return a; + } + + friend bigfloat operator-(const unsigned long x, const bigfloat& y) { + bigfloat bx(x); + return bx-y; + } + + friend bigfloat operator-(const bigfloat& x, const unsigned long y) { + bigfloat by(y); + return x-by; + } + + friend bigfloat operator-(const bigfloat& x) { + bigfloat a; + a.x=-x.x; + return a; + } + + friend bigfloat operator*(const bigfloat& x, const bigfloat& y) { + bigfloat a; + a.x=x.x*y.x; + return a; + } + + friend bigfloat operator*(const bigfloat& x, const unsigned long y) { + bigfloat a; + a.x=x.x*y; + return a; + } + + friend bigfloat operator/(const bigfloat& x, const bigfloat& y){ + bigfloat a; + a.x=x.x/y.x; + return a; + } + + friend bigfloat operator/(const unsigned long x, const bigfloat& y){ + bigfloat bx(x); + return bx/y; + } + + friend bigfloat operator/(const bigfloat& x, const unsigned long y){ + bigfloat by(y); + return x/by; + } + + friend bigfloat sqrt_bf(const bigfloat& x){ + bigfloat a; + a.x= sqrt(x.x); + return a; + } + + friend bigfloat sqrt_bf(const unsigned long x){ + bigfloat a(x); + return sqrt_bf(a); + } + + friend bigfloat abs_bf(const bigfloat& x){ + bigfloat a; + a.x=fabs(x.x); + return a; + } + + friend bigfloat pow_bf(const bigfloat& a, long power) { + bigfloat b; + b.x=pow(a.x,power); + return b; + } + + friend bigfloat pow_bf(const bigfloat& a, bigfloat &power) { + bigfloat b; + b.x=pow(a.x,power.x); + return b; + } + + friend bigfloat exp_bf(const bigfloat& a) { + bigfloat b; + b.x=exp(a.x); + return b; + } + + /* Comparison Functions */ + friend int operator>(const bigfloat& x, const bigfloat& y) { + return x.x>y.x; + } + + friend int operator<(const bigfloat& x, const bigfloat& y) { + return x.x=0 ) return 1; + else return 0; + } + + /* Miscellaneous Functions */ + + // friend bigfloat& random(void); +}; + + diff --git a/lib/parallelIO/GridNerscIO.h b/lib/parallelIO/GridNerscIO.h index 4094ab70..8a1775f1 100644 --- a/lib/parallelIO/GridNerscIO.h +++ b/lib/parallelIO/GridNerscIO.h @@ -481,7 +481,7 @@ inline void writeNerscObject(Lattice &Umu,std::string file,munger munge,in // Now write the body ////////////////////////////////////////////////// { - std::ofstream fout(file,std::ios::binary|std::ios::in); + std::ofstream fout(file,std::ios::binary|std::ios::out); fout.seekp(offset); Umu = zero; diff --git a/scripts/configure-commands b/scripts/configure-commands index 5e83fa89..89b72294 100755 --- a/scripts/configure-commands +++ b/scripts/configure-commands @@ -17,46 +17,46 @@ echo -e $YELLOW case $WD in g++-sse4) - CXX=g++-5 ../../configure --enable-simd=SSE4 CXXFLAGS="-msse4 -O3 -std=c++11" --enable-comms=none + CXX=g++-5 ../../configure --enable-simd=SSE4 CXXFLAGS="-msse4 -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; g++-avx) - CXX=g++-5 ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" --enable-comms=none + CXX=g++-5 ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; icpc-avx) - CXX=icpc ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" --enable-comms=none + CXX=icpc ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; icpc-avx2) - CXX=icpc ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -std=c++11" --enable-comms=none + CXX=icpc ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; icpc-avx512) - CXX=icpc ../../configure --enable-simd=AVX512 CXXFLAGS="-xCOMMON-AVX512 -O3 -std=c++11" --host=none --enable-comms=none + CXX=icpc ../../configure --enable-simd=AVX512 CXXFLAGS="-xCOMMON-AVX512 -O3 -std=c++11" --host=none LIBS="-lgmp -lmpfr" --enable-comms=none ;; icpc-mic) - CXX=icpc ../../configure --host=none --enable-simd=AVX512 CXXFLAGS="-mmic -O3 -std=c++11" LDFLAGS=-mmic --enable-comms=none + CXX=icpc ../../configure --host=none --enable-simd=AVX512 CXXFLAGS="-mmic -O3 -std=c++11" LDFLAGS=-mmic LIBS="-lgmp -lmpfr" --enable-comms=none ;; clang-avx) -CXX=clang++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" --enable-comms=none +CXX=clang++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; clang-avx2) -CXX=clang++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -std=c++11" --enable-comms=none +CXX=clang++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -std=c++11" LIBS="-lgmp -lmpfr" --enable-comms=none ;; clang-avx-openmp) -CXX=clang-omp++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -fopenmp -O3 -std=c++11" LDFLAGS="-fopenmp" --enable-comms=none +CXX=clang-omp++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -fopenmp -O3 -std=c++11" LDFLAGS="-fopenmp" LIBS="-lgmp -lmpfr" --enable-comms=none ;; clang-avx2-openmp) -CXX=clang-omp++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -fopenmp -O3 -std=c++11" LDFLAGS="-fopenmp" --enable-comms=none +CXX=clang-omp++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -fopenmp -O3 -std=c++11" LDFLAGS="-fopenmp" LIBS="-lgmp -lmpfr" --enable-comms=none ;; clang-avx-openmp-mpi) -CXX=clang-omp++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -fopenmp -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx -fopenmp" --enable-comms=mpi +CXX=clang-omp++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -fopenmp -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx -fopenmp" LIBS="-lgmp -lmpfr" --enable-comms=mpi ;; clang-avx2-openmp-mpi) -CXX=clang-omp++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -fopenmp -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx -fopenmp" --enable-comms=mpi +CXX=clang-omp++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -fopenmp -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx -fopenmp" LIBS="-lgmp -lmpfr" --enable-comms=mpi ;; clang-avx-mpi) -CXX=clang++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx " --enable-comms=mpi +CXX=clang++ ../../configure --enable-simd=AVX CXXFLAGS="-mavx -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx " LIBS="-lgmp -lmpfr" --enable-comms=mpi ;; clang-avx2-mpi) -CXX=clang++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx " --enable-comms=mpi +CXX=clang++ ../../configure --enable-simd=AVX2 CXXFLAGS="-mavx2 -mfma -O3 -I/opt/local/include/openmpi-mp/ -std=c++11" LDFLAGS=-L/opt/local/lib/openmpi-mp/ LIBS="-lmpi -lmpi_cxx " LIBS="-lgmp -lmpfr" --enable-comms=mpi ;; esac echo -e $NORMAL diff --git a/tests/Grid_remez.cc b/tests/Grid_remez.cc new file mode 100644 index 00000000..b5993917 --- /dev/null +++ b/tests/Grid_remez.cc @@ -0,0 +1,103 @@ +#include + +using namespace std; +using namespace Grid; +using namespace Grid::QCD; + +class MultiShiftFunction { +public: + std::vector poles; + std::vector residues; + double norm; + double lo,hi; + MultiShiftFunction(int n,double _lo,double _hi): poles(n), residues(n), lo(_lo), hi(_hi) {;}; + double approx(double x); + void csv(std::ostream &out); + void gnuplot(std::ostream &out); +}; +double MultiShiftFunction::approx(double x) +{ + double a = norm; + for(int n=0;n