tensor.hpp 8.74 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
#ifndef TENSOR_HPP
#define TENSOR_HPP
3

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
#include "common_header.hpp"
Chao Liu's avatar
Chao Liu committed
12

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

Chao Liu's avatar
Chao Liu committed
28
typedef enum {
Chao Liu's avatar
Chao Liu committed
29
30
31
32
33
34
35
36
37
38
39
40
    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
41
42
43
44
45
46
47
48
49
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
50
    constexpr std::size_t N = std::tuple_size<T>{};
Chao Liu's avatar
Chao Liu committed
51
52
53
54
55
56
57
58
59
60
61
62
63

    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
64
    constexpr std::size_t N = std::tuple_size<T>{};
Chao Liu's avatar
Chao Liu committed
65
66
67
68

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

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

    void CalculateStrides();

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

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

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

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

Chao Liu's avatar
Chao Liu committed
98
    template <class... Is>
99
    std::size_t GetOffsetFromMultiIndex(Is... is) const
Chao Liu's avatar
Chao Liu committed
100
    {
Chao Liu's avatar
Chao Liu committed
101
        assert(sizeof...(Is) == this->GetNumOfDimension());
Chao Liu's avatar
Chao Liu committed
102
103
        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
104
105
106
107
108
109
110
    }

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

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

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

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

template <class F, class... Xs>
struct ParallelTensorFunctor
{
    F mF;
Chao Liu's avatar
Chao Liu committed
132
    static constexpr std::size_t NDIM = sizeof...(Xs);
Chao Liu's avatar
Chao Liu committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    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
147
148
149
150
151
152
153
154
155
156
157
158
159
    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
160
    void operator()(std::size_t num_thread) const
Chao Liu's avatar
Chao Liu committed
161
162
163
164
165
166
167
168
    {
        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
169
            std::size_t iw_end   = std::min((it + 1) * work_per_thread, mN1d);
Chao Liu's avatar
Chao Liu committed
170
171
172

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

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

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

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

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

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

Chao Liu's avatar
Chao Liu committed
209
210
211
    template <class G>
    void GenerateTensorValue(G g, std::size_t num_thread = 1)
    {
Chao Liu's avatar
Chao Liu committed
212
        switch(mDesc.GetNumOfDimension())
Chao Liu's avatar
Chao Liu committed
213
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
        {
        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)
    {
252
        return mData[mDesc.GetOffsetFromMultiIndex(is...)];
Chao Liu's avatar
Chao Liu committed
253
254
255
256
257
    }

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

    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;
};
272

Chao Liu's avatar
Chao Liu committed
273
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
// 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);
}

315
#endif