rheolef  7.0
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
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
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 class csr_concat_value {
34 public:
35  typedef enum { scalar, vector, vector_transpose, vector_vec, vector_vec_transpose, matrix} variant_type;
36  csr_concat_value (int x) : s(x), v(), vv(), m(), variant(scalar) {}
37  csr_concat_value (const T& x) : s(x), v(), vv(), m(), variant(scalar) {}
38  csr_concat_value (const vec<T,M>& x) : s(), v(x), vv(), m(), variant(vector) {}
39  csr_concat_value (const vec_trans<T,M>& vt) : s(), v(vt.v), vv(), m(), variant(vector_transpose) {}
40  csr_concat_value (const std::vector<vec<T,M> >& x) : s(), v(), vv(x), m(), variant(vector_vec) {}
41  csr_concat_value (const vector_vec_trans<T,M>& vt) : s(), v(), vv(vt.v), m(), variant(vector_vec_transpose) {}
42  csr_concat_value (const csr<T,M>& x) : s(), v(), vv(), m(x), variant(matrix) {}
43 
44 public:
45  T s;
47  std::vector<vec<T,M> > vv;
49  variant_type variant;
50 };
51 
52 template <class T, class M>
53 class csr_concat_line {
54 public:
55  typedef typename csr<T,M>::size_type size_type;
57  csr_concat_line () : _l() {}
58 
59 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
60  csr_concat_line (const std::initializer_list<value_type>& il) : _l() {
61 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
62  typedef typename std::initializer_list<value_type>::const_iterator const_iterator;
63 #else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
64  typedef const value_type* const_iterator;
65 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
66  for (const_iterator iter = il.begin(); iter != il.end(); ++iter) {
67  _l.push_back(*iter);
68  }
69  }
70 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
71 
72  void push_back (const value_type& x) { _l.push_back (x); }
73 
74  friend std::ostream& operator<< (std::ostream& o, const csr_concat_line<T,M>& x) {
75  std::cout << "{";
76  for(typename std::list<value_type>::const_iterator iter = x._l.begin(); iter != x._l.end(); ++iter) {
77  std::cout << *iter << " ";
78  }
79  return std::cout << "}";
80  }
81  static void set_sizes (
82  std::pair<size_type,size_type>& row_sizes,
83  std::pair<size_type,size_type>& col_sizes,
84  const std::pair<size_type,size_type>& new_row_sizes,
85  const std::pair<size_type,size_type>& new_col_sizes);
86 
87  static void finalize_sizes (
88  std::pair<size_type,size_type>& sizes,
89  const communicator& comm);
90 
91  static void finalize_sizes (
92  std::vector<std::pair<size_type,size_type> >& sizes,
93  const communicator& comm);
94 
95  void build_csr_pass0 (
96  std::pair<size_type,size_type>& row_sizes,
97  std::vector<std::pair<size_type,size_type> >& col_sizes,
98  communicator& comm) const;
99 
100  void build_csr_pass1 (
101  const std::pair<size_type,size_type>& row_sizes,
102  const std::vector<std::pair<size_type,size_type> >& col_sizes,
103  const communicator& comm,
104  distributor& row_ownership,
105  distributor& col_ownership,
106  std::vector<distributor>& col_start_by_component) const;
107 
108  void build_csr_pass2 (
109  asr<T,M>& a,
110  const std::vector<std::pair<size_type,size_type> >& col_sizes,
111  const std::vector<distributor>& col_start_by_component,
112  size_type row_start_comp_i = 0,
113  size_type row_start_comp_dis_i = 0) const;
114 
115  csr<T,M> build_csr () const;
116 
117 protected:
118  std::list<value_type> _l;
119 };
120 
121 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
122 #define RHEOLEF_csr_cstor(M) \
123 template <class T> \
124 inline \
125 csr<T,M>::csr (const std::initializer_list<csr_concat_value<T,M> >& init_list) \
126 { \
127  csr_concat_line<T,M> cc (init_list); \
128  csr<T,M>::operator= (cc.build_csr()); \
129 }
130 RHEOLEF_csr_cstor(sequential)
131 #ifdef _RHEOLEF_HAVE_MPI
132 RHEOLEF_csr_cstor(distributed)
133 #endif // _RHEOLEF_HAVE_MPI
134 #undef RHEOLEF_csr_cstor
135 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
136 
137 template <class T, class M>
138 class csr_concat {
139 public:
140  typedef typename csr<T,M>::size_type size_type;
143  csr_concat () : _l() {}
144 
145 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
146  csr_concat (const std::initializer_list<line_type>& il) : _l() {
147 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
148  typedef typename std::initializer_list<line_type>::const_iterator const_iterator;
149 #else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
150  typedef const line_type* const_iterator;
151 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
152  for(const_iterator iter = il.begin(); iter != il.end(); ++iter) {
153  _l.push_back(*iter);
154  }
155  }
156 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
157 
158  void push_back (const line_type& line) { _l.push_back(line); }
159 
160 #ifdef TO_CLEAN
161  friend std::ostream& operator<< (std::ostream& o, const csr_concat<T,M>& x) {
162  std::cout << "{";
163  for(typename std::list<line_type>::const_iterator iter = x._l.begin(); iter != x._l.end(); ++iter) {
164  std::cout << *iter << " ";
165  }
166  return std::cout << "}";
167  }
168 #endif // TO_CLEAN
169 
170  csr<T,M> build_csr () const;
171 
172 protected:
173  std::list<line_type> _l;
174 };
175 
176 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
177 #define RHEOLEF_csr_cstor(M) \
178 template <class T> \
179 inline \
180 csr<T,M>::csr (const std::initializer_list<csr_concat_line<T,M> >& init_list) \
181 { \
182  csr_concat<T,M> cc (init_list); \
183  csr<T,M>::operator= (cc.build_csr()); \
184 }
185 RHEOLEF_csr_cstor(sequential)
186 #ifdef _RHEOLEF_HAVE_MPI
187 RHEOLEF_csr_cstor(distributed)
188 #endif // _RHEOLEF_HAVE_MPI
189 #undef RHEOLEF_csr_cstor
190 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
191 
192 } // namespace rheolef
193 #endif // _RHEOLEF_CSR_CONCAT_H
distributor - data distribution table
Definition: distributor.h:19
void push_back(const line_type &line)
Definition: csr_concat.h:158
csr_concat_value(const vec_trans< T, M > &vt)
Definition: csr_concat.h:39
csr_concat_value(const vec< T, M > &x)
Definition: csr_concat.h:38
std::list< line_type > _l
Definition: csr_concat.h:173
irheostream, orheostream - large data streams
Definition: compiler.h:7
csr_concat_value< T, M > value_type
Definition: csr_concat.h:56
csr_concat_value(const csr< T, M > &x)
Definition: csr_concat.h:42
csr_concat_line< T, M > line_type
Definition: csr_concat.h:141
vec_trans(const vec< T, M > &w)
Definition: csr_concat.h:12
csr_concat_value(const std::vector< vec< T, M > > &x)
Definition: csr_concat.h:40
csr< T, M >::size_type size_type
Definition: csr_concat.h:55
void push_back(const value_type &x)
Definition: csr_concat.h:72
vector_vec_trans(const std::vector< vec< T, M > > &w)
Definition: csr_concat.h:23
csr_concat_value(const T &x)
Definition: csr_concat.h:37
vec< T, M > v
Definition: csr_concat.h:11
std::list< value_type > _l
Definition: csr_concat.h:118
vec - vector in distributed environment
Definition: vec.h:48
csr_concat_value(const vector_vec_trans< T, M > &vt)
Definition: csr_concat.h:41
csr< T, sequential > trans(const csr< T, sequential > &a)
Definition: csr.h:381
csr_concat_value< T, M > value_type
Definition: csr_concat.h:142
csr - compressed sparse row matrix
Definition: asr.h:8
asr - associative sparse matrix
Definition: asr.h:26
csr< T, M >::size_type size_type
Definition: csr_concat.h:140
std::vector< vec< T, M > > vv
Definition: csr_concat.h:47
std::vector< vec< T, M > > v
Definition: csr_concat.h:22