diff --git a/Grid/algorithms/Algorithms.h b/Grid/algorithms/Algorithms.h index ef147c53..b716c48f 100644 --- a/Grid/algorithms/Algorithms.h +++ b/Grid/algorithms/Algorithms.h @@ -48,6 +48,12 @@ Author: Peter Boyle #include #include #include +#include +#include +#include +#include +#include +#include #include #include #include diff --git a/Grid/algorithms/CoarsenedMatrix.h b/Grid/algorithms/CoarsenedMatrix.h index 8af8d7ac..a6c6c030 100644 --- a/Grid/algorithms/CoarsenedMatrix.h +++ b/Grid/algorithms/CoarsenedMatrix.h @@ -211,6 +211,7 @@ namespace Grid { for(int b=0;b compressor; + Stencil.HaloExchange(in,compressor); + + auto point = [dir, disp](){ + if(dir == 0 and disp == 0) + return 8; + else + return (4 * dir + 1 - disp) / 2; + }(); + + parallel_for(int ss=0;ssoSites();ss++){ + siteVector res = zero; + siteVector nbr; + int ptype; + StencilEntry *SE; + + SE=Stencil.GetEntry(ptype,point,ss); + + if(SE->_is_local&&SE->_permute) { + permute(nbr,in._odata[SE->_offset],ptype); + } else if(SE->_is_local) { + nbr = in._odata[SE->_offset]; + } else { + nbr = Stencil.CommBuf()[SE->_offset]; + } + + res = res + A[point]._odata[ss]*nbr; + + vstream(out._odata[ss],res); + } + }; + + void Mdiag(const CoarseVector &in, CoarseVector &out){ + Mdir(in, out, 0, 0); // use the self coupling (= last) point of the stencil + }; CoarsenedMatrix(GridCartesian &CoarseGrid) : @@ -417,7 +463,7 @@ namespace Grid { std::cout< + +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 */ +#ifndef GRID_COMMUNICATION_AVOIDING_GENERALISED_MINIMAL_RESIDUAL_H +#define GRID_COMMUNICATION_AVOIDING_GENERALISED_MINIMAL_RESIDUAL_H + +namespace Grid { + +template +class CommunicationAvoidingGeneralisedMinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // Throw an assert when CAGMRES fails to converge, + // defaults to true + + RealD Tolerance; + + Integer MaxIterations; + Integer RestartLength; + Integer MaxNumberOfRestarts; + Integer IterationCount; // Number of iterations the CAGMRES took to finish, + // filled in upon completion + + GridStopWatch MatrixTimer; + GridStopWatch LinalgTimer; + GridStopWatch QrTimer; + GridStopWatch CompSolutionTimer; + + Eigen::MatrixXcd H; + + std::vector> y; + std::vector> gamma; + std::vector> c; + std::vector> s; + + CommunicationAvoidingGeneralisedMinimalResidual(RealD tol, + Integer maxit, + Integer restart_length, + bool err_on_no_conv = true) + : Tolerance(tol) + , MaxIterations(maxit) + , RestartLength(restart_length) + , MaxNumberOfRestarts(MaxIterations/RestartLength + ((MaxIterations%RestartLength == 0) ? 0 : 1)) + , ErrorOnNoConverge(err_on_no_conv) + , H(Eigen::MatrixXcd::Zero(RestartLength, RestartLength + 1)) // sizes taken from DD-αAMG code base + , y(RestartLength + 1, 0.) + , gamma(RestartLength + 1, 0.) + , c(RestartLength + 1, 0.) + , s(RestartLength + 1, 0.) {}; + + void operator()(LinearOperatorBase &LinOp, const Field &src, Field &psi) { + + std::cout << GridLogWarning << "This algorithm currently doesn't differ from regular GMRES" << std::endl; + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD cp; + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + Field r(src._grid); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "CommunicationAvoidingGeneralisedMinimalResidual: guess " << guess << std::endl; + std::cout << GridLogIterative << "CommunicationAvoidingGeneralisedMinimalResidual: src " << ssq << std::endl; + + MatrixTimer.Reset(); + LinalgTimer.Reset(); + QrTimer.Reset(); + CompSolutionTimer.Reset(); + + GridStopWatch SolverTimer; + SolverTimer.Start(); + + IterationCount = 0; + + for (int k=0; k &LinOp, const Field &src, Field &psi, RealD rsq) { + + RealD cp = 0; + + Field w(src._grid); + Field r(src._grid); + + // this should probably be made a class member so that it is only allocated once, not in every restart + std::vector v(RestartLength + 1, src._grid); for (auto &elem : v) elem = zero; + + MatrixTimer.Start(); + LinOp.Op(psi, w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + r = src - w; + + gamma[0] = sqrt(norm2(r)); + + v[0] = (1. / gamma[0]) * r; + LinalgTimer.Stop(); + + for (int i=0; i &LinOp, std::vector &v, Field &w, int iter) { + + MatrixTimer.Start(); + LinOp.Op(v[iter], w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + for (int i = 0; i <= iter; ++i) { + H(iter, i) = innerProduct(v[i], w); + w = w - H(iter, i) * v[i]; + } + + H(iter, iter + 1) = sqrt(norm2(w)); + v[iter + 1] = (1. / H(iter, iter + 1)) * w; + LinalgTimer.Stop(); + } + + void qrUpdate(int iter) { + + QrTimer.Start(); + for (int i = 0; i < iter ; ++i) { + auto tmp = -s[i] * H(iter, i) + c[i] * H(iter, i + 1); + H(iter, i) = std::conj(c[i]) * H(iter, i) + std::conj(s[i]) * H(iter, i + 1); + H(iter, i + 1) = tmp; + } + + // Compute new Givens Rotation + ComplexD nu = sqrt(std::norm(H(iter, iter)) + std::norm(H(iter, iter + 1))); + c[iter] = H(iter, iter) / nu; + s[iter] = H(iter, iter + 1) / nu; + + // Apply new Givens rotation + H(iter, iter) = nu; + H(iter, iter + 1) = 0.; + + gamma[iter + 1] = -s[iter] * gamma[iter]; + gamma[iter] = std::conj(c[iter]) * gamma[iter]; + QrTimer.Stop(); + } + + void computeSolution(std::vector const &v, Field &psi, int iter) { + + CompSolutionTimer.Start(); + for (int i = iter; i >= 0; i--) { + y[i] = gamma[i]; + for (int k = i + 1; k <= iter; k++) + y[i] = y[i] - H(k, i) * y[k]; + y[i] = y[i] / H(i, i); + } + + for (int i = 0; i <= iter; i++) + psi = psi + v[i] * y[i]; + CompSolutionTimer.Stop(); + } +}; +} +#endif diff --git a/Grid/algorithms/iterative/FlexibleCommunicationAvoidingGeneralisedMinimalResidual.h b/Grid/algorithms/iterative/FlexibleCommunicationAvoidingGeneralisedMinimalResidual.h new file mode 100644 index 00000000..db857248 --- /dev/null +++ b/Grid/algorithms/iterative/FlexibleCommunicationAvoidingGeneralisedMinimalResidual.h @@ -0,0 +1,256 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/algorithms/iterative/FlexibleCommunicationAvoidingGeneralisedMinimalResidual.h + +Copyright (C) 2015 + +Author: Daniel Richtmann + +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 */ +#ifndef GRID_FLEXIBLE_COMMUNICATION_AVOIDING_GENERALISED_MINIMAL_RESIDUAL_H +#define GRID_FLEXIBLE_COMMUNICATION_AVOIDING_GENERALISED_MINIMAL_RESIDUAL_H + +namespace Grid { + +template +class FlexibleCommunicationAvoidingGeneralisedMinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // Throw an assert when FCAGMRES fails to converge, + // defaults to true + + RealD Tolerance; + + Integer MaxIterations; + Integer RestartLength; + Integer MaxNumberOfRestarts; + Integer IterationCount; // Number of iterations the FCAGMRES took to finish, + // filled in upon completion + + GridStopWatch MatrixTimer; + GridStopWatch PrecTimer; + GridStopWatch LinalgTimer; + GridStopWatch QrTimer; + GridStopWatch CompSolutionTimer; + + Eigen::MatrixXcd H; + + std::vector> y; + std::vector> gamma; + std::vector> c; + std::vector> s; + + LinearFunction &Preconditioner; + + FlexibleCommunicationAvoidingGeneralisedMinimalResidual(RealD tol, + Integer maxit, + LinearFunction &Prec, + Integer restart_length, + bool err_on_no_conv = true) + : Tolerance(tol) + , MaxIterations(maxit) + , RestartLength(restart_length) + , MaxNumberOfRestarts(MaxIterations/RestartLength + ((MaxIterations%RestartLength == 0) ? 0 : 1)) + , ErrorOnNoConverge(err_on_no_conv) + , H(Eigen::MatrixXcd::Zero(RestartLength, RestartLength + 1)) // sizes taken from DD-αAMG code base + , y(RestartLength + 1, 0.) + , gamma(RestartLength + 1, 0.) + , c(RestartLength + 1, 0.) + , s(RestartLength + 1, 0.) + , Preconditioner(Prec) {}; + + void operator()(LinearOperatorBase &LinOp, const Field &src, Field &psi) { + + std::cout << GridLogWarning << "This algorithm currently doesn't differ from regular FGMRES" << std::endl; + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD cp; + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + Field r(src._grid); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "FlexibleCommunicationAvoidingGeneralisedMinimalResidual: guess " << guess << std::endl; + std::cout << GridLogIterative << "FlexibleCommunicationAvoidingGeneralisedMinimalResidual: src " << ssq << std::endl; + + PrecTimer.Reset(); + MatrixTimer.Reset(); + LinalgTimer.Reset(); + QrTimer.Reset(); + CompSolutionTimer.Reset(); + + GridStopWatch SolverTimer; + SolverTimer.Start(); + + IterationCount = 0; + + for (int k=0; k &LinOp, const Field &src, Field &psi, RealD rsq) { + + RealD cp = 0; + + Field w(src._grid); + Field r(src._grid); + + // these should probably be made class members so that they are only allocated once, not in every restart + std::vector v(RestartLength + 1, src._grid); for (auto &elem : v) elem = zero; + std::vector z(RestartLength + 1, src._grid); for (auto &elem : z) elem = zero; + + MatrixTimer.Start(); + LinOp.Op(psi, w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + r = src - w; + + gamma[0] = sqrt(norm2(r)); + + v[0] = (1. / gamma[0]) * r; + LinalgTimer.Stop(); + + for (int i=0; i &LinOp, std::vector &v, std::vector &z, Field &w, int iter) { + + PrecTimer.Start(); + Preconditioner(v[iter], z[iter]); + PrecTimer.Stop(); + + MatrixTimer.Start(); + LinOp.Op(z[iter], w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + for (int i = 0; i <= iter; ++i) { + H(iter, i) = innerProduct(v[i], w); + w = w - H(iter, i) * v[i]; + } + + H(iter, iter + 1) = sqrt(norm2(w)); + v[iter + 1] = (1. / H(iter, iter + 1)) * w; + LinalgTimer.Stop(); + } + + void qrUpdate(int iter) { + + QrTimer.Start(); + for (int i = 0; i < iter ; ++i) { + auto tmp = -s[i] * H(iter, i) + c[i] * H(iter, i + 1); + H(iter, i) = std::conj(c[i]) * H(iter, i) + std::conj(s[i]) * H(iter, i + 1); + H(iter, i + 1) = tmp; + } + + // Compute new Givens Rotation + ComplexD nu = sqrt(std::norm(H(iter, iter)) + std::norm(H(iter, iter + 1))); + c[iter] = H(iter, iter) / nu; + s[iter] = H(iter, iter + 1) / nu; + + // Apply new Givens rotation + H(iter, iter) = nu; + H(iter, iter + 1) = 0.; + + gamma[iter + 1] = -s[iter] * gamma[iter]; + gamma[iter] = std::conj(c[iter]) * gamma[iter]; + QrTimer.Stop(); + } + + void computeSolution(std::vector const &z, Field &psi, int iter) { + + CompSolutionTimer.Start(); + for (int i = iter; i >= 0; i--) { + y[i] = gamma[i]; + for (int k = i + 1; k <= iter; k++) + y[i] = y[i] - H(k, i) * y[k]; + y[i] = y[i] / H(i, i); + } + + for (int i = 0; i <= iter; i++) + psi = psi + z[i] * y[i]; + CompSolutionTimer.Stop(); + } +}; +} +#endif diff --git a/Grid/algorithms/iterative/FlexibleGeneralisedMinimalResidual.h b/Grid/algorithms/iterative/FlexibleGeneralisedMinimalResidual.h new file mode 100644 index 00000000..efc8c787 --- /dev/null +++ b/Grid/algorithms/iterative/FlexibleGeneralisedMinimalResidual.h @@ -0,0 +1,254 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/algorithms/iterative/FlexibleGeneralisedMinimalResidual.h + +Copyright (C) 2015 + +Author: Daniel Richtmann + +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 */ +#ifndef GRID_FLEXIBLE_GENERALISED_MINIMAL_RESIDUAL_H +#define GRID_FLEXIBLE_GENERALISED_MINIMAL_RESIDUAL_H + +namespace Grid { + +template +class FlexibleGeneralisedMinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // Throw an assert when FGMRES fails to converge, + // defaults to true + + RealD Tolerance; + + Integer MaxIterations; + Integer RestartLength; + Integer MaxNumberOfRestarts; + Integer IterationCount; // Number of iterations the FGMRES took to finish, + // filled in upon completion + + GridStopWatch MatrixTimer; + GridStopWatch PrecTimer; + GridStopWatch LinalgTimer; + GridStopWatch QrTimer; + GridStopWatch CompSolutionTimer; + + Eigen::MatrixXcd H; + + std::vector> y; + std::vector> gamma; + std::vector> c; + std::vector> s; + + LinearFunction &Preconditioner; + + FlexibleGeneralisedMinimalResidual(RealD tol, + Integer maxit, + LinearFunction &Prec, + Integer restart_length, + bool err_on_no_conv = true) + : Tolerance(tol) + , MaxIterations(maxit) + , RestartLength(restart_length) + , MaxNumberOfRestarts(MaxIterations/RestartLength + ((MaxIterations%RestartLength == 0) ? 0 : 1)) + , ErrorOnNoConverge(err_on_no_conv) + , H(Eigen::MatrixXcd::Zero(RestartLength, RestartLength + 1)) // sizes taken from DD-αAMG code base + , y(RestartLength + 1, 0.) + , gamma(RestartLength + 1, 0.) + , c(RestartLength + 1, 0.) + , s(RestartLength + 1, 0.) + , Preconditioner(Prec) {}; + + void operator()(LinearOperatorBase &LinOp, const Field &src, Field &psi) { + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD cp; + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + Field r(src._grid); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "FlexibleGeneralisedMinimalResidual: guess " << guess << std::endl; + std::cout << GridLogIterative << "FlexibleGeneralisedMinimalResidual: src " << ssq << std::endl; + + PrecTimer.Reset(); + MatrixTimer.Reset(); + LinalgTimer.Reset(); + QrTimer.Reset(); + CompSolutionTimer.Reset(); + + GridStopWatch SolverTimer; + SolverTimer.Start(); + + IterationCount = 0; + + for (int k=0; k &LinOp, const Field &src, Field &psi, RealD rsq) { + + RealD cp = 0; + + Field w(src._grid); + Field r(src._grid); + + // these should probably be made class members so that they are only allocated once, not in every restart + std::vector v(RestartLength + 1, src._grid); for (auto &elem : v) elem = zero; + std::vector z(RestartLength + 1, src._grid); for (auto &elem : z) elem = zero; + + MatrixTimer.Start(); + LinOp.Op(psi, w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + r = src - w; + + gamma[0] = sqrt(norm2(r)); + + v[0] = (1. / gamma[0]) * r; + LinalgTimer.Stop(); + + for (int i=0; i &LinOp, std::vector &v, std::vector &z, Field &w, int iter) { + + PrecTimer.Start(); + Preconditioner(v[iter], z[iter]); + PrecTimer.Stop(); + + MatrixTimer.Start(); + LinOp.Op(z[iter], w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + for (int i = 0; i <= iter; ++i) { + H(iter, i) = innerProduct(v[i], w); + w = w - H(iter, i) * v[i]; + } + + H(iter, iter + 1) = sqrt(norm2(w)); + v[iter + 1] = (1. / H(iter, iter + 1)) * w; + LinalgTimer.Stop(); + } + + void qrUpdate(int iter) { + + QrTimer.Start(); + for (int i = 0; i < iter ; ++i) { + auto tmp = -s[i] * H(iter, i) + c[i] * H(iter, i + 1); + H(iter, i) = std::conj(c[i]) * H(iter, i) + std::conj(s[i]) * H(iter, i + 1); + H(iter, i + 1) = tmp; + } + + // Compute new Givens Rotation + ComplexD nu = sqrt(std::norm(H(iter, iter)) + std::norm(H(iter, iter + 1))); + c[iter] = H(iter, iter) / nu; + s[iter] = H(iter, iter + 1) / nu; + + // Apply new Givens rotation + H(iter, iter) = nu; + H(iter, iter + 1) = 0.; + + gamma[iter + 1] = -s[iter] * gamma[iter]; + gamma[iter] = std::conj(c[iter]) * gamma[iter]; + QrTimer.Stop(); + } + + void computeSolution(std::vector const &z, Field &psi, int iter) { + + CompSolutionTimer.Start(); + for (int i = iter; i >= 0; i--) { + y[i] = gamma[i]; + for (int k = i + 1; k <= iter; k++) + y[i] = y[i] - H(k, i) * y[k]; + y[i] = y[i] / H(i, i); + } + + for (int i = 0; i <= iter; i++) + psi = psi + z[i] * y[i]; + CompSolutionTimer.Stop(); + } +}; +} +#endif diff --git a/Grid/algorithms/iterative/GeneralisedMinimalResidual.h b/Grid/algorithms/iterative/GeneralisedMinimalResidual.h new file mode 100644 index 00000000..10636234 --- /dev/null +++ b/Grid/algorithms/iterative/GeneralisedMinimalResidual.h @@ -0,0 +1,242 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/algorithms/iterative/GeneralisedMinimalResidual.h + +Copyright (C) 2015 + +Author: Daniel Richtmann + +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 */ +#ifndef GRID_GENERALISED_MINIMAL_RESIDUAL_H +#define GRID_GENERALISED_MINIMAL_RESIDUAL_H + +namespace Grid { + +template +class GeneralisedMinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // Throw an assert when GMRES fails to converge, + // defaults to true + + RealD Tolerance; + + Integer MaxIterations; + Integer RestartLength; + Integer MaxNumberOfRestarts; + Integer IterationCount; // Number of iterations the GMRES took to finish, + // filled in upon completion + + GridStopWatch MatrixTimer; + GridStopWatch LinalgTimer; + GridStopWatch QrTimer; + GridStopWatch CompSolutionTimer; + + Eigen::MatrixXcd H; + + std::vector> y; + std::vector> gamma; + std::vector> c; + std::vector> s; + + GeneralisedMinimalResidual(RealD tol, + Integer maxit, + Integer restart_length, + bool err_on_no_conv = true) + : Tolerance(tol) + , MaxIterations(maxit) + , RestartLength(restart_length) + , MaxNumberOfRestarts(MaxIterations/RestartLength + ((MaxIterations%RestartLength == 0) ? 0 : 1)) + , ErrorOnNoConverge(err_on_no_conv) + , H(Eigen::MatrixXcd::Zero(RestartLength, RestartLength + 1)) // sizes taken from DD-αAMG code base + , y(RestartLength + 1, 0.) + , gamma(RestartLength + 1, 0.) + , c(RestartLength + 1, 0.) + , s(RestartLength + 1, 0.) {}; + + void operator()(LinearOperatorBase &LinOp, const Field &src, Field &psi) { + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD cp; + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + Field r(src._grid); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "GeneralisedMinimalResidual: guess " << guess << std::endl; + std::cout << GridLogIterative << "GeneralisedMinimalResidual: src " << ssq << std::endl; + + MatrixTimer.Reset(); + LinalgTimer.Reset(); + QrTimer.Reset(); + CompSolutionTimer.Reset(); + + GridStopWatch SolverTimer; + SolverTimer.Start(); + + IterationCount = 0; + + for (int k=0; k &LinOp, const Field &src, Field &psi, RealD rsq) { + + RealD cp = 0; + + Field w(src._grid); + Field r(src._grid); + + // this should probably be made a class member so that it is only allocated once, not in every restart + std::vector v(RestartLength + 1, src._grid); for (auto &elem : v) elem = zero; + + MatrixTimer.Start(); + LinOp.Op(psi, w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + r = src - w; + + gamma[0] = sqrt(norm2(r)); + + v[0] = (1. / gamma[0]) * r; + LinalgTimer.Stop(); + + for (int i=0; i &LinOp, std::vector &v, Field &w, int iter) { + + MatrixTimer.Start(); + LinOp.Op(v[iter], w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + for (int i = 0; i <= iter; ++i) { + H(iter, i) = innerProduct(v[i], w); + w = w - H(iter, i) * v[i]; + } + + H(iter, iter + 1) = sqrt(norm2(w)); + v[iter + 1] = (1. / H(iter, iter + 1)) * w; + LinalgTimer.Stop(); + } + + void qrUpdate(int iter) { + + QrTimer.Start(); + for (int i = 0; i < iter ; ++i) { + auto tmp = -s[i] * H(iter, i) + c[i] * H(iter, i + 1); + H(iter, i) = std::conj(c[i]) * H(iter, i) + std::conj(s[i]) * H(iter, i + 1); + H(iter, i + 1) = tmp; + } + + // Compute new Givens Rotation + ComplexD nu = sqrt(std::norm(H(iter, iter)) + std::norm(H(iter, iter + 1))); + c[iter] = H(iter, iter) / nu; + s[iter] = H(iter, iter + 1) / nu; + + // Apply new Givens rotation + H(iter, iter) = nu; + H(iter, iter + 1) = 0.; + + gamma[iter + 1] = -s[iter] * gamma[iter]; + gamma[iter] = std::conj(c[iter]) * gamma[iter]; + QrTimer.Stop(); + } + + void computeSolution(std::vector const &v, Field &psi, int iter) { + + CompSolutionTimer.Start(); + for (int i = iter; i >= 0; i--) { + y[i] = gamma[i]; + for (int k = i + 1; k <= iter; k++) + y[i] = y[i] - H(k, i) * y[k]; + y[i] = y[i] / H(i, i); + } + + for (int i = 0; i <= iter; i++) + psi = psi + v[i] * y[i]; + CompSolutionTimer.Stop(); + } +}; +} +#endif diff --git a/Grid/algorithms/iterative/MinimalResidual.h b/Grid/algorithms/iterative/MinimalResidual.h new file mode 100644 index 00000000..fa1912cf --- /dev/null +++ b/Grid/algorithms/iterative/MinimalResidual.h @@ -0,0 +1,156 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/algorithms/iterative/MinimalResidual.h + +Copyright (C) 2015 + +Author: Daniel Richtmann + +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 */ +#ifndef GRID_MINIMAL_RESIDUAL_H +#define GRID_MINIMAL_RESIDUAL_H + +namespace Grid { + +template class MinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // throw an assert when the MR fails to converge. + // Defaults true. + RealD Tolerance; + Integer MaxIterations; + RealD overRelaxParam; + Integer IterationsToComplete; // Number of iterations the MR took to finish. + // Filled in upon completion + + MinimalResidual(RealD tol, Integer maxit, Real ovrelparam = 1.0, bool err_on_no_conv = true) + : Tolerance(tol), MaxIterations(maxit), overRelaxParam(ovrelparam), ErrorOnNoConverge(err_on_no_conv){}; + + void operator()(LinearOperatorBase &Linop, const Field &src, Field &psi) { + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + Complex a, c; + Real d; + + Field Mr(src); + Field r(src); + + // Initial residual computation & set up + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + Linop.Op(psi, Mr); + + r = src - Mr; + + RealD cp = norm2(r); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "MinimalResidual: guess " << guess << std::endl; + std::cout << GridLogIterative << "MinimalResidual: src " << ssq << std::endl; + std::cout << GridLogIterative << "MinimalResidual: mp " << d << std::endl; + std::cout << GridLogIterative << "MinimalResidual: cp,r " << cp << std::endl; + + if (cp <= rsq) { + return; + } + + std::cout << GridLogIterative << "MinimalResidual: k=0 residual " << cp << " target " << rsq << std::endl; + + GridStopWatch LinalgTimer; + GridStopWatch MatrixTimer; + GridStopWatch SolverTimer; + + SolverTimer.Start(); + int k; + for (k = 1; k <= MaxIterations; k++) { + + MatrixTimer.Start(); + Linop.Op(r, Mr); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + + c = innerProduct(Mr, r); + + d = norm2(Mr); + + a = c / d; + + a = a * overRelaxParam; + + psi = psi + r * a; + + r = r - Mr * a; + + cp = norm2(r); + + LinalgTimer.Stop(); + + std::cout << GridLogIterative << "MinimalResidual: Iteration " << k + << " residual " << cp << " target " << rsq << std::endl; + std::cout << GridLogDebug << "a = " << a << " c = " << c << " d = " << d << std::endl; + + // Stopping condition + if (cp <= rsq) { + SolverTimer.Stop(); + + Linop.Op(psi, Mr); + r = src - Mr; + + RealD srcnorm = sqrt(ssq); + RealD resnorm = sqrt(norm2(r)); + RealD true_residual = resnorm / srcnorm; + + std::cout << GridLogMessage << "MinimalResidual Converged on iteration " << k + << " computed residual " << sqrt(cp / ssq) + << " true residual " << true_residual + << " target " << Tolerance << std::endl; + + std::cout << GridLogMessage << "MR Time elapsed: Total " << SolverTimer.Elapsed() << std::endl; + std::cout << GridLogMessage << "MR Time elapsed: Matrix " << MatrixTimer.Elapsed() << std::endl; + std::cout << GridLogMessage << "MR Time elapsed: Linalg " << LinalgTimer.Elapsed() << std::endl; + + if (ErrorOnNoConverge) + assert(true_residual / Tolerance < 10000.0); + + IterationsToComplete = k; + + return; + } + } + + std::cout << GridLogMessage << "MinimalResidual did NOT converge" + << std::endl; + + if (ErrorOnNoConverge) + assert(0); + + IterationsToComplete = k; + } +}; +} // namespace Grid +#endif diff --git a/Grid/algorithms/iterative/MixedPrecisionFlexibleGeneralisedMinimalResidual.h b/Grid/algorithms/iterative/MixedPrecisionFlexibleGeneralisedMinimalResidual.h new file mode 100644 index 00000000..04113684 --- /dev/null +++ b/Grid/algorithms/iterative/MixedPrecisionFlexibleGeneralisedMinimalResidual.h @@ -0,0 +1,273 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./lib/algorithms/iterative/MixedPrecisionFlexibleGeneralisedMinimalResidual.h + +Copyright (C) 2015 + +Author: Daniel Richtmann + +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 */ +#ifndef GRID_MIXED_PRECISION_FLEXIBLE_GENERALISED_MINIMAL_RESIDUAL_H +#define GRID_MIXED_PRECISION_FLEXIBLE_GENERALISED_MINIMAL_RESIDUAL_H + +namespace Grid { + +template::value == 2, int>::type = 0, typename std::enable_if< getPrecision::value == 1, int>::type = 0> +class MixedPrecisionFlexibleGeneralisedMinimalResidual : public OperatorFunction { + public: + bool ErrorOnNoConverge; // Throw an assert when MPFGMRES fails to converge, + // defaults to true + + RealD Tolerance; + + Integer MaxIterations; + Integer RestartLength; + Integer MaxNumberOfRestarts; + Integer IterationCount; // Number of iterations the MPFGMRES took to finish, + // filled in upon completion + + GridStopWatch MatrixTimer; + GridStopWatch PrecTimer; + GridStopWatch LinalgTimer; + GridStopWatch QrTimer; + GridStopWatch CompSolutionTimer; + GridStopWatch ChangePrecTimer; + + Eigen::MatrixXcd H; + + std::vector> y; + std::vector> gamma; + std::vector> c; + std::vector> s; + + GridBase* SinglePrecGrid; + + LinearFunction &Preconditioner; + + MixedPrecisionFlexibleGeneralisedMinimalResidual(RealD tol, + Integer maxit, + GridBase * sp_grid, + LinearFunction &Prec, + Integer restart_length, + bool err_on_no_conv = true) + : Tolerance(tol) + , MaxIterations(maxit) + , RestartLength(restart_length) + , MaxNumberOfRestarts(MaxIterations/RestartLength + ((MaxIterations%RestartLength == 0) ? 0 : 1)) + , ErrorOnNoConverge(err_on_no_conv) + , H(Eigen::MatrixXcd::Zero(RestartLength, RestartLength + 1)) // sizes taken from DD-αAMG code base + , y(RestartLength + 1, 0.) + , gamma(RestartLength + 1, 0.) + , c(RestartLength + 1, 0.) + , s(RestartLength + 1, 0.) + , SinglePrecGrid(sp_grid) + , Preconditioner(Prec) {}; + + void operator()(LinearOperatorBase &LinOp, const FieldD &src, FieldD &psi) { + + psi.checkerboard = src.checkerboard; + conformable(psi, src); + + RealD guess = norm2(psi); + assert(std::isnan(guess) == 0); + + RealD cp; + RealD ssq = norm2(src); + RealD rsq = Tolerance * Tolerance * ssq; + + FieldD r(src._grid); + + std::cout << std::setprecision(4) << std::scientific; + std::cout << GridLogIterative << "MPFGMRES: guess " << guess << std::endl; + std::cout << GridLogIterative << "MPFGMRES: src " << ssq << std::endl; + + PrecTimer.Reset(); + MatrixTimer.Reset(); + LinalgTimer.Reset(); + QrTimer.Reset(); + CompSolutionTimer.Reset(); + ChangePrecTimer.Reset(); + + GridStopWatch SolverTimer; + SolverTimer.Start(); + + IterationCount = 0; + + for (int k=0; k &LinOp, const FieldD &src, FieldD &psi, RealD rsq) { + + RealD cp = 0; + + FieldD w(src._grid); + FieldD r(src._grid); + + // these should probably be made class members so that they are only allocated once, not in every restart + std::vector v(RestartLength + 1, src._grid); for (auto &elem : v) elem = zero; + std::vector z(RestartLength + 1, src._grid); for (auto &elem : z) elem = zero; + + MatrixTimer.Start(); + LinOp.Op(psi, w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + r = src - w; + + gamma[0] = sqrt(norm2(r)); + + v[0] = (1. / gamma[0]) * r; + LinalgTimer.Stop(); + + for (int i=0; i &LinOp, std::vector &v, std::vector &z, FieldD &w, int iter) { + + FieldF v_f(SinglePrecGrid); + FieldF z_f(SinglePrecGrid); + + ChangePrecTimer.Start(); + precisionChange(v_f, v[iter]); + precisionChange(z_f, z[iter]); + ChangePrecTimer.Stop(); + + PrecTimer.Start(); + Preconditioner(v_f, z_f); + PrecTimer.Stop(); + + ChangePrecTimer.Start(); + precisionChange(z[iter], z_f); + ChangePrecTimer.Stop(); + + MatrixTimer.Start(); + LinOp.Op(z[iter], w); + MatrixTimer.Stop(); + + LinalgTimer.Start(); + for (int i = 0; i <= iter; ++i) { + H(iter, i) = innerProduct(v[i], w); + w = w - H(iter, i) * v[i]; + } + + H(iter, iter + 1) = sqrt(norm2(w)); + v[iter + 1] = (1. / H(iter, iter + 1)) * w; + LinalgTimer.Stop(); + } + + void qrUpdate(int iter) { + + QrTimer.Start(); + for (int i = 0; i < iter ; ++i) { + auto tmp = -s[i] * H(iter, i) + c[i] * H(iter, i + 1); + H(iter, i) = std::conj(c[i]) * H(iter, i) + std::conj(s[i]) * H(iter, i + 1); + H(iter, i + 1) = tmp; + } + + // Compute new Givens Rotation + ComplexD nu = sqrt(std::norm(H(iter, iter)) + std::norm(H(iter, iter + 1))); + c[iter] = H(iter, iter) / nu; + s[iter] = H(iter, iter + 1) / nu; + + // Apply new Givens rotation + H(iter, iter) = nu; + H(iter, iter + 1) = 0.; + + gamma[iter + 1] = -s[iter] * gamma[iter]; + gamma[iter] = std::conj(c[iter]) * gamma[iter]; + QrTimer.Stop(); + } + + void computeSolution(std::vector const &z, FieldD &psi, int iter) { + + CompSolutionTimer.Start(); + for (int i = iter; i >= 0; i--) { + y[i] = gamma[i]; + for (int k = i + 1; k <= iter; k++) + y[i] = y[i] - H(k, i) * y[k]; + y[i] = y[i] / H(i, i); + } + + for (int i = 0; i <= iter; i++) + psi = psi + z[i] * y[i]; + CompSolutionTimer.Stop(); + } +}; +} +#endif diff --git a/Grid/algorithms/iterative/PrecGeneralisedConjugateResidual.h b/Grid/algorithms/iterative/PrecGeneralisedConjugateResidual.h index fd11352e..c723c4a9 100644 --- a/Grid/algorithms/iterative/PrecGeneralisedConjugateResidual.h +++ b/Grid/algorithms/iterative/PrecGeneralisedConjugateResidual.h @@ -139,8 +139,11 @@ namespace Grid { MatTimer.Start(); Linop.HermOpAndNorm(psi,Az,zAz,zAAz); MatTimer.Stop(); + + LinalgTimer.Start(); r=src-Az; - + LinalgTimer.Stop(); + ///////////////////// // p = Prec(r) ///////////////////// @@ -152,8 +155,10 @@ namespace Grid { Linop.HermOp(z,tmp); MatTimer.Stop(); + LinalgTimer.Start(); ttmp=tmp; tmp=tmp-r; + LinalgTimer.Stop(); /* std::cout< &logstreams); +extern GridLogger GridLogMG; extern GridLogger GridLogIRL; extern GridLogger GridLogSolver; extern GridLogger GridLogError; diff --git a/Grid/qcd/utils/LinalgUtils.h b/Grid/qcd/utils/LinalgUtils.h index 5eaf1c2a..04a224e5 100644 --- a/Grid/qcd/utils/LinalgUtils.h +++ b/Grid/qcd/utils/LinalgUtils.h @@ -173,6 +173,39 @@ void G5R5(Lattice &z,const Lattice &x) } } } +} -}} +// I explicitly need these outside the QCD namespace +template +void G5C(Lattice &z, const Lattice &x) +{ + GridBase *grid = x._grid; + z.checkerboard = x.checkerboard; + conformable(x, z); + + QCD::Gamma G5(QCD::Gamma::Algebra::Gamma5); + z = G5 * x; +} + +template +void G5C(Lattice> &z, const Lattice> &x) +{ + GridBase *grid = x._grid; + z.checkerboard = x.checkerboard; + conformable(x, z); + + static_assert(nbasis % 2 == 0, ""); + int nb = nbasis / 2; + + parallel_for(int ss = 0; ss < grid->oSites(); ss++) { + for(int n = 0; n < nb; ++n) { + z._odata[ss](n) = x._odata[ss](n); + } + for(int n = nb; n < nbasis; ++n) { + z._odata[ss](n) = -x._odata[ss](n); + } + } +} + +} #endif diff --git a/tests/solver/Test_multigrid_common.h b/tests/solver/Test_multigrid_common.h new file mode 100644 index 00000000..add833f2 --- /dev/null +++ b/tests/solver/Test_multigrid_common.h @@ -0,0 +1,670 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/solver/Test_multigrid_common.h + + Copyright (C) 2015-2018 + + Author: Daniel Richtmann + + 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 */ +#ifndef GRID_TEST_MULTIGRID_COMMON_H +#define GRID_TEST_MULTIGRID_COMMON_H + +namespace Grid { + +// TODO: Can think about having one parameter struct per level and then a +// vector of these structs. How well would that work together with the +// serialization strategy of Grid? + +// clang-format off +struct MultiGridParams : Serializable { +public: + GRID_SERIALIZABLE_CLASS_MEMBERS(MultiGridParams, + int, nLevels, + std::vector>, blockSizes, // size == nLevels - 1 + std::vector, smootherTol, // size == nLevels - 1 + std::vector, smootherMaxOuterIter, // size == nLevels - 1 + std::vector, smootherMaxInnerIter, // size == nLevels - 1 + bool, kCycle, + std::vector, kCycleTol, // size == nLevels - 1 + std::vector, kCycleMaxOuterIter, // size == nLevels - 1 + std::vector, kCycleMaxInnerIter, // size == nLevels - 1 + double, coarseSolverTol, + int, coarseSolverMaxOuterIter, + int, coarseSolverMaxInnerIter); + + // constructor with default values + MultiGridParams(int _nLevels = 2, + std::vector> _blockSizes = {{4, 4, 4, 4}}, + std::vector _smootherTol = {1e-14}, + std::vector _smootherMaxOuterIter = {4}, + std::vector _smootherMaxInnerIter = {4}, + bool _kCycle = true, + std::vector _kCycleTol = {1e-1}, + std::vector _kCycleMaxOuterIter = {2}, + std::vector _kCycleMaxInnerIter = {5}, + double _coarseSolverTol = 5e-2, + int _coarseSolverMaxOuterIter = 10, + int _coarseSolverMaxInnerIter = 500) + : nLevels(_nLevels) + , blockSizes(_blockSizes) + , smootherTol(_smootherTol) + , smootherMaxOuterIter(_smootherMaxOuterIter) + , smootherMaxInnerIter(_smootherMaxInnerIter) + , kCycle(_kCycle) + , kCycleTol(_kCycleTol) + , kCycleMaxOuterIter(_kCycleMaxOuterIter) + , kCycleMaxInnerIter(_kCycleMaxInnerIter) + , coarseSolverTol(_coarseSolverTol) + , coarseSolverMaxOuterIter(_coarseSolverMaxOuterIter) + , coarseSolverMaxInnerIter(_coarseSolverMaxInnerIter) + {} +}; +// clang-format on + +void checkParameterValidity(MultiGridParams const ¶ms) { + + auto correctSize = params.nLevels - 1; + + assert(correctSize == params.blockSizes.size()); + assert(correctSize == params.smootherTol.size()); + assert(correctSize == params.smootherMaxOuterIter.size()); + assert(correctSize == params.smootherMaxInnerIter.size()); + assert(correctSize == params.kCycleTol.size()); + assert(correctSize == params.kCycleMaxOuterIter.size()); + assert(correctSize == params.kCycleMaxInnerIter.size()); +} + +struct LevelInfo { +public: + std::vector> Seeds; + std::vector Grids; + std::vector PRNGs; + + LevelInfo(GridCartesian *FineGrid, MultiGridParams const &mgParams) { + + auto nCoarseLevels = mgParams.blockSizes.size(); + + assert(nCoarseLevels == mgParams.nLevels - 1); + + // set up values for finest grid + Grids.push_back(FineGrid); + Seeds.push_back({1, 2, 3, 4}); + PRNGs.push_back(GridParallelRNG(Grids.back())); + PRNGs.back().SeedFixedIntegers(Seeds.back()); + + // set up values for coarser grids + for(int level = 1; level < mgParams.nLevels; ++level) { + auto Nd = Grids[level - 1]->_ndimension; + auto tmp = Grids[level - 1]->_fdimensions; + assert(tmp.size() == Nd); + + Seeds.push_back(std::vector(Nd)); + + for(int d = 0; d < Nd; ++d) { + tmp[d] /= mgParams.blockSizes[level - 1][d]; + Seeds[level][d] = (level)*Nd + d + 1; + } + + Grids.push_back(QCD::SpaceTimeGrid::makeFourDimGrid(tmp, Grids[level - 1]->_simd_layout, GridDefaultMpi())); + PRNGs.push_back(GridParallelRNG(Grids[level])); + + PRNGs[level].SeedFixedIntegers(Seeds[level]); + } + + std::cout << GridLogMessage << "Constructed " << mgParams.nLevels << " levels" << std::endl; + + for(int level = 0; level < mgParams.nLevels; ++level) { + std::cout << GridLogMessage << "level = " << level << ":" << std::endl; + Grids[level]->show_decomposition(); + } + } +}; + +template class MultiGridPreconditionerBase : public LinearFunction { +public: + virtual ~MultiGridPreconditionerBase() = default; + virtual void setup() = 0; + virtual void operator()(Field const &in, Field &out) = 0; + virtual void runChecks(RealD tolerance) = 0; + virtual void reportTimings() = 0; +}; + +template +class MultiGridPreconditioner : public MultiGridPreconditionerBase> { +public: + ///////////////////////////////////////////// + // Type Definitions + ///////////////////////////////////////////// + + // clang-format off + typedef Aggregation Aggregates; + typedef CoarsenedMatrix CoarseDiracMatrix; + typedef typename Aggregates::CoarseVector CoarseVector; + typedef typename Aggregates::siteVector CoarseSiteVector; + typedef Matrix FineDiracMatrix; + typedef typename Aggregates::FineField FineVector; + typedef MultiGridPreconditioner, nBasis, nCoarserLevels - 1, CoarseDiracMatrix> NextPreconditionerLevel; + // clang-format on + + ///////////////////////////////////////////// + // Member Data + ///////////////////////////////////////////// + + int _CurrentLevel; + int _NextCoarserLevel; + + MultiGridParams &_MultiGridParams; + LevelInfo & _LevelInfo; + + FineDiracMatrix & _FineMatrix; + FineDiracMatrix & _SmootherMatrix; + Aggregates _Aggregates; + CoarseDiracMatrix _CoarseMatrix; + + std::unique_ptr _NextPreconditionerLevel; + + GridStopWatch _SetupTotalTimer; + GridStopWatch _SetupCreateSubspaceTimer; + GridStopWatch _SetupProjectToChiralitiesTimer; + GridStopWatch _SetupCoarsenOperatorTimer; + GridStopWatch _SetupNextLevelTimer; + GridStopWatch _SolveTotalTimer; + GridStopWatch _SolveRestrictionTimer; + GridStopWatch _SolveProlongationTimer; + GridStopWatch _SolveSmootherTimer; + GridStopWatch _SolveNextLevelTimer; + + ///////////////////////////////////////////// + // Member Functions + ///////////////////////////////////////////// + + MultiGridPreconditioner(MultiGridParams &mgParams, LevelInfo &LvlInfo, FineDiracMatrix &FineMat, FineDiracMatrix &SmootherMat) + : _CurrentLevel(mgParams.nLevels - (nCoarserLevels + 1)) // _Level = 0 corresponds to finest + , _NextCoarserLevel(_CurrentLevel + 1) // incremented for instances on coarser levels + , _MultiGridParams(mgParams) + , _LevelInfo(LvlInfo) + , _FineMatrix(FineMat) + , _SmootherMatrix(SmootherMat) + , _Aggregates(_LevelInfo.Grids[_NextCoarserLevel], _LevelInfo.Grids[_CurrentLevel], 0) + , _CoarseMatrix(*_LevelInfo.Grids[_NextCoarserLevel]) { + + _NextPreconditionerLevel + = std::unique_ptr(new NextPreconditionerLevel(_MultiGridParams, _LevelInfo, _CoarseMatrix, _CoarseMatrix)); + + resetTimers(); + } + + void setup() { + + _SetupTotalTimer.Start(); + + static_assert((nBasis & 0x1) == 0, "MG Preconditioner only supports an even number of basis vectors"); + int nb = nBasis / 2; + + MdagMLinearOperator fineMdagMOp(_FineMatrix); + + _SetupCreateSubspaceTimer.Start(); + _Aggregates.CreateSubspace(_LevelInfo.PRNGs[_CurrentLevel], fineMdagMOp, nb); + _SetupCreateSubspaceTimer.Stop(); + + _SetupProjectToChiralitiesTimer.Start(); + FineVector tmp1(_Aggregates.subspace[0]._grid); + FineVector tmp2(_Aggregates.subspace[0]._grid); + for(int n = 0; n < nb; n++) { + auto tmp1 = _Aggregates.subspace[n]; + G5C(tmp2, _Aggregates.subspace[n]); + axpby(_Aggregates.subspace[n], 0.5, 0.5, tmp1, tmp2); + axpby(_Aggregates.subspace[n + nb], 0.5, -0.5, tmp1, tmp2); + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Chirally doubled vector " << n << ". " + << "norm2(vec[" << n << "]) = " << norm2(_Aggregates.subspace[n]) << ". " + << "norm2(vec[" << n + nb << "]) = " << norm2(_Aggregates.subspace[n + nb]) << std::endl; + } + _SetupProjectToChiralitiesTimer.Stop(); + + _SetupCoarsenOperatorTimer.Start(); + _CoarseMatrix.CoarsenOperator(_LevelInfo.Grids[_CurrentLevel], fineMdagMOp, _Aggregates); + _SetupCoarsenOperatorTimer.Stop(); + + _SetupNextLevelTimer.Start(); + _NextPreconditionerLevel->setup(); + _SetupNextLevelTimer.Stop(); + + _SetupTotalTimer.Stop(); + } + + virtual void operator()(FineVector const &in, FineVector &out) { + + conformable(_LevelInfo.Grids[_CurrentLevel], in._grid); + conformable(in, out); + + // TODO: implement a W-cycle + if(_MultiGridParams.kCycle) + kCycle(in, out); + else + vCycle(in, out); + } + + void vCycle(FineVector const &in, FineVector &out) { + + _SolveTotalTimer.Start(); + + RealD inputNorm = norm2(in); + + CoarseVector coarseSrc(_LevelInfo.Grids[_NextCoarserLevel]); + CoarseVector coarseSol(_LevelInfo.Grids[_NextCoarserLevel]); + coarseSol = zero; + + FineVector fineTmp(in._grid); + + auto maxSmootherIter = _MultiGridParams.smootherMaxOuterIter[_CurrentLevel] * _MultiGridParams.smootherMaxInnerIter[_CurrentLevel]; + + TrivialPrecon fineTrivialPreconditioner; + FlexibleGeneralisedMinimalResidual fineFGMRES(_MultiGridParams.smootherTol[_CurrentLevel], + maxSmootherIter, + fineTrivialPreconditioner, + _MultiGridParams.smootherMaxInnerIter[_CurrentLevel], + false); + + MdagMLinearOperator fineMdagMOp(_FineMatrix); + MdagMLinearOperator fineSmootherMdagMOp(_SmootherMatrix); + + _SolveRestrictionTimer.Start(); + _Aggregates.ProjectToSubspace(coarseSrc, in); + _SolveRestrictionTimer.Stop(); + + _SolveNextLevelTimer.Start(); + (*_NextPreconditionerLevel)(coarseSrc, coarseSol); + _SolveNextLevelTimer.Stop(); + + _SolveProlongationTimer.Start(); + _Aggregates.PromoteFromSubspace(coarseSol, out); + _SolveProlongationTimer.Stop(); + + fineMdagMOp.Op(out, fineTmp); + fineTmp = in - fineTmp; + auto r = norm2(fineTmp); + auto residualAfterCoarseGridCorrection = std::sqrt(r / inputNorm); + + _SolveSmootherTimer.Start(); + fineFGMRES(fineSmootherMdagMOp, in, out); + _SolveSmootherTimer.Stop(); + + fineMdagMOp.Op(out, fineTmp); + fineTmp = in - fineTmp; + r = norm2(fineTmp); + auto residualAfterPostSmoother = std::sqrt(r / inputNorm); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": V-cycle: Input norm = " << std::sqrt(inputNorm) + << " Coarse residual = " << residualAfterCoarseGridCorrection << " Post-Smoother residual = " << residualAfterPostSmoother + << std::endl; + + _SolveTotalTimer.Stop(); + } + + void kCycle(FineVector const &in, FineVector &out) { + + _SolveTotalTimer.Start(); + + RealD inputNorm = norm2(in); + + CoarseVector coarseSrc(_LevelInfo.Grids[_NextCoarserLevel]); + CoarseVector coarseSol(_LevelInfo.Grids[_NextCoarserLevel]); + coarseSol = zero; + + FineVector fineTmp(in._grid); + + auto smootherMaxIter = _MultiGridParams.smootherMaxOuterIter[_CurrentLevel] * _MultiGridParams.smootherMaxInnerIter[_CurrentLevel]; + auto kCycleMaxIter = _MultiGridParams.kCycleMaxOuterIter[_CurrentLevel] * _MultiGridParams.kCycleMaxInnerIter[_CurrentLevel]; + + TrivialPrecon fineTrivialPreconditioner; + FlexibleGeneralisedMinimalResidual fineFGMRES(_MultiGridParams.smootherTol[_CurrentLevel], + smootherMaxIter, + fineTrivialPreconditioner, + _MultiGridParams.smootherMaxInnerIter[_CurrentLevel], + false); + FlexibleGeneralisedMinimalResidual coarseFGMRES(_MultiGridParams.kCycleTol[_CurrentLevel], + kCycleMaxIter, + *_NextPreconditionerLevel, + _MultiGridParams.kCycleMaxInnerIter[_CurrentLevel], + false); + + MdagMLinearOperator fineMdagMOp(_FineMatrix); + MdagMLinearOperator fineSmootherMdagMOp(_SmootherMatrix); + MdagMLinearOperator coarseMdagMOp(_CoarseMatrix); + + _SolveRestrictionTimer.Start(); + _Aggregates.ProjectToSubspace(coarseSrc, in); + _SolveRestrictionTimer.Stop(); + + _SolveNextLevelTimer.Start(); + coarseFGMRES(coarseMdagMOp, coarseSrc, coarseSol); + _SolveNextLevelTimer.Stop(); + + _SolveProlongationTimer.Start(); + _Aggregates.PromoteFromSubspace(coarseSol, out); + _SolveProlongationTimer.Stop(); + + fineMdagMOp.Op(out, fineTmp); + fineTmp = in - fineTmp; + auto r = norm2(fineTmp); + auto residualAfterCoarseGridCorrection = std::sqrt(r / inputNorm); + + _SolveSmootherTimer.Start(); + fineFGMRES(fineSmootherMdagMOp, in, out); + _SolveSmootherTimer.Stop(); + + fineMdagMOp.Op(out, fineTmp); + fineTmp = in - fineTmp; + r = norm2(fineTmp); + auto residualAfterPostSmoother = std::sqrt(r / inputNorm); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": K-cycle: Input norm = " << std::sqrt(inputNorm) + << " Coarse residual = " << residualAfterCoarseGridCorrection << " Post-Smoother residual = " << residualAfterPostSmoother + << std::endl; + + _SolveTotalTimer.Stop(); + } + + void runChecks(RealD tolerance) { + + std::vector fineTmps(7, _LevelInfo.Grids[_CurrentLevel]); + std::vector coarseTmps(4, _LevelInfo.Grids[_NextCoarserLevel]); + + MdagMLinearOperator fineMdagMOp(_FineMatrix); + MdagMLinearOperator coarseMdagMOp(_CoarseMatrix); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": MG correctness check: 0 == (M - (Mdiag + Σ_μ Mdir_μ)) * v" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + + random(_LevelInfo.PRNGs[_CurrentLevel], fineTmps[0]); + + fineMdagMOp.Op(fineTmps[0], fineTmps[1]); // M * v + fineMdagMOp.OpDiag(fineTmps[0], fineTmps[2]); // Mdiag * v + + fineTmps[4] = zero; + for(int dir = 0; dir < 4; dir++) { // Σ_μ Mdir_μ * v + for(auto disp : {+1, -1}) { + fineMdagMOp.OpDir(fineTmps[0], fineTmps[3], dir, disp); + fineTmps[4] = fineTmps[4] + fineTmps[3]; + } + } + + fineTmps[5] = fineTmps[2] + fineTmps[4]; // (Mdiag + Σ_μ Mdir_μ) * v + + fineTmps[6] = fineTmps[1] - fineTmps[5]; + auto deviation = std::sqrt(norm2(fineTmps[6]) / norm2(fineTmps[1])); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2(M * v) = " << norm2(fineTmps[1]) << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2(Mdiag * v) = " << norm2(fineTmps[2]) << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2(Σ_μ Mdir_μ * v) = " << norm2(fineTmps[4]) << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2((Mdiag + Σ_μ Mdir_μ) * v) = " << norm2(fineTmps[5]) << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": relative deviation = " << deviation; + + if(deviation > tolerance) { + std::cout << " > " << tolerance << " -> check failed" << std::endl; + abort(); + } else { + std::cout << " < " << tolerance << " -> check passed" << std::endl; + } + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": MG correctness check: 0 == (1 - P R) v" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + + for(auto i = 0; i < _Aggregates.subspace.size(); ++i) { + _Aggregates.ProjectToSubspace(coarseTmps[0], _Aggregates.subspace[i]); // R v_i + _Aggregates.PromoteFromSubspace(coarseTmps[0], fineTmps[0]); // P R v_i + + fineTmps[1] = _Aggregates.subspace[i] - fineTmps[0]; // v_i - P R v_i + deviation = std::sqrt(norm2(fineTmps[1]) / norm2(_Aggregates.subspace[i])); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Vector " << i << ": norm2(v_i) = " << norm2(_Aggregates.subspace[i]) + << " | norm2(R v_i) = " << norm2(coarseTmps[0]) << " | norm2(P R v_i) = " << norm2(fineTmps[0]) + << " | relative deviation = " << deviation; + + if(deviation > tolerance) { + std::cout << " > " << tolerance << " -> check failed" << std::endl; + abort(); + } else { + std::cout << " < " << tolerance << " -> check passed" << std::endl; + } + } + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": MG correctness check: 0 == (1 - R P) v_c" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + + random(_LevelInfo.PRNGs[_NextCoarserLevel], coarseTmps[0]); + + _Aggregates.PromoteFromSubspace(coarseTmps[0], fineTmps[0]); // P v_c + _Aggregates.ProjectToSubspace(coarseTmps[1], fineTmps[0]); // R P v_c + + coarseTmps[2] = coarseTmps[0] - coarseTmps[1]; // v_c - R P v_c + deviation = std::sqrt(norm2(coarseTmps[2]) / norm2(coarseTmps[0])); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2(v_c) = " << norm2(coarseTmps[0]) + << " | norm2(R P v_c) = " << norm2(coarseTmps[1]) << " | norm2(P v_c) = " << norm2(fineTmps[0]) + << " | relative deviation = " << deviation; + + if(deviation > tolerance) { + std::cout << " > " << tolerance << " -> check failed" << std::endl; + abort(); + } else { + std::cout << " < " << tolerance << " -> check passed" << std::endl; + } + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": MG correctness check: 0 == (R D P - D_c) v_c" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + + random(_LevelInfo.PRNGs[_NextCoarserLevel], coarseTmps[0]); + + _Aggregates.PromoteFromSubspace(coarseTmps[0], fineTmps[0]); // P v_c + fineMdagMOp.Op(fineTmps[0], fineTmps[1]); // D P v_c + _Aggregates.ProjectToSubspace(coarseTmps[1], fineTmps[1]); // R D P v_c + + coarseMdagMOp.Op(coarseTmps[0], coarseTmps[2]); // D_c v_c + + coarseTmps[3] = coarseTmps[1] - coarseTmps[2]; // R D P v_c - D_c v_c + deviation = std::sqrt(norm2(coarseTmps[3]) / norm2(coarseTmps[1])); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": norm2(R D P v_c) = " << norm2(coarseTmps[1]) + << " | norm2(D_c v_c) = " << norm2(coarseTmps[2]) << " | relative deviation = " << deviation; + + if(deviation > tolerance) { + std::cout << " > " << tolerance << " -> check failed" << std::endl; + abort(); + } else { + std::cout << " < " << tolerance << " -> check passed" << std::endl; + } + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": MG correctness check: 0 == |(Im(v_c^dag D_c^dag D_c v_c)|" << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": **************************************************" << std::endl; + + random(_LevelInfo.PRNGs[_NextCoarserLevel], coarseTmps[0]); + + coarseMdagMOp.Op(coarseTmps[0], coarseTmps[1]); // D_c v_c + coarseMdagMOp.AdjOp(coarseTmps[1], coarseTmps[2]); // D_c^dag D_c v_c + + auto dot = innerProduct(coarseTmps[0], coarseTmps[2]); //v_c^dag D_c^dag D_c v_c + deviation = std::abs(imag(dot)) / std::abs(real(dot)); + + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Re(v_c^dag D_c^dag D_c v_c) = " << real(dot) + << " | Im(v_c^dag D_c^dag D_c v_c) = " << imag(dot) << " | relative deviation = " << deviation; + + if(deviation > tolerance) { + std::cout << " > " << tolerance << " -> check failed" << std::endl; + abort(); + } else { + std::cout << " < " << tolerance << " -> check passed" << std::endl; + } + + _NextPreconditionerLevel->runChecks(tolerance); + } + + void reportTimings() { + + // clang-format off + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Sum total " << _SetupTotalTimer.Elapsed() + _SolveTotalTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Setup total " << _SetupTotalTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Setup create subspace " << _SetupCreateSubspaceTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Setup project chiral " << _SetupProjectToChiralitiesTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Setup coarsen operator " << _SetupCoarsenOperatorTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Setup next level " << _SetupNextLevelTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve total " << _SolveTotalTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve restriction " << _SolveRestrictionTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve prolongation " << _SolveProlongationTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve smoother " << _SolveSmootherTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve next level " << _SolveNextLevelTimer.Elapsed() << std::endl; + // clang-format on + + _NextPreconditionerLevel->reportTimings(); + } + + void resetTimers() { + + _SetupTotalTimer.Reset(); + _SetupCreateSubspaceTimer.Reset(); + _SetupProjectToChiralitiesTimer.Reset(); + _SetupCoarsenOperatorTimer.Reset(); + _SetupNextLevelTimer.Reset(); + _SolveTotalTimer.Reset(); + _SolveRestrictionTimer.Reset(); + _SolveProlongationTimer.Reset(); + _SolveSmootherTimer.Reset(); + _SolveNextLevelTimer.Reset(); + + _NextPreconditionerLevel->resetTimers(); + } +}; + +// Specialization for the coarsest level +template +class MultiGridPreconditioner : public MultiGridPreconditionerBase> { +public: + ///////////////////////////////////////////// + // Type Definitions + ///////////////////////////////////////////// + + typedef Matrix FineDiracMatrix; + typedef Lattice FineVector; + + ///////////////////////////////////////////// + // Member Data + ///////////////////////////////////////////// + + int _CurrentLevel; + + MultiGridParams &_MultiGridParams; + LevelInfo & _LevelInfo; + + FineDiracMatrix &_FineMatrix; + FineDiracMatrix &_SmootherMatrix; + + GridStopWatch _SolveTotalTimer; + GridStopWatch _SolveSmootherTimer; + + ///////////////////////////////////////////// + // Member Functions + ///////////////////////////////////////////// + + MultiGridPreconditioner(MultiGridParams &mgParams, LevelInfo &LvlInfo, FineDiracMatrix &FineMat, FineDiracMatrix &SmootherMat) + : _CurrentLevel(mgParams.nLevels - (0 + 1)) + , _MultiGridParams(mgParams) + , _LevelInfo(LvlInfo) + , _FineMatrix(FineMat) + , _SmootherMatrix(SmootherMat) { + + resetTimers(); + } + + void setup() {} + + virtual void operator()(FineVector const &in, FineVector &out) { + + _SolveTotalTimer.Start(); + + conformable(_LevelInfo.Grids[_CurrentLevel], in._grid); + conformable(in, out); + + auto coarseSolverMaxIter = _MultiGridParams.coarseSolverMaxOuterIter * _MultiGridParams.coarseSolverMaxInnerIter; + + // On the coarsest level we only have what I above call the fine level, no coarse one + TrivialPrecon fineTrivialPreconditioner; + FlexibleGeneralisedMinimalResidual fineFGMRES( + _MultiGridParams.coarseSolverTol, coarseSolverMaxIter, fineTrivialPreconditioner, _MultiGridParams.coarseSolverMaxInnerIter, false); + + MdagMLinearOperator fineMdagMOp(_FineMatrix); + + _SolveSmootherTimer.Start(); + fineFGMRES(fineMdagMOp, in, out); + _SolveSmootherTimer.Stop(); + + _SolveTotalTimer.Stop(); + } + + void runChecks(RealD tolerance) {} + + void reportTimings() { + + // clang-format off + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve total " << _SolveTotalTimer.Elapsed() << std::endl; + std::cout << GridLogMG << " Level " << _CurrentLevel << ": Time elapsed: Solve smoother " << _SolveSmootherTimer.Elapsed() << std::endl; + // clang-format on + } + + void resetTimers() { + + _SolveTotalTimer.Reset(); + _SolveSmootherTimer.Reset(); + } +}; + +template +using NLevelMGPreconditioner = MultiGridPreconditioner; + +template +std::unique_ptr>> +createMGInstance(MultiGridParams &mgParams, LevelInfo &levelInfo, Matrix &FineMat, Matrix &SmootherMat) { + +#define CASE_FOR_N_LEVELS(nLevels) \ + case nLevels: \ + return std::unique_ptr>( \ + new NLevelMGPreconditioner(mgParams, levelInfo, FineMat, SmootherMat)); \ + break; + + switch(mgParams.nLevels) { + CASE_FOR_N_LEVELS(2); + CASE_FOR_N_LEVELS(3); + CASE_FOR_N_LEVELS(4); + default: + std::cout << GridLogError << "We currently only support nLevels ∈ {2, 3, 4}" << std::endl; + exit(EXIT_FAILURE); + break; + } +#undef CASE_FOR_N_LEVELS +} + +} +#endif diff --git a/tests/solver/Test_staggered_cagmres_unprec.cc b/tests/solver/Test_staggered_cagmres_unprec.cc new file mode 100644 index 00000000..b82ecaeb --- /dev/null +++ b/tests/solver/Test_staggered_cagmres_unprec.cc @@ -0,0 +1,72 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_staggered_cagmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + typedef typename ImprovedStaggeredFermionR::FermionField FermionField; + typedef typename ImprovedStaggeredFermionR::ComplexField ComplexField; + typename ImprovedStaggeredFermionR::ImplParams params; + + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Ds); + CommunicationAvoidingGeneralisedMinimalResidual CAGMRES(1.0e-8, 10000, 25); + CAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_staggered_fcagmres_prec.cc b/tests/solver/Test_staggered_fcagmres_prec.cc new file mode 100644 index 00000000..7685585b --- /dev/null +++ b/tests/solver/Test_staggered_fcagmres_prec.cc @@ -0,0 +1,75 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_staggered_fcagmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + typedef typename ImprovedStaggeredFermionR::FermionField FermionField; + typedef typename ImprovedStaggeredFermionR::ComplexField ComplexField; + typename ImprovedStaggeredFermionR::ImplParams params; + + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Ds); + + TrivialPrecon simple; + + FlexibleCommunicationAvoidingGeneralisedMinimalResidual FCAGMRES(1.0e-8, 10000, simple, 25); + FCAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_staggered_fgmres_prec.cc b/tests/solver/Test_staggered_fgmres_prec.cc new file mode 100644 index 00000000..30905e35 --- /dev/null +++ b/tests/solver/Test_staggered_fgmres_prec.cc @@ -0,0 +1,75 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_staggered_fgmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + typedef typename ImprovedStaggeredFermionR::FermionField FermionField; + typedef typename ImprovedStaggeredFermionR::ComplexField ComplexField; + typename ImprovedStaggeredFermionR::ImplParams params; + + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Ds); + + TrivialPrecon simple; + + FlexibleGeneralisedMinimalResidual FGMRES(1.0e-8, 10000, simple, 25); + FGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_staggered_gmres_unprec.cc b/tests/solver/Test_staggered_gmres_unprec.cc new file mode 100644 index 00000000..d65b0b31 --- /dev/null +++ b/tests/solver/Test_staggered_gmres_unprec.cc @@ -0,0 +1,72 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_staggered_gmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + typedef typename ImprovedStaggeredFermionR::FermionField FermionField; + typedef typename ImprovedStaggeredFermionR::ComplexField ComplexField; + typename ImprovedStaggeredFermionR::ImplParams params; + + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Ds); + GeneralisedMinimalResidual GMRES(1.0e-8, 10000, 25); + GMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_staggered_mr_unprec.cc b/tests/solver/Test_staggered_mr_unprec.cc new file mode 100644 index 00000000..ca60edb4 --- /dev/null +++ b/tests/solver/Test_staggered_mr_unprec.cc @@ -0,0 +1,72 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_staggered_mr_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + typedef typename ImprovedStaggeredFermionR::FermionField FermionField; + typedef typename ImprovedStaggeredFermionR::ComplexField ComplexField; + typename ImprovedStaggeredFermionR::ImplParams params; + + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Ds); + MinimalResidual MR(1.0e-8,10000,0.8); + MR(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_cagmres_unprec.cc b/tests/solver/Test_wilson_cagmres_unprec.cc new file mode 100644 index 00000000..46f9e6a6 --- /dev/null +++ b/tests/solver/Test_wilson_cagmres_unprec.cc @@ -0,0 +1,65 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilson_cagmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + LatticeFermion src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + LatticeFermion result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dw); + CommunicationAvoidingGeneralisedMinimalResidual CAGMRES(1.0e-8, 10000, 25); + CAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_fcagmres_prec.cc b/tests/solver/Test_wilson_fcagmres_prec.cc new file mode 100644 index 00000000..f802984f --- /dev/null +++ b/tests/solver/Test_wilson_fcagmres_prec.cc @@ -0,0 +1,68 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilson_fcagmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + LatticeFermion src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + LatticeFermion result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dw); + + TrivialPrecon simple; + + FlexibleCommunicationAvoidingGeneralisedMinimalResidual FCAGMRES(1.0e-8, 10000, simple, 25); + FCAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_fgmres_prec.cc b/tests/solver/Test_wilson_fgmres_prec.cc new file mode 100644 index 00000000..f55516da --- /dev/null +++ b/tests/solver/Test_wilson_fgmres_prec.cc @@ -0,0 +1,68 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilson_fgmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + LatticeFermion src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + LatticeFermion result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dw); + + TrivialPrecon simple; + + FlexibleGeneralisedMinimalResidual FGMRES(1.0e-8, 10000, simple, 25); + FGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_gmres_unprec.cc b/tests/solver/Test_wilson_gmres_unprec.cc new file mode 100644 index 00000000..443f7ebc --- /dev/null +++ b/tests/solver/Test_wilson_gmres_unprec.cc @@ -0,0 +1,65 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilson_gmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + LatticeFermion src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + LatticeFermion result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dw); + GeneralisedMinimalResidual GMRES(1.0e-8, 10000, 25); + GMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_mg.cc b/tests/solver/Test_wilson_mg.cc new file mode 100644 index 00000000..1609c1fc --- /dev/null +++ b/tests/solver/Test_wilson_mg.cc @@ -0,0 +1,114 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/solver/Test_wilson_mg.cc + + Copyright (C) 2015-2018 + + Author: Daniel Richtmann + + 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 +#include + +using namespace std; +using namespace Grid; +using namespace Grid::QCD; + +int main(int argc, char **argv) { + + Grid_init(&argc, &argv); + + GridCartesian * FGrid = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplex::Nsimd()), GridDefaultMpi()); + GridRedBlackCartesian *FrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid); + + std::vector fSeeds({1, 2, 3, 4}); + GridParallelRNG fPRNG(FGrid); + fPRNG.SeedFixedIntegers(fSeeds); + + // clang-format off + LatticeFermion src(FGrid); gaussian(fPRNG, src); + LatticeFermion result(FGrid); result = zero; + LatticeGaugeField Umu(FGrid); SU3::HotConfiguration(fPRNG, Umu); + // clang-format on + + RealD mass = -0.25; + + MultiGridParams mgParams; + std::string inputXml{"./mg_params.xml"}; + + if(GridCmdOptionExists(argv, argv + argc, "--inputxml")) { + inputXml = GridCmdOptionPayload(argv, argv + argc, "--inputxml"); + assert(inputXml.length() != 0); + } + + { + XmlWriter writer("mg_params_template.xml"); + write(writer, "Params", mgParams); + std::cout << GridLogMessage << "Written mg_params_template.xml" << std::endl; + + XmlReader reader(inputXml); + read(reader, "Params", mgParams); + std::cout << GridLogMessage << "Read in " << inputXml << std::endl; + } + + checkParameterValidity(mgParams); + std::cout << mgParams << std::endl; + + LevelInfo levelInfo(FGrid, mgParams); + + // Note: We do chiral doubling, so actually only nbasis/2 full basis vectors are used + const int nbasis = 40; + + WilsonFermionR Dw(Umu, *FGrid, *FrbGrid, mass); + + MdagMLinearOperator MdagMOpDw(Dw); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing Multigrid for Wilson" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + TrivialPrecon TrivialPrecon; + auto MGPreconDw = createMGInstance(mgParams, levelInfo, Dw, Dw); + + MGPreconDw->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + RealD toleranceForMGChecks = (getPrecision::value == 1) ? 1e-6 : 1e-13; + MGPreconDw->runChecks(toleranceForMGChecks); + } + + std::vector>> solversDw; + + solversDw.emplace_back(new ConjugateGradient(1.0e-12, 50000, false)); + solversDw.emplace_back(new FlexibleGeneralisedMinimalResidual(1.0e-12, 50000, TrivialPrecon, 100, false)); + solversDw.emplace_back(new FlexibleGeneralisedMinimalResidual(1.0e-12, 50000, *MGPreconDw, 100, false)); + + for(auto const &solver : solversDw) { + std::cout << std::endl << "Starting with a new solver" << std::endl; + result = zero; + (*solver)(MdagMOpDw, src, result); + } + + MGPreconDw->reportTimings(); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_mg_mp.cc b/tests/solver/Test_wilson_mg_mp.cc new file mode 100644 index 00000000..0cd51227 --- /dev/null +++ b/tests/solver/Test_wilson_mg_mp.cc @@ -0,0 +1,166 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/solver/Test_wilson_mg_mp.cc + + Copyright (C) 2015-2018 + + Author: Daniel Richtmann + + 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 +#include + +using namespace std; +using namespace Grid; +using namespace Grid::QCD; + +int main(int argc, char **argv) { + + Grid_init(&argc, &argv); + + // clang-format off + GridCartesian *FGrid_d = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplexD::Nsimd()), GridDefaultMpi()); + GridCartesian *FGrid_f = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplexF::Nsimd()), GridDefaultMpi()); + GridRedBlackCartesian *FrbGrid_d = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid_d); + GridRedBlackCartesian *FrbGrid_f = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid_f); + // clang-format on + + std::vector fSeeds({1, 2, 3, 4}); + GridParallelRNG fPRNG(FGrid_d); + fPRNG.SeedFixedIntegers(fSeeds); + + // clang-format off + LatticeFermionD src_d(FGrid_d); gaussian(fPRNG, src_d); + LatticeFermionD resultMGD_d(FGrid_d); resultMGD_d = zero; + LatticeFermionD resultMGF_d(FGrid_d); resultMGF_d = zero; + LatticeGaugeFieldD Umu_d(FGrid_d); SU3::HotConfiguration(fPRNG, Umu_d); + LatticeGaugeFieldF Umu_f(FGrid_f); precisionChange(Umu_f, Umu_d); + // clang-format on + + RealD mass = -0.25; + + MultiGridParams mgParams; + std::string inputXml{"./mg_params.xml"}; + + if(GridCmdOptionExists(argv, argv + argc, "--inputxml")) { + inputXml = GridCmdOptionPayload(argv, argv + argc, "--inputxml"); + assert(inputXml.length() != 0); + } + + { + XmlWriter writer("mg_params_template.xml"); + write(writer, "Params", mgParams); + std::cout << GridLogMessage << "Written mg_params_template.xml" << std::endl; + + XmlReader reader(inputXml); + read(reader, "Params", mgParams); + std::cout << GridLogMessage << "Read in " << inputXml << std::endl; + } + + checkParameterValidity(mgParams); + std::cout << mgParams << std::endl; + + LevelInfo levelInfo_d(FGrid_d, mgParams); + LevelInfo levelInfo_f(FGrid_f, mgParams); + + // Note: We do chiral doubling, so actually only nbasis/2 full basis vectors are used + const int nbasis = 40; + + WilsonFermionD Dw_d(Umu_d, *FGrid_d, *FrbGrid_d, mass); + WilsonFermionF Dw_f(Umu_f, *FGrid_f, *FrbGrid_f, mass); + + MdagMLinearOperator MdagMOpDw_d(Dw_d); + MdagMLinearOperator MdagMOpDw_f(Dw_f); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing single-precision Multigrid for Wilson" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + auto MGPreconDw_f = createMGInstance(mgParams, levelInfo_f, Dw_f, Dw_f); + + MGPreconDw_f->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + MGPreconDw_f->runChecks(1e-6); + } + + MixedPrecisionFlexibleGeneralisedMinimalResidual MPFGMRESPREC(1.0e-12, 50000, FGrid_f, *MGPreconDw_f, 100, false); + + std::cout << std::endl << "Starting with a new solver" << std::endl; + MPFGMRESPREC(MdagMOpDw_d, src_d, resultMGF_d); + + MGPreconDw_f->reportTimings(); + + if(GridCmdOptionExists(argv, argv + argc, "--docomparison")) { + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing double-precision Multigrid for Wilson" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + auto MGPreconDw_d = createMGInstance(mgParams, levelInfo_d, Dw_d, Dw_d); + + MGPreconDw_d->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + MGPreconDw_d->runChecks(1e-13); + } + + FlexibleGeneralisedMinimalResidual FGMRESPREC(1.0e-12, 50000, *MGPreconDw_d, 100, false); + + std::cout << std::endl << "Starting with a new solver" << std::endl; + FGMRESPREC(MdagMOpDw_d, src_d, resultMGD_d); + + MGPreconDw_d->reportTimings(); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Comparing single-precision Multigrid with double-precision one for Wilson" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + LatticeFermionD diffFullSolver(FGrid_d); + + RealD deviationFullSolver = axpy_norm(diffFullSolver, -1.0, resultMGF_d, resultMGD_d); + + // clang-format off + LatticeFermionF src_f(FGrid_f); precisionChange(src_f, src_d); + LatticeFermionF resMGF_f(FGrid_f); resMGF_f = zero; + LatticeFermionD resMGD_d(FGrid_d); resMGD_d = zero; + // clang-format on + + (*MGPreconDw_f)(src_f, resMGF_f); + (*MGPreconDw_d)(src_d, resMGD_d); + + LatticeFermionD diffOnlyMG(FGrid_d); + LatticeFermionD resMGF_d(FGrid_d); + precisionChange(resMGF_d, resMGF_f); + + RealD deviationOnlyPrec = axpy_norm(diffOnlyMG, -1.0, resMGF_d, resMGD_d); + + // clang-format off + std::cout << GridLogMessage << "Absolute difference between FGMRES preconditioned by double and single precicision MG: " << deviationFullSolver << std::endl; + std::cout << GridLogMessage << "Relative deviation between FGMRES preconditioned by double and single precicision MG: " << deviationFullSolver / norm2(resultMGD_d) << std::endl; + std::cout << GridLogMessage << "Absolute difference between one iteration of MG Prec in double and single precision: " << deviationOnlyPrec << std::endl; + std::cout << GridLogMessage << "Relative deviation between one iteration of MG Prec in double and single precision: " << deviationOnlyPrec / norm2(resMGD_d) << std::endl; + // clang-format on + } + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilson_mr_unprec.cc b/tests/solver/Test_wilson_mr_unprec.cc new file mode 100644 index 00000000..976130d3 --- /dev/null +++ b/tests/solver/Test_wilson_mr_unprec.cc @@ -0,0 +1,65 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilson_mr_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + LatticeFermion src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + LatticeFermion result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dw); + MinimalResidual MR(1.0e-8,10000,0.8); + MR(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_cagmres_unprec.cc b/tests/solver/Test_wilsonclover_cagmres_unprec.cc new file mode 100644 index 00000000..3ecdf738 --- /dev/null +++ b/tests/solver/Test_wilsonclover_cagmres_unprec.cc @@ -0,0 +1,71 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilsonclover_cagmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + typedef typename WilsonCloverFermionR::FermionField FermionField; + typename WilsonCloverFermionR::ImplParams params; + WilsonAnisotropyCoefficients anis; + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dwc); + CommunicationAvoidingGeneralisedMinimalResidual CAGMRES(1.0e-8, 10000, 25); + CAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_fcagmres_prec.cc b/tests/solver/Test_wilsonclover_fcagmres_prec.cc new file mode 100644 index 00000000..3cbbfc02 --- /dev/null +++ b/tests/solver/Test_wilsonclover_fcagmres_prec.cc @@ -0,0 +1,74 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilsonclover_fcagmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + typedef typename WilsonCloverFermionR::FermionField FermionField; + typename WilsonCloverFermionR::ImplParams params; + WilsonAnisotropyCoefficients anis; + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dwc); + + TrivialPrecon simple; + + FlexibleCommunicationAvoidingGeneralisedMinimalResidual FCAGMRES(1.0e-8, 10000, simple, 25); + FCAGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_fgmres_prec.cc b/tests/solver/Test_wilsonclover_fgmres_prec.cc new file mode 100644 index 00000000..7ad0fa24 --- /dev/null +++ b/tests/solver/Test_wilsonclover_fgmres_prec.cc @@ -0,0 +1,74 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilsonclover_fgmres_prec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + typedef typename WilsonCloverFermionR::FermionField FermionField; + typename WilsonCloverFermionR::ImplParams params; + WilsonAnisotropyCoefficients anis; + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dwc); + + TrivialPrecon simple; + + FlexibleGeneralisedMinimalResidual FGMRES(1.0e-8, 10000, simple, 25); + FGMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_gmres_unprec.cc b/tests/solver/Test_wilsonclover_gmres_unprec.cc new file mode 100644 index 00000000..a9fe7181 --- /dev/null +++ b/tests/solver/Test_wilsonclover_gmres_unprec.cc @@ -0,0 +1,71 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilsonclover_gmres_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + typedef typename WilsonCloverFermionR::FermionField FermionField; + typename WilsonCloverFermionR::ImplParams params; + WilsonAnisotropyCoefficients anis; + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dwc); + GeneralisedMinimalResidual GMRES(1.0e-8, 10000, 25); + GMRES(HermOp,src,result); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_mg.cc b/tests/solver/Test_wilsonclover_mg.cc new file mode 100644 index 00000000..e749aacb --- /dev/null +++ b/tests/solver/Test_wilsonclover_mg.cc @@ -0,0 +1,117 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/solver/Test_wilsonclover_mg.cc + + Copyright (C) 2015-2018 + + Author: Daniel Richtmann + + 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 +#include + +using namespace std; +using namespace Grid; +using namespace Grid::QCD; + +int main(int argc, char **argv) { + + Grid_init(&argc, &argv); + + GridCartesian * FGrid = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplex::Nsimd()), GridDefaultMpi()); + GridRedBlackCartesian *FrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid); + + std::vector fSeeds({1, 2, 3, 4}); + GridParallelRNG fPRNG(FGrid); + fPRNG.SeedFixedIntegers(fSeeds); + + // clang-format off + LatticeFermion src(FGrid); gaussian(fPRNG, src); + LatticeFermion result(FGrid); result = zero; + LatticeGaugeField Umu(FGrid); SU3::HotConfiguration(fPRNG, Umu); + // clang-format on + + RealD mass = -0.25; + RealD csw_r = 1.0; + RealD csw_t = 1.0; + + MultiGridParams mgParams; + std::string inputXml{"./mg_params.xml"}; + + if(GridCmdOptionExists(argv, argv + argc, "--inputxml")) { + inputXml = GridCmdOptionPayload(argv, argv + argc, "--inputxml"); + assert(inputXml.length() != 0); + } + + { + XmlWriter writer("mg_params_template.xml"); + write(writer, "Params", mgParams); + std::cout << GridLogMessage << "Written mg_params_template.xml" << std::endl; + + XmlReader reader(inputXml); + read(reader, "Params", mgParams); + std::cout << GridLogMessage << "Read in " << inputXml << std::endl; + } + + checkParameterValidity(mgParams); + std::cout << mgParams << std::endl; + + LevelInfo levelInfo(FGrid, mgParams); + + // Note: We do chiral doubling, so actually only nbasis/2 full basis vectors are used + const int nbasis = 40; + + WilsonCloverFermionR Dwc(Umu, *FGrid, *FrbGrid, mass, csw_r, csw_t); + + MdagMLinearOperator MdagMOpDwc(Dwc); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing Multigrid for Wilson Clover" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + TrivialPrecon TrivialPrecon; + auto MGPreconDwc = createMGInstance(mgParams, levelInfo, Dwc, Dwc); + + MGPreconDwc->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + RealD toleranceForMGChecks = (getPrecision::value == 1) ? 1e-6 : 1e-13; + MGPreconDwc->runChecks(toleranceForMGChecks); + } + + std::vector>> solversDwc; + + solversDwc.emplace_back(new ConjugateGradient(1.0e-12, 50000, false)); + solversDwc.emplace_back(new FlexibleGeneralisedMinimalResidual(1.0e-12, 50000, TrivialPrecon, 100, false)); + solversDwc.emplace_back(new FlexibleGeneralisedMinimalResidual(1.0e-12, 50000, *MGPreconDwc, 100, false)); + + for(auto const &solver : solversDwc) { + std::cout << std::endl << "Starting with a new solver" << std::endl; + result = zero; + (*solver)(MdagMOpDwc, src, result); + std::cout << std::endl; + } + + MGPreconDwc->reportTimings(); + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_mg_mp.cc b/tests/solver/Test_wilsonclover_mg_mp.cc new file mode 100644 index 00000000..d9ed1d33 --- /dev/null +++ b/tests/solver/Test_wilsonclover_mg_mp.cc @@ -0,0 +1,169 @@ +/************************************************************************************* + + Grid physics library, www.github.com/paboyle/Grid + + Source file: ./tests/solver/Test_wilsonclover_mg_mp.cc + + Copyright (C) 2015-2018 + + Author: Daniel Richtmann + + 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 +#include + +using namespace std; +using namespace Grid; +using namespace Grid::QCD; + +int main(int argc, char **argv) { + + Grid_init(&argc, &argv); + + // clang-format off + GridCartesian *FGrid_d = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplexD::Nsimd()), GridDefaultMpi()); + GridCartesian *FGrid_f = SpaceTimeGrid::makeFourDimGrid(GridDefaultLatt(), GridDefaultSimd(Nd, vComplexF::Nsimd()), GridDefaultMpi()); + GridRedBlackCartesian *FrbGrid_d = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid_d); + GridRedBlackCartesian *FrbGrid_f = SpaceTimeGrid::makeFourDimRedBlackGrid(FGrid_f); + // clang-format on + + std::vector fSeeds({1, 2, 3, 4}); + GridParallelRNG fPRNG(FGrid_d); + fPRNG.SeedFixedIntegers(fSeeds); + + // clang-format off + LatticeFermionD src_d(FGrid_d); gaussian(fPRNG, src_d); + LatticeFermionD resultMGD_d(FGrid_d); resultMGD_d = zero; + LatticeFermionD resultMGF_d(FGrid_d); resultMGF_d = zero; + LatticeGaugeFieldD Umu_d(FGrid_d); SU3::HotConfiguration(fPRNG, Umu_d); + LatticeGaugeFieldF Umu_f(FGrid_f); precisionChange(Umu_f, Umu_d); + // clang-format on + + RealD mass = -0.25; + RealD csw_r = 1.0; + RealD csw_t = 1.0; + + MultiGridParams mgParams; + std::string inputXml{"./mg_params.xml"}; + + if(GridCmdOptionExists(argv, argv + argc, "--inputxml")) { + inputXml = GridCmdOptionPayload(argv, argv + argc, "--inputxml"); + assert(inputXml.length() != 0); + } + + { + XmlWriter writer("mg_params_template.xml"); + write(writer, "Params", mgParams); + std::cout << GridLogMessage << "Written mg_params_template.xml" << std::endl; + + XmlReader reader(inputXml); + read(reader, "Params", mgParams); + std::cout << GridLogMessage << "Read in " << inputXml << std::endl; + } + + checkParameterValidity(mgParams); + std::cout << mgParams << std::endl; + + LevelInfo levelInfo_d(FGrid_d, mgParams); + LevelInfo levelInfo_f(FGrid_f, mgParams); + + // Note: We do chiral doubling, so actually only nbasis/2 full basis vectors are used + const int nbasis = 40; + + WilsonCloverFermionD Dwc_d(Umu_d, *FGrid_d, *FrbGrid_d, mass, csw_r, csw_t); + WilsonCloverFermionF Dwc_f(Umu_f, *FGrid_f, *FrbGrid_f, mass, csw_r, csw_t); + + MdagMLinearOperator MdagMOpDwc_d(Dwc_d); + MdagMLinearOperator MdagMOpDwc_f(Dwc_f); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing single-precision Multigrid for Wilson Clover" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + auto MGPreconDwc_f = createMGInstance(mgParams, levelInfo_f, Dwc_f, Dwc_f); + + MGPreconDwc_f->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + MGPreconDwc_f->runChecks(1e-6); + } + + MixedPrecisionFlexibleGeneralisedMinimalResidual MPFGMRESPREC( + 1.0e-12, 50000, FGrid_f, *MGPreconDwc_f, 100, false); + + std::cout << std::endl << "Starting with a new solver" << std::endl; + MPFGMRESPREC(MdagMOpDwc_d, src_d, resultMGF_d); + + MGPreconDwc_f->reportTimings(); + + if(GridCmdOptionExists(argv, argv + argc, "--docomparison")) { + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Testing double-precision Multigrid for Wilson Clover" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + auto MGPreconDwc_d = createMGInstance(mgParams, levelInfo_d, Dwc_d, Dwc_d); + + MGPreconDwc_d->setup(); + + if(GridCmdOptionExists(argv, argv + argc, "--runchecks")) { + MGPreconDwc_d->runChecks(1e-13); + } + + FlexibleGeneralisedMinimalResidual FGMRESPREC(1.0e-12, 50000, *MGPreconDwc_d, 100, false); + + std::cout << std::endl << "Starting with a new solver" << std::endl; + FGMRESPREC(MdagMOpDwc_d, src_d, resultMGD_d); + + MGPreconDwc_d->reportTimings(); + + std::cout << GridLogMessage << "**************************************************" << std::endl; + std::cout << GridLogMessage << "Comparing single-precision Multigrid with double-precision one for Wilson Clover" << std::endl; + std::cout << GridLogMessage << "**************************************************" << std::endl; + + LatticeFermionD diffFullSolver(FGrid_d); + + RealD deviationFullSolver = axpy_norm(diffFullSolver, -1.0, resultMGF_d, resultMGD_d); + + // clang-format off + LatticeFermionF src_f(FGrid_f); precisionChange(src_f, src_d); + LatticeFermionF resMGF_f(FGrid_f); resMGF_f = zero; + LatticeFermionD resMGD_d(FGrid_d); resMGD_d = zero; + // clang-format on + + (*MGPreconDwc_f)(src_f, resMGF_f); + (*MGPreconDwc_d)(src_d, resMGD_d); + + LatticeFermionD diffOnlyMG(FGrid_d); + LatticeFermionD resMGF_d(FGrid_d); + precisionChange(resMGF_d, resMGF_f); + + RealD deviationOnlyPrec = axpy_norm(diffOnlyMG, -1.0, resMGF_d, resMGD_d); + + // clang-format off + std::cout << GridLogMessage << "Absolute difference between FGMRES preconditioned by double and single precicision MG: " << deviationFullSolver << std::endl; + std::cout << GridLogMessage << "Relative deviation between FGMRES preconditioned by double and single precicision MG: " << deviationFullSolver / norm2(resultMGD_d) << std::endl; + std::cout << GridLogMessage << "Absolute difference between one iteration of MG Prec in double and single precision: " << deviationOnlyPrec << std::endl; + std::cout << GridLogMessage << "Relative deviation between one iteration of MG Prec in double and single precision: " << deviationOnlyPrec / norm2(resMGD_d) << std::endl; + // clang-format on + } + + Grid_finalize(); +} diff --git a/tests/solver/Test_wilsonclover_mr_unprec.cc b/tests/solver/Test_wilsonclover_mr_unprec.cc new file mode 100644 index 00000000..e3aa8838 --- /dev/null +++ b/tests/solver/Test_wilsonclover_mr_unprec.cc @@ -0,0 +1,71 @@ +/************************************************************************************* + +Grid physics library, www.github.com/paboyle/Grid + +Source file: ./tests/solver/Test_wilsonclover_mr_unprec.cc + +Copyright (C) 2015-2018 + +Author: Daniel Richtmann + +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 Grid; +using namespace Grid::QCD; + +int main (int argc, char ** argv) +{ + Grid_init(&argc,&argv); + + std::vector latt_size = GridDefaultLatt(); + std::vector simd_layout = GridDefaultSimd(Nd,vComplex::Nsimd()); + std::vector mpi_layout = GridDefaultMpi(); + GridCartesian Grid(latt_size,simd_layout,mpi_layout); + GridRedBlackCartesian RBGrid(&Grid); + + std::vector seeds({1,2,3,4}); + GridParallelRNG pRNG(&Grid); pRNG.SeedFixedIntegers(seeds); + + typedef typename WilsonCloverFermionR::FermionField FermionField; + typename WilsonCloverFermionR::ImplParams params; + WilsonAnisotropyCoefficients anis; + + FermionField src(&Grid); random(pRNG,src); + RealD nrm = norm2(src); + FermionField result(&Grid); result=zero; + LatticeGaugeField Umu(&Grid); SU3::HotConfiguration(pRNG,Umu); + + double volume=1; + for(int mu=0;mu HermOp(Dwc); + MinimalResidual MR(1.0e-8,10000,0.8); + MR(HermOp,src,result); + + Grid_finalize(); +}