parallel_for.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #ifndef IGL_PARALLEL_FOR_H
  9. #define IGL_PARALLEL_FOR_H
  10. #include "igl_inline.h"
  11. #include <functional>
  12. //#warning "Defining IGL_PARALLEL_FOR_FORCE_SERIAL"
  13. //#define IGL_PARALLEL_FOR_FORCE_SERIAL
  14. namespace igl
  15. {
  16. /// Functional implementation of a basic, open-mp style, parallel
  17. /// for loop. If the inner block of a for-loop can be rewritten/encapsulated in
  18. /// a single (anonymous/lambda) function call `func` so that the serial code
  19. /// looks like:
  20. ///
  21. /// \code{cpp}
  22. /// for(int i = 0;i<loop_size;i++)
  23. /// {
  24. /// func(i);
  25. /// }
  26. /// \endcode
  27. ///
  28. /// then `parallel_for(loop_size,func,min_parallel)` will use as many threads as
  29. /// available on the current hardware to parallelize this for loop so long as
  30. /// loop_size<min_parallel, otherwise it will just use a serial for loop.
  31. ///
  32. /// Often if your code looks like:
  33. ///
  34. /// \code{cpp}
  35. /// for(int i = 0;i<loop_size;i++)
  36. /// {
  37. /// …
  38. /// }
  39. /// \endcode
  40. ///
  41. /// Then you can make a minimal two-line change to parallelize it:
  42. ///
  43. /// \code{cpp}
  44. /// //for(int i = 0;i<loop_size;i++)
  45. /// parallel_for(loop_size,[&](int i)
  46. /// {
  47. /// …
  48. /// }
  49. /// ,1000);
  50. /// \endcode
  51. ///
  52. /// @param[in] loop_size number of iterations. I.e. for(int i = 0;i<loop_size;i++) ...
  53. /// @param[in] func function handle taking iteration index as only argument to compute
  54. /// inner block of for loop I.e. for(int i ...){ func(i); }
  55. /// @param[in] min_parallel min size of loop_size such that parallel (non-serial)
  56. /// thread pooling should be attempted {0}
  57. /// @return true iff thread pool was invoked
  58. template<typename Index, typename FunctionType >
  59. inline bool parallel_for(
  60. const Index loop_size,
  61. const FunctionType & func,
  62. const size_t min_parallel=0);
  63. /// Functional implementation of an open-mp style, parallel for loop with
  64. /// accumulation. For example, serial code separated into n chunks (each to be
  65. /// parallelized with a thread) might look like:
  66. ///
  67. /// \code{cpp}
  68. /// Eigen::VectorXd S;
  69. /// const auto & prep_func = [&S](int n){ S = Eigen:VectorXd::Zero(n); };
  70. /// const auto & func = [&X,&S](int i, int t){ S(t) += X(i); };
  71. /// const auto & accum_func = [&S,&sum](int t){ sum += S(t); };
  72. /// prep_func(n);
  73. /// for(int i = 0;i<loop_size;i++)
  74. /// {
  75. /// func(i,i%n);
  76. /// }
  77. /// double sum = 0;
  78. /// for(int t = 0;t<n;t++)
  79. /// {
  80. /// accum_func(t);
  81. /// }
  82. /// \endcode
  83. ///
  84. /// @param[in] loop_size number of iterations. I.e. for(int i = 0;i<loop_size;i++) ...
  85. /// @param[in] prep_func function handle taking n >= number of threads as only
  86. /// argument
  87. /// @param[in] func function handle taking iteration index i and thread id t as only
  88. /// arguments to compute inner block of for loop I.e.
  89. /// for(int i ...){ func(i,t); }
  90. /// @param[in] accum_func function handle taking thread index as only argument, to be
  91. /// called after all calls of func, e.g., for serial accumulation across
  92. /// all n (potential) threads, see n in description of prep_func.
  93. /// @param[in] min_parallel min size of loop_size such that parallel (non-serial)
  94. /// thread pooling should be attempted {0}
  95. /// @return true iff thread pool was invoked
  96. template<
  97. typename Index,
  98. typename PrepFunctionType,
  99. typename FunctionType,
  100. typename AccumFunctionType
  101. >
  102. inline bool parallel_for(
  103. const Index loop_size,
  104. const PrepFunctionType & prep_func,
  105. const FunctionType & func,
  106. const AccumFunctionType & accum_func,
  107. const size_t min_parallel=0);
  108. }
  109. // Implementation
  110. #include "default_num_threads.h"
  111. #include <cmath>
  112. #include <cassert>
  113. #include <thread>
  114. #include <vector>
  115. #include <algorithm>
  116. template<typename Index, typename FunctionType >
  117. inline bool igl::parallel_for(
  118. const Index loop_size,
  119. const FunctionType & func,
  120. const size_t min_parallel)
  121. {
  122. using namespace std;
  123. // no op preparation/accumulation
  124. const auto & no_op = [](const size_t /*n/t*/){};
  125. // two-parameter wrapper ignoring thread id
  126. const auto & wrapper = [&func](Index i,size_t /*t*/){ func(i); };
  127. return parallel_for(loop_size,no_op,wrapper,no_op,min_parallel);
  128. }
  129. template<
  130. typename Index,
  131. typename PreFunctionType,
  132. typename FunctionType,
  133. typename AccumFunctionType>
  134. inline bool igl::parallel_for(
  135. const Index loop_size,
  136. const PreFunctionType & prep_func,
  137. const FunctionType & func,
  138. const AccumFunctionType & accum_func,
  139. const size_t min_parallel)
  140. {
  141. assert(loop_size>=0);
  142. if(loop_size==0) return false;
  143. // Estimate number of threads in the pool
  144. // http://ideone.com/Z7zldb
  145. #ifdef IGL_PARALLEL_FOR_FORCE_SERIAL
  146. const size_t nthreads = 1;
  147. #else
  148. const size_t nthreads = igl::default_num_threads();
  149. #endif
  150. if(loop_size<min_parallel || nthreads<=1)
  151. {
  152. // serial
  153. prep_func(1);
  154. for(Index i = 0;i<loop_size;i++) func(i,0);
  155. accum_func(0);
  156. return false;
  157. }else
  158. {
  159. // Size of a slice for the range functions
  160. Index slice =
  161. std::max(
  162. (Index)std::round((loop_size+1)/static_cast<double>(nthreads)),(Index)1);
  163. // [Helper] Inner loop
  164. const auto & range = [&func](const Index k1, const Index k2, const size_t t)
  165. {
  166. for(Index k = k1; k < k2; k++) func(k,t);
  167. };
  168. prep_func(nthreads);
  169. // Create pool and launch jobs
  170. std::vector<std::thread> pool;
  171. pool.reserve(nthreads);
  172. // Inner range extents
  173. Index i1 = 0;
  174. Index i2 = std::min(0 + slice, loop_size);
  175. {
  176. size_t t = 0;
  177. for (; t+1 < nthreads && i1 < loop_size; ++t)
  178. {
  179. pool.emplace_back(range, i1, i2, t);
  180. i1 = i2;
  181. i2 = std::min(i2 + slice, loop_size);
  182. }
  183. if (i1 < loop_size)
  184. {
  185. pool.emplace_back(range, i1, loop_size, t);
  186. }
  187. }
  188. // Wait for jobs to finish
  189. for (std::thread &t : pool) if (t.joinable()) t.join();
  190. // Accumulate across threads
  191. for(size_t t = 0;t<nthreads;t++)
  192. {
  193. accum_func(t);
  194. }
  195. return true;
  196. }
  197. }
  198. //#ifndef IGL_STATIC_LIBRARY
  199. //#include "parallel_for.cpp"
  200. //#endif
  201. #endif