"example/CMakeLists.txt" did not exist on "6014185ac65e75f2a84cb67ef6ba83b48ae0fcb3"
tensor.hpp 8.77 KB
Newer Older
1
2
3
#ifndef CK_TENSOR_HPP
#define CK_TENSOR_HPP

Chao Liu's avatar
Chao Liu committed
4
5
6
#include <thread>
#include <vector>
#include <numeric>
Chao Liu's avatar
Chao Liu committed
7
#include <algorithm>
Chao Liu's avatar
Chao Liu committed
8
#include <utility>
Chao Liu's avatar
Chao Liu committed
9
10
#include <cassert>
#include <iostream>
Chao Liu's avatar
Chao Liu committed
11
12
#include "config.hpp"
#include "common_header.hpp"
Chao Liu's avatar
Chao Liu committed
13

Chao Liu's avatar
Chao Liu committed
14
template <class Range>
Chao Liu's avatar
Chao Liu committed
15
std::ostream& LogRange(std::ostream& os, Range&& range, std::string delim)
Chao Liu's avatar
Chao Liu committed
16
17
{
    bool first = true;
Chao Liu's avatar
Chao Liu committed
18
    for(auto&& v : range)
Chao Liu's avatar
Chao Liu committed
19
20
21
22
23
    {
        if(first)
            first = false;
        else
            os << delim;
Chao Liu's avatar
Chao Liu committed
24
        os << v;
Chao Liu's avatar
Chao Liu committed
25
26
27
28
    }
    return os;
}

Chao Liu's avatar
Chao Liu committed
29
typedef enum {
Chao Liu's avatar
Chao Liu committed
30
31
32
33
34
35
36
37
38
39
40
41
    Half  = 0,
    Float = 1,
} DataType_t;

template <class T>
struct DataType;

template <>
struct DataType<float> : std::integral_constant<DataType_t, DataType_t::Float>
{
};

Chao Liu's avatar
Chao Liu committed
42
43
44
45
46
47
48
49
50
template <class F, class T, std::size_t... Is>
auto call_f_unpack_args_impl(F f, T args, std::index_sequence<Is...>)
{
    return f(std::get<Is>(args)...);
}

template <class F, class T>
auto call_f_unpack_args(F f, T args)
{
Chao Liu's avatar
Chao Liu committed
51
    constexpr std::size_t N = std::tuple_size<T>{};
Chao Liu's avatar
Chao Liu committed
52
53
54
55
56
57
58
59
60
61
62
63
64

    return call_f_unpack_args_impl(f, args, std::make_index_sequence<N>{});
}

template <class F, class T, std::size_t... Is>
auto construct_f_unpack_args_impl(T args, std::index_sequence<Is...>)
{
    return F(std::get<Is>(args)...);
}

template <class F, class T>
auto construct_f_unpack_args(F, T args)
{
Chao Liu's avatar
Chao Liu committed
65
    constexpr std::size_t N = std::tuple_size<T>{};
Chao Liu's avatar
Chao Liu committed
66
67
68
69

    return construct_f_unpack_args_impl<F>(args, std::make_index_sequence<N>{});
}

Chao Liu's avatar
Chao Liu committed
70
71
72
struct TensorDescriptor
{
    TensorDescriptor() = delete;
Chao Liu's avatar
Chao Liu committed
73
74
    TensorDescriptor(std::initializer_list<std::size_t> lens);
    TensorDescriptor(std::initializer_list<std::size_t> lens,
Chao Liu's avatar
Chao Liu committed
75
                     std::initializer_list<std::size_t> strides);
Chao Liu's avatar
Chao Liu committed
76
    TensorDescriptor(std::vector<std::size_t> lens, std::vector<std::size_t> strides);
Chao Liu's avatar
Chao Liu committed
77
78
79
80

    void CalculateStrides();

    template <class Range>
Chao Liu's avatar
Chao Liu committed
81
    TensorDescriptor(const Range& lens) : mLens(lens.begin(), lens.end())
Chao Liu's avatar
Chao Liu committed
82
83
84
85
    {
        this->CalculateStrides();
    }

Chao Liu's avatar
Chao Liu committed
86
    template <class Range1, class Range2>
Chao Liu's avatar
Chao Liu committed
87
88
    TensorDescriptor(const Range1& lens, const Range2& strides)
        : mLens(lens.begin(), lens.end()), mStrides(strides.begin(), strides.end())
Chao Liu's avatar
Chao Liu committed
89
90
    {
    }
Chao Liu's avatar
Chao Liu committed
91

Chao Liu's avatar
Chao Liu committed
92
    std::size_t GetNumOfDimension() const;
Chao Liu's avatar
Chao Liu committed
93
94
95
    std::size_t GetElementSize() const;
    std::size_t GetElementSpace() const;

Chao Liu's avatar
Chao Liu committed
96
97
98
    const std::vector<std::size_t>& GetLengths() const;
    const std::vector<std::size_t>& GetStrides() const;

Chao Liu's avatar
Chao Liu committed
99
    template <class... Is>
100
    std::size_t GetOffsetFromMultiIndex(Is... is) const
Chao Liu's avatar
Chao Liu committed
101
    {
Chao Liu's avatar
Chao Liu committed
102
        assert(sizeof...(Is) == this->GetNumOfDimension());
Chao Liu's avatar
Chao Liu committed
103
104
        std::initializer_list<std::size_t> iss{static_cast<std::size_t>(is)...};
        return std::inner_product(iss.begin(), iss.end(), mStrides.begin(), std::size_t{0});
Chao Liu's avatar
Chao Liu committed
105
106
107
108
109
110
111
    }

