// Copyright 2009-2020 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "parallel_for.h" namespace embree { template struct ParallelPrefixSumState { enum { MAX_TASKS = 64 }; Value counts[MAX_TASKS]; Value sums [MAX_TASKS]; }; template __forceinline Value parallel_prefix_sum( ParallelPrefixSumState& state, Index first, Index last, Index minStepSize, const Value& identity, const Func& func, const Reduction& reduction) { /* calculate number of tasks to use */ const size_t numThreads = TaskScheduler::threadCount(); const size_t numBlocks = (last-first+minStepSize-1)/minStepSize; const size_t taskCount = min(numThreads,numBlocks,size_t(ParallelPrefixSumState::MAX_TASKS)); /* perform parallel prefix sum */ parallel_for(taskCount, [&](const size_t taskIndex) { const size_t i0 = first+(taskIndex+0)*(last-first)/taskCount; const size_t i1 = first+(taskIndex+1)*(last-first)/taskCount; state.counts[taskIndex] = func(range(i0,i1),state.sums[taskIndex]); }); /* calculate prefix sum */ Value sum=identity; for (size_t i=0; i __forceinline Value parallel_prefix_sum(const SrcArray& src, DstArray& dst, size_t N, const Value& identity, const Add& add, const size_t SINGLE_THREAD_THRESHOLD = 4096) { /* perform single threaded prefix operation for small N */ if (N < SINGLE_THREAD_THRESHOLD) { Value sum=identity; for (size_t i=0; i state; /* initial run just sets up start values for subtasks */ parallel_prefix_sum( state, size_t(0), size_t(N), size_t(1024), identity, [&](const range& r, const Value& sum) -> Value { Value s = identity; for (size_t i=r.begin(); i& r, const Value& sum) -> Value { Value s = identity; for (size_t i=r.begin(); i