/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./tests/Test_simd.cc Copyright (C) 2015 Author: Peter Boyle Author: neo This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. See the full license in the file "LICENSE" in the top level distribution directory *************************************************************************************/ /* END LEGAL */ #include using namespace std; using namespace Grid; using namespace Grid::QCD; class funcPlus { public: funcPlus() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = i1+i2;} std::string name(void) const { return std::string("Plus"); } }; class funcMinus { public: funcMinus() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = i1-i2;} std::string name(void) const { return std::string("Minus"); } }; class funcTimes { public: funcTimes() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = i1*i2;} std::string name(void) const { return std::string("Times"); } }; class funcDivide { public: funcDivide() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = i1/i2;} std::string name(void) const { return std::string("Divide"); } }; class funcConj { public: funcConj() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = conjugate(i1);} std::string name(void) const { return std::string("Conj"); } }; class funcAdj { public: funcAdj() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = adj(i1);} std::string name(void) const { return std::string("Adj"); } }; class funcImag { public: funcImag() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = imag(i1);} std::string name(void) const { return std::string("imag"); } }; class funcReal { public: funcReal() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = real(i1);} std::string name(void) const { return std::string("real"); } }; class funcTimesI { public: funcTimesI() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = timesI(i1);} std::string name(void) const { return std::string("timesI"); } }; class funcTimesMinusI { public: funcTimesMinusI() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = timesMinusI(i1);} std::string name(void) const { return std::string("timesMinusI"); } }; class funcInnerProduct { public: funcInnerProduct() {}; template void operator()(vec &rr,vec &i1,vec &i2) const { rr = innerProduct(i1,i2);} std::string name(void) const { return std::string("innerProduct"); } }; // FIXME still to test: // // innerProduct, // norm2, // Reduce, // // mac,mult,sub,add, vone,vzero,vcomplex_i, =zero, // vset,vsplat,vstore,vstream,vload, scalar*vec, vec*scalar // unary -, // *= , -=, += // outerproduct, // zeroit // permute class funcReduce { public: funcReduce() {}; template void vfunc(reduce &rr,vec &i1,vec &i2) const { rr = Reduce(i1);} template void sfunc(reduce &rr,scal &i1,scal &i2) const { rr = i1;} std::string name(void) const { return std::string("Reduce"); } }; template void Tester(const functor &func) { GridSerialRNG sRNG; sRNG.SeedRandomDevice(); int Nsimd = vec::Nsimd(); std::vector input1(Nsimd); std::vector input2(Nsimd); std::vector result(Nsimd); std::vector reference(Nsimd); std::vector > buf(3); vec & v_input1 = buf[0]; vec & v_input2 = buf[1]; vec & v_result = buf[2]; for(int i=0;i(v_input1,input1); merge(v_input2,input2); merge(v_result,result); func(v_result,v_input1,v_input2); for(int i=0;i(v_result,result); std::cout << GridLogMessage << " " << func.name() << std::endl; std::cout << GridLogDebug << v_input1 << std::endl; std::cout << GridLogDebug << v_input2 << std::endl; std::cout << GridLogDebug << v_result << std::endl; int ok=0; for(int i=0;i1.0e-7){ std::cout< void IntTester(const functor &func) { typedef Integer scal; typedef vInteger vec; GridSerialRNG sRNG; sRNG.SeedRandomDevice(); int Nsimd = vec::Nsimd(); std::vector input1(Nsimd); std::vector input2(Nsimd); std::vector result(Nsimd); std::vector reference(Nsimd); std::vector > buf(3); vec & v_input1 = buf[0]; vec & v_input2 = buf[1]; vec & v_result = buf[2]; for(int i=0;i(v_input1,input1); merge(v_input2,input2); merge(v_result,result); func(v_result,v_input1,v_input2); for(int i=0;i(v_result,result); std::cout << GridLogMessage << " " << func.name() << std::endl; std::cout << GridLogDebug << v_input1 << std::endl; std::cout << GridLogDebug << v_input2 << std::endl; std::cout << GridLogDebug << v_result << std::endl; int ok=0; for(int i=0;i void ReductionTester(const functor &func) { GridSerialRNG sRNG; sRNG.SeedRandomDevice(); int Nsimd = vec::Nsimd(); std::vector input1(Nsimd); std::vector input2(Nsimd); reduced result(0); reduced reference(0); reduced tmp; std::vector > buf(3); vec & v_input1 = buf[0]; vec & v_input2 = buf[1]; for(int i=0;i(v_input1,input1); merge(v_input2,input2); func.template vfunc(result,v_input1,v_input2); for(int i=0;i(tmp,input1[i],input2[i]); reference+=tmp; } std::cout< 1.0e-6 ){ // rounding is possible for reduce order std::cout< void operator()(vec &rr,vec &i1,vec &i2) const { permute(rr,i1,n);} template void apply(std::vector &rr,std::vector &in) const { int sz=in.size(); int msk = sz>>(n+1); for(int i=0;i void operator()(vec &rr,vec &i1,vec &i2) const { rr=rotate(i1,n);} template void apply(std::vector &rr,std::vector &in) const { int sz = in.size(); for(int i=0;i void PermTester(const functor &func) { GridSerialRNG sRNG; sRNG.SeedRandomDevice(); int Nsimd = vec::Nsimd(); std::vector input1(Nsimd); std::vector input2(Nsimd); std::vector result(Nsimd); std::vector reference(Nsimd); std::vector > buf(3); vec & v_input1 = buf[0]; vec & v_input2 = buf[1]; vec & v_result = buf[2]; for(int i=0;i(v_input1,input1); merge(v_input2,input2); merge(v_result,result); func(v_result,v_input1,v_input2); func.apply(reference,input1); extract(v_result,result); std::cout<1.0e-7){ std::cout< latt_size = GridDefaultLatt(); std::vector simd_layout = GridDefaultSimd(4,vComplex::Nsimd()); std::vector mpi_layout = GridDefaultMpi(); GridCartesian Grid(latt_size,simd_layout,mpi_layout); std::vector seeds({1,2,3,4}); // Insist that operations on random scalars gives // identical results to on vectors. std::cout << GridLogMessage <<"==================================="<< std::endl; std::cout << GridLogMessage <<"Testing vRealF "<(funcPlus()); Tester(funcMinus()); Tester(funcTimes()); Tester(funcDivide()); Tester(funcAdj()); Tester(funcConj()); Tester(funcInnerProduct()); ReductionTester(funcReduce()); std::cout<(funcPermute(i)); } std::cout<(funcRotate(r)); } std::cout << GridLogMessage <<"==================================="<< std::endl; std::cout << GridLogMessage <<"Testing vRealD "<(funcPlus()); Tester(funcMinus()); Tester(funcTimes()); Tester(funcDivide()); Tester(funcAdj()); Tester(funcConj()); Tester(funcInnerProduct()); ReductionTester(funcReduce()); std::cout<(funcPermute(i)); } std::cout<(funcRotate(r)); } std::cout << GridLogMessage <<"==================================="<< std::endl; std::cout << GridLogMessage <<"Testing vComplexF "<(funcTimesI()); Tester(funcTimesMinusI()); Tester(funcPlus()); Tester(funcMinus()); Tester(funcTimes()); Tester(funcConj()); Tester(funcAdj()); Tester(funcReal()); Tester(funcImag()); Tester(funcInnerProduct()); ReductionTester(funcReduce()); std::cout<(funcPermute(i)); } std::cout<(funcRotate(r)); } std::cout<(funcTimesI()); Tester(funcTimesMinusI()); Tester(funcPlus()); Tester(funcMinus()); Tester(funcTimes()); Tester(funcConj()); Tester(funcAdj()); Tester(funcReal()); Tester(funcImag()); Tester(funcInnerProduct()); ReductionTester(funcReduce()); std::cout << GridLogMessage << "===================================" << std::endl; std::cout << GridLogMessage << "Testing vComplexD permutes " << std::endl; std::cout << GridLogMessage << "===================================" << std::endl; // Log2 iteration for (int i = 0; (1 << i) < vComplexD::Nsimd(); i++) { PermTester(funcPermute(i)); } std::cout<(funcRotate(r)); } std::cout<