Frequently, I have to find Sum( f(i), 1, N ) or Product( f(i), 1, N ), where f(i) is computationally CPU-intensive, while integral i is from sequential range but huge.
Using C++20 compiler I can write function:
uint64_t solution(uint64_t N)
{
std::vector<uint64_t> v(N);
std::iota(v.begin(), v.end(), 1ULL);
return std::transform_reduce(
std::execution::par,
v.cbegin(), v.cend(),
0ull,
std::plus<>(),
[]f(const uint64_t& i)->uint64_t {
uint64_t result(0);
// expensive computation of result=f(i) goes here
// ...
return result;
});
}
But that will be RAM constrained.
How I can completely eliminate intermediate memory operations with input vector in run-time using only C++20 STL (i.e. no vendor specific or 3rd party libraries) and yet have efficient parallel execution?