Skip to content

transform_reduce algorithms - #4523

Open
roystgnr wants to merge 15 commits into
libMesh:develfrom
roystgnr:reduce_algorithms
Open

transform_reduce algorithms#4523
roystgnr wants to merge 15 commits into
libMesh:develfrom
roystgnr:reduce_algorithms

Conversation

@roystgnr

@roystgnr roystgnr commented Aug 16, 2026

Copy link
Copy Markdown
Member

I haven't created benchmarks for these specific changes in libMesh, but in some test code the results were surprisingly awesome.

In theory it's possible for a seq (default) STL reduce to be slower than a hand-coded loop; in practice I've found one case (plain integer accumulation) where g++ slows down by 20% (though clang++ gets it right), and for pretty much everything else the STL is like 50% faster or more, even in sequential mode, if only because using "reduce()" tells the compiler that we don't care about element ordering and so it can do SIMD tricks that treat FP arithmetic as associative.

I'm hoping to eventually get good enough with these algorithms to do GPGPU tricks with nvc++ in new code paths, but even before then we can probably get some great threaded and some decent serial speedup in slightly-updated existing code paths.

Edit: I'm going to restrict this PR to the serial simplification+speedup, especially since it now requires configure tests just to be sure we can use std::transform_reduce at all. Parallelizing C++17 algorithms requires TBB with g++/clang++, so I'll need some more configure tweaking before we turn that on.

Edit 2: My tests on -O2 builds of real work are ranging from 2%-50% faster, and now I see that trying -O3 and/or -march only makes them help a bit more, but with -O0 on tiny kernels I'm seeing ~60% slowdown over hand-coded loops. Our dbg mode is already expected to be quite slow, but if we start using these everywhere it's not going to help.

This simplifies the code a bit in DenseVector, and might optimize it a
bit in DenseMatrix
We'd like to support user types as type T, but user types are supposed
to use ADL for these things, not put things in namespace std.
In theory it's possible for a seq (default) STL reduce to be slower than
a hand-coded loop; in practice I've found one case (plain integer
accumulation) where g++ slows down by 20% (though clang++ gets it
right), and for pretty much everything else the STL is like 50% faster
or more, even in sequential mode, if only because using "reduce()" tells
the compiler that we don't care about element ordering and so it can do
SIMD tricks that treat FP arithmetic as associative.
Otherwise our tests trip one for me when built with -march=native
Comment thread include/numerics/dense_matrix.h Outdated
return my_min;
return std::transform_reduce
(_val.begin(), _val.end(), std::numeric_limits<T>::max(),
[](auto a, auto b){using std::min; return min(a,b);},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do references? in case we end up doing DenseMatrix

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to "Copy Markdown" to see DenseMatrix<ADReal>, but yeah, you're quite right.

Comment thread include/numerics/dense_matrix.h Outdated
return my_max;
return std::transform_reduce
(_val.begin(), _val.end(), std::numeric_limits<T>::lowest(),
[](auto a, auto b){using std::max; return max(a,b);},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@roystgnr

Copy link
Copy Markdown
Member Author

Well, aside from the T=std::complex regression, the test failures are interesting. Looks like g++ 8.5.0 (or more specifically the libstdc++ it's bundled with) doesn't support std::transform_reduce, which I guess is part of the Parallel algorithms and execution policies that didn't make it in until GCC 9. I thought we were requiring C++17 support already but it turns out we're just requiring most of C++17 support.

I'll add a shim as a workaround for now, I think.

@moosebuild

Copy link
Copy Markdown

Job Coverage, step Generate coverage on 33cf8b9 wanted to post the following:

Coverage

9e965b #4523 33cf8b
Total Total +/- New
Rate 65.94% 65.94% +0.00% 100.00%
Hits 79392 79390 -2 21
Misses 41016 41010 -6 0

Diff coverage report

Full coverage report

This comment will be updated on new commits.

@roystgnr roystgnr changed the title Parallelizable reduce algorithms transform_reduce algorithms Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants