jeudi 14 février 2019

Variadic template for taking multiple containers references

I want to have a function taking multiple references to containers and returning the element-wise combination of all of them. Since this operation is performed in an extremely hot loop I would like to be able to unroll as many operations as possible statically without writing 5 instances of basically the same function.

The algorithm I am performing basically behaves as

const auto result = s0 + a1 * s1 + a2 * s2 + ...

Where all of the si are containers containing all the same number of elements. The number of elements to sum over is known at compile-time.

The function I am looking for should behave as: (hypothetically)

inline Container sum(const Container& s0, double a1, const Container& s2, ....){
    auto result = Container(s0);
    for (int i = 0; i < result.size(); ++i)
        result[i] += a1 * s1[i] + a2 * s2[i] + ...;
    return result;
}

For performance reasons it is not desirable to write some inner loop with runtime bounds checks. Also when trying to use runtime bounds I encountered the issue of not being easily able to pass a variable number of references to the function, should I just resort to pointers in that case.

All code needs to be valid C++11, I do not have access to a more modern compiler in this project.

Aucun commentaire:

Enregistrer un commentaire