Efficient pairwise reduce
- Dominant language
- C++
- Stars
- 3k
- Forks
- 352
- PR merge metrics
- No merged PRs in 30d
Description
I was looking into implementing a pairwise reduce addition to see what kind of results it would give (in terms of precision), but I'm not sure how it should be done efficiently.
I tried the "naive" recursion version, but ISPC seems to have difficulties inlining it:
https://godbolt.org/z/s9jSBn
```c
#define Scalar float
inline uniform Scalar reduce_add_pairwise(const varying Scalar& values, uniform int first, uniform int last)
{
if (first + 1 == last)
return extract(values, first) + extract(values, last);
const uniform int middle = (last+first) / 2;
return reduce_add_pairwise(values, first, middle) + reduce_add_pairwise(values, middle + 1, last);
}
uniform Scalar reduce_add_pairwise(varying Scalar values)
{
return reduce_add_pairwise(values, 0, programCount - 1);
}
```
For testing purposes I also tried doing it manually based on the programCount, but this might not work on all platforms and doesn't seem future proof ?
```c
uniform Scalar reduce_add_pairwise_manual(varying Scalar values)
{
if (programCount == 4)
return (extract(values, 0) + extract(values, 1)) + (extract(values, 2) + extract(values, 3));
if (programCount == 8)
return ((extract(values, 0) + extract(values, 1)) + (extract(values, 2) + extract(values, 3)))
+((extract(values, 4) + extract(values, 5)) + (extract(values, 6) + extract(values, 7)));
assert(false);
}
```
What would be the suggested/preferred way to do it ?
Contributor guide
Assessment
This issue has not been assessed yet.