shape.hpp 4.38 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
    std::size_t elements() const;
    std::size_t bytes() const;
Scott Thornton's avatar
Scott Thornton committed
66
    std::size_t type_size() const;
Paul's avatar
Paul committed
67

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

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

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

Paul's avatar
Paul committed
85
    /// Returns true if the shape is packed with no padding
Paul's avatar
Paul committed
86
    bool packed() const;
Paul's avatar
Paul committed
87
88
    /// Returns true is the shape has been transposed. That is the strides are not in descending
    /// order
Paul's avatar
Paul committed
89
    bool transposed() const;
Paul's avatar
Paul committed
90
    /// Returns true if the shape is broadcasting a dimension. That is, one of the strides are zero
Paul's avatar
Paul committed
91
    bool broadcasted() const;
Paul's avatar
Paul committed
92
93
    /// 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
94
    bool standard() const;
Paul's avatar
Paul committed
95

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

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

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

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

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

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

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

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

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

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

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

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

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

#endif