shape.cpp 4.29 KB
Newer Older
Paul's avatar
Paul committed
1
2

#include <rtg/shape.hpp>
Paul's avatar
Paul committed
3
#include <rtg/stringutils.hpp>
Paul's avatar
Paul committed
4
5
6
#include <numeric>
#include <algorithm>
#include <functional>
Paul's avatar
Paul committed
7
#include <iostream>
Paul's avatar
Paul committed
8
9
10

namespace rtg {

Paul's avatar
Paul committed
11
shape::shape() : m_type(float_type), m_packed(false) {}
Paul's avatar
Paul committed
12

Paul's avatar
Paul committed
13
14
shape::shape(type_t t) : m_type(t), m_lens({1}), m_strides({1}), m_packed(true) {}
shape::shape(type_t t, std::vector<std::size_t> l) : m_type(t), m_lens(std::move(l)), m_packed(true)
Paul's avatar
Paul committed
15
16
{
    this->calculate_strides();
Paul's avatar
Paul committed
17
    assert(m_lens.size() == m_strides.size());
Paul's avatar
Paul committed
18
19
}
shape::shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
Paul's avatar
Paul committed
20
    : m_type(t), m_lens(std::move(l)), m_strides(std::move(s))
Paul's avatar
Paul committed
21
{
Paul's avatar
Paul committed
22
    assert(m_lens.size() == m_strides.size());
Paul's avatar
Paul committed
23
24
    assert(std::any_of(m_strides.begin(), m_strides.end(), [](auto x) { return x > 0; }) and
           "At least one stride must be non-zero");
Paul's avatar
Paul committed
25
    m_packed = this->elements() == this->element_space();
Paul's avatar
Paul committed
26
27
28
29
}

void shape::calculate_strides()
{
Paul's avatar
Paul committed
30
31
32
    m_strides.clear();
    m_strides.resize(m_lens.size(), 0);
    if(m_strides.empty())
Paul's avatar
Paul committed
33
        return;
Paul's avatar
Paul committed
34
    m_strides.back() = 1;
Paul's avatar
Paul committed
35
    std::partial_sum(
Paul's avatar
Paul committed
36
        m_lens.rbegin(), m_lens.rend() - 1, m_strides.rbegin() + 1, std::multiplies<std::size_t>());
Paul's avatar
Paul committed
37
38
}

Paul's avatar
Paul committed
39
40
41
shape::type_t shape::type() const { return this->m_type; }
const std::vector<std::size_t>& shape::lens() const { return this->m_lens; }
const std::vector<std::size_t>& shape::strides() const { return this->m_strides; }
Paul's avatar
Paul committed
42
43
std::size_t shape::elements() const
{
Paul's avatar
Paul committed
44
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
45
46
47
48
49
50
51
52
53
    return std::accumulate(
        this->lens().begin(), this->lens().end(), std::size_t{1}, std::multiplies<std::size_t>());
}
std::size_t shape::bytes() const
{
    std::size_t n = 0;
    this->visit_type([&](auto as) { n = as.size(); });
    return n * this->element_space();
}
Paul's avatar
Paul committed
54
55
56
57
58
59
60
61
62
63
64
65
std::size_t shape::index(std::initializer_list<std::size_t> l) const
{
    assert(l.size() <= this->lens().size());
    assert(this->lens().size() == this->strides().size());
    return std::inner_product(l.begin(), l.end(), this->strides().begin(), std::size_t{0});
}
std::size_t shape::index(const std::vector<std::size_t>& l) const
{
    assert(l.size() <= this->lens().size());
    assert(this->lens().size() == this->strides().size());
    return std::inner_product(l.begin(), l.end(), this->strides().begin(), std::size_t{0});
}
Paul's avatar
Paul committed
66
67
68
std::size_t shape::index(std::size_t i) const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
69
70
71
    if(this->packed())
        return i;
    else
Paul's avatar
Paul committed
72
73
74
75
76
77
78
79
80
        return std::inner_product(this->lens().begin(),
                                  this->lens().end(),
                                  this->strides().begin(),
                                  std::size_t{0},
                                  std::plus<std::size_t>{},
                                  [&](std::size_t len, std::size_t stride) {
                                      assert(stride > 0 and len > 0);
                                      return ((i / stride) % len) * stride;
                                  });
Paul's avatar
Paul committed
81
}
Paul's avatar
Paul committed
82
bool shape::packed() const { return this->m_packed; }
Paul's avatar
Paul committed
83
84
85
std::size_t shape::element_space() const
{
    // TODO: Get rid of intermediate vector
Paul's avatar
Paul committed
86
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
87
88
89
90
91
92
93
94
95
96
97
    std::vector<std::size_t> max_indices(this->lens().size());
    std::transform(this->lens().begin(),
                   this->lens().end(),
                   std::vector<std::size_t>(this->lens().size(), 1).begin(),
                   max_indices.begin(),
                   std::minus<std::size_t>());
    return std::inner_product(
               max_indices.begin(), max_indices.end(), this->strides().begin(), std::size_t{0}) +
           1;
}

Paul's avatar
Paul committed
98
99
std::string shape::type_string() const
{
Paul's avatar
Paul committed
100
    switch(this->m_type)
Paul's avatar
Paul committed
101
    {
Paul's avatar
Paul committed
102
    case any_type: return "any";
Paul's avatar
Paul committed
103
#define RTG_SHAPE_TYPE_STRING_CASE(x, t) \
Paul's avatar
Paul committed
104
    case x: return #x;
Paul's avatar
Paul committed
105
106
107
        RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_TYPE_STRING_CASE)
#undef RTG_SHAPE_TYPE_STRING_CASE
    }
Paul's avatar
Paul committed
108
    RTG_THROW("Invalid type");
Paul's avatar
Paul committed
109
110
}

Paul's avatar
Paul committed
111
112
113
114
bool operator==(const shape& x, const shape& y)
{
    return x.type() == y.type() && x.lens() == y.lens() && x.strides() == y.strides();
}
Paul's avatar
Paul committed
115
bool operator!=(const shape& x, const shape& y) { return !(x == y); }
Paul's avatar
Paul committed
116

Paul's avatar
Paul committed
117
118
119
120
121
122
123
124
std::ostream& operator<<(std::ostream& os, const shape& x)
{
    os << x.type_string() << ", ";
    os << "{" << to_string(x.lens()) << "}, ";
    os << "{" << to_string(x.strides()) << "}";
    return os;
}

Paul's avatar
Paul committed
125
} // namespace rtg