"src/include/rtg/program.hpp" did not exist on "894a0fca832ad5a452eabe204f0fcb87ecdfdedb"
shape.cpp 3.75 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
7
8
9
#include <numeric>
#include <algorithm>
#include <functional>

namespace rtg {

Paul's avatar
Paul committed
10
shape::shape() : type_(float_type), packed_(false) {}
Paul's avatar
Paul committed
11

Paul's avatar
Paul committed
12
13
shape::shape(type_t t) : type_(t), lens_({1}), strides_({1}), packed_(true) {}
shape::shape(type_t t, std::vector<std::size_t> l) : type_(t), lens_(std::move(l)), packed_(true)
Paul's avatar
Paul committed
14
15
16
17
18
{
    this->calculate_strides();
    assert(lens_.size() == strides_.size());
}
shape::shape(type_t t, std::vector<std::size_t> l, std::vector<std::size_t> s)
Paul's avatar
Paul committed
19
    : type_(t), lens_(std::move(l)), strides_(std::move(s))
Paul's avatar
Paul committed
20
21
{
    assert(lens_.size() == strides_.size());
Paul's avatar
Paul committed
22
    packed_ = this->elements() == this->element_space();
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
31
32
33
34
35
}

void shape::calculate_strides()
{
    strides_.clear();
    strides_.resize(lens_.size(), 0);
    if(strides_.empty())
        return;
    strides_.back() = 1;
    std::partial_sum(
        lens_.rbegin(), lens_.rend() - 1, strides_.rbegin() + 1, std::multiplies<std::size_t>());
}

Paul's avatar
Paul committed
36
37
38
shape::type_t shape::type() const { return this->type_; }
const std::vector<std::size_t>& shape::lens() const { return this->lens_; }
const std::vector<std::size_t>& shape::strides() const { return this->strides_; }
Paul's avatar
Paul committed
39
40
std::size_t shape::elements() const
{
Paul's avatar
Paul committed
41
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
42
43
44
45
46
47
48
49
50
    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
51
52
53
54
55
56
57
58
59
60
61
62
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
63
64
65
std::size_t shape::index(std::size_t i) const
{
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
66
67
68
69
70
71
72
73
74
    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) { return ((i / stride) % len) * stride; });
}
bool shape::packed() const { return this->packed_; }
Paul's avatar
Paul committed
75
76
77
std::size_t shape::element_space() const
{
    // TODO: Get rid of intermediate vector
Paul's avatar
Paul committed
78
    assert(this->lens().size() == this->strides().size());
Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
87
88
89
    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
90
91
std::string shape::type_string() const
{
Paul's avatar
Paul committed
92
    switch(this->type_)
Paul's avatar
Paul committed
93
94
    {
#define RTG_SHAPE_TYPE_STRING_CASE(x, t) \
Paul's avatar
Paul committed
95
    case x: return #x;
Paul's avatar
Paul committed
96
97
98
99
100
101
        RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_TYPE_STRING_CASE)
#undef RTG_SHAPE_TYPE_STRING_CASE
    }
    throw "Invalid type";
}

Paul's avatar
Paul committed
102
103
104
105
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
106
bool operator!=(const shape& x, const shape& y) { return !(x == y); }
Paul's avatar
Paul committed
107

Paul's avatar
Paul committed
108
109
110
111
112
113
114
115
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
116
} // namespace rtg