literal.hpp 2.75 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_LITERAL_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <migraph/shape.hpp>
Paul's avatar
Paul committed
5
#include <migraph/shape_for_each.hpp>
Paul's avatar
Paul committed
6
7
8
#include <migraph/argument.hpp>
#include <migraph/tensor_view.hpp>
#include <migraph/raw_data.hpp>
Paul's avatar
Paul committed
9

Paul's avatar
Paul committed
10
11
#include <memory>

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

Paul's avatar
Paul committed
14
15
16
17
/**
 * @brief Represents a raw literal
 * @details This stores the literal has a raw buffer that is owned by this class
 */
Paul's avatar
Paul committed
18
struct literal : raw_data<literal>
Paul's avatar
Paul committed
19
{
Paul's avatar
Paul committed
20
    literal() {}
Paul's avatar
Paul committed
21

Paul's avatar
Paul committed
22
    template <class T>
Paul's avatar
Paul committed
23
    literal(T x) : buffer(std::make_unique<char[]>(sizeof(T))), m_shape(shape::get_type<T>{})
Paul's avatar
Paul committed
24
25
    {
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
26
        *(reinterpret_cast<T*>(buffer.get())) = x;
Paul's avatar
Paul committed
27
28
    }

Paul's avatar
Paul committed
29
    template <class T>
Paul's avatar
Paul committed
30
31
    literal(shape s, const std::vector<T>& x)
        : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
32
    {
Paul's avatar
Paul committed
33
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
34
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
35
36
    }

Paul's avatar
Paul committed
37
    template <class T>
Paul's avatar
Paul committed
38
39
    literal(shape s, const std::initializer_list<T>& x)
        : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
40
    {
Paul's avatar
Paul committed
41
        static_assert(std::is_trivial<T>{}, "Literals can only be trivial types");
Paul's avatar
Paul committed
42
        fill(x.begin(), x.end());
Paul's avatar
Paul committed
43
44
    }

Paul's avatar
Paul committed
45
    template <class Iterator>
Paul's avatar
Paul committed
46
47
    literal(shape s, Iterator start, Iterator end)
        : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
48
    {
Paul's avatar
Paul committed
49
        fill(start, end);
Paul's avatar
Paul committed
50
51
    }

Paul's avatar
Paul committed
52
    literal(shape s, const char* x) : buffer(std::make_unique<char[]>(s.bytes())), m_shape(s)
Paul's avatar
Paul committed
53
54
55
    {
        std::copy(x, x + s.bytes(), buffer.get());
    }
Paul's avatar
Paul committed
56

Paul's avatar
Paul committed
57
    /// Whether data is available
Paul's avatar
Paul committed
58
    bool empty() const { return this->buffer == nullptr; }
Paul's avatar
Paul committed
59

Paul's avatar
Paul committed
60
    /// Provides a raw pointer to the data
Paul's avatar
Paul committed
61
    const char* data() const { return this->buffer.get(); }
Paul's avatar
Paul committed
62

Paul's avatar
Paul committed
63
    const shape& get_shape() const { return this->m_shape; }
Paul's avatar
Paul committed
64

Paul's avatar
Paul committed
65
    /// Convert the data to an argument
Paul's avatar
Paul committed
66
67
    argument get_argument() const
    {
Paul's avatar
Paul committed
68
        std::vector<char> b(buffer.get(), buffer.get() + m_shape.bytes());
Paul's avatar
Paul committed
69
        return {m_shape, [b]() mutable { return b.data(); }};
Paul's avatar
Paul committed
70
71
    }

Paul's avatar
Paul committed
72
    private:
Paul's avatar
Paul committed
73
    std::shared_ptr<char> buffer;
Paul's avatar
Paul committed
74
    shape m_shape;
Paul's avatar
Paul committed
75
76
77
78

    template <class Iterator>
    void fill(Iterator start, Iterator end)
    {
Paul's avatar
Paul committed
79
        if(m_shape.standard())
Paul's avatar
Paul committed
80
        {
Paul's avatar
Paul committed
81
            m_shape.visit_type([&](auto as) { std::copy(start, end, as.from(buffer.get())); });
Paul's avatar
Paul committed
82
83
84
85
86
        }
        else
        {
            auto it = start;
            m_shape.visit_type([&](auto as) {
Paul's avatar
Paul committed
87
                auto output = make_view(m_shape, as.from(buffer.get()));
Paul's avatar
Paul committed
88
89
90
91
92
93
94
                shape_for_each(output.get_shape(), [&](const auto& idx) {
                    it++;
                    output(idx.begin(), idx.end()) = *it;
                });
            });
        }
    }
Paul's avatar
Paul committed
95
96
};

Paul's avatar
Paul committed
97
} // namespace migraph
Paul's avatar
Paul committed
98
99

#endif