| 39 | |
| 40 | template<typename SizeFunc> |
| 41 | __forceinline void init ( const size_t numArrays, const SizeFunc& getSize, const size_t minStepSize ) |
| 42 | { |
| 43 | /* first calculate total number of elements */ |
| 44 | size_t N = 0; |
| 45 | for (size_t i=0; i<numArrays; i++) { |
| 46 | N += getSize(i); |
| 47 | } |
| 48 | this->N = N; |
| 49 | |
| 50 | /* calculate number of tasks to use */ |
| 51 | const size_t numThreads = TaskScheduler::threadCount(); |
| 52 | const size_t numBlocks = (N+minStepSize-1)/minStepSize; |
| 53 | taskCount = max(size_t(1),min(numThreads,numBlocks,size_t(ParallelForForState::MAX_TASKS))); |
| 54 | |
| 55 | /* calculate start (i,j) for each task */ |
| 56 | size_t taskIndex = 0; |
| 57 | i0[taskIndex] = 0; |
| 58 | j0[taskIndex] = 0; |
| 59 | size_t k0 = (++taskIndex)*N/taskCount; |
| 60 | for (size_t i=0, k=0; taskIndex < taskCount; i++) |
| 61 | { |
| 62 | assert(i<numArrays); |
| 63 | size_t j=0, M = getSize(i); |
| 64 | while (j<M && k+M-j >= k0 && taskIndex < taskCount) { |
| 65 | assert(taskIndex<taskCount); |
| 66 | i0[taskIndex] = i; |
| 67 | j0[taskIndex] = j += k0-k; |
| 68 | k=k0; |
| 69 | k0 = (++taskIndex)*N/taskCount; |
| 70 | } |
| 71 | k+=M-j; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | template<typename ArrayArray> |
| 76 | __forceinline void init ( ArrayArray& array2, const size_t minStepSize ) |