shape.hpp 4.35 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
#include <memory>
Paul's avatar
Paul committed
9

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

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

Paul's avatar
Paul committed
14
15
struct shape_impl;

Paul's avatar
Paul committed
16
17
struct shape
{
Paul's avatar
Paul committed
18
19

// Add new types here
Paul's avatar
Paul committed
20
// clang-format off
Paul's avatar
Paul committed
21
#define MIGRAPH_SHAPE_VISIT_TYPES(m) \
Paul's avatar
Paul committed
22
    m(float_type, float) \
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
    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
31
    m(uint64_type, uint64_t)
Paul's avatar
Paul committed
32
// clang-format on
Paul's avatar
Paul committed
33

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

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

wsttiger's avatar
wsttiger committed
51
52
53
54
55
    template <class T>
    struct get_type<const T> : get_type<T>
    {
    };

Paul's avatar
Paul committed
56
57
58
59
60
61
    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
62
63
    const std::vector<std::size_t>& lens() const;
    const std::vector<std::size_t>& strides() const;
Paul's avatar
Paul committed
64
65
66
    std::size_t elements() const;
    std::size_t bytes() const;

Paul's avatar
Paul committed
67
    /// Map multiple indices to space index
Paul's avatar
Paul committed
68
    std::size_t index(std::initializer_list<std::size_t> l) const;
Paul's avatar
Paul committed
69
    /// Map multiple indices to space index
Paul's avatar
Paul committed
70
    std::size_t index(const std::vector<std::size_t>& l) const;
Paul's avatar
Paul committed
71

Paul's avatar
Paul committed
72
    /// Map multiple indices from a range of iterator to a space index
Paul's avatar
Paul committed
73
    template <class Iterator>
Paul's avatar
Paul committed
74
75
76
77
78
79
    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
80

Paul's avatar
Paul committed
81
    /// Map element index to space index
Paul's avatar
Paul committed
82
83
    std::size_t index(std::size_t i) const;

Paul's avatar
Paul committed
84
    /// Returns true if the shape is packed with no padding
Paul's avatar
Paul committed
85
    bool packed() const;
Paul's avatar
Paul committed
86
87
    /// Returns true is the shape has been transposed. That is the strides are not in descending
    /// order
Paul's avatar
Paul committed
88
    bool transposed() const;
Paul's avatar
Paul committed
89
    /// Returns true if the shape is broadcasting a dimension. That is, one of the strides are zero
Paul's avatar
Paul committed
90
    bool broadcasted() const;
Paul's avatar
Paul committed
91
92
    /// Returns true if the shape is in its standard format. That is, the shape is both packed and
    /// not transposed.
Paul's avatar
Paul committed
93
    bool standard() const;
Paul's avatar
Paul committed
94

Paul's avatar
Paul committed
95
96
    friend bool operator==(const shape& x, const shape& y);
    friend bool operator!=(const shape& x, const shape& y);
Paul's avatar
Paul committed
97
    friend std::ostream& operator<<(std::ostream& os, const shape& x);
Paul's avatar
Paul committed
98

Paul's avatar
Paul committed
99
    template <class T>
Paul's avatar
Paul committed
100
101
102
103
    struct as
    {
        using type = T;

Paul's avatar
Paul committed
104
        template <class U>
Paul's avatar
Paul committed
105
106
107
108
109
        T operator()(U u) const
        {
            return T(u);
        }

Paul's avatar
Paul committed
110
        template <class U>
Paul's avatar
Paul committed
111
112
113
114
115
        T* operator()(U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
116
        template <class U>
Paul's avatar
Paul committed
117
118
119
120
121
        const T* operator()(const U* u) const
        {
            return static_cast<T*>(u);
        }

Paul's avatar
Paul committed
122
        T operator()() const { return {}; }
Paul's avatar
Paul committed
123

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

Paul's avatar
Paul committed
126
127
        template <class U>
        T* from(U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
128
        {
Paul's avatar
Paul committed
129
            return reinterpret_cast<T*>(buffer) + n;
Paul's avatar
Paul committed
130
        }
Paul's avatar
Paul committed
131

Paul's avatar
Paul committed
132
133
        template <class U>
        const T* from(const U* buffer, std::size_t n = 0) const
Paul's avatar
Paul committed
134
        {
Paul's avatar
Paul committed
135
            return reinterpret_cast<const T*>(buffer) + n;
Paul's avatar
Paul committed
136
        }
Paul's avatar
Paul committed
137
138
    };

Paul's avatar
Paul committed
139
    template <class Visitor>
Paul's avatar
Paul committed
140
141
    void visit_type(Visitor v) const
    {
Paul's avatar
Paul committed
142
        switch(this->type())
Paul's avatar
Paul committed
143
        {
Paul's avatar
Paul committed
144
#define MIGRAPH_SHAPE_VISITOR_CASE(x, t) \
Paul's avatar
Paul committed
145
    case x: v(as<t>()); return;
Paul's avatar
Paul committed
146
147
            MIGRAPH_SHAPE_VISIT_TYPES(MIGRAPH_SHAPE_VISITOR_CASE)
#undef MIGRAPH_SHAPE_VISITOR_CASE
Paul's avatar
Paul committed
148
        }
Paul's avatar
Paul committed
149
        MIGRAPH_THROW("Unknown type");
Paul's avatar
Paul committed
150
    }
Paul's avatar
Paul committed
151
152

    private:
Paul's avatar
Paul committed
153
    std::shared_ptr<const shape_impl> impl;
Paul's avatar
Paul committed
154

Paul's avatar
Paul committed
155
    std::size_t element_space() const;
Paul's avatar
Paul committed
156
    std::string type_string() const;
Paul's avatar
Paul committed
157
158
};

Paul's avatar
Paul committed
159
} // namespace migraph
Paul's avatar
Paul committed
160
161

#endif