thrust_algebra.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/external/thrust/thrust_algebra.hpp
  4. [begin_description]
  5. An algebra for thrusts device_vectors.
  6. [end_description]
  7. Copyright 2010-2013 Mario Mulansky
  8. Copyright 2010-2011 Karsten Ahnert
  9. Copyright 2013 Kyle Lutz
  10. Distributed under the Boost Software License, Version 1.0.
  11. (See accompanying file LICENSE_1_0.txt or
  12. copy at http://www.boost.org/LICENSE_1_0.txt)
  13. */
  14. #ifndef BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_THRUST_ALGEBRA_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_THRUST_ALGEBRA_HPP_INCLUDED
  16. #include <thrust/device_vector.h>
  17. #include <thrust/for_each.h>
  18. #include <thrust/iterator/zip_iterator.h>
  19. #include <boost/range.hpp>
  20. namespace boost {
  21. namespace numeric {
  22. namespace odeint {
  23. namespace detail {
  24. // to use in thrust::reduce
  25. template< class Value >
  26. struct maximum
  27. {
  28. template< class Fac1 , class Fac2 >
  29. __host__ __device__
  30. Value operator()( const Fac1 t1 , const Fac2 t2 ) const
  31. {
  32. return ( abs( t1 ) < abs( t2 ) ) ? t2 : t1 ;
  33. }
  34. typedef Value result_type;
  35. };
  36. }
  37. /** ToDO extend until for_each14 for rk78 */
  38. /*
  39. * The const versions are needed for boost.range to work, i.e.
  40. * it allows you to do
  41. * for_each1( make_pair( vec1.begin() , vec1.begin() + 10 ) , op );
  42. */
  43. struct thrust_algebra
  44. {
  45. template< class StateType , class Operation >
  46. static void for_each1( StateType &s , Operation op )
  47. {
  48. thrust::for_each( boost::begin(s) , boost::end(s) , op );
  49. }
  50. template< class StateType1 , class StateType2 , class Operation >
  51. static void for_each2( StateType1 &s1 , StateType2 &s2 , Operation op )
  52. {
  53. thrust::for_each(
  54. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  55. boost::begin(s2) ) ) ,
  56. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  57. boost::end(s2) ) ) ,
  58. op);
  59. }
  60. template< class StateType1 , class StateType2 , class StateType3 , class Operation >
  61. static void for_each3( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , Operation op )
  62. {
  63. thrust::for_each(
  64. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  65. boost::begin(s2) ,
  66. boost::begin(s3) ) ) ,
  67. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  68. boost::end(s2) ,
  69. boost::end(s3) ) ) ,
  70. op);
  71. }
  72. template< class StateType1 , class StateType2 , class StateType3 , class StateType4 ,
  73. class Operation >
  74. static void for_each4( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , StateType4 &s4 ,
  75. Operation op )
  76. {
  77. thrust::for_each(
  78. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  79. boost::begin(s2) ,
  80. boost::begin(s3) ,
  81. boost::begin(s4) ) ) ,
  82. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  83. boost::end(s2) ,
  84. boost::end(s3) ,
  85. boost::end(s4) ) ) ,
  86. op);
  87. }
  88. template< class StateType1 , class StateType2 , class StateType3 ,
  89. class StateType4 , class StateType5 ,class Operation >
  90. static void for_each5( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , StateType4 &s4 ,
  91. StateType5 &s5 , Operation op )
  92. {
  93. thrust::for_each(
  94. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  95. boost::begin(s2) ,
  96. boost::begin(s3) ,
  97. boost::begin(s4) ,
  98. boost::begin(s5) ) ) ,
  99. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  100. boost::end(s2) ,
  101. boost::end(s3) ,
  102. boost::end(s4) ,
  103. boost::end(s5) ) ) ,
  104. op);
  105. }
  106. template< class StateType1 , class StateType2 , class StateType3 ,
  107. class StateType4 , class StateType5 , class StateType6 , class Operation >
  108. static void for_each6( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , StateType4 &s4 ,
  109. StateType5 &s5 , StateType6 &s6 , Operation op )
  110. {
  111. thrust::for_each(
  112. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  113. boost::begin(s2) ,
  114. boost::begin(s3) ,
  115. boost::begin(s4) ,
  116. boost::begin(s5) ,
  117. boost::begin(s6) ) ) ,
  118. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  119. boost::end(s2) ,
  120. boost::end(s3) ,
  121. boost::end(s4) ,
  122. boost::end(s5) ,
  123. boost::end(s6) ) ) ,
  124. op);
  125. }
  126. template< class StateType1 , class StateType2 , class StateType3 , class StateType4 ,
  127. class StateType5 , class StateType6 , class StateType7 , class Operation >
  128. static void for_each7( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , StateType4 &s4 ,
  129. StateType5 &s5 , StateType6 &s6 , StateType7 &s7 , Operation op )
  130. {
  131. thrust::for_each(
  132. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  133. boost::begin(s2) ,
  134. boost::begin(s3) ,
  135. boost::begin(s4) ,
  136. boost::begin(s5) ,
  137. boost::begin(s6) ,
  138. boost::begin(s7) ) ) ,
  139. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  140. boost::end(s2) ,
  141. boost::end(s3) ,
  142. boost::end(s4) ,
  143. boost::end(s5) ,
  144. boost::end(s6) ,
  145. boost::end(s7) ) ) ,
  146. op);
  147. }
  148. template< class StateType1 , class StateType2 , class StateType3 , class StateType4 ,
  149. class StateType5 , class StateType6 , class StateType7 , class StateType8 , class Operation >
  150. static void for_each8( StateType1 &s1 , StateType2 &s2 , StateType3 &s3 , StateType4 &s4 ,
  151. StateType5 &s5 , StateType6 &s6 , StateType7 &s7 , StateType8 &s8 , Operation op )
  152. {
  153. thrust::for_each(
  154. thrust::make_zip_iterator( thrust::make_tuple( boost::begin(s1) ,
  155. boost::begin(s2) ,
  156. boost::begin(s3) ,
  157. boost::begin(s4) ,
  158. boost::begin(s5) ,
  159. boost::begin(s6) ,
  160. boost::begin(s7) ,
  161. boost::begin(s8) ) ) ,
  162. thrust::make_zip_iterator( thrust::make_tuple( boost::end(s1) ,
  163. boost::end(s2) ,
  164. boost::end(s3) ,
  165. boost::end(s4) ,
  166. boost::end(s5) ,
  167. boost::end(s6) ,
  168. boost::end(s7) ,
  169. boost::end(s8) ) ) ,
  170. op);
  171. }
  172. template< class S >
  173. static typename S::value_type norm_inf( const S &s )
  174. {
  175. typedef typename S::value_type value_type;
  176. return thrust::reduce( boost::begin( s ) , boost::end( s ) ,
  177. static_cast<value_type>(0) ,
  178. detail::maximum<value_type>() );
  179. }
  180. };
  181. } // odeint
  182. } // numeric
  183. } // boost
  184. #endif // BOOST_NUMERIC_ODEINT_EXTERNAL_THRUST_THRUST_ALGEBRA_HPP_INCLUDED