1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * blitz/tinymat.h Declaration of TinyMatrix<T, N, M> |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
8 | * |
---|
9 | * This file is a part of Blitz. |
---|
10 | * |
---|
11 | * Blitz is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License |
---|
13 | * as published by the Free Software Foundation, either version 3 |
---|
14 | * of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * Blitz is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public |
---|
22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
26 | * |
---|
27 | * For more information, please see the Blitz++ Home Page: |
---|
28 | * https://sourceforge.net/projects/blitz/ |
---|
29 | * |
---|
30 | ***************************************************************************/ |
---|
31 | |
---|
32 | #ifndef BZ_TINYMAT_H |
---|
33 | #define BZ_TINYMAT_H |
---|
34 | |
---|
35 | #include <blitz/blitz.h> |
---|
36 | #include <blitz/tinyvec2.h> |
---|
37 | #include <blitz/tinyvec2io.cc> // fix this - for some reason it doesn't get included through tinyvec2.h |
---|
38 | #include <blitz/listinit.h> |
---|
39 | #include <blitz/etbase.h> |
---|
40 | #include <blitz/array/slice.h> |
---|
41 | #include <blitz/indexmap-forward.h> |
---|
42 | //#include <blitz/meta/vecassign.h> |
---|
43 | //#include <blitz/update.h> |
---|
44 | |
---|
45 | #ifdef BZ_HAVE_BOOST_SERIALIZATION |
---|
46 | #include <boost/serialization/serialization.hpp> |
---|
47 | #endif |
---|
48 | |
---|
49 | |
---|
50 | BZ_NAMESPACE(blitz) |
---|
51 | |
---|
52 | /***************************************************************************** |
---|
53 | * Forward declarations |
---|
54 | */ |
---|
55 | |
---|
56 | template<typename P_numtype, int N_rows, int N_columns> |
---|
57 | class FastTM2Iterator; |
---|
58 | template<typename P_numtype, int N_rows, int N_columns> |
---|
59 | class FastTM2CopyIterator; |
---|
60 | |
---|
61 | template<typename P_expr> |
---|
62 | class _bz_ArrayExpr; |
---|
63 | |
---|
64 | template<int N0> |
---|
65 | class IndexPlaceholder; |
---|
66 | |
---|
67 | /***************************************************************************** |
---|
68 | * Declaration of class TinyMatrix |
---|
69 | */ |
---|
70 | |
---|
71 | template<typename P_numtype, int N_rows, int N_columns> |
---|
72 | class TinyMatrix : public ETBase<TinyMatrix<P_numtype, N_rows, N_columns> > |
---|
73 | { |
---|
74 | public: |
---|
75 | typedef P_numtype T_numtype; |
---|
76 | // typedef _bz_tinyMatrixRef<T_numtype, N_rows, N_columns, N_columns, 1> |
---|
77 | // T_reference; |
---|
78 | typedef TinyVector<int, 2> T_index; |
---|
79 | typedef TinyMatrix<T_numtype, N_rows, N_columns> T_matrix; |
---|
80 | typedef FastTM2Iterator<T_numtype,N_rows, N_columns> T_iterator; |
---|
81 | typedef T_numtype* iterator; |
---|
82 | typedef const T_numtype* const_iterator; |
---|
83 | typedef FastTM2CopyIterator<P_numtype, N_rows, N_columns> T_range_result; |
---|
84 | |
---|
85 | static const int |
---|
86 | //numArrayOperands = 1, |
---|
87 | //numIndexPlaceholders = 0, |
---|
88 | rank_ = 2; |
---|
89 | |
---|
90 | TinyMatrix() { } |
---|
91 | |
---|
92 | inline TinyMatrix(const T_matrix&); |
---|
93 | |
---|
94 | template <typename T_numtype2> |
---|
95 | inline TinyMatrix(const TinyMatrix<T_numtype2, N_rows, N_columns>& x); |
---|
96 | |
---|
97 | inline TinyMatrix(T_numtype initValue); |
---|
98 | |
---|
99 | static TinyVector<int, 2> base() |
---|
100 | { return 0; } |
---|
101 | |
---|
102 | static int base(int rank) |
---|
103 | { return 0; } |
---|
104 | |
---|
105 | iterator begin() |
---|
106 | { return iterator(*this); } |
---|
107 | |
---|
108 | const_iterator begin() const |
---|
109 | { return const_iterator(*this); } |
---|
110 | |
---|
111 | T_iterator beginFast() const |
---|
112 | { return T_iterator(*this); } |
---|
113 | |
---|
114 | static int cols() |
---|
115 | { return N_columns; } |
---|
116 | |
---|
117 | static int columns() |
---|
118 | { return N_columns; } |
---|
119 | |
---|
120 | const T_numtype* restrict data() const |
---|
121 | { return data_; } |
---|
122 | |
---|
123 | T_numtype* restrict data() |
---|
124 | { return data_; } |
---|
125 | |
---|
126 | const T_numtype* restrict dataFirst() const |
---|
127 | { return data_; } |
---|
128 | |
---|
129 | T_numtype* restrict dataFirst() |
---|
130 | { return data_; } |
---|
131 | |
---|
132 | static int dimensions() |
---|
133 | { return 2; } |
---|
134 | |
---|
135 | static RectDomain<2> domain(); |
---|
136 | |
---|
137 | iterator end() |
---|
138 | { return data_+size(); } |
---|
139 | |
---|
140 | const_iterator end() const |
---|
141 | { return data_+size(); } |
---|
142 | |
---|
143 | static int extent(int rank) |
---|
144 | { BZPRECONDITION(rank<2); return rank==0 ? N_rows : N_columns; } |
---|
145 | |
---|
146 | static const TinyVector<int,2> extent() |
---|
147 | { return TinyVector<int,2>(N_rows, N_columns); } |
---|
148 | |
---|
149 | static bool isMajorRank(int rank) |
---|
150 | { BZPRECONDITION(rank<2); return rank==0; } |
---|
151 | static bool isMinorRank(int rank) |
---|
152 | { BZPRECONDITION(rank<2); return rank==1; } |
---|
153 | static bool isRankStoredAscending(int rank) |
---|
154 | { BZPRECONDITION(rank<2); return true; } |
---|
155 | |
---|
156 | static int lbound(int rank) |
---|
157 | { BZPRECONDITION(rank<2); return 0; } |
---|
158 | static TinyVector<int,2> lbound() |
---|
159 | { return 0; } |
---|
160 | |
---|
161 | static int length(int rank) |
---|
162 | { BZPRECONDITION(rank<2); return rank==0 ? N_rows : N_columns; } |
---|
163 | static TinyVector<int, 2> length() |
---|
164 | { return TinyVector<int,2>(N_rows, N_columns); } |
---|
165 | |
---|
166 | static sizeType numElements() |
---|
167 | { return N_rows*N_columns; } |
---|
168 | |
---|
169 | static int ordering(int rank) |
---|
170 | { BZPRECONDITION(rank<2); return 1-rank; } |
---|
171 | |
---|
172 | static TinyVector<int, 2> ordering() |
---|
173 | { return TinyVector<int,2>(0,1); } |
---|
174 | |
---|
175 | static int rank() |
---|
176 | { return rank_; } |
---|
177 | |
---|
178 | static int rows() |
---|
179 | { return N_rows; } |
---|
180 | |
---|
181 | static TinyVector<int, 2> shape() |
---|
182 | { return length(); } |
---|
183 | |
---|
184 | static sizeType size() |
---|
185 | { return numElements(); } |
---|
186 | |
---|
187 | static TinyVector<diffType, 2> stride() |
---|
188 | { return TinyVector<int,2>(N_columns,1); } |
---|
189 | |
---|
190 | static diffType stride(int rank) |
---|
191 | { BZPRECONDITION(rank<2); return rank==0 ? N_columns : 1; } |
---|
192 | |
---|
193 | static int ubound(int rank) |
---|
194 | { return length(rank) - 1; } |
---|
195 | |
---|
196 | static TinyVector<int, 2> ubound() |
---|
197 | { return length()-1; } |
---|
198 | |
---|
199 | static int zeroOffset() |
---|
200 | { return 0; } |
---|
201 | |
---|
202 | ////////////////////////////////////////////// |
---|
203 | // Debugging routines |
---|
204 | ////////////////////////////////////////////// |
---|
205 | |
---|
206 | bool isInRange(const T_index& index) const { |
---|
207 | for (int i=0; i < rank_; ++i) |
---|
208 | if (index[i] < base(i) || (index[i] - base(i)) >= length(i)) |
---|
209 | return false; |
---|
210 | |
---|
211 | return true; |
---|
212 | } |
---|
213 | |
---|
214 | bool isInRange(int i0, int i1) const { |
---|
215 | return i0 >= base(0) && (i0 - base(0)) < length(0) |
---|
216 | && i1 >= base(1) && (i1 - base(1)) < length(1); |
---|
217 | } |
---|
218 | |
---|
219 | bool assertInRange(const T_index& BZ_DEBUG_PARAM(index)) const { |
---|
220 | BZPRECHECK(isInRange(index), "TinyMatrix index out of range: " << index |
---|
221 | << endl << "Lower bounds: " << base() << endl |
---|
222 | << "Length: " << length() << endl); |
---|
223 | return true; |
---|
224 | } |
---|
225 | |
---|
226 | bool assertInRange(int BZ_DEBUG_PARAM(i0), int BZ_DEBUG_PARAM(i1)) const { |
---|
227 | BZPRECHECK(isInRange(i0,i1), "TinyMatrix index out of range: (" |
---|
228 | << i0 << ", " << i1 << ")" |
---|
229 | << endl << "Lower bounds: " << base() << endl |
---|
230 | << "Length: " << length() << endl); |
---|
231 | return true; |
---|
232 | } |
---|
233 | |
---|
234 | ////////////////////////////////////////////// |
---|
235 | // Subscripting operators |
---|
236 | ////////////////////////////////////////////// |
---|
237 | |
---|
238 | |
---|
239 | T_matrix& noConst() const |
---|
240 | { return const_cast<T_matrix&>(*this); } |
---|
241 | |
---|
242 | const T_numtype& restrict operator()(unsigned i, unsigned j) const |
---|
243 | { |
---|
244 | assertInRange(i,j); |
---|
245 | return data_[i*N_columns + j]; |
---|
246 | } |
---|
247 | |
---|
248 | T_numtype& restrict operator()(unsigned i, unsigned j) |
---|
249 | { |
---|
250 | assertInRange(i,j); |
---|
251 | return data_[i*N_columns + j]; |
---|
252 | } |
---|
253 | |
---|
254 | const T_numtype& restrict operator()(T_index i) const |
---|
255 | { |
---|
256 | assertInRange(i); |
---|
257 | return data_[i[0]*N_columns + i[1]]; |
---|
258 | } |
---|
259 | |
---|
260 | T_numtype& restrict operator()(T_index i) |
---|
261 | { |
---|
262 | assertInRange(i); |
---|
263 | return data_[i[0]*N_columns + i[1]]; |
---|
264 | } |
---|
265 | |
---|
266 | template<int N0, int N1> |
---|
267 | inline |
---|
268 | _bz_ArrayExpr<ArrayIndexMapping<typename asExpr<T_matrix>::T_expr, N0, N1> > |
---|
269 | operator()(IndexPlaceholder<N0>, IndexPlaceholder<N1>) const; |
---|
270 | |
---|
271 | /** Fastread must return reference so the iterator can turn it into an |
---|
272 | iterator for the contained in case we have a multicomponent. */ |
---|
273 | const T_numtype& fastRead(diffType i) const |
---|
274 | { return data_[i]; } |
---|
275 | |
---|
276 | /** Since data_ is simd aligned by construction, we just have |
---|
277 | to check the offest. */ |
---|
278 | bool isVectorAligned(diffType offset) const |
---|
279 | { return (offset%simdTypes<T_numtype>::vecWidth)==0; } |
---|
280 | |
---|
281 | // T_reference getRef() |
---|
282 | // { return T_reference((T_numtype*)data_); } |
---|
283 | |
---|
284 | // const T_reference getRef() const |
---|
285 | // { return T_reference((T_numtype*)data_); } |
---|
286 | |
---|
287 | ////////////////////////////////////////////// |
---|
288 | // Assignment Operators |
---|
289 | ////////////////////////////////////////////// |
---|
290 | |
---|
291 | // Scalar operand |
---|
292 | ListInitializationSwitch<T_matrix,T_numtype*> |
---|
293 | operator=(T_numtype x) |
---|
294 | { |
---|
295 | return ListInitializationSwitch<T_matrix,T_numtype*>(*this, x); |
---|
296 | } |
---|
297 | |
---|
298 | T_matrix& initialize(T_numtype x); |
---|
299 | |
---|
300 | T_numtype* restrict getInitializationIterator() |
---|
301 | { return dataFirst(); } |
---|
302 | |
---|
303 | template<typename T_expr> |
---|
304 | T_matrix& operator=(const ETBase<T_expr>&); |
---|
305 | |
---|
306 | template<typename T> T_matrix& operator+=(const T&); |
---|
307 | template<typename T> T_matrix& operator-=(const T&); |
---|
308 | template<typename T> T_matrix& operator*=(const T&); |
---|
309 | template<typename T> T_matrix& operator/=(const T&); |
---|
310 | template<typename T> T_matrix& operator%=(const T&); |
---|
311 | template<typename T> T_matrix& operator^=(const T&); |
---|
312 | template<typename T> T_matrix& operator&=(const T&); |
---|
313 | template<typename T> T_matrix& operator|=(const T&); |
---|
314 | template<typename T> T_matrix& operator>>=(const T&); |
---|
315 | template<typename T> T_matrix& operator<<=(const T&); |
---|
316 | |
---|
317 | static bool canCollapse(int outerRank, int innerRank) { |
---|
318 | #ifdef BZ_DEBUG_TRAVERSE |
---|
319 | BZ_DEBUG_MESSAGE("stride(" << innerRank << ")=" << stride(innerRank) |
---|
320 | << ", extent()=" << extent(innerRank) << ", stride(outerRank)=" |
---|
321 | << stride(outerRank)); |
---|
322 | #endif |
---|
323 | return (stride(innerRank) * extent(innerRank) == stride(outerRank)); |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | private: |
---|
328 | template<typename T_expr, typename T_update> |
---|
329 | void _tm_evaluate(const T_expr& expr, T_update); |
---|
330 | |
---|
331 | #ifdef BZ_HAVE_BOOST_SERIALIZATION |
---|
332 | friend class boost::serialization::access; |
---|
333 | |
---|
334 | template<class T_arch> |
---|
335 | void serialize(T_arch& ar, const unsigned int version) { |
---|
336 | ar & data_; |
---|
337 | }; |
---|
338 | #endif |
---|
339 | |
---|
340 | BZ_ALIGN_VARIABLE(T_numtype, data_[N_rows * N_columns], BZ_SIMD_WIDTH) |
---|
341 | }; |
---|
342 | |
---|
343 | BZ_NAMESPACE_END |
---|
344 | |
---|
345 | #ifdef BZ_HAVE_BOOST_SERIALIZATION |
---|
346 | namespace boost { |
---|
347 | namespace mpi { |
---|
348 | template<typename T> struct is_mpi_datatype; |
---|
349 | template <typename T, int N, int M> |
---|
350 | struct is_mpi_datatype<blitz::TinyMatrix<T, N, M> > |
---|
351 | : public is_mpi_datatype<T> { }; |
---|
352 | } }; |
---|
353 | #endif |
---|
354 | |
---|
355 | #include <blitz/tm2fastiter.h> // Iterators |
---|
356 | |
---|
357 | #endif // BZ_TINYMAT_H |
---|
358 | |
---|