2#include <cusp/dia_matrix.h>
3#include <cusp/coo_matrix.h>
35template<
class ContainerType>
40 using HCooMatrix = cusp::coo_matrix<int, value_type, cusp::host_memory>;
41 using HDiaMatrix = cusp::dia_matrix<int, value_type, cusp::host_memory>;
51 MCG(
const ContainerType& copyable,
unsigned max_iterations)
53 m_ap = m_p = m_r = copyable;
54 m_max_iter = max_iterations;
55 set_iter( max_iterations);
67 unsigned get_max()
const {
return m_max_iter;}
78 template<
class ...Params>
82 *
this =
MCG( std::forward<Params>( ps)...);
97 template<
class MatrixType,
class DiaMatrixType,
class ContainerType0,
98 class ContainerType1,
class ContainerType2>
99 void Ry( MatrixType&& A,
const DiaMatrixType& T,
100 const ContainerType0& y, ContainerType1& x,
101 const ContainerType2& b)
110 for (
unsigned i=0; i<
y.size()-1; i++)
114 T.values(i,1) + T.values( i-1,2);
145 template<
class MatrixType,
class ContainerType0,
class ContainerType1>
152 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
159 DG_RANK0 std::cout <<
"# Norm of b "<<nrmb <<
"\n";
160 DG_RANK0 std::cout <<
"# Res factor "<<res_fac <<
"\n";
161 DG_RANK0 std::cout <<
"# Residual errors: \n";
171 value_type alpha = 0, beta = 0, nrmzr_new = 0, alpha_old = 0., beta_old = 0.;
172 for(
unsigned i=0; i<m_max_iter; i++)
174 alpha_old = alpha, beta_old = beta;
179 beta = nrmzr_new/nrmzr_old;
182 DG_RANK0 std::cout <<
"# ||r||_W = " << sqrt(nrmzr_new) <<
"\tat i = " << i <<
"\n";
186 m_TH.values(i,0) = 0.;
187 m_TH.values(i,1) = 1./alpha;
188 m_TH.values(i,2) = -beta/alpha;
192 m_TH.values(i,0) = -1./alpha_old;
193 m_TH.values(i,1) = 1./alpha + beta_old/alpha_old;
194 m_TH.values(i,2) = -beta/alpha;
196 if( res_fac*sqrt( nrmzr_new)
197 < eps*(nrmb + nrmb_correction))
215 HVec e1H(m_iter, 0.);
222 void set_iter(
unsigned new_iter) {
225 m_TH.resize(new_iter, new_iter, 3*new_iter-2, 3, m_max_iter);
226 m_TH.diagonal_offsets[0] = -1;
227 m_TH.diagonal_offsets[1] = 0;
228 m_TH.diagonal_offsets[2] = 1;
231 ContainerType m_r, m_ap, m_p;
232 unsigned m_max_iter, m_iter;
234 bool m_verbose =
false;
256template<
class Container>
261 using HDiaMatrix = cusp::dia_matrix<int, value_type, cusp::host_memory>;
262 using HCooMatrix = cusp::coo_matrix<int, value_type, cusp::host_memory>;
263 using HArray2d = cusp::array2d< value_type, cusp::host_memory>;
264 using HArray1d = cusp::array1d< value_type, cusp::host_memory>;
276 m_e1H.assign(max_iterations, 0.);
278 m_yH.assign(max_iterations, 1.);
279 m_mcg.
construct(copyable, max_iterations);
282 template<
class ...Params>
303 template <
class MatrixType,
class ContainerType0,
class ContainerType1 ,
304 class ContainerType2,
class UnaryOp>
306 MatrixType&& A,
const ContainerType1& b,
316 m_TH = m_mcg(std::forward<MatrixType>(A), b,
weights, eps,
317 nrmb_correction, res_fac);
320 m_alpha.resize(iter);
321 m_delta.resize(iter,1.);
322 m_beta.resize(iter-1);
323 m_evals.resize(iter);
324 m_evecs.resize(iter,iter);
325 m_e1H.resize(iter, 0.);
329 for(
unsigned i = 0; i<iter; i++)
331 m_alpha[i] = m_TH.values(i,1);
333 if (m_TH.values(i,2) > 0.) m_beta[i] = sqrt(m_TH.values(i,2)*m_TH.values(i+1,0));
334 else if (m_TH.values(i,2) < 0.) m_beta[i] = -sqrt(m_TH.values(i,2)*m_TH.values(i+1,0));
337 if (i>0) m_delta[i] = m_delta[i-1]*sqrt(m_TH.values(i,0)/m_TH.values(i-1,2));
340 cusp::lapack::stev(m_alpha, m_beta, m_evals, m_evecs);
352 catch(boost::exception& e)
361 m_mcg.
Ry(std::forward<MatrixType>(A), m_TH, m_yH,
x, b);
370 HArray1d m_alpha, m_beta, m_delta, m_evals;
EXPERIMENTAL Shortcut for solve via exploiting first a Krylov projection achieved by the M-CG method...
Definition: mcg.h:258
dg::get_value_type< Container > value_type
Definition: mcg.h:260
cusp::coo_matrix< int, value_type, cusp::host_memory > HCooMatrix
Definition: mcg.h:262
dg::HVec HVec
Definition: mcg.h:265
cusp::dia_matrix< int, value_type, cusp::host_memory > HDiaMatrix
Definition: mcg.h:261
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: mcg.h:283
cusp::array1d< value_type, cusp::host_memory > HArray1d
Definition: mcg.h:264
MCGFuncEigen()
Allocate nothing, Call construct method before usage.
Definition: mcg.h:267
MCGFuncEigen(const Container ©able, unsigned max_iterations)
Construct MCGFuncEigen.
Definition: mcg.h:274
cusp::array2d< value_type, cusp::host_memory > HArray2d
Definition: mcg.h:263
unsigned operator()(ContainerType0 &x, UnaryOp f, MatrixType &&A, const ContainerType1 &b, const ContainerType2 &weights, value_type eps, value_type nrmb_correction, value_type res_fac)
Compute via M-CG method and Eigen decomposition.
Definition: mcg.h:305
EXPERIMENTAL Tridiagonalize and approximate via CG algorithm. A is self-adjoint in the weights .
Definition: mcg.h:37
value_type get_bnorm() const
Norm of b from last call to operator()
Definition: mcg.h:75
void set_verbose(bool verbose)
Set or unset debugging output during iterations.
Definition: mcg.h:71
unsigned get_max() const
Get the current maximum number of iterations.
Definition: mcg.h:67
const HDiaMatrix & operator()(MatrixType &&A, const ContainerType0 &b, const ContainerType1 &weights, value_type eps=1e-12, value_type nrmb_correction=1., value_type res_fac=1.)
Tridiagonalize the system using CG.
Definition: mcg.h:146
void Ry(MatrixType &&A, const DiaMatrixType &T, const ContainerType0 &y, ContainerType1 &x, const ContainerType2 &b)
Compute x = R y.
Definition: mcg.h:99
cusp::coo_matrix< int, value_type, cusp::host_memory > HCooMatrix
Definition: mcg.h:40
dg::get_value_type< ContainerType > value_type
value type of the ContainerType class
Definition: mcg.h:39
dg::HVec HVec
Definition: mcg.h:42
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition: mcg.h:79
cusp::dia_matrix< int, value_type, cusp::host_memory > HDiaMatrix
Definition: mcg.h:41
void set_max(unsigned new_max)
Set the maximum number of iterations.
Definition: mcg.h:60
MCG()
Allocate nothing, Call construct method before usage.
Definition: mcg.h:44
MCG(const ContainerType ©able, unsigned max_iterations)
Allocate memory for the mcg method.
Definition: mcg.h:51
unsigned get_iter() const
Get the number of iterations in the last call to operator() (size of T)
Definition: mcg.h:87
HVec make_e1()
Return the vector with size get_iter()
Definition: mcg.h:214
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
void transform(const ContainerType1 &x, ContainerType &y, UnaryOp op)
void axpby(get_value_type< ContainerType > alpha, const ContainerType1 &x, get_value_type< ContainerType > beta, ContainerType &y)
void pointwiseDivide(get_value_type< ContainerType > alpha, const ContainerType1 &x1, const ContainerType2 &x2, get_value_type< ContainerType > beta, ContainerType &y)
void pointwiseDot(get_value_type< ContainerType > alpha, const ContainerType1 &x1, const ContainerType2 &x2, get_value_type< ContainerType > beta, ContainerType &y)
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
get_value_type< MatrixType > dot(const ContainerType1 &x, const MatrixType &m, const ContainerType2 &y)
MPI_Vector< thrust::host_vector< real_type > > weights(const aRealMPITopology2d< real_type > &g)
void transpose(unsigned nx, unsigned ny, const ContainerType &in, ContainerType &out)
dg::MIHMatrix_t< real_type > convert(const dg::IHMatrix_t< real_type > &global, const ConversionPolicy &policy)
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
thrust::host_vector< double > HVec
Classes for Krylov space approximations of a Matrix-Vector product.