    private:
    std::vector<std::size_t> mLens;
    std::vector<std::size_t> mStrides;
};

Chao Liu's avatar
Chao Liu committed
112
struct joinable_thread : std::thread
Chao Liu's avatar
Chao Liu committed
113
{
Chao Liu's avatar
Chao Liu committed
114
115
116
117
    template <class... Xs>
    joinable_thread(Xs&&... xs) : std::thread(std::forward<Xs>(xs)...)
    {
    }
Chao Liu's avatar
Chao Liu committed
118

Chao Liu's avatar
Chao Liu committed
119
120
    joinable_thread(joinable_thread&&) = default;
    joinable_thread& operator=(joinable_thread&&) = default;
Chao Liu's avatar
Chao Liu committed
121

Chao Liu's avatar
Chao Liu committed
122
123
124
125
126
127
    ~joinable_thread()
    {
        if(this->joinable())
            this->join();
    }
};
Chao Liu's avatar
Chao Liu committed
128
129
130
131
132

template <class F, class... Xs>
struct ParallelTensorFunctor
{
    F mF;
Chao Liu's avatar
Chao Liu committed
133
    static constexpr std::size_t NDIM = sizeof...(Xs);
Chao Liu's avatar
Chao Liu committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    std::array<std::size_t, NDIM> mLens;
    std::array<std::size_t, NDIM> mStrides;
    std::size_t mN1d;

    ParallelTensorFunctor(F f, Xs... xs) : mF(f), mLens({static_cast<std::size_t>(xs)...})
    {
        mStrides.back() = 1;
        std::partial_sum(mLens.rbegin(),
                         mLens.rend() - 1,
                         mStrides.rbegin() + 1,
                         std::multiplies<std::size_t>());
        mN1d = mStrides[0] * mLens[0];
    }

Chao Liu's avatar
Chao Liu committed
148
149
150
151
152
153
154
155
156
157
158
159
160
    std::array<std::size_t, NDIM> GetNdIndices(std::size_t i) const
    {
        std::array<std::size_t, NDIM> indices;

        for(int idim = 0; idim < NDIM; ++idim)
        {
            indices[idim] = i / mStrides[idim];
            i -= indices[idim] * mStrides[idim];
        }

        return indices;
    }

Chao Liu's avatar
Chao Liu committed
161
    void operator()(std::size_t num_thread) const
Chao Liu's avatar
Chao Liu committed
162
163
164
165
166
167
168
169
    {
        std::size_t work_per_thread = (mN1d + num_thread - 1) / num_thread;

        std::vector<joinable_thread> threads(num_thread);

        for(std::size_t it = 0; it < num_thread; ++it)
        {
            std::size_t iw_begin = it * work_per_thread;
Chao Liu's avatar
Chao Liu committed
170
            std::size_t iw_end   = std::min((it + 1) * work_per_thread, mN1d);
Chao Liu's avatar
Chao Liu committed
171
172
173

            auto f = [=] {
                for(std::size_t iw = iw_begin; iw < iw_end; ++iw)
Chao Liu's avatar
Chao Liu committed
174
175
176
                {
                    call_f_unpack_args(mF, GetNdIndices(iw));
                }
Chao Liu's avatar
Chao Liu committed
177
178
179
180
181
182
            };
            threads[it] = joinable_thread(f);
        }
    }
};

Chao Liu's avatar
Chao Liu committed
183
184
template <class F, class... Xs>
auto make_ParallelTensorFunctor(F f, Xs... xs)
Chao Liu's avatar
Chao Liu committed
185
{
Chao Liu's avatar
Chao Liu committed
186
    return ParallelTensorFunctor<F, Xs...>(f, xs...);
Chao Liu's avatar
Chao Liu committed
187
188
}

