如何在编译时生成嵌套循环

How to generate nested loops at compile time

本文关键字:嵌套循环 编译      更新时间:2023-10-16

我有一个整数N,我在编译时知道。我还有一个 std::array 保存描述 N 维数组形状的整数。我想在编译时使用元编程技术在编译时生成嵌套循环,如下所述。

constexpr int N {4};
constexpr std::array<int, N> shape {{1,3,5,2}};

auto f = [/* accept object which uses coords */] (auto... coords) { 
     // do sth with coords
}; 
// This is what I want to generate.
for(int i = 0; i < shape[0]; i++) {
     for(int j = 0; j < shape[1]; j++) {
          for(int k = 0; k < shape[2]; k++) {
                for(int l = 0; l < shape[3]; l++) {
                    f(i,j,k,l) // object is modified via the lambda function.
                }
          }
     }
}

请注意,参数 N 在编译时是已知的,但在编译之间可能会发生不可预测的变化,因此我无法像上面那样对循环进行硬编码。理想情况下,循环生成机制将提供一个接口,该接口接受 lambda 函数,生成循环并调用生成上述等效代码的函数。我知道可以在运行时使用单个 while 循环和索引数组编写等效循环,并且已经有这个问题的答案。但是,我对这个解决方案不感兴趣。我对涉及预处理器魔法的解决方案也不感兴趣。

像这样的东西(注意:我将"形状"作为可变参数模板参数集。

#include <iostream>
template <int I, int ...N>
struct Looper{
    template <typename F, typename ...X>
    constexpr void operator()(F& f, X... x) {
        for (int i = 0; i < I; ++i) {
            Looper<N...>()(f, x..., i);
        }
    }
};
template <int I>
struct Looper<I>{
    template <typename F, typename ...X>
    constexpr void operator()(F& f, X... x) {
        for (int i = 0; i < I; ++i) {
            f(x..., i);
        }
    }
};
int main()
{
    int v = 0;
    auto f = [&](int i, int j, int k, int l) {
        v += i + j + k + l;
    };
    Looper<1, 3, 5, 2>()(f);
    auto g = [&](int i) {
        v += i;
    };
    Looper<5>()(g);
    std::cout << v << std::endl;
}

假设你不希望全循环展开,只需生成fijk等参数元组:

#include <stdio.h>
#include <utility>      // std::integer_sequence
template< int dim >
constexpr auto item_size_at()
    -> int
{ return ::shape[dim + 1]*item_size_at<dim + 1>(); }
template<> constexpr auto item_size_at<::N-1>() -> int { return 1; }
template< size_t... dim >
void call_f( int i, std::index_sequence<dim...> )
{
    f( (i/item_size_at<dim>() % ::shape[dim])... );
}
auto main()
    -> int
{
    int const n_items = ::shape[0]*item_size_at<0>();
    for( int i = 0; i < n_items; ++i )
    {
        call_f( i, std::make_index_sequence<::N>() );
    }
}

我想这正是您所要求的:

#include <array>
#include <iostream>
constexpr int N{4};
constexpr std::array<int, N> shape {{1,3,5,2}};
// Diagnositcs
template<typename V, typename ...Vals>
struct TPrintf {
        constexpr static void call(V v, Vals ...vals) {
                std::cout << v << " ";
                TPrintf<Vals...>::call(vals...);
        }
};
template<typename V>
struct TPrintf<V> {
        constexpr static void call(V v) {
                std::cout << v << std::endl;
        }
};

template<typename ...Vals>
constexpr void t_printf(Vals ...vals) {
        TPrintf<Vals...>::call(vals...);
}
// Unroll
template<int CtIdx, typename F>
struct NestedLoops {
        template<typename ...RtIdx>
        constexpr static void call(const F& f, RtIdx ...idx) {
                for(int i = 0; i < shape[CtIdx]; ++i) {
                        NestedLoops<CtIdx + 1, F>::call(f, idx..., i);
                }
        }
};
template<typename F>
struct NestedLoops<N-1, F> {
        template<typename ...RtIdx>
        constexpr static void call(const F& f, RtIdx ...idx) {
                for(int i = 0; i < shape[N-1]; ++i) {
                        f(idx..., i);
                }
        }
};
template<typename F>
void nested_loops(const F& f) {
        NestedLoops<0, F>::call(f);
}
int main()
{
        auto lf = [](int i, int j, int k, int l) {
                t_printf(i,j,k,l);
        };
        nested_loops(lf);
        return 0;
}

同一事物的另一个变体:

template <size_t shape_index, size_t shape_size>
struct Looper
{
    template <typename Functor>
    void operator()(const std::array<int, shape_size>& shape, Functor functor)
    {
        for (int index = 0; index < shape[shape_index]; ++index)
        {
            Looper<shape_index + 1, shape_size>()
                (
                    shape,
                    [index, &functor](auto... tail){ functor(index, tail...); }
                );
        }
    }
};
template <size_t shape_size>
struct Looper<shape_size, shape_size>
{
    template <typename Functor>
    void operator()(const std::array<int, shape_size>&, Functor functor)
    {
        functor();
    }
};
template <size_t shape_size, typename Functor>
void loop(const std::array<int, shape_size>& shape, Functor functor)
{
    Looper<0, shape_size>()(shape, functor);
}

使用示例:

constexpr size_t N {4};
constexpr std::array<int, N> shape {{1,3,5,2}};
void f(int i, int j, int k, int l)
{
    std::cout
        << std::setw(5) << i
        << std::setw(5) << j
        << std::setw(5) << k
        << std::setw(5) << l
        << std::endl;
}
// ...
loop(shape, f);

现场演示