parallel_for.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "../common/tasking/taskscheduler.h"
  18. #include "../sys/array.h"
  19. #include "../math/math.h"
  20. #include "../common/math/range.h"
  21. namespace embree
  22. {
  23. /* parallel_for without range */
  24. template<typename Index, typename Func>
  25. __forceinline void parallel_for( const Index N, const Func& func)
  26. {
  27. #if defined(TASKING_INTERNAL)
  28. if (N) {
  29. TaskScheduler::spawn(Index(0),N,Index(1),[&] (const range<Index>& r) {
  30. assert(r.size() == 1);
  31. func(r.begin());
  32. });
  33. if (!TaskScheduler::wait())
  34. throw std::runtime_error("task cancelled");
  35. }
  36. #elif defined(TASKING_TBB)
  37. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  38. func(i);
  39. });
  40. if (tbb::task::self().is_cancelled())
  41. throw std::runtime_error("task cancelled");
  42. #elif defined(TASKING_PPL)
  43. concurrency::parallel_for(Index(0),N,Index(1),[&](Index i) {
  44. func(i);
  45. });
  46. #else
  47. # error "no tasking system enabled"
  48. #endif
  49. }
  50. /* parallel for with range and granulatity */
  51. template<typename Index, typename Func>
  52. __forceinline void parallel_for( const Index first, const Index last, const Index minStepSize, const Func& func)
  53. {
  54. assert(first <= last);
  55. #if defined(TASKING_INTERNAL)
  56. TaskScheduler::spawn(first,last,minStepSize,func);
  57. if (!TaskScheduler::wait())
  58. throw std::runtime_error("task cancelled");
  59. #elif defined(TASKING_TBB)
  60. tbb::parallel_for(tbb::blocked_range<Index>(first,last,minStepSize),[&](const tbb::blocked_range<Index>& r) {
  61. func(range<Index>(r.begin(),r.end()));
  62. });
  63. if (tbb::task::self().is_cancelled())
  64. throw std::runtime_error("task cancelled");
  65. #elif defined(TASKING_PPL)
  66. concurrency::parallel_for(first, last, Index(1) /*minStepSize*/, [&](Index i) {
  67. func(range<Index>(i,i+1));
  68. });
  69. #else
  70. # error "no tasking system enabled"
  71. #endif
  72. }
  73. /* parallel for with range */
  74. template<typename Index, typename Func>
  75. __forceinline void parallel_for( const Index first, const Index last, const Func& func)
  76. {
  77. assert(first <= last);
  78. parallel_for(first,last,(Index)1,func);
  79. }
  80. #if defined(TASKING_TBB) && (TBB_INTERFACE_VERSION > 4001)
  81. template<typename Index, typename Func>
  82. __forceinline void parallel_for_static( const Index N, const Func& func)
  83. {
  84. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  85. func(i);
  86. },tbb::simple_partitioner());
  87. if (tbb::task::self().is_cancelled())
  88. throw std::runtime_error("task cancelled");
  89. }
  90. typedef tbb::affinity_partitioner affinity_partitioner;
  91. template<typename Index, typename Func>
  92. __forceinline void parallel_for_affinity( const Index N, const Func& func, tbb::affinity_partitioner& ap)
  93. {
  94. tbb::parallel_for(Index(0),N,Index(1),[&](Index i) {
  95. func(i);
  96. },ap);
  97. if (tbb::task::self().is_cancelled())
  98. throw std::runtime_error("task cancelled");
  99. }
  100. #else
  101. template<typename Index, typename Func>
  102. __forceinline void parallel_for_static( const Index N, const Func& func)
  103. {
  104. parallel_for(N,func);
  105. }
  106. struct affinity_partitioner {
  107. };
  108. template<typename Index, typename Func>
  109. __forceinline void parallel_for_affinity( const Index N, const Func& func, affinity_partitioner& ap)
  110. {
  111. parallel_for(N,func);
  112. }
  113. #endif
  114. }