openmp_state.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. [auto_generated]
  3. boost/numeric/odeint/external/openmp/openmp_state.hpp
  4. [begin_description]
  5. Wrappers for OpenMP.
  6. [end_description]
  7. Copyright 2013 Karsten Ahnert
  8. Copyright 2013 Mario Mulansky
  9. Copyright 2013 Pascal Germroth
  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_OPENMP_OPENMP_STATE_HPP_INCLUDED
  15. #define BOOST_NUMERIC_ODEINT_EXTERNAL_OPENMP_OPENMP_STATE_HPP_INCLUDED
  16. #include <omp.h>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <boost/range/adaptor/sliced.hpp>
  20. #include <boost/numeric/odeint/util/copy.hpp>
  21. #include <boost/numeric/odeint/util/split.hpp>
  22. #include <boost/numeric/odeint/util/resize.hpp>
  23. #include <boost/numeric/odeint/external/openmp/openmp_nested_algebra.hpp>
  24. namespace boost {
  25. namespace numeric {
  26. namespace odeint {
  27. /** \brief A container that is split into distinct parts, for threading.
  28. * Just a wrapper for vector<vector<T>>, use `copy` for splitting/joining.
  29. */
  30. template< class T >
  31. struct openmp_state : public std::vector< std::vector< T > >
  32. {
  33. openmp_state() {}
  34. openmp_state(size_t n, const std::vector<T>& val = std::vector<T>())
  35. : std::vector< std::vector< T > >(n, val) {}
  36. template<class InputIterator>
  37. openmp_state(InputIterator first, InputIterator last)
  38. : std::vector< std::vector< T > >(first, last) {}
  39. openmp_state(const std::vector< std::vector< T > > &orig)
  40. : std::vector< std::vector< T > >(orig) {}
  41. };
  42. template< class T >
  43. struct is_resizeable< openmp_state< T > > : boost::true_type { };
  44. template< class T >
  45. struct same_size_impl< openmp_state< T > , openmp_state< T > >
  46. {
  47. static bool same_size( const openmp_state< T > &x , const openmp_state< T > &y )
  48. {
  49. if( x.size() != y.size() ) return false;
  50. for( size_t i = 0 ; i != x.size() ; i++ )
  51. if( x[i].size() != y[i].size() ) return false;
  52. return true;
  53. }
  54. };
  55. template< class T >
  56. struct resize_impl< openmp_state< T > , openmp_state< T > >
  57. {
  58. static void resize( openmp_state< T > &x , const openmp_state< T > &y )
  59. {
  60. x.resize( y.size() );
  61. # pragma omp parallel for schedule(dynamic)
  62. for(size_t i = 0 ; i < x.size() ; i++)
  63. x[i].resize( y[i].size() );
  64. }
  65. };
  66. /** \brief Copy data between openmp_states of same size. */
  67. template< class T >
  68. struct copy_impl< openmp_state< T >, openmp_state< T > >
  69. {
  70. static void copy( const openmp_state< T > &from, openmp_state< T > &to )
  71. {
  72. # pragma omp parallel for schedule(dynamic)
  73. for(size_t i = 0 ; i < from.size() ; i++)
  74. std::copy( from[i].begin() , from[i].end() , to.begin() );
  75. }
  76. };
  77. /** \brief Copy data from some container to an openmp_state and resize it.
  78. * Target container size will determine number of blocks to split into.
  79. * If it is empty, it will be resized to the maximum number of OpenMP threads.
  80. * SourceContainer must support `s::value_type`, `s::const_iterator`, `s.begin()`, `s.end()` and `s.size()`,
  81. * with Random Access Iterators; i.e. it must be a Random Access Container. */
  82. template< class SourceContainer >
  83. struct split_impl< SourceContainer, openmp_state< typename SourceContainer::value_type > >
  84. {
  85. static void split( const SourceContainer &from, openmp_state< typename SourceContainer::value_type > &to )
  86. {
  87. if(to.size() == 0) to.resize( omp_get_max_threads() );
  88. const size_t part = from.size() / to.size();
  89. # pragma omp parallel for schedule(dynamic)
  90. for(size_t i = 0 ; i < to.size() ; i++) {
  91. typedef typename SourceContainer::const_iterator it_t;
  92. const it_t begin = from.begin() + i * part;
  93. it_t end = begin + part;
  94. // for cases where from.size() % to.size() > 0
  95. if(i + 1 == to.size() || end > from.end()) end = from.end();
  96. to[i].resize(end - begin);
  97. std::copy(begin, end, to[i].begin());
  98. }
  99. }
  100. };
  101. /** \brief Copy data from an openmp_state to some container and resize it.
  102. * TargetContainer must support `s::value_type`, `s::iterator`, `s.begin()` and `s.resize(n)`,
  103. * i.e. it must be a `std::vector`. */
  104. template< class TargetContainer >
  105. struct unsplit_impl< openmp_state< typename TargetContainer::value_type >, TargetContainer >
  106. {
  107. static void unsplit( const openmp_state< typename TargetContainer::value_type > &from , TargetContainer &to )
  108. {
  109. // resize target
  110. size_t total_size = 0;
  111. for(size_t i = 0 ; i < from.size() ; i++)
  112. total_size += from[i].size();
  113. to.resize( total_size );
  114. // copy parts
  115. typename TargetContainer::iterator out = to.begin();
  116. for(size_t i = 0 ; i < from.size() ; i++)
  117. out = std::copy(from[i].begin(), from[i].end(), out);
  118. }
  119. };
  120. /** \brief OpenMP-parallelized algebra.
  121. * For use with openmp_state.
  122. */
  123. typedef openmp_nested_algebra< range_algebra > openmp_algebra;
  124. /** \brief Use `openmp_algebra` for `openmp_state`. */
  125. template< class T >
  126. struct algebra_dispatcher< openmp_state< T > >
  127. {
  128. typedef openmp_algebra algebra_type;
  129. };
  130. }
  131. }
  132. }
  133. #endif