parallel_for.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../tasking/taskscheduler.h"
  5. #include "../sys/array.h"
  6. #include "../math/math.h"
  7. #include "../math/range.h"
  8. namespace embree
  9. {
  10. /* parallel_for without range */
  11. template<typename Index, typename Func>
  12. __forceinline void parallel_for( const Index N, const Func& func)
  13. {
  14. #if defined(TASKING_INTERNAL)
  15. if (N) {
  16. TaskScheduler::spawn(Index(0),N,Index(1),[&] (const range<Index>& r) {
  17. assert(r.size() == 1);
  18. func(r.begin());
  19. });
  20. if (!TaskScheduler::wait())
  21. // -- GODOT start --
  22. // throw std::runtime_error("task cancelled");
  23. abort();
  24. // -- GODOT end --
  25. }
  26. #elif defined(TASKING_TBB)
  27. #if TBB_INTERFACE_VERSION >= 12002
  28. tbb::task_group_context context;
  29. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  30. func(i);
  31. },context);
  32. if (context.is_group_execution_cancelled())
  33. // -- GODOT start --
  34. // throw std::runtime_error("task cancelled");
  35. abort();
  36. // -- GODOT end --
  37. #else
  38. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  39. func(i);
  40. });
  41. if (tbb::task::self().is_cancelled())
  42. // -- GODOT start --
  43. // throw std::runtime_error("task cancelled");
  44. abort();
  45. // -- GODOT end --
  46. #endif
  47. #elif defined(TASKING_PPL)
  48. concurrency::parallel_for(Index(0),N,Index(1),[&](Index i) {
  49. func(i);
  50. });
  51. #else
  52. # error "no tasking system enabled"
  53. #endif
  54. }
  55. /* parallel for with range and granulatity */
  56. template<typename Index, typename Func>
  57. __forceinline void parallel_for( const Index first, const Index last, const Index minStepSize, const Func& func)
  58. {
  59. assert(first <= last);
  60. #if defined(TASKING_INTERNAL)
  61. TaskScheduler::spawn(first,last,minStepSize,func);
  62. if (!TaskScheduler::wait())
  63. // -- GODOT start --
  64. // throw std::runtime_error("task cancelled");
  65. abort();
  66. // -- GODOT end --
  67. #elif defined(TASKING_TBB)
  68. #if TBB_INTERFACE_VERSION >= 12002
  69. tbb::task_group_context context;
  70. tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
  71. func(range<Index>(r.begin(),r.end()));
  72. },context);
  73. if (context.is_group_execution_cancelled())
  74. // -- GODOT start --
  75. // throw std::runtime_error("task cancelled");
  76. abort();
  77. // -- GODOT end --
  78. #else
  79. tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
  80. func(range<Index>(r.begin(),r.end()));
  81. });
  82. if (tbb::task::self().is_cancelled())
  83. // -- GODOT start --
  84. // throw std::runtime_error("task cancelled");
  85. abort();
  86. // -- GODOT end --
  87. #endif
  88. #elif defined(TASKING_PPL)
  89. concurrency::parallel_for(first, last, Index(1) /*minStepSize*/, [&](Index i) {
  90. func(range<Index>(i,i+1));
  91. });
  92. #else
  93. # error "no tasking system enabled"
  94. #endif
  95. }
  96. /* parallel for with range */
  97. template<typename Index, typename Func>
  98. __forceinline void parallel_for( const Index first, const Index last, const Func& func)
  99. {
  100. assert(first <= last);
  101. parallel_for(first,last,(Index)1,func);
  102. }
  103. #if defined(TASKING_TBB) && (TBB_INTERFACE_VERSION > 4001)
  104. template<typename Index, typename Func>
  105. __forceinline void parallel_for_static( const Index N, const Func& func)
  106. {
  107. #if TBB_INTERFACE_VERSION >= 12002
  108. tbb::task_group_context context;
  109. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  110. func(i);
  111. },tbb::simple_partitioner(),context);
  112. if (context.is_group_execution_cancelled())
  113. // -- GODOT start --
  114. // throw std::runtime_error("task cancelled");
  115. abort();
  116. // -- GODOT end --
  117. #else
  118. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  119. func(i);
  120. },tbb::simple_partitioner());
  121. if (tbb::task::self().is_cancelled())
  122. // -- GODOT start --
  123. // throw std::runtime_error("task cancelled");
  124. abort();
  125. // -- GODOT end --
  126. #endif
  127. }
  128. typedef tbb::affinity_partitioner affinity_partitioner;
  129. template<typename Index, typename Func>
  130. __forceinline void parallel_for_affinity( const Index N, const Func& func, tbb::affinity_partitioner& ap)
  131. {
  132. #if TBB_INTERFACE_VERSION >= 12002
  133. tbb::task_group_context context;
  134. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  135. func(i);
  136. },ap,context);
  137. if (context.is_group_execution_cancelled())
  138. // -- GODOT start --
  139. // throw std::runtime_error("task cancelled");
  140. abort();
  141. // -- GODOT end --
  142. #else
  143. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  144. func(i);
  145. },ap);
  146. if (tbb::task::self().is_cancelled())
  147. // -- GODOT start --
  148. // throw std::runtime_error("task cancelled");
  149. abort();
  150. // -- GODOT end --
  151. #endif
  152. }
  153. #else
  154. template<typename Index, typename Func>
  155. __forceinline void parallel_for_static( const Index N, const Func& func)
  156. {
  157. parallel_for(N,func);
  158. }
  159. struct affinity_partitioner {
  160. };
  161. template<typename Index, typename Func>
  162. __forceinline void parallel_for_affinity( const Index N, const Func& func, affinity_partitioner& ap)
  163. {
  164. parallel_for(N,func);
  165. }
  166. #endif
  167. }