blitz  Version 1.0.2
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>
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 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;
81  typedef T_numtype* iterator;
82  typedef const T_numtype* const_iterator;
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>
96 
97  inline TinyMatrix(T_numtype initValue);
98 
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; }
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 
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
270 
273  const T_numtype& fastRead(diffType i) const
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
293  operator=(T_numtype x)
294  {
296  }
297 
298  T_matrix& initialize(T_numtype x);
299 
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 }
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 
iterator end()
Definition: tinymat2.h:137
const_iterator end() const
Definition: tinymat2.h:140
static int dimensions()
Definition: tinymat2.h:132
Definition: range.h:58
FastTM2CopyIterator< P_numtype, N_rows, N_columns > T_range_result
Definition: tinymat2.h:83
T_matrix & operator&=(const T &)
T_numtype &restrict operator()(unsigned i, unsigned j)
Definition: tinymat2.h:248
T_matrix & noConst() const
Definition: tinymat2.h:239
static int base(int rank)
Definition: tinymat2.h:102
TinyVector< int, 2 > T_index
Definition: tinymat2.h:78
_bz_global blitz::IndexPlaceholder< 0 > i
Definition: indexexpr.h:256
Helper class that defines the width of the simd instructions for a given type.
Definition: simdtypes.h:31
static TinyVector< int, 2 > length()
Definition: tinymat2.h:163
T_matrix & operator+=(const T &)
const T_numtype *restrict dataFirst() const
Definition: tinymat2.h:126
_bz_global blitz::IndexPlaceholder< 1 > j
Definition: indexexpr.h:257
T_matrix & operator-=(const T &)
static TinyVector< int, 2 > base()
Definition: tinymat2.h:99
Definition: listinit.h:71
static sizeType size()
Definition: tinymat2.h:184
const T_numtype &restrict operator()(T_index i) const
Definition: tinymat2.h:254
Definition: et-forward.h:25
static int ordering(int rank)
Definition: tinymat2.h:169
static TinyVector< diffType, 2 > stride()
Definition: tinymat2.h:187
T_matrix & operator*=(const T &)
T_numtype * iterator
Definition: tinymat2.h:81
static int lbound(int rank)
Definition: tinymat2.h:156
static int ubound(int rank)
Definition: tinymat2.h:193
bool isVectorAligned(diffType offset) const
Since data_ is simd aligned by construction, we just have to check the offest.
Definition: tinymat2.h:278
Definition: et-forward.h:10
bool isInRange(int i0, int i1) const
Definition: tinymat2.h:214
static TinyVector< int, 2 > shape()
Definition: tinymat2.h:181
T_numtype *restrict dataFirst()
Definition: tinymat2.h:129
static int rank()
Definition: tinymat2.h:175
P_numtype T_numtype
Definition: tinymat2.h:75
T_matrix & initialize(T_numtype x)
static const int rank_
Definition: tinymat2.h:88
bool assertInRange(int BZ_DEBUG_PARAM(i0), int BZ_DEBUG_PARAM(i1)) const
Definition: tinymat2.h:226
static int length(int rank)
Definition: tinymat2.h:161
T_iterator beginFast() const
Definition: tinymat2.h:111
static RectDomain< 2 > domain()
static int extent(int rank)
Definition: tinymat2.h:143
static diffType stride(int rank)
Definition: tinymat2.h:190
static int columns()
Definition: tinymat2.h:117
Definition: et-forward.h:23
Definition: array-impl.h:82
T_numtype *restrict data()
Definition: tinymat2.h:123
TinyMatrix< T_numtype, N_rows, N_columns > T_matrix
Definition: tinymat2.h:79
static TinyVector< int, 2 > lbound()
Definition: tinymat2.h:158
Definition: array-impl.h:66
Definition: et-forward.h:48
TinyMatrix()
Definition: tinymat2.h:90
ptrdiff_t diffType
Definition: blitz.h:111
T_matrix & operator/=(const T &)
T_matrix & operator<<=(const T &)
static int rows()
Definition: tinymat2.h:178
static TinyVector< int, 2 > ordering()
Definition: tinymat2.h:172
ListInitializationSwitch< T_matrix, T_numtype * > operator=(T_numtype x)
Definition: tinymat2.h:293
size_t sizeType
Definition: blitz.h:110
T_matrix & operator^=(const T &)
T_numtype &restrict operator()(T_index i)
Definition: tinymat2.h:260
static bool isRankStoredAscending(int rank)
Definition: tinymat2.h:153
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
static sizeType numElements()
Definition: tinymat2.h:166
T_matrix & operator%=(const T &)
FastTM2Iterator< T_numtype, N_rows, N_columns > T_iterator
Definition: tinymat2.h:80
iterator begin()
Definition: tinymat2.h:105
T_numtype *restrict getInitializationIterator()
Definition: tinymat2.h:300
static bool isMinorRank(int rank)
Definition: tinymat2.h:151
static const TinyVector< int, 2 > extent()
Definition: tinymat2.h:146
const_iterator begin() const
Definition: tinymat2.h:108
const T_numtype * const_iterator
Definition: tinymat2.h:82
void _tm_evaluate(const T_expr &expr, T_update)
bool isInRange(const T_index &index) const
Definition: tinymat2.h:206
#define BZ_SIMD_WIDTH
Definition: bzconfig.h:387
static int zeroOffset()
Definition: tinymat2.h:199
static TinyVector< int, 2 > ubound()
Definition: tinymat2.h:196
T_matrix & operator|=(const T &)
#define restrict
Definition: compiler.h:95
T_matrix & operator>>=(const T &)
The TinyVector class is a one-dimensional, fixed length vector that implements the blitz expression t...
Definition: et-forward.h:14
const T_numtype *restrict data() const
Definition: tinymat2.h:120
#define BZ_ALIGN_VARIABLE(vartype, varname, alignment)
Definition: bzconfig.h:22
static bool isMajorRank(int rank)
Definition: tinymat2.h:149
const T_numtype &restrict operator()(unsigned i, unsigned j) const
Definition: tinymat2.h:242
Definition: et-forward.h:15
bool assertInRange(const T_index &BZ_DEBUG_PARAM(index)) const
Definition: tinymat2.h:219
static bool canCollapse(int outerRank, int innerRank)
Definition: tinymat2.h:317
static int cols()
Definition: tinymat2.h:114