parallel_for_for.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 SizeFunc>
  28. __forceinline ParallelForForState (const size_t numArrays, const SizeFunc& getSize, const size_t minStepSize) {
  29. init(numArrays,getSize,minStepSize);
  30. }
  31. template<typename SizeFunc>
  32. __forceinline void init ( const size_t numArrays, const SizeFunc& getSize, const size_t minStepSize )
  33. {
  34. /* first calculate total number of elements */
  35. size_t N = 0;
  36. for (size_t i=0; i<numArrays; i++) {
  37. N += getSize(i);
  38. }
  39. this->N = N;
  40. /* calculate number of tasks to use */
  41. const size_t numThreads = TaskScheduler::threadCount();
  42. const size_t numBlocks = (N+minStepSize-1)/minStepSize;
  43. taskCount = max(size_t(1),min(numThreads,numBlocks,size_t(ParallelForForState::MAX_TASKS)));
  44. /* calculate start (i,j) for each task */
  45. size_t taskIndex = 0;
  46. i0[taskIndex] = 0;
  47. j0[taskIndex] = 0;
  48. size_t k0 = (++taskIndex)*N/taskCount;
  49. for (size_t i=0, k=0; taskIndex < taskCount; i++)
  50. {
  51. assert(i<numArrays);
  52. size_t j=0, M = getSize(i);
  53. while (j<M && k+M-j >= k0 && taskIndex < taskCount) {
  54. assert(taskIndex<taskCount);
  55. i0[taskIndex] = i;
  56. j0[taskIndex] = j += k0-k;
  57. k=k0;
  58. k0 = (++taskIndex)*N/taskCount;
  59. }
  60. k+=M-j;
  61. }
  62. }
  63. template<typename ArrayArray>
  64. __forceinline void init ( ArrayArray& array2, const size_t minStepSize )
  65. {
  66. init(array2.size(),[&](size_t i) { return array2[i] ? array2[i]->size() : 0; },minStepSize);
  67. }
  68. __forceinline size_t size() const {
  69. return N;
  70. }
  71. public:
  72. size_t i0[MAX_TASKS];
  73. size_t j0[MAX_TASKS];
  74. size_t taskCount;
  75. size_t N;
  76. };
  77. template<typename ArrayArray, typename Func>
  78. __forceinline void parallel_for_for( ArrayArray& array2, const size_t minStepSize, const Func& func )
  79. {
  80. ParallelForForState state(array2,minStepSize);
  81. parallel_for(state.taskCount, [&](const size_t taskIndex)
  82. {
  83. /* calculate range */
  84. const size_t k0 = (taskIndex+0)*state.size()/state.taskCount;
  85. const size_t k1 = (taskIndex+1)*state.size()/state.taskCount;
  86. size_t i0 = state.i0[taskIndex];
  87. size_t j0 = state.j0[taskIndex];
  88. /* iterate over arrays */
  89. size_t k=k0;
  90. for (size_t i=i0; k<k1; i++) {
  91. const size_t N = array2[i] ? array2[i]->size() : 0;
  92. const size_t r0 = j0, r1 = min(N,r0+k1-k);
  93. if (r1 > r0) func(array2[i],range<size_t>(r0,r1),k);
  94. k+=r1-r0; j0 = 0;
  95. }
  96. });
  97. }
  98. template<typename ArrayArray, typename Func>
  99. __forceinline void parallel_for_for( ArrayArray& array2, const Func& func )
  100. {
  101. parallel_for_for(array2,1,func);
  102. }
  103. template<typename ArrayArray, typename Value, typename Func, typename Reduction>
  104. __forceinline Value parallel_for_for_reduce( ArrayArray& array2, const size_t minStepSize, const Value& identity, const Func& func, const Reduction& reduction )
  105. {
  106. ParallelForForState state(array2,minStepSize);
  107. Value temp[ParallelForForState::MAX_TASKS];
  108. for (size_t i=0; i<state.taskCount; i++)
  109. temp[i] = identity;
  110. parallel_for(state.taskCount, [&](const size_t taskIndex)
  111. {
  112. /* calculate range */
  113. const size_t k0 = (taskIndex+0)*state.size()/state.taskCount;
  114. const size_t k1 = (taskIndex+1)*state.size()/state.taskCount;
  115. size_t i0 = state.i0[taskIndex];
  116. size_t j0 = state.j0[taskIndex];
  117. /* iterate over arrays */
  118. size_t k=k0;
  119. for (size_t i=i0; k<k1; i++) {
  120. const size_t N = array2[i] ? array2[i]->size() : 0;
  121. const size_t r0 = j0, r1 = min(N,r0+k1-k);
  122. if (r1 > r0) temp[taskIndex] = reduction(temp[taskIndex],func(array2[i],range<size_t>(r0,r1),k));
  123. k+=r1-r0; j0 = 0;
  124. }
  125. });
  126. Value ret = identity;
  127. for (size_t i=0; i<state.taskCount; i++)
  128. ret = reduction(ret,temp[i]);
  129. return ret;
  130. }
  131. template<typename ArrayArray, typename Value, typename Func, typename Reduction>
  132. __forceinline Value parallel_for_for_reduce( ArrayArray& array2, const Value& identity, const Func& func, const Reduction& reduction)
  133. {
  134. return parallel_for_for_reduce(array2,1,identity,func,reduction);
  135. }
  136. }