C++模板元编程成员函数循环展开

C++ template meta-programming member function loop unrolling

本文关键字:成员 函数 循环展开 编程 C++      更新时间:2023-10-16

我刚刚开始在我的代码中使用模板元编程。我有一个类,它的成员是多维笛卡尔点的向量。以下是该类的基本设置:

template<size_t N>
class TGrid{
public:
     void round_points_3(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
             Xp[i][1] = min[1] + (std::floor((Xp[i][1] - min[1]) * nbins[1] / (max[1] - min[1])) * bin_w[1]) + bin_w[1]/2.0;
             Xp[i][2] = min[2] + (std::floor((Xp[i][2] - min[2]) * nbins[2] / (max[2] - min[2])) * bin_w[2]) + bin_w[2]/2.0;
         }
    }
    void round_points_2(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
             Xp[i][1] = min[1] + (std::floor((Xp[i][1] - min[1]) * nbins[1] / (max[1] - min[1])) * bin_w[1]) + bin_w[1]/2.0;
         }
    }
    void round_points_1(){
         for(std::size_t i = 0; i < Xp.size();i++){
             Xp[i][0] = min[0] + (std::floor((Xp[i][0] - min[0]) * nbins[0] / (max[0] - min[0])) * bin_w[0]) + bin_w[0]/2.0;
         }
    }
public:
std::vector<std::array<double,N> > Xp;
std::vector<double> min, max, nbins, bin_w;
};

此类表示多维网格。维度由模板值 N 指定。我将有许多操作,通过为特定维度定制模板特定的成员函数(例如循环展开),可以提高效率。

在类 TGrid 中,我有 3 个特定于维度 D=1、D=2 和 D=3 的函数。这由函数的下标 _1、_2 和 _3 指示。

我正在寻找一种面向模板元编程的方法来编写这三个功能更紧凑。

我见过循环展开的示例,但所有这些示例都没有考虑模板类的成员函数。

这是否是一个适当的优化,或者是否应该首先考虑其他优化的问题放在一边,这就是我会这样做的方式。(但我确实同意,有时显式展开循环显然更好 - 编译器并不总是最好的判断者。

不能部分专用化成员函数,也不能在不专用外部结构的情况下专用化嵌套结构,因此唯一的解决方案是为展开机制使用单独的模板化结构。随意将其放在其他命名空间:)

展开实施:

template <int N>
struct sequence {
    template <typename F,typename... Args>
    static void run(F&& f,Args&&... args) {
        sequence<N-1>::run(std::forward<F>(f),std::forward<Args>(args)...);
        f(args...,N-1);
    }
};
template <>
struct sequence<0> {
    template <typename F,typename... Args>
    static void run(F&& f,Args&&... args) {}
};

这需要一个任意函数对象和一个参数列表,然后使用参数和附加的最终参数调用该对象 N 次,其中最后一个参数的范围从 0 到 N-1。通用引用和可变参数模板不是必需的;同样的想法可以在 C++98 中使用,但不太笼统。

然后round_points<K>使用帮助程序静态成员函数调用sequence::run<K>

template <size_t N>
class TGrid {
public:
    template <size_t K>
    void round_points(){
        for (std::size_t i = 0; i < Xp.size();i++) {
            sequence<K>::run(TGrid<N>::round_item,*this,i);
        }
    }
    static void round_item(TGrid &G,int i,int j) {
        G.Xp[i][j] = G.min[j] + (std::floor((G.Xp[i][j] - G.min[j]) * G.nbins[j] / (G.max[j] - G.min[j])) * G.bin_w[j]) + G.bin_w[j]/2.0;
    }
    // ...
};

编辑:附录

编译器似乎很难内联使用指向成员的指针函数执行等效操作。作为替代方案,为了避免使用静态round_item,您可以使用 lambda,例如:

template <size_t N>
class TGrid {
public:
    template <size_t K>
    void round_points(){
        for (std::size_t i = 0; i < Xp.size();i++) {
            sequence<K>::run([&](int j) {round_item(i,j);});
        }
    }
    void round_item(int i,int j) {
        Xp[i][j] = min[j] + (std::floor((Xp[i][j] - min[j]) * nbins[j] / (max[j] - min[j])) * bin_w[j]) + bin_w[j]/2.0;
    }
    // ...
};