parallel_for_for.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "parallel_for.h"
  5. namespace embree
  6. {
  7. template<typename ArrayArray, typename Func>
  8. __forceinline void sequential_for_for( ArrayArray& array2, const size_t minStepSize, const Func& func )
  9. {
  10. size_t k=0;
  11. for (size_t i=0; i!=array2.size(); ++i) {
  12. const size_t N = array2[i]->size();
  13. if (N) func(array2[i],range<size_t>(0,N),k);
  14. k+=N;
  15. }
  16. }
  17. class ParallelForForState
  18. {
  19. public:
  20. enum { MAX_TASKS = 64 };
  21. __forceinline ParallelForForState ()
  22. : taskCount(0) {}
  23. template<typename ArrayArray>
  24. __forceinline ParallelForForState (ArrayArray& array2, const size_t minStepSize) {
  25. init(array2,minStepSize);
  26. }
  27. template<typename ArrayArray>
  28. __forceinline void init ( ArrayArray& array2, const size_t minStepSize )
  29. {
  30. /* first calculate total number of elements */
  31. size_t N = 0;
  32. for (size_t i=0; i<array2.size(); i++) {
  33. N += array2[i] ? array2[i]->size() : 0;
  34. }
  35. this->N = N;
  36. /* calculate number of tasks to use */
  37. const size_t numThreads = TaskScheduler::threadCount();
  38. const size_t numBlocks = (N+minStepSize-1)/minStepSize;
  39. taskCount = max(size_t(1),min(numThreads,numBlocks,size_t(ParallelForForState::MAX_TASKS)));
  40. /* calculate start (i,j) for each task */
  41. size_t taskIndex = 0;
  42. i0[taskIndex] = 0;
  43. j0[taskIndex] = 0;
  44. size_t k0 = (++taskIndex)*N/taskCount;
  45. for (size_t i=0, k=0; taskIndex < taskCount; i++)
  46. {
  47. assert(i<array2.size());
  48. size_t j=0, M = array2[i] ? array2[i]->size() : 0;
  49. while (j<M && k+M-j >= k0 && taskIndex < taskCount) {
  50. assert(taskIndex<taskCount);
  51. i0[taskIndex] = i;
  52. j0[taskIndex] = j += k0-k;
  53. k=k0;
  54. k0 = (++taskIndex)*N/taskCount;
  55. }
  56. k+=M-j;
  57. }
  58. }
  59. __forceinline size_t size() const {
  60. return N;
  61. }
  62. public:
  63. size_t i0[MAX_TASKS];
  64. size_t j0[MAX_TASKS];
  65. size_t taskCount;
  66. size_t N;
  67. };
  68. template<typename ArrayArray, typename Func>
  69. __forceinline void parallel_for_for( ArrayArray& array2, const size_t minStepSize, const Func& func )
  70. {
  71. ParallelForForState state(array2,minStepSize);
  72. parallel_for(state.taskCount, [&](const size_t taskIndex)
  73. {
  74. /* calculate range */
  75. const size_t k0 = (taskIndex+0)*state.size()/state.taskCount;
  76. const size_t k1 = (taskIndex+1)*state.size()/state.taskCount;
  77. size_t i0 = state.i0[taskIndex];
  78. size_t j0 = state.j0[taskIndex];
  79. /* iterate over arrays */
  80. size_t k=k0;
  81. for (size_t i=i0; k<k1; i++) {
  82. const size_t N = array2[i] ? array2[i]->size() : 0;
  83. const size_t r0 = j0, r1 = min(N,r0+k1-k);
  84. if (r1 > r0) func(array2[i],range<size_t>(r0,r1),k);
  85. k+=r1-r0; j0 = 0;
  86. }
  87. });
  88. }
  89. template<typename ArrayArray, typename Func>
  90. __forceinline void parallel_for_for( ArrayArray& array2, const Func& func )
  91. {
  92. parallel_for_for(array2,1,func);
  93. }
  94. template<typename ArrayArray, typename Value, typename Func, typename Reduction>
  95. __forceinline Value parallel_for_for_reduce( ArrayArray& array2, const size_t minStepSize, const Value& identity, const Func& func, const Reduction& reduction )
  96. {
  97. ParallelForForState state(array2,minStepSize);
  98. Value temp[ParallelForForState::MAX_TASKS];
  99. for (size_t i=0; i<state.taskCount; i++)
  100. temp[i] = identity;
  101. parallel_for(state.taskCount, [&](const size_t taskIndex)
  102. {
  103. /* calculate range */
  104. const size_t k0 = (taskIndex+0)*state.size()/state.taskCount;
  105. const size_t k1 = (taskIndex+1)*state.size()/state.taskCount;
  106. size_t i0 = state.i0[taskIndex];
  107. size_t j0 = state.j0[taskIndex];
  108. /* iterate over arrays */
  109. size_t k=k0;
  110. for (size_t i=i0; k<k1; i++) {
  111. const size_t N = array2[i] ? array2[i]->size() : 0;
  112. const size_t r0 = j0, r1 = min(N,r0+k1-k);
  113. if (r1 > r0) temp[taskIndex] = reduction(temp[taskIndex],func(array2[i],range<size_t>(r0,r1),k));
  114. k+=r1-r0; j0 = 0;
  115. }
  116. });
  117. Value ret = identity;
  118. for (size_t i=0; i<state.taskCount; i++)
  119. ret = reduction(ret,temp[i]);
  120. return ret;
  121. }
  122. template<typename ArrayArray, typename Value, typename Func, typename Reduction>
  123. __forceinline Value parallel_for_for_reduce( ArrayArray& array2, const Value& identity, const Func& func, const Reduction& reduction)
  124. {
  125. return parallel_for_for_reduce(array2,1,identity,func,reduction);
  126. }
  127. }