Chao Liu's avatar
Chao Liu committed
189
190
template <class T>
struct Tensor
Chao Liu's avatar
Chao Liu committed
191
{
Chao Liu's avatar
Chao Liu committed
192
    template <class X>
Chao Liu's avatar
Chao Liu committed
193
    Tensor(std::initializer_list<X> lens) : mDesc(lens), mData(mDesc.GetElementSpace())
Chao Liu's avatar
Chao Liu committed
194
195
    {
    }
Chao Liu's avatar
Chao Liu committed
196

Chao Liu's avatar
Chao Liu committed
197
    template <class X>
Chao Liu's avatar
Chao Liu committed
198
    Tensor(std::vector<X> lens) : mDesc(lens), mData(mDesc.GetElementSpace())
Chao Liu's avatar
Chao Liu committed
199
200
    {
    }
Chao Liu's avatar
Chao Liu committed
201

Chao Liu's avatar
Chao Liu committed
202
203
    template <class X, class Y>
    Tensor(std::vector<X> lens, std::vector<Y> strides)
Chao Liu's avatar
Chao Liu committed
204
        : mDesc(lens, strides), mData(mDesc.GetElementSpace())
Chao Liu's avatar
Chao Liu committed
205
206
    {
    }
Chao Liu's avatar
Chao Liu committed
207

Chao Liu's avatar
Chao Liu committed
208
209
    Tensor(const TensorDescriptor& desc) : mDesc(desc), mData(mDesc.GetElementSpace()) {}

Chao Liu's avatar
Chao Liu committed
210
211
212
    template <class G>
    void GenerateTensorValue(G g, std::size_t num_thread = 1)
    {
Chao Liu's avatar
Chao Liu committed
213
        switch(mDesc.GetNumOfDimension())
Chao Liu's avatar
Chao Liu committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
        {
        case 1:
        {
            auto f = [&](auto i) { (*this)(i) = g(i); };
            make_ParallelTensorFunctor(f, mDesc.GetLengths()[0])(num_thread);
            break;
        }
        case 2:
        {
            auto f = [&](auto i0, auto i1) { (*this)(i0, i1) = g(i0, i1); };
            make_ParallelTensorFunctor(f, mDesc.GetLengths()[0], mDesc.GetLengths()[1])(num_thread);
            break;
        }
        case 3:
        {
            auto f = [&](auto i0, auto i1, auto i2) { (*this)(i0, i1, i2) = g(i0, i1, i2); };
            make_ParallelTensorFunctor(
                f, mDesc.GetLengths()[0], mDesc.GetLengths()[1], mDesc.GetLengths()[2])(num_thread);
            break;
        }
        case 4:
        {
            auto f = [&](auto i0, auto i1, auto i2, auto i3) {
                (*this)(i0, i1, i2, i3) = g(i0, i1, i2, i3);
            };
            make_ParallelTensorFunctor(f,
                                       mDesc.GetLengths()[0],
                                       mDesc.GetLengths()[1],
                                       mDesc.GetLengths()[2],
                                       mDesc.GetLengths()[3])(num_thread);
            break;
        }
        default: throw std::runtime_error("unspported dimension");
        }
    }

    template <class... Is>
    T& operator()(Is... is)
    {
253
        return mData[mDesc.GetOffsetFromMultiIndex(is...)];
Chao Liu's avatar
Chao Liu committed
254
255
256
257
258
    }

    template <class... Is>
    const T& operator()(Is... is) const
    {
259
        return mData[mDesc.GetOffsetFromMultiIndex(is...)];
Chao Liu's avatar
Chao Liu committed
260
261
262
263
264
265
266
267
268
269
270
271
272
    }

    typename std::vector<T>::iterator begin() { return mData.begin(); }

    typename std::vector<T>::iterator end() { return mData.end(); }

    typename std::vector<T>::const_iterator begin() const { return mData.begin(); }

    typename std::vector<T>::const_iterator end() const { return mData.end(); }

    TensorDescriptor mDesc;
    std::vector<T> mData;
};
273

Chao Liu's avatar
Chao Liu committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// this is ugly, only for 4d
template <class TConstTensorDesc>
void ostream_ConstantTensorDescriptor(TConstTensorDesc, std::ostream& os = std::cout)
{
    using namespace ck;

    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

    constexpr auto I0   = Number<0>{};
    constexpr auto I1   = Number<1>{};
    constexpr auto I2   = Number<2>{};
    constexpr auto I3   = Number<3>{};
    constexpr auto desc = TConstTensorDesc{};

    os << "Lengths: {" << desc.GetLength(I0) << ", " << desc.GetLength(I1) << ", "
       << desc.GetLength(I2) << ", " << desc.GetLength(I3) << "}, "
       << "Strides: {" << desc.GetStride(I0) << ", " << desc.GetStride(I1) << ", "
       << desc.GetStride(I2) << ", " << desc.GetStride(I3) << "}" << std::endl;
}

// this is ugly, only for 4d
template <class TConstTensorDesc>
auto make_TensorDescriptor(TConstTensorDesc)
{
    using namespace ck;

    static_assert(TConstTensorDesc::nDim == 4, "nDim is not 4");

    constexpr auto I0   = Number<0>{};
    constexpr auto I1   = Number<1>{};
    constexpr auto I2   = Number<2>{};
    constexpr auto I3   = Number<3>{};
    constexpr auto desc = TConstTensorDesc{};

    std::initializer_list<index_t> lengths = {
        desc.GetLength(I0), desc.GetLength(I1), desc.GetLength(I2), desc.GetLength(I3)};
    std::initializer_list<index_t> strides = {
        desc.GetStride(I0), desc.GetStride(I1), desc.GetStride(I2), desc.GetStride(I3)};

    return TensorDescriptor(lengths, strides);
}

316
#endif