openmp_state.hpp 5.1 KB

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