blitz Version 1.0.2
Loading...
Searching...
No Matches
tinymat2.h
Go to the documentation of this file.
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>
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
50namespace blitz {
51
52/*****************************************************************************
53 * Forward declarations
54 */
55
56template<typename P_numtype, int N_rows, int N_columns>
57class FastTM2Iterator;
58template<typename P_numtype, int N_rows, int N_columns>
59class FastTM2CopyIterator;
60
61template<typename P_expr>
63
64template<int N0>
66
67/*****************************************************************************
68 * Declaration of class TinyMatrix
69 */
70
71template<typename P_numtype, int N_rows, int N_columns>
72class TinyMatrix : public ETBase<TinyMatrix<P_numtype, N_rows, N_columns> >
73{
74public:
75 typedef P_numtype T_numtype;
76 // typedef _bz_tinyMatrixRef<T_numtype, N_rows, N_columns, N_columns, 1>
77 // T_reference;
82 typedef const T_numtype* const_iterator;
84
85 static const int
86 //numArrayOperands = 1,
87 //numIndexPlaceholders = 0,
88 rank_ = 2;
89
91
92 inline TinyMatrix(const T_matrix&);
93
94 template <typename T_numtype2>
96
97 inline TinyMatrix(T_numtype initValue);
98
100 { return 0; }
101
102 static int base(int rank)
103 { return 0; }
104
106 { return iterator(*this); }
107
109 { return const_iterator(*this); }
110
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
124 { return data_; }
125
127 { return data_; }
128
130 { return data_; }
131
132 static int dimensions()
133 { return 2; }
134
136
138 { return data_+size(); }
139
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; }
159 { return 0; }
160
161 static int length(int rank)
162 { BZPRECONDITION(rank<2); return rank==0 ? N_rows : N_columns; }
164 { return TinyVector<int,2>(N_rows, N_columns); }
165
167 { return N_rows*N_columns; }
168
169 static int ordering(int rank)
170 { BZPRECONDITION(rank<2); return 1-rank; }
171
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
182 { return length(); }
183
184 static sizeType size()
185 { return numElements(); }
186
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
197 { return length()-1; }
198
199 static int zeroOffset()
200 { return 0; }
201
203 // Debugging routines
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
235 // Subscripting operators
237
238
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
255 {
256 assertInRange(i);
257 return data_[i[0]*N_columns + i[1]];
258 }
259
261 {
262 assertInRange(i);
263 return data_[i[0]*N_columns + i[1]];
264 }
265
266 template<int N0, int N1>
267 inline
270
274 { return data_[i]; }
275
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
288 // Assignment Operators
290
291 // Scalar operand
297
299
302
303 template<typename 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
327private:
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}
344
345#ifdef BZ_HAVE_BOOST_SERIALIZATION
346namespace 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
Definition etbase.h:38
Definition tm2fastiter.h:427
Definition tm2fastiter.h:370
Definition tinymat2.h:65
Definition listinit.h:71
Definition range.h:58
Definition tinymat2.h:73
static int base(int rank)
Definition tinymat2.h:102
T_matrix & operator>>=(const T &)
T_numtype *restrict getInitializationIterator()
Definition tinymat2.h:300
static TinyVector< int, 2 > shape()
Definition tinymat2.h:181
T_numtype * iterator
Definition tinymat2.h:81
static int length(int rank)
Definition tinymat2.h:161
T_matrix & operator^=(const T &)
iterator begin()
Definition tinymat2.h:105
const_iterator begin() const
Definition tinymat2.h:108
TinyMatrix< T_numtype, N_rows, N_columns > T_matrix
Definition tinymat2.h:79
static int columns()
Definition tinymat2.h:117
iterator end()
Definition tinymat2.h:137
T_numtype &restrict operator()(unsigned i, unsigned j)
Definition tinymat2.h:248
static TinyVector< int, 2 > lbound()
Definition tinymat2.h:158
T_matrix & noConst() const
Definition tinymat2.h:239
T_numtype *restrict data()
Definition tinymat2.h:123
static TinyVector< int, 2 > length()
Definition tinymat2.h:163
static bool canCollapse(int outerRank, int innerRank)
Definition tinymat2.h:317
T_numtype &restrict operator()(T_index i)
Definition tinymat2.h:260
T_iterator beginFast() const
Definition tinymat2.h:111
T_numtype *restrict dataFirst()
Definition tinymat2.h:129
static diffType stride(int rank)
Definition tinymat2.h:190
static int zeroOffset()
Definition tinymat2.h:199
static RectDomain< 2 > domain()
static int rank()
Definition tinymat2.h:175
static bool isRankStoredAscending(int rank)
Definition tinymat2.h:153
static const int rank_
Definition tinymat2.h:88
static sizeType numElements()
Definition tinymat2.h:166
static int ubound(int rank)
Definition tinymat2.h:193
T_matrix & operator/=(const T &)
bool isVectorAligned(diffType offset) const
Since data_ is simd aligned by construction, we just have to check the offest.
Definition tinymat2.h:278
T_matrix & operator-=(const T &)
static TinyVector< int, 2 > base()
Definition tinymat2.h:99
const T_numtype * const_iterator
Definition tinymat2.h:82
P_numtype T_numtype
Definition tinymat2.h:75
T_matrix & operator&=(const T &)
static int lbound(int rank)
Definition tinymat2.h:156
ListInitializationSwitch< T_matrix, T_numtype * > operator=(T_numtype x)
Definition tinymat2.h:293
const T_numtype *restrict dataFirst() const
Definition tinymat2.h:126
static sizeType size()
Definition tinymat2.h:184
const T_numtype &restrict operator()(T_index i) const
Definition tinymat2.h:254
TinyMatrix()
Definition tinymat2.h:90
TinyVector< int, 2 > T_index
Definition tinymat2.h:78
T_matrix & operator+=(const T &)
static TinyVector< int, 2 > ubound()
Definition tinymat2.h:196
const_iterator end() const
Definition tinymat2.h:140
bool assertInRange(int BZ_DEBUG_PARAM(i0), int BZ_DEBUG_PARAM(i1)) const
Definition tinymat2.h:226
T_matrix & operator|=(const T &)
const T_numtype *restrict data() const
Definition tinymat2.h:120
static const TinyVector< int, 2 > extent()
Definition tinymat2.h:146
static TinyVector< diffType, 2 > stride()
Definition tinymat2.h:187
static TinyVector< int, 2 > ordering()
Definition tinymat2.h:172
static int cols()
Definition tinymat2.h:114
FastTM2CopyIterator< P_numtype, N_rows, N_columns > T_range_result
Definition tinymat2.h:83
static bool isMinorRank(int rank)
Definition tinymat2.h:151
_bz_ArrayExpr< ArrayIndexMapping< typename asExpr< T_matrix >::T_expr, N0, N1 > > operator()(IndexPlaceholder< N0 >, IndexPlaceholder< N1 >) const
FastTM2Iterator< T_numtype, N_rows, N_columns > T_iterator
Definition tinymat2.h:80
void _tm_evaluate(const T_expr &expr, T_update)
T_matrix & initialize(T_numtype x)
static int extent(int rank)
Definition tinymat2.h:143
static int ordering(int rank)
Definition tinymat2.h:169
static int rows()
Definition tinymat2.h:178
T_matrix & operator<<=(const T &)
static bool isMajorRank(int rank)
Definition tinymat2.h:149
const T_numtype & fastRead(diffType i) const
Fastread must return reference so the iterator can turn it into an iterator for the contained in case...
Definition tinymat2.h:273
bool isInRange(int i0, int i1) const
Definition tinymat2.h:214
T_matrix & operator%=(const T &)
TinyMatrix(const TinyMatrix< T_numtype2, N_rows, N_columns > &x)
bool assertInRange(const T_index &BZ_DEBUG_PARAM(index)) const
Definition tinymat2.h:219
bool isInRange(const T_index &index) const
Definition tinymat2.h:206
T_matrix & operator=(const ETBase< T_expr > &)
T_matrix & operator*=(const T &)
TinyMatrix(const T_matrix &)
TinyMatrix(T_numtype initValue)
const T_numtype &restrict operator()(unsigned i, unsigned j) const
Definition tinymat2.h:242
static int dimensions()
Definition tinymat2.h:132
The TinyVector class is a one-dimensional, fixed length vector that implements the blitz expression t...
Definition tinyvec2.h:73
Definition tinymat2.h:62
Helper class that defines the width of the simd instructions for a given type.
Definition simdtypes.h:31
#define restrict
Definition compiler.h:95
Definition array-impl.h:66
ptrdiff_t diffType
Definition blitz.h:111
size_t sizeType
Definition blitz.h:110