"...resnet50_tensorflow.git" did not exist on "b4aa41f5f86c81b31f1e4f8255c6b1d719db3b4b"
functional3.hpp 3.18 KB
Newer Older
1
2
3
#ifndef CK_FUNCTIONAL3_HPP
#define CK_FUNCTIONAL3_HPP

Chao Liu's avatar
Chao Liu committed
4
5
6
7
#include "functional.hpp"
#include "functional2.hpp"
#include "Sequence.hpp"
#include "Array.hpp"
Chao Liu's avatar
Chao Liu committed
8

9
10
namespace ck {

Chao Liu's avatar
Chao Liu committed
11
12
13
14
15
16
17
// RemainLengths: Sequence<...>
template <class RemainLengths>
struct static_ford_impl
{
    // F signature: F(Sequence<...> multi_id)
    // CurrentMultiIndex: Sequence<...>
    template <class F, class CurrentMultiIndex>
Chao Liu's avatar
Chao Liu committed
18
    __host__ __device__ constexpr void operator()(F f, CurrentMultiIndex) const
Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    {
        static_assert(RemainLengths::GetSize() > 0, "wrong! should not get here");

        static_for<0, RemainLengths::Front(), 1>{}([=](auto I) {
            static_ford_impl<decltype(RemainLengths::PopFront())>{}(f,
                                                                    CurrentMultiIndex::PushBack(I));
        });
    }
};

template <>
struct static_ford_impl<Sequence<>>
{
    // F signature: F(Sequence<...> multi_id)
    // CurrentMultiIndex: Sequence<...>
    template <class F, class CurrentMultiIndex>
Chao Liu's avatar
Chao Liu committed
35
    __host__ __device__ constexpr void operator()(F f, CurrentMultiIndex) const
Chao Liu's avatar
Chao Liu committed
36
37
38
39
40
41
42
43
44
45
46
    {
        f(CurrentMultiIndex{});
    }
};

// Lengths is Sequence<...>
template <class Lengths>
struct static_ford
{
    // F signature: F(Sequence<...> multi_id)
    template <class F>
Chao Liu's avatar
Chao Liu committed
47
    __host__ __device__ constexpr void operator()(F f) const
Chao Liu's avatar
Chao Liu committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    {
        static_assert(Lengths::GetSize() > 0, "wrong! Lengths is empty");

        static_ford_impl<Lengths>{}(f, Sequence<>{});
    }
};

template <index_t RemainDim>
struct ford_impl
{
    // F signature: F(Array<...> multi_id)
    // CurrentMultiIndex: Array<...>
    // RemainLengths: Sequence<...>
    template <class F, class CurrentMultiIndex, class RemainLengths>
Chao Liu's avatar
Chao Liu committed
62
    __host__ __device__ constexpr void
Chao Liu's avatar
Chao Liu committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    operator()(F f, CurrentMultiIndex current_multi_id, RemainLengths) const
    {
        static_assert(RemainLengths::GetSize() == RemainDim, "wrong!");
        static_assert(RemainDim > 1, "wrong!");

        constexpr auto next_length = RemainLengths{}.Front();

        for(index_t i = 0; i < next_length; ++i)
        {
            ford_impl<RemainDim - 1>{}(f, current_multi_id.PushBack(i), RemainLengths{}.PopFront());
        }
    }
};

template <>
struct ford_impl<1>
{
    // F signature: F(Array<...> multi_id)
    // CurrentMultiIndex: Array<...>
    // RemainLengths: Sequence<...>
    template <class F, class CurrentMultiIndex, class RemainLengths>
Chao Liu's avatar
Chao Liu committed
84
    __host__ __device__ constexpr void
Chao Liu's avatar
Chao Liu committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    operator()(F f, CurrentMultiIndex current_multi_id, RemainLengths) const
    {
        static_assert(RemainLengths::GetSize() == 1, "wrong!");

        constexpr index_t last_length = RemainLengths{}.Front();

        for(index_t i = 0; i < last_length; ++i)
        {
            f(current_multi_id.PushBack(i));
        }
    }
};

// Lengths is Sequence<...>
template <class Lengths>
struct ford
{
    // F signature: F(Array<...> multi_id)
    template <class F>
Chao Liu's avatar
Chao Liu committed
104
    __host__ __device__ constexpr void operator()(F f) const
Chao Liu's avatar
Chao Liu committed
105
106
107
108
109
110
111
112
113
    {
        constexpr index_t first_length = Lengths{}.Front();

        for(index_t i = 0; i < first_length; ++i)
        {
            ford_impl<Lengths::GetSize() - 1>{}(f, Array<index_t, 1>{i}, Lengths{}.PopFront());
        }
    }
};
114
115
116

} // namespace ck
#endif