msgpack.cpp 9.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
25
#include <migraphx/msgpack.hpp>
#include <migraphx/serialize.hpp>
Paul's avatar
Paul committed
26
#include <migraphx/functional.hpp>
27
#include <msgpack.hpp>
Paul's avatar
Format  
Paul committed
28
29
#include <variant>

Paul's avatar
Paul committed
30
31
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Format  
Paul committed
32

Paul's avatar
Paul committed
33
34
// Leave an extra byte for error checking
constexpr std::size_t msgpack_size_limit = std::numeric_limits<uint32_t>::max() - 1;
Paul's avatar
Format  
Paul committed
35

Paul's avatar
Format  
Paul committed
36
template <class Range>
Paul's avatar
Paul committed
37
38
std::size_t msgpack_chunk_size(const Range& r)
{
Paul's avatar
Paul committed
39
    return 1 + r.size() / msgpack_size_limit;
Paul's avatar
Paul committed
40
}
Paul's avatar
Format  
Paul committed
41

Paul's avatar
Format  
Paul committed
42
template <class Iterator, class F>
Paul's avatar
Paul committed
43
44
45
46
47
48
49
50
51
52
53
54
55
void msgpack_chunk_for_each(Iterator start, Iterator last, F f)
{
    while(std::distance(start, last) > msgpack_size_limit)
    {
        auto next = std::next(start, msgpack_size_limit);
        f(start, next);
        start = next;
    }
    f(start, last);
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
56
57
58
59
60
61
62
63
64
65
66
67
68

namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
{
    namespace adaptor {

    template <>
    struct convert<migraphx::value>
    {
        const msgpack::object& operator()(const msgpack::object& o, migraphx::value& v) const
        {
            switch(o.type)
            {
bpickrel's avatar
bpickrel committed
69
            case msgpack::type::NIL: {
70
71
72
                v = nullptr;
                break;
            }
bpickrel's avatar
bpickrel committed
73
            case msgpack::type::BOOLEAN: {
74
75
76
                v = o.as<bool>();
                break;
            }
bpickrel's avatar
bpickrel committed
77
            case msgpack::type::POSITIVE_INTEGER: {
78
79
80
                v = o.as<std::uint64_t>();
                break;
            }
bpickrel's avatar
bpickrel committed
81
            case msgpack::type::NEGATIVE_INTEGER: {
82
83
84
85
                v = o.as<std::int64_t>();
                break;
            }
            case msgpack::type::FLOAT32:
bpickrel's avatar
bpickrel committed
86
            case msgpack::type::FLOAT64: {
87
88
89
                v = o.as<double>();
                break;
            }
bpickrel's avatar
bpickrel committed
90
            case msgpack::type::STR: {
91
92
93
                v = o.as<std::string>();
                break;
            }
bpickrel's avatar
bpickrel committed
94
            case msgpack::type::ARRAY: {
Paul's avatar
Format  
Paul committed
95
96
97
98
99
                if(o.via.array.size == 0)
                {
                    v = migraphx::value::array{};
                    break;
                }
Paul's avatar
Format  
Paul committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
                std::variant<migraphx::value::array,
                             migraphx::value::binary,
                             migraphx::value::object>
                    r;
                switch(o.via.array.ptr->type)
                {
                case msgpack::type::BIN: {
                    r = migraphx::value::binary{};
                    break;
                }
                case msgpack::type::ARRAY: {
                    r = migraphx::value::array{};
                    break;
                }
                case msgpack::type::MAP: {
                    r = migraphx::value::object{};
                    break;
Paul's avatar
Format  
Paul committed
117
                }
Paul's avatar
Format  
Paul committed
118
119
                default: MIGRAPHX_THROW("Incorrect chunking");
                }
120
121
122
                std::for_each(
                    o.via.array.ptr,
                    o.via.array.ptr + o.via.array.size,
Paul's avatar
Format  
Paul committed
123
124
                    [&](const msgpack::object& sa) {
                        std::visit(
Paul's avatar
Paul committed
125
                            migraphx::overload(
Paul's avatar
Format  
Paul committed
126
                                [&](migraphx::value::binary& bin) {
Paul's avatar
Paul committed
127
                                    assert(sa.type == msgpack::type::BIN);
Paul's avatar
Format  
Paul committed
128
129
130
                                    bin.insert(bin.end(),
                                               sa.via.bin.ptr,
                                               sa.via.bin.ptr + sa.via.bin.size);
Paul's avatar
Format  
Paul committed
131
132
                                },
                                [&](migraphx::value::array& arr) {
Paul's avatar
Paul committed
133
                                    assert(sa.type == msgpack::type::ARRAY);
Paul's avatar
Format  
Paul committed
134
135
136
137
138
139
140
                                    std::for_each(sa.via.array.ptr,
                                                  sa.via.array.ptr + sa.via.array.size,
                                                  [&](const msgpack::object& so) {
                                                      arr.push_back(so.as<migraphx::value>());
                                                  });
                                },
                                [&](migraphx::value::object& obj) {
Paul's avatar
Paul committed
141
                                    assert(sa.type == msgpack::type::MAP);
Paul's avatar
Format  
Paul committed
142
143
144
145
146
147
148
149
                                    std::for_each(sa.via.map.ptr,
                                                  sa.via.map.ptr + sa.via.map.size,
                                                  [&](const msgpack::object_kv& p) {
                                                      obj[p.key.as<std::string>()] =
                                                          p.val.as<migraphx::value>();
                                                  });
                                }),
                            r);
Paul's avatar
Format  
Paul committed
150
                    });
Paul's avatar
Format  
Paul committed
151
                std::visit([&](const auto& x) { v = x; }, r);
152
153
                break;
            }
Paul's avatar
Format  
Paul committed
154
        case msgpack::type::MAP:
Paul's avatar
Format  
Paul committed
155
156
        case msgpack::type::BIN: MIGRAPHX_THROW("Unexpected msgpack type");
        case msgpack::type::EXT: MIGRAPHX_THROW("msgpack EXT type not supported.");
157
            }
Paul's avatar
Paul committed
158
            return o;
159
160
161
        }
    };

162
163
164
165
166
167
168
169
170
    template <>
    struct pack<migraphx::value::binary>
    {
        template <class Stream>
        packer<Stream>& operator()(msgpack::packer<Stream>& o,
                                   const migraphx::value::binary& x) const
        {
            const auto* data = reinterpret_cast<const char*>(x.data());
            auto size        = x.size();
Paul's avatar
Paul committed
171
            o.pack_array(migraphx::msgpack_chunk_size(x));
Paul's avatar
Format  
Paul committed
172
173
174
175
176
            migraphx::msgpack_chunk_for_each(
                data, data + size, [&](const char* start, const char* last) {
                    o.pack_bin(last - start);
                    o.pack_bin_body(data, last - start);
                });
177
178
179
180
            return o;
        }
    };

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    template <>
    struct pack<migraphx::value>
    {
        template <class Stream>
        void write(msgpack::packer<Stream>& o, const std::nullptr_t&) const
        {
            o.pack_nil();
        }
        template <class Stream, class T>
        void write(msgpack::packer<Stream>& o, const T& x) const
        {
            o.pack(x);
        }
        template <class Stream>
        void write(msgpack::packer<Stream>& o, const std::vector<migraphx::value>& v) const
        {
            if(v.empty())
            {
                o.pack_array(0);
                return;
            }
Paul's avatar
Paul committed
202
203
            o.pack_array(migraphx::msgpack_chunk_size(v));

204
205
            if(not v.front().get_key().empty())
            {
Paul's avatar
Paul committed
206
                migraphx::msgpack_chunk_for_each(v.begin(), v.end(), [&](auto start, auto last) {
Paul's avatar
Format  
Paul committed
207
                    o.pack_map(last - start);
Paul's avatar
Paul committed
208
209
210
211
212
                    std::for_each(start, last, [&](auto&& x) {
                        o.pack(x.get_key());
                        o.pack(x.without_key());
                    });
                });
213
214
215
            }
            else
            {
Paul's avatar
Paul committed
216
                migraphx::msgpack_chunk_for_each(v.begin(), v.end(), [&](auto start, auto last) {
Paul's avatar
Format  
Paul committed
217
218
                    o.pack_array(last - start);
                    std::for_each(start, last, [&](auto&& x) { o.pack(x); });
Paul's avatar
Paul committed
219
                });
220
221
222
223
224
            }
        }
        template <class Stream>
        packer<Stream>& operator()(msgpack::packer<Stream>& o, const migraphx::value& v) const
        {
225
            v.visit_value([&](auto&& x) { this->write(o, x); });
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
            return o;
        }
    };

    } // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

struct vector_stream
{
    std::vector<char> buffer{};
    vector_stream& write(const char* b, std::size_t n)
    {
        buffer.insert(buffer.end(), b, b + n);
        return *this;
    }
};

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
struct writer_stream
{
    std::function<void(const char*, std::size_t)> writer;
    writer_stream& write(const char* b, std::size_t n)
    {
        writer(b, n);
        return *this;
    }
};

void to_msgpack(const value& v, std::function<void(const char*, std::size_t)> writer)
{
    writer_stream ws{std::move(writer)};
    msgpack::pack(ws, v);
}

263
264
265
266
267
268
std::vector<char> to_msgpack(const value& v)
{
    vector_stream vs;
    msgpack::pack(vs, v);
    return vs.buffer;
}
269
value from_msgpack(const char* buffer, std::size_t size)
270
{
271
    msgpack::object_handle oh = msgpack::unpack(buffer, size);
272
273
    return oh.get().as<value>();
}
274
275
276
277
value from_msgpack(const std::vector<char>& buffer)
{
    return from_msgpack(buffer.data(), buffer.size());
}
278
279
280

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx