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_POLYNOMIAL_PRODUCT_H_
28 #define MPBLOCKS_CUDA_POLYNOMIAL_PRODUCT_H_
29 
30 namespace mpblocks {
31 namespace cuda {
32 namespace polynomial {
33 
34 
35 namespace product_detail
36 {
37 
38 struct Null;
39 
42 template< int A, int B >
43 struct IdxPair{};
44 
47 template< int Key, class Value, class Next=Null >
48 struct Node{};
49 
51 template< class A, class B >
52 struct less_than{};
53 
54 template< int KeyA, class ValueA, class NextA,
55  int KeyB, class ValueB, class NextB >
56 struct less_than< Node<KeyA,ValueA,NextA>,
57  Node<KeyB,ValueB,NextB> >
58 {
59  enum{ value = (KeyA < KeyB) };
60 };
61 
62 template< int KeyA, class ValueA, class NextA >
63 struct less_than< Node<KeyA,ValueA,NextA>, Null >
64 {
65  enum{ value = true };
66 };
67 
68 template< int KeyA, class ValueA, class NextA >
69 struct less_than< Null, Node<KeyA,ValueA,NextA> >
70 {
71  enum{ value = false };
72 };
73 
74 
77 template< class NodeA, class NodeB, bool ALessThanB >
78 struct insert_postTest{};
79 
80 template< int KeyA, class ValueA, class NextA, class NodeB >
81 struct insert_postTest< Node<KeyA,ValueA,NextA>, NodeB, true >
82 {
83  typedef Node<KeyA,ValueA,
84  typename insert_postTest<
86  >::result
87  > result;
88 };
89 
90 template< class NodeA, int KeyB, class ValueB >
91 struct insert_postTest< NodeA, Node<KeyB,ValueB,Null>, false >
92 {
94 };
95 
97 template< class Root, class Node >
98 struct insert
99 {
100  typedef typename insert_postTest<
102 };
103 
105 template< class Node >
106 struct insert< Null,Node >
107 {
108  typedef Node result;
109 };
110 
111 
113 template< int Coeff1, class Spec2, class Map >
114 struct inner_loop{};
115 
116 template< int Coeff1, int Head, class Tail, class Map >
117 struct inner_loop<Coeff1, IntList<Head,Tail>, Map >
118 {
122 };
123 
124 template< int Coeff1, int Head, class Map >
125 struct inner_loop<Coeff1, IntList<Head,intlist::Terminal>, Map >
126 {
129 };
130 
132 template< class Spec1, class Spec2, class Map >
133 struct outer_loop{};
134 
135 template< int Head, class Tail, class Spec2, class Map >
136 struct outer_loop< IntList<Head,Tail>, Spec2, Map >
137 {
140 };
141 
142 template< int Head, class Spec2, class Map >
143 struct outer_loop< IntList<Head,intlist::Terminal>, Spec2, Map >
144 {
146 };
147 
148 
150 template< class Spec1, class Spec2 >
152 {
154 };
155 
156 
157 template < class Spec >
159 {
160  static void print( std::ostream& out )
161  {
162  out << "ERR, ";
163  }
164 };
165 
166 template < >
167 struct SpecPrinter< Null >
168 {
169  static void print( std::ostream& out )
170  {
171  out << "FINI";
172  }
173 };
174 
175 template< int Key, int ValA, int ValB, class Next >
176 struct SpecPrinter< Node<Key,IdxPair<ValA,ValB>,Next> >
177 {
178  static void print( std::ostream& out )
179  {
180  out << "(" << Key << "," << ValA << "," << ValB << "), ";
182  }
183 };
184 
185 
186 template< int Key, int ValA, int ValB, class Next >
187 std::ostream& operator<<( std::ostream& out,
188  SpecPrinter< Node<Key,IdxPair<ValA,ValB>,Next> > printer)
189 {
190  out << "[";
191  printer.print(out);
192  out << "]";
193  return out;
194 }
195 
196 
197 
198 
199 } //< namespace product_detail
200 
201 
203 template <typename Scalar,
204  class Exp1, class Spec1,
205  class Exp2, class Spec2 >
206 struct Product :
207  public RValue< Scalar, Product<Scalar,Exp1,Spec1,Exp2,Spec2>,
208  typename intlist::allpairs_sum<Spec1,Spec2>::result >
209 {
210  Exp1 const& m_A;
211  Exp2 const& m_B;
212 
214  Product( Exp1 const& A, Exp2 const& B ):
215  m_A(A),
216  m_B(B)
217  {
218  }
219 
221  Scalar eval( Scalar x )
222  {
223  return m_A.eval(x) * m_B.eval(x);
224  }
225 };
226 
227 
228 namespace product_detail
229 {
230 
231 template <int i, typename Scalar, class Exp1, class Exp2, class Spec >
232 struct Summation{};
233 
234 template <int i, typename Scalar, class Exp1, class Exp2,
235  int Key, int ValA, int ValB, class Next >
236 struct Summation< i, Scalar, Exp1, Exp2, Node<Key,IdxPair<ValA,ValB>,Next> >
237 {
239  static Scalar do_it( const Exp1& exp1, const Exp2& exp2 )
240  {
242  }
243 };
244 
245 template <int i, typename Scalar, class Exp1, class Exp2,
246  int ValA, int ValB, class Next >
247 struct Summation< i, Scalar, Exp1, Exp2, Node<i,IdxPair<ValA,ValB>,Next> >
248 {
250  static Scalar do_it( const Exp1& exp1, const Exp2& exp2 )
251  {
252  return get<ValA>(exp1)*get<ValB>(exp2) +
254  }
255 };
256 
257 template <int i, typename Scalar, class Exp1, class Exp2,
258  int Key, int ValA, int ValB >
259 struct Summation< i, Scalar, Exp1, Exp2, Node<Key,IdxPair<ValA,ValB>,Null> >
260 {
262  static Scalar do_it( const Exp1& exp1, const Exp2& exp2 )
263  {
264  return 0;
265  }
266 };
267 
268 template <int i, typename Scalar, class Exp1, class Exp2,
269  int ValA, int ValB >
270 struct Summation< i, Scalar, Exp1, Exp2, Node<i,IdxPair<ValA,ValB>,Null> >
271 {
273  static Scalar do_it( const Exp1& exp1, const Exp2& exp2 )
274  {
275  return get<ValA>(exp1)*get<ValB>(exp2);
276  }
277 };
278 
279 } //< namespace product_detail
280 
281 
282 template <typename Scalar,
283  class Exp1, class Spec1,
284  class Exp2, class Spec2 >
285 struct get_spec< Product<Scalar,Exp1,Spec1,Exp2,Spec2> >
286 {
288 };
289 
290 template <int i, typename Scalar,
291  class Exp1, class Spec1,
292  class Exp2, class Spec2 >
295 {
298  exp.m_A, exp.m_B );
299 }
300 
301 
302 template <typename Scalar,
303  class Exp1, class Spec1,
304  class Exp2, class Spec2 >
307  RValue<Scalar,Exp1,Spec1> const& A,
308  RValue<Scalar,Exp2,Spec2> const& B )
309 {
311  static_cast<Exp1 const&>(A),
312  static_cast<Exp2 const&>(B));
313 }
314 
315 
316 
317 
318 } // polynomial
319 } // cuda
320 } // mpblocks
321 
322 
323 
324 
325 
326 #endif // PRODUCT_H_
__host__ __device__ Polynomial< Scalar, IntList< pow, intlist::Terminal > > operator*(Scalar v, CoefficientKey< pow > c)
Definition: Construct.h:58
evaluates ordering on nodes by resolving their keys
Definition: Product.h:52
pairs one index of the first polynomial with one index of the second polynomial
Definition: Product.h:43
expression template for rvalues
Definition: RValue.h:40
strip_dups< typename merge_sort< typename allpairs_sum_unsorted< IntList1, IntList2 >::result >::result >::result result
Definition: IntList.h:379
__host__ __device__ Product(Exp1 const &A, Exp2 const &B)
Definition: Product.h:214
insert a new key/value pair into the map
Definition: Product.h:98
std::ostream & operator<<(std::ostream &out, SpecPrinter< Node< Key, IdxPair< ValA, ValB >, Next > > printer)
Definition: Product.h:187
__host__ __device__ Scalar eval(Scalar x)
Definition: Product.h:221
Node< KeyB, ValueB, NodeA > result
Definition: Product.h:93
insert_postTest< Root, Node, less_than< Root, Node >::value >::result result
Definition: Product.h:101
expression template for sum of two matrix expressions
Definition: Product.h:206
Node< KeyA, ValueA, typename insert_postTest< NextA, NodeB, less_than< NextA, NodeB >::value >::result > result
Definition: Product.h:87
#define __device__
Definition: fakecuda.h:34
compile time map of output monomial to input monomial pairs that contribute to that coefficient ...
Definition: Product.h:48
outer_loop< Spec1, Spec2, Null >::result result
Definition: Product.h:153
#define __host__
Definition: fakecuda.h:37
if ALessThanB is true, then template recurses, otherwise B is inserted in front of A ...
Definition: Product.h:78
compile time integer list (because cuda doesn't understand variadic templates )
Definition: IntList.h:50