Path: blob/master/thirdparty/embree/common/algorithms/parallel_reduce.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "parallel_for.h"67namespace embree8{9template<typename Index, typename Value, typename Func, typename Reduction>10__forceinline Value sequential_reduce( const Index first, const Index last, const Value& identity, const Func& func, const Reduction& reduction )11{12return func(range<Index>(first,last));13}1415template<typename Index, typename Value, typename Func, typename Reduction>16__forceinline Value sequential_reduce( const Index first, const Index last, const Index minStepSize, const Value& identity, const Func& func, const Reduction& reduction )17{18return func(range<Index>(first,last));19}2021template<typename Index, typename Value, typename Func, typename Reduction>22__noinline Value parallel_reduce_internal( Index taskCount, const Index first, const Index last, const Index minStepSize, const Value& identity, const Func& func, const Reduction& reduction )23{24const Index maxTasks = 512;25const Index threadCount = (Index) TaskScheduler::threadCount();26taskCount = min(taskCount,threadCount,maxTasks);2728/* parallel invocation of all tasks */29dynamic_large_stack_array(Value,values,taskCount,8192); // consumes at most 8192 bytes on the stack30parallel_for(taskCount, [&](const Index taskIndex) {31const Index k0 = first+(taskIndex+0)*(last-first)/taskCount;32const Index k1 = first+(taskIndex+1)*(last-first)/taskCount;33values[taskIndex] = func(range<Index>(k0,k1));34});3536/* perform reduction over all tasks */37Value v = identity;38for (Index i=0; i<taskCount; i++) v = reduction(v,values[i]);39return v;40}4142template<typename Index, typename Value, typename Func, typename Reduction>43__forceinline Value parallel_reduce( const Index first, const Index last, const Index minStepSize, const Value& identity, const Func& func, const Reduction& reduction )44{45#if defined(TASKING_INTERNAL) && !defined(TASKING_TBB)4647/* fast path for small number of iterations */48Index taskCount = (last-first+minStepSize-1)/minStepSize;49if (likely(taskCount == 1)) {50return func(range<Index>(first,last));51}52return parallel_reduce_internal(taskCount,first,last,minStepSize,identity,func,reduction);5354#elif defined(TASKING_TBB)55#if TBB_INTERFACE_VERSION >= 1200256tbb::task_group_context context;57const Value v = tbb::parallel_reduce(tbb::blocked_range<Index>(first,last,minStepSize),identity,58[&](const tbb::blocked_range<Index>& r, const Value& start) { return reduction(start,func(range<Index>(r.begin(),r.end()))); },59reduction,context);60//if (context.is_group_execution_cancelled())61// throw std::runtime_error("task cancelled");62return v;63#else64const Value v = tbb::parallel_reduce(tbb::blocked_range<Index>(first,last,minStepSize),identity,65[&](const tbb::blocked_range<Index>& r, const Value& start) { return reduction(start,func(range<Index>(r.begin(),r.end()))); },66reduction);67//if (tbb::task::self().is_cancelled())68// throw std::runtime_error("task cancelled");69return v;70#endif71#else // TASKING_PPL72struct AlignedValue73{74char storage[__alignof(Value)+sizeof(Value)];75static uintptr_t alignUp(uintptr_t p, size_t a) { return p + (~(p - 1) % a); };76Value* getValuePtr() { return reinterpret_cast<Value*>(alignUp(uintptr_t(storage), __alignof(Value))); }77const Value* getValuePtr() const { return reinterpret_cast<Value*>(alignUp(uintptr_t(storage), __alignof(Value))); }78AlignedValue(const Value& v) { new(getValuePtr()) Value(v); }79AlignedValue(const AlignedValue& v) { new(getValuePtr()) Value(*v.getValuePtr()); }80AlignedValue(const AlignedValue&& v) { new(getValuePtr()) Value(*v.getValuePtr()); };81AlignedValue& operator = (const AlignedValue& v) { *getValuePtr() = *v.getValuePtr(); return *this; };82AlignedValue& operator = (const AlignedValue&& v) { *getValuePtr() = *v.getValuePtr(); return *this; };83operator Value() const { return *getValuePtr(); }84};8586struct Iterator_Index87{88Index v;89typedef std::forward_iterator_tag iterator_category;90typedef AlignedValue value_type;91typedef Index difference_type;92typedef Index distance_type;93typedef AlignedValue* pointer;94typedef AlignedValue& reference;95__forceinline Iterator_Index() {}96__forceinline Iterator_Index(Index v) : v(v) {}97__forceinline bool operator== (Iterator_Index other) { return v == other.v; }98__forceinline bool operator!= (Iterator_Index other) { return v != other.v; }99__forceinline Iterator_Index operator++() { return Iterator_Index(++v); }100__forceinline Iterator_Index operator++(int) { return Iterator_Index(v++); }101};102103auto range_reduction = [&](Iterator_Index begin, Iterator_Index end, const AlignedValue& start) {104assert(begin.v < end.v);105return reduction(start, func(range<Index>(begin.v, end.v)));106};107const Value v = concurrency::parallel_reduce(Iterator_Index(first), Iterator_Index(last), AlignedValue(identity), range_reduction, reduction);108return v;109#endif110}111112template<typename Index, typename Value, typename Func, typename Reduction>113__forceinline Value parallel_reduce( const Index first, const Index last, const Index minStepSize, const Index parallel_threshold, const Value& identity, const Func& func, const Reduction& reduction )114{115if (likely(last-first < parallel_threshold)) {116return func(range<Index>(first,last));117} else {118return parallel_reduce(first,last,minStepSize,identity,func,reduction);119}120}121122template<typename Index, typename Value, typename Func, typename Reduction>123__forceinline Value parallel_reduce( const range<Index> range, const Index minStepSize, const Index parallel_threshold, const Value& identity, const Func& func, const Reduction& reduction )124{125return parallel_reduce(range.begin(),range.end(),minStepSize,parallel_threshold,identity,func,reduction);126}127128template<typename Index, typename Value, typename Func, typename Reduction>129__forceinline Value parallel_reduce( const Index first, const Index last, const Value& identity, const Func& func, const Reduction& reduction )130{131auto funcr = [&] ( const range<Index> r ) {132Value v = identity;133for (Index i=r.begin(); i<r.end(); i++)134v = reduction(v,func(i));135return v;136};137return parallel_reduce(first,last,Index(1),identity,funcr,reduction);138}139140template<typename Index, typename Value, typename Func, typename Reduction>141__forceinline Value parallel_reduce( const range<Index> range, const Value& identity, const Func& func, const Reduction& reduction )142{143return parallel_reduce(range.begin(),range.end(),Index(1),identity,func,reduction);144}145}146147148