sub_matrix_pc.hpp 5.51 KB
Newer Older
1
// Software License for MTL
2
3
//
// Copyright (c) 2007 The Trustees of Indiana University.
4
//               2008 Dresden University of Technology and the Trustees of Indiana University.
5
//               2010 SimuNova UG, www.simunova.com.
6
7
// All rights reserved.
// Authors: Peter Gottschling and Andrew Lumsdaine
8
//
9
// This file is part of the Matrix Template Library
10
//
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// See also tools/license/license.mtl.txt in the distribution.

#ifndef ITL_PC_SUB_MATRIX_PC_INCLUDE
#define ITL_PC_SUB_MATRIX_PC_INCLUDE

#include <boost/static_assert.hpp>
#include <boost/mpl/if.hpp>

#include <boost/numeric/mtl/utility/range_generator.hpp>
#include <boost/numeric/mtl/utility/property_map.hpp>
#include <boost/numeric/mtl/vector/dense_vector.hpp>
#include <boost/numeric/mtl/interface/vpt.hpp>
#include <boost/numeric/itl/pc/solver.hpp>

namespace itl { namespace pc {

/// Class for applying \tparam Preconditioner only on a sub-matrix
28
/** Other entries are just copied.
29
    Optionally preconditioner can be referred from outside instead of storing it by setting
30
    \tparam Store to true.
31
32
33
34
35
36
37
38
39
**/
template <typename Preconditioner, typename Matrix, bool Store= true>
class sub_matrix_pc
{
    typedef mtl::vector::dense_vector<bool>                                               tag_type;
    typedef typename boost::mpl::if_c<Store, Preconditioner, const Preconditioner&>::type pc_type;

    struct matrix_container
    {
40
	matrix_container() : Ap(0) {}
41
42
43

	matrix_container(const tag_type& tags, const Matrix& src)
	{
44
	    using std::size_t;
45
46
47
48
49
50
51
52
53
54
55
56
	    using namespace mtl;

	    mtl::vector::dense_vector<size_t> perm(size(tags));
	    size_t n= 0;
	    for (size_t i= 0; i < size(tags); ++i) {
		perm[i]= n;
		if (tags[i])
		    ++n;
	    }

	    Ap= new Matrix(n, n);

57
58
59
	    typename traits::row<Matrix>::type             row(src);
	    typename traits::col<Matrix>::type             col(src);
	    typename traits::const_value<Matrix>::type     value(src);
60
61
62
	    typedef typename traits::range_generator<tag::major, Matrix>::type  cursor_type;

	    matrix::inserter<Matrix> ins(*Ap, Ap->nnz() / Ap->dim1());
63
64

	    for (cursor_type cursor = mtl::begin<tag::major>(src), cend = mtl::end<tag::major>(src);
65
66
		 cursor != cend; ++cursor) {
		// std::cout << dest << '\n';
67

68
		typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type;
69
70
		for (icursor_type icursor = mtl::begin<tag::nz>(cursor), icend = mtl::end<tag::nz>(cursor);
		     icursor != icend; ++icursor)
71
		    if (tags[row(*icursor)] && tags[col(*icursor)])
72
			ins(perm[row(*icursor)], perm[col(*icursor)]) << value(*icursor);
73
74
75
76
77
78
79
80
81
82
83
84
	    }
	}

	~matrix_container() { delete Ap; }

	Matrix* Ap;
    };

    size_t count_entries() const
    {
	using mtl::size;
	size_t n= 0;
85
	for (size_t i= 0; i < size(tags); ++i)
86
87
88
89
90
91
92
93
94
95
	    if (tags[i]) ++n;
	return n;
    }

  public:

    sub_matrix_pc(const tag_type& tags, const Matrix& A)
      : tags(tags), n(count_entries()), mc(tags, A), P(*mc.Ap)
    {
	BOOST_STATIC_ASSERT((Store));
96
	delete mc.Ap;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	mc.Ap= 0;
    }

#if 0 // to do later
    sub_matrix_pc(const tag_type& tags, const Preconditioner& P)
      : tags(tags), P(P)
    {
	// check sizes
    }
#endif

  private:
    template <typename VectorIn>
    VectorIn& create_x0(VectorIn) const
    {
	static VectorIn  x0(n);
	return x0;
    }

    template <typename VectorOut>
    VectorOut& create_y0(VectorOut) const
    {
	static VectorOut  y0(n);
	return y0;
    }

    template <typename VectorIn>
    void restrict(const VectorIn& x, VectorIn& x0) const
    {
126
127
	for (size_t i= 0, j= 0; i < size(tags); ++i)
	    if (tags[i])
128
129
130
131
132
133
		x0[j++]= x[i];
    }

    template <typename VectorIn, typename VectorOut>
    void prolongate(const VectorIn& x, const VectorOut& y0, VectorOut& y) const
    {
134
135
	for (size_t i= 0, j= 0; i < size(tags); ++i)
	    if (tags[i])
136
137
		y[i]= y0[j++];
	    else
138
		y[i]= x[i];
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    }

  public:
    /// Solve Px = y approximately on according sub-system; remaining entries are copied
    template <typename VectorIn, typename VectorOut>
    void solve(const VectorIn& x, VectorOut& y) const
    {
	mtl::vampir_trace<5056> tracer;
	y.checked_change_resource(x);

	VectorIn&  x0= create_x0(x);
	VectorOut& y0= create_y0(y);

	restrict(x, x0);
	P.solve(x0, y0);
	// y0= solve(P, x0); // doesn't compile yet for unknown reasons
	prolongate(x, y0, y);
    }

    /// Solve Px = y approximately on according sub-system; remaining entries are copied
    template <typename VectorIn, typename VectorOut>
    void adjoint_solve(const VectorIn& x, VectorOut& y) const
    {
	mtl::vampir_trace<5057> tracer;
	y.checked_change_resource(x);

	VectorIn&  x0= create_x0(x);
	VectorOut& y0= create_y0(y);

	restrict(x, x0);
	P.adjoint_solve(x0, y0);
	// y0= adjoint_solve(P, x0); // doesn't compile yet for unknown reasons
	prolongate(x, y0, y);
    }

  private:
    tag_type          tags;
    size_t            n;
    matrix_container  mc;
    pc_type           P;
};

template <typename Preconditioner, typename Matrix, bool Store, typename Vector>
solver<sub_matrix_pc<Preconditioner, Matrix, Store>, Vector, false>
inline solve(const sub_matrix_pc<Preconditioner, Matrix, Store>& P, const Vector& x)
{
    return solver<sub_matrix_pc<Preconditioner, Matrix, Store>, Vector, false>(P, x);
}

template <typename Preconditioner, typename Matrix, bool Store, typename Vector>
solver<sub_matrix_pc<Preconditioner, Matrix, Store>, Vector, true>
inline adjoint_solve(const sub_matrix_pc<Preconditioner, Matrix, Store>& P, const Vector& x)
{
    return solver<sub_matrix_pc<Preconditioner, Matrix, Store>, Vector, true>(P, x);
}


}} // namespace itl::pc

#endif // ITL_PC_SUB_MATRIX_PC_INCLUDE