cheshirekow  v0.1.0
Matrix.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_CUDA_LINALG2_MATRIX_H_
28 #define MPBLOCKS_CUDA_LINALG2_MATRIX_H_
29 
30 #include <iostream>
31 #include <cassert>
32 
33 namespace mpblocks {
34 namespace cuda {
35 namespace linalg2 {
36 
37 
39 template <typename Scalar, Size_t ROWS, Size_t COLS, Size_t i, Size_t j>
40 struct MatrixElement :
41  public MatrixElement<Scalar,ROWS,COLS,i,j+1>
42 {
43  Scalar m_data;
44 };
45 
47 template <typename Scalar, Size_t ROWS, Size_t COLS, Size_t i>
48 struct MatrixElement<Scalar,ROWS,COLS,i,0> :
49  public MatrixElement<Scalar,ROWS,COLS,i,1>,
50  public MatrixElement<Scalar,ROWS,COLS,i+1,0>
51 {
52  Scalar m_data;
53 };
54 
56 template <typename Scalar, Size_t ROWS, Size_t COLS>
57 struct MatrixElement<Scalar,ROWS,COLS,ROWS,0>
58 {};
59 
61 template <typename Scalar, Size_t ROWS, Size_t COLS, Size_t i>
62 struct MatrixElement<Scalar,ROWS,COLS,i,COLS>
63 {};
64 
65 
66 template <typename Scalar, Size_t ROWS, Size_t COLS>
67 class Matrix :
68  protected MatrixElement<Scalar,ROWS,COLS,0,0>,
69  public LValue< Scalar,ROWS,COLS, Matrix<Scalar,ROWS,COLS> >,
70  public RValue< Scalar,ROWS,COLS, Matrix<Scalar,ROWS,COLS> >
71 {
72  public:
75 
76  public:
78  template <Size_t i>
80  Scalar& sve()
81  {
82 // if( COLS == 1U )
83  return static_cast<
85 // else
86 // return static_cast<
87 // MatrixElement<Scalar,ROWS,COLS,0,i>& >(*this).m_data;
88  }
89 
91  template <Size_t i>
93  Scalar gve() const
94  {
95 // if( COLS == 1U )
96  return static_cast<
98 // else
99 // return static_cast<
100 // MatrixElement<Scalar,ROWS,COLS,0,i> const& >(*this).m_data;
101  }
102 
104  template <Size_t i>
106  Scalar& ve()
107  {
108 // if( COLS == 1U )
109  return static_cast<
111 // else
112 // return static_cast<
113 // MatrixElement<Scalar,ROWS,COLS,0,i>& >(*this).m_data;
114  }
115 
117  template <Size_t i>
119  Scalar ve() const
120  {
121 // if( COLS == 1U )
122  return static_cast<
124 // else
125 // return static_cast<
126 // MatrixElement<Scalar,ROWS,COLS,0,i> const& >(*this).m_data;
127  }
128 
130  template <Size_t i, Size_t j>
132  Scalar& me()
133  {
134  return static_cast<
136  }
137 
139  template <Size_t i, Size_t j>
141  Scalar me() const
142  {
143  return static_cast<
145  }
146 
149  Matrix(){}
150 
151 
153  template <typename Exp>
156  {
157  LValue_t::operator=(exp);
158  }
159 
161 
166  Scalar& operator[](int i)
167  {
168  Scalar* buf = (Scalar*)(this);
169  return buf[ROWS*COLS -1 - i];
170  }
171 
173  Scalar const& operator[](int i) const
174  {
175  Scalar* buf = (Scalar*)(this);
176  return buf[ROWS*COLS -1 - i];
177  }
178 
180 
185  Scalar& operator()(int i, int j)
186  {
187  Scalar* buf = (Scalar*)(this);
188  unsigned int off = 0;
189  if(j == 0 )
190  off = i;
191  else
192  off = ROWS + (COLS-1)*(ROWS-1-i) + (j-1);
193 
194  if( off > ROWS*COLS-1 )
195  return buf[0];
196 
197  return buf[ROWS*COLS-1 - off];
198  }
199 
201  Scalar const& operator()(int i, int j) const
202  {
203  Scalar* buf = (Scalar*)(this);
204  unsigned int off = 0;
205  if(j == 0 )
206  off = i;
207  else
208  off = ROWS + (COLS-1)*(ROWS-1-i) + (j-1);
209 
210  if( off > ROWS*COLS-1 )
211  return buf[0];
212 
213  return buf[ROWS*COLS-1 - off];
214  }
215 };
216 
217 
218 
219 
220 
221 
222 
223 
224 } // linalg
225 } // cuda
226 } // mpblocks
227 
228 
229 
230 
231 
232 #endif // LINALG_H_
__device__ __host__ Scalar me() const
matrix accessor
Definition: Matrix.h:141
__device__ __host__ Scalar & operator()(int i, int j)
dynamic accessor for matrix expressions
Definition: Matrix.h:185
expression template for rvalues
Definition: RValue.h:36
LValue< Scalar, ROWS, COLS, Matrix_t > LValue_t
Definition: Matrix.h:74
__device__ __host__ Scalar & me()
matrix accessor
Definition: Matrix.h:132
__device__ __host__ Scalar ve() const
vector accessor
Definition: Matrix.h:119
specialization for 0'th column, also inherits row
Definition: Matrix.h:48
__device__ __host__ LValue< Scalar, ROWS, COLS, Exp > & operator=(RValue< Scalar, ROWS, COLS, Exp2 > const &B)
assignment
Definition: LValue.h:78
__device__ __host__ Matrix()
Default constructor.
Definition: Matrix.h:149
__device__ __host__ Scalar const & operator[](int i) const
Definition: Matrix.h:173
default template has column inheritence
Definition: Matrix.h:40
Matrix< Scalar, ROWS, COLS > Matrix_t
Definition: Matrix.h:73
#define __device__
Definition: fakecuda.h:34
__device__ __host__ Scalar & operator[](int i)
dynamic accessor for vector expressions
Definition: Matrix.h:166
#define __host__
Definition: fakecuda.h:37
__device__ __host__ Scalar & ve()
vector accessor
Definition: Matrix.h:106
__device__ __host__ Matrix(RValue< Scalar, ROWS, COLS, Exp > const &exp)
Construct from any MatrixExpression:
Definition: Matrix.h:155
expression template for rvalues
Definition: LValue.h:36
__device__ __host__ Scalar gve() const
vector accessor
Definition: Matrix.h:93
__device__ __host__ Scalar const & operator()(int i, int j) const
Definition: Matrix.h:201
__device__ __host__ Scalar & sve()
vector accessor
Definition: Matrix.h:80