cheshirekow  v0.1.0
Product.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_PRODUCT_H_
28 #define MPBLOCKS_CUDA_LINALG2_PRODUCT_H_
29 
30 namespace mpblocks {
31 namespace cuda {
32 namespace linalg2 {
33 
34 
35 namespace product {
36 
37 
39 template <typename Scalar,
40  Size_t ROWS, Size_t COLS, Size_t COMMON,
41  Size_t i, Size_t j, Size_t k,
42  class Exp1, class Exp2>
43 struct Iterator
44 {
46  static Scalar result( const Exp1& A, const Exp2& B )
47  {
48  return
49  ( A.Exp1::template me<i,k>() *
50  B.Exp2::template me<k,j>() ) +
52  }
53 };
54 
56 template <typename Scalar,
57  Size_t ROWS, Size_t COLS, Size_t COMMON,
58  Size_t i, Size_t j,
59  class Exp1, class Exp2>
60 struct Iterator<Scalar,ROWS,COLS,COMMON,i,j,COMMON,Exp1,Exp2>
61 {
63  static Scalar result( const Exp1& A, const Exp2& B )
64  {
65  return 0;
66  }
67 };
68 
69 
70 }
71 
73 template <typename Scalar,
74  Size_t ROWS, Size_t COLS, Size_t COMMON,
75  class Exp1, class Exp2>
76 class Product:
77  public RValue< Scalar, ROWS, COLS, Product< Scalar, ROWS, COLS, COMMON, Exp1, Exp2> >
78 {
79  Exp1 const& m_A;
80  Exp2 const& m_B;
81 
82  public:
84  Product( Exp1 const& A, Exp2 const& B ):
85  m_A(A),
86  m_B(B)
87  {}
88 
90  template< Size_t i >
92  Scalar ve() const
93  {
94  if( COLS == 1 )
95  return product::
98  else
99  return product::
101  ::result(m_A,m_B);
102  }
103 
105  template< Size_t i, Size_t j >
107  Scalar me() const
108  {
110  ::result(m_A,m_B);
111  }
112 };
113 
114 
115 
116 
117 template <typename Scalar,
118  Size_t ROWS, Size_t COMMON, Size_t COLS,
119  class Exp1, class Exp2>
120 __device__ __host__ inline
121 Product< Scalar, ROWS, COLS, COMMON, Exp1, Exp2 >
124 {
126  static_cast< Exp1 const& >(A),
127  static_cast< Exp2 const& >(B) );
128 }
129 
130 
131 
132 
133 
134 } // linalg
135 } // cuda
136 } // mpblocks
137 
138 
139 
140 
141 
142 #endif // PRODUCT_H_
unsigned int Size_t
Definition: linalg2.h:35
expression template for product expressions
Definition: Product.h:76
__device__ __host__ Scalar ve() const
return the evaluated i'th element of a vector expression
Definition: Product.h:92
__device__ __host__ Scalar me() const
return the evaluated (j,i)'th element of a matrix expression
Definition: Product.h:107
expression template for rvalues
Definition: RValue.h:36
__device__ __host__ Product(Exp1 const &A, Exp2 const &B)
Definition: Product.h:84
__device__ __host__ Product< Scalar, ROWS, COLS, COMMON, Exp1, Exp2 > operator*(RValue< Scalar, ROWS, COMMON, Exp1 > const &A, RValue< Scalar, COMMON, COLS, Exp2 > const &B)
Definition: Product.h:122
__device__ static __host__ Scalar result(const Exp1 &A, const Exp2 &B)
Definition: Product.h:46
#define __device__
Definition: fakecuda.h:34
__device__ static __host__ Scalar result(const Exp1 &A, const Exp2 &B)
Definition: Product.h:63
#define __host__
Definition: fakecuda.h:37
returns the result of the i,j element of the product
Definition: Product.h:43