rheolef  6.5
csr_concat.h
Go to the documentation of this file.
1 #ifndef _RHEOLEF_CSR_CONCAT_H
2 #define _RHEOLEF_CSR_CONCAT_H
3 #include "rheolef/csr.h"
4 
5 namespace rheolef {
6 
7 // A = {a, u}; // matrix & vector
8 // A = {trans(u), 3.2}; // trans(vector) & scalar
9 template <class T, class M>
10 struct vec_trans {
12  vec_trans (const vec<T,M>& w) : v(w) {}
13 };
14 template <class T, class M>
15 inline
16 vec_trans<T,M>
17 trans (const vec<T,M>& w) {
18  return vec_trans<T,M> (w);
19 }
20 template <class T, class M>
22  std::vector<vec<T,M> > v;
23  vector_vec_trans (const std::vector<vec<T,M> >& w) : v(w) {}
24 };
25 template <class T, class M>
26 inline
27 vector_vec_trans<T,M>
28 trans (const std::vector<vec<T,M> >& w) {
29  return vector_vec_trans<T,M> (w);
30 }
31 
32 template <class T, class M>
33 struct csr_concat_value {
35  csr_concat_value (const T& x) : s(x), v(), vv(), m(), variant(scalar) {}
36  csr_concat_value (const vec<T,M>& x) : s(), v(x), vv(), m(), variant(vector) {}
37  csr_concat_value (const vec_trans<T,M>& vt) : s(), v(vt.v), vv(), m(), variant(vector_transpose) {}
38  csr_concat_value (const std::vector<vec<T,M> >& x) : s(), v(), vv(x), m(), variant(vector_vec) {}
40  csr_concat_value (const csr<T,M>& x) : s(), v(), vv(), m(x), variant(matrix) {}
41 
42 public:
43  T s;
45  std::vector<vec<T,M> > vv;
48 };
49 
50 template <class T, class M>
51 struct csr_concat_line {
52  typedef typename csr<T,M>::size_type size_type;
54  csr_concat_line () : _l() {}
55 
56 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
57  csr_concat_line (const std::initializer_list<value_type>& il) : _l() {
58 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
59  typedef typename std::initializer_list<value_type>::const_iterator const_iterator;
60 #else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
61  typedef const value_type* const_iterator;
62 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
63  for (const_iterator iter = il.begin(); iter != il.end(); ++iter) {
64  _l.push_back(*iter);
65  }
66  }
67 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
68 
69  void push_back (const value_type& x) { _l.push_back (x); }
70 
71  friend std::ostream& operator<< (std::ostream& o, const csr_concat_line<T,M>& x) {
72  std::cout << "{";
73  for(typename std::list<value_type>::const_iterator iter = x._l.begin(); iter != x._l.end(); ++iter) {
74  std::cout << *iter << " ";
75  }
76  return std::cout << "}";
77  }
78  static void set_sizes (
79  std::pair<size_type,size_type>& row_sizes,
80  std::pair<size_type,size_type>& col_sizes,
81  const std::pair<size_type,size_type>& new_row_sizes,
82  const std::pair<size_type,size_type>& new_col_sizes);
83 
84  static void finalize_sizes (
85  std::pair<size_type,size_type>& sizes,
86  const communicator& comm);
87 
88  static void finalize_sizes (
89  std::vector<std::pair<size_type,size_type> >& sizes,
90  const communicator& comm);
91 
92  void build_csr_pass0 (
93  std::pair<size_type,size_type>& row_sizes,
94  std::vector<std::pair<size_type,size_type> >& col_sizes,
95  communicator& comm) const;
96 
97  void build_csr_pass1 (
98  const std::pair<size_type,size_type>& row_sizes,
99  const std::vector<std::pair<size_type,size_type> >& col_sizes,
100  const communicator& comm,
101  distributor& row_ownership,
102  distributor& col_ownership,
103  std::vector<distributor>& col_start_by_component) const;
104 
105  void build_csr_pass2 (
106  asr<T,M>& a,
107  const std::vector<std::pair<size_type,size_type> >& col_sizes,
108  const std::vector<distributor>& col_start_by_component,
109  size_type row_start_comp_i = 0,
110  size_type row_start_comp_dis_i = 0) const;
111 
112  csr<T,M> build_csr () const;
113 
114 protected:
115  std::list<value_type> _l;
116 };
117 
118 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
119 #define RHEOLEF_csr_cstor(M) \
120 template <class T> \
121 inline \
122 csr<T,M>::csr (const std::initializer_list<csr_concat_value<T,M> >& init_list) \
123 { \
124  csr_concat_line<T,M> cc (init_list); \
125  csr<T,M>::operator= (cc.build_csr()); \
126 }
128 #ifdef _RHEOLEF_HAVE_MPI
130 #endif // _RHEOLEF_HAVE_MPI
131 #undef RHEOLEF_csr_cstor
132 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
133 
134 template <class T, class M>
135 struct csr_concat {
136  typedef typename csr<T,M>::size_type size_type;
139  csr_concat () : _l() {}
140 
141 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
142  csr_concat (const std::initializer_list<line_type>& il) : _l() {
143 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
144  typedef typename std::initializer_list<line_type>::const_iterator const_iterator;
145 #else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
146  typedef const line_type* const_iterator;
147 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
148  for(const_iterator iter = il.begin(); iter != il.end(); ++iter) {
149  _l.push_back(*iter);
150  }
151  }
152 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
153 
154  void push_back (const line_type& line) { _l.push_back(line); }
155 
156 #ifdef TO_CLEAN
157  friend std::ostream& operator<< (std::ostream& o, const csr_concat<T,M>& x) {
158  std::cout << "{";
159  for(typename std::list<line_type>::const_iterator iter = x._l.begin(); iter != x._l.end(); ++iter) {
160  std::cout << *iter << " ";
161  }
162  return std::cout << "}";
163  }
164 #endif // TO_CLEAN
165 
166  csr<T,M> build_csr () const;
167 
168 protected:
169  std::list<line_type> _l;
170 };
171 
172 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
173 #define RHEOLEF_csr_cstor(M) \
174 template <class T> \
175 inline \
176 csr<T,M>::csr (const std::initializer_list<csr_concat_line<T,M> >& init_list) \
177 { \
178  csr_concat<T,M> cc (init_list); \
179  csr<T,M>::operator= (cc.build_csr()); \
180 }
181 RHEOLEF_csr_cstor(sequential)
182 #ifdef _RHEOLEF_HAVE_MPI
184 #endif // _RHEOLEF_HAVE_MPI
185 #undef RHEOLEF_csr_cstor
186 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
187 
188 } // namespace rheolef
189 #endif // _RHEOLEF_CSR_CONCAT_H
190