shape.hpp 3.74 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_SHAPE_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_SHAPE_HPP
Paul's avatar
Paul committed
3
4
5

#include <vector>
#include <cassert>
Paul's avatar
Paul committed
6
#include <ostream>
Paul's avatar
Paul committed
7
#include <numeric>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
#include <migraph/errors.hpp>
Paul's avatar
Paul committed
10

Paul's avatar
Paul committed
11
namespace migraph {
Paul's avatar
Paul committed
12
13
14

struct shape
{
Paul's avatar
Paul committed
15
16

// Add new types here
Paul's avatar
Paul committed
17
// clang-format off
Paul's avatar
Paul committed
18
#define MIGRAPH_SHAPE_VISIT_TYPES(m) \
Paul's avatar
Paul committed
19
    m(float_type, float) \
Paul's avatar
Paul committed
20
21
22
23
24
25
26
27
    m(double_type, double) \
    m(uint8_type, uint8_t) \
    m(int8_type, int8_t) \
    m(uint16_type, uint16_t) \
    m(int16_type, int16_t) \
    m(int32_type, int32_t) \
    m(int64_type, int64_t) \
    m(uint32_type, uint32_t) \
Paul's avatar
Paul committed
28
    m(uint64_type, uint64_t)
Paul's avatar
Paul committed
29
// clang-format on
Paul's avatar
Paul committed
30

Paul's avatar
Paul committed
31
#define MIGRAPH_SHAPE_ENUM_TYPES(x, t) x,
Paul's avatar
Paul committed
32
33
    enum type_t
    {
Paul's avatar
Paul committed
34
        MIGRAPH_SHAPE_VISIT_TYPES(MIGRAPH_SHAPE_ENUM_TYPES)
Paul's avatar
Paul committed
35
    };
Paul's avatar
Paul committed
36
#undef MIGRAPH_SHAPE_ENUM_TYPES
Paul's avatar
Paul committed
37

Paul's avatar
Paul committed
38
    template <class T, class = void>
Paul's avatar
Paul committed
39
    struct get_type;
Paul's avatar
Paul committed
40
#define MIGRAPH_SHAPE_GET_TYPE(x, t)                          \
Paul's avatar
Paul committed
41
    template <class T>                                        \
Paul's avatar
Paul committed
42
    struct get_type<t, T> : std::integral_constant<type_t, x> \
Paul's avatar
Paul committed
43
44
    {                                                         \
    };
Paul's avatar
Paul committed
45
46
    MIGRAPH_SHAPE_VISIT_TYPES(MIGRAPH_SHAPE_GET_TYPE)
#undef MIGRAPH_SHAPE_GET_TYPE
Paul's avatar
Paul committed
47
48
49
50
51
52
53

    shape();
    shape(type_t t);
    shape(type_t t, std::vector<std::size_t> l);
    shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s);

    type_t type() const;
Paul's avatar
Paul committed
54
55
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Paul's avatar
Paul committed
56
57
58
    std::size_t elements() const;
    std::size_t bytes() const;

Paul's avatar
Paul committed
59
60
    std::size_t index(std::initializer_list<std::size_t> l) const;
    std::size_t index(const std::vector<std::size_t>& l) const;
Paul's avatar
Paul committed
61
62

    template <class Iterator>
Paul's avatar
Paul committed
63
64
65
66
67
68
    std::size_t index(Iterator start, Iterator last) const
    {
        assert(std::distance(start, last) <= this->lens().size());
        assert(this->lens().size() == this->strides().size());
        return std::inner_product(start, last, this->strides().begin(), std::size_t{0});
    }
Paul's avatar
Paul committed
69

Paul's avatar
Paul committed
70
71
72
73
    // Map element index to space index
    std::size_t index(std::size_t i) const;

    bool packed() const;
Paul's avatar
Paul committed
74
    bool broadcasted() const;
Paul's avatar
Paul committed
75

Paul's avatar
Paul committed
76
77
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
78
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
79

Paul's avatar
Paul committed
80
    template <class T>
Paul's avatar
Paul committed
81
82
83
84
    struct as
    {
        using type = T;

Paul's avatar
Paul committed
85
        template <class U>
Paul's avatar
Paul committed
86
87
88
89
90
        T operator()(U u) const
        {
            return T(u);
        }

Paul's avatar
Paul committed
91
        template <class U>
Paul's avatar
Paul committed
92
93
94
95
96
        T* operator()(U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
97
        template <class U>
Paul's avatar
Paul committed
98
99
100
101
102
        const T* operator()(const U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
103
        T operator()() const { return {}; }
Paul's avatar
Paul committed
104

Paul's avatar
Paul committed
105
        std::size_t size(std::size_t n = 1) const { return sizeof(T) * n; }
Paul's avatar
Paul committed
106

Paul's avatar
Paul committed
107
108
        template <class U>
        T* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
109
        {
Paul's avatar
Paul committed
110
            return reinterpret_cast<T*>(buffer) + n;
Paul's avatar
Paul committed
111
        }
Paul's avatar
Paul committed
112

Paul's avatar
Paul committed
113
114
        template <class U>
        const T* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
115
        {
Paul's avatar
Paul committed
116
            return reinterpret_cast<const T*>(buffer) + n;
Paul's avatar
Paul committed
117
        }
Paul's avatar
Paul committed
118
119
    };

Paul's avatar
Paul committed
120
    template <class Visitor>
Paul's avatar
Paul committed
121
122
    void visit_type(Visitor v) const
    {
Paul's avatar
Paul committed
123
        switch(this->m_type)
Paul's avatar
Paul committed
124
        {
Paul's avatar
Paul committed
125
#define MIGRAPH_SHAPE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
126
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
127
128
            MIGRAPH_SHAPE_VISIT_TYPES(MIGRAPH_SHAPE_VISITOR_CASE)
#undef MIGRAPH_SHAPE_VISITOR_CASE
Paul's avatar
Paul committed
129
        }
Paul's avatar
Paul committed
130
        MIGRAPH_THROW("Unknown type");
Paul's avatar
Paul committed
131
    }
Paul's avatar
Paul committed
132
133

    private:
Paul's avatar
Paul committed
134
135
136
137
    type_t m_type;
    std::vector<std::size_t> m_lens;
    std::vector<std::size_t> m_strides;
    bool m_packed;
Paul's avatar
Paul committed
138
139
140

    void calculate_strides();
    std::size_t element_space() const;
Paul's avatar
Paul committed
141
    std::string type_string() const;
Paul's avatar
Paul committed
142
143
};

Paul's avatar
Paul committed
144
} // namespace migraph
Paul's avatar
Paul committed
145
146

#endif