parallel_for.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2009-2020 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. throw std::runtime_error("task cancelled");
  22. }
  23. #elif defined(TASKING_TBB)
  24. #if TBB_INTERFACE_VERSION >= 12002
  25. tbb::task_group_context context;
  26. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  27. func(i);
  28. },context);
  29. if (context.is_group_execution_cancelled())
  30. throw std::runtime_error("task cancelled");
  31. #else
  32. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  33. func(i);
  34. });
  35. if (tbb::task::self().is_cancelled())
  36. throw std::runtime_error("task cancelled");
  37. #endif
  38. #elif defined(TASKING_PPL)
  39. concurrency::parallel_for(Index(0),N,Index(1),[&](Index i) {
  40. func(i);
  41. });
  42. #else
  43. # error "no tasking system enabled"
  44. #endif
  45. }
  46. /* parallel for with range and granulatity */
  47. template<typename Index, typename Func>
  48. __forceinline void parallel_for( const Index first, const Index last, const Index minStepSize, const Func& func)
  49. {
  50. assert(first <= last);
  51. #if defined(TASKING_INTERNAL)
  52. TaskScheduler::spawn(first,last,minStepSize,func);
  53. if (!TaskScheduler::wait())
  54. throw std::runtime_error("task cancelled");
  55. #elif defined(TASKING_TBB)
  56. #if TBB_INTERFACE_VERSION >= 12002
  57. tbb::task_group_context context;
  58. tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
  59. func(range<Index>(r.begin(),r.end()));
  60. },context);
  61. if (context.is_group_execution_cancelled())
  62. throw std::runtime_error("task cancelled");
  63. #else
  64. tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
  65. func(range<Index>(r.begin(),r.end()));
  66. });
  67. if (tbb::task::self().is_cancelled())
  68. throw std::runtime_error("task cancelled");
  69. #endif
  70. #elif defined(TASKING_PPL)
  71. concurrency::parallel_for(first, last, Index(1) /*minStepSize*/, [&](Index i) {
  72. func(range<Index>(i,i+1));
  73. });
  74. #else
  75. # error "no tasking system enabled"
  76. #endif
  77. }
  78. /* parallel for with range */
  79. template<typename Index, typename Func>
  80. __forceinline void parallel_for( const Index first, const Index last, const Func& func)
  81. {
  82. assert(first <= last);
  83. parallel_for(first,last,(Index)1,func);
  84. }
  85. #if defined(TASKING_TBB) && (TBB_INTERFACE_VERSION > 4001)
  86. template<typename Index, typename Func>
  87. __forceinline void parallel_for_static( const Index N, const Func& func)
  88. {
  89. #if TBB_INTERFACE_VERSION >= 12002
  90. tbb::task_group_context context;
  91. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  92. func(i);
  93. },tbb::simple_partitioner(),context);
  94. if (context.is_group_execution_cancelled())
  95. throw std::runtime_error("task cancelled");
  96. #else
  97. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  98. func(i);
  99. },tbb::simple_partitioner());
  100. if (tbb::task::self().is_cancelled())
  101. throw std::runtime_error("task cancelled");
  102. #endif
  103. }
  104. typedef tbb::affinity_partitioner affinity_partitioner;
  105. template<typename Index, typename Func>
  106. __forceinline void parallel_for_affinity( const Index N, const Func& func, tbb::affinity_partitioner& ap)
  107. {
  108. #if TBB_INTERFACE_VERSION >= 12002
  109. tbb::task_group_context context;
  110. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  111. func(i);
  112. },ap,context);
  113. if (context.is_group_execution_cancelled())
  114. throw std::runtime_error("task cancelled");
  115. #else
  116. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  117. func(i);
  118. },ap);
  119. if (tbb::task::self().is_cancelled())
  120. throw std::runtime_error("task cancelled");
  121. #endif
  122. }
  123. #else
  124. template<typename Index, typename Func>
  125. __forceinline void parallel_for_static( const Index N, const Func& func)
  126. {
  127. parallel_for(N,func);
  128. }
  129. struct affinity_partitioner {
  130. };
  131. template<typename Index, typename Func>
  132. __forceinline void parallel_for_affinity( const Index N, const Func& func, affinity_partitioner& ap)
  133. {
  134. parallel_for(N,func);
  135. }
  136. #endif
  137. }