program.cpp 18.3 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
#include <migraphx/program.hpp>
#include <migraphx/stringutils.hpp>
#include <migraphx/instruction.hpp>
4
#include <migraphx/op/identity.hpp>
Paul's avatar
Paul committed
5
#include <migraphx/target.hpp>
Paul's avatar
Paul committed
6
7
8
9
#include <migraphx/env.hpp>
#include <migraphx/ranges.hpp>
#include <migraphx/time.hpp>
#include <migraphx/iterator_for.hpp>
Paul's avatar
Paul committed
10
#include <iostream>
Paul's avatar
Paul committed
11
#include <sstream>
Paul's avatar
Paul committed
12
#include <algorithm>
Paul's avatar
Paul committed
13
#include <utility>
Paul's avatar
Paul committed
14

Paul's avatar
Paul committed
15
namespace migraphx {
Paul's avatar
Paul committed
16
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
17

Paul's avatar
Paul committed
18
19
20
21
struct program_impl
{
    // A list is used to keep references to an instruction stable
    std::list<instruction> instructions;
Paul's avatar
Paul committed
22
    context ctx;
Paul's avatar
Paul committed
23
24
};

25
const operation& get_operation(instruction_ref ins) { return ins->get_operator(); }
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
28
29
static void print_instruction(std::ostream& os,
                              instruction_ref ins,
                              const std::unordered_map<instruction_ref, std::string>& names)
Paul's avatar
Paul committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{
    os << names.at(ins) << " = ";

    os << ins->get_operator();

    if(ins->name() == "@literal")
    {
        if(ins->get_literal().get_shape().elements() > 10)
            os << "{ ... }";
        else
            os << "{" << ins->get_literal() << "}";
    }

    if(!ins->inputs().empty())
    {
        char delim = '(';
        for(auto&& arg : ins->inputs())
        {
            os << delim << names.at(arg);
            delim = ',';
        }
        os << ")";
    }
Paul's avatar
Paul committed
53

Paul's avatar
Paul committed
54
55
56
    os << " -> " << ins->get_shape();
}

Paul's avatar
Paul committed
57
template <class F>
Khalique's avatar
Khalique committed
58
static void print_program(const program& p, F print_func)
Paul's avatar
Paul committed
59
{
60
    std::unordered_map<instruction_ref, std::string> names;
Paul's avatar
Paul committed
61
62
    int count = 0;

63
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
64
65
    {
        std::string var_name = "@" + std::to_string(count);
Paul's avatar
Paul committed
66
        if(ins->name() == "@param")
Paul's avatar
Paul committed
67
        {
68
            var_name = any_cast<builtin::param>(ins->get_operator()).parameter;
Paul's avatar
Paul committed
69
        }
Paul's avatar
Paul committed
70
        names.emplace(ins, var_name);
Paul's avatar
Paul committed
71

Paul's avatar
Paul committed
72
73
        // TODO: Use all_of
        for(auto&& arg : ins->inputs())
Paul's avatar
Paul committed
74
        {
Paul's avatar
Paul committed
75
76
            assert(p.has_instruction(arg) && "Instruction not found");
            (void)arg;
Paul's avatar
Paul committed
77
78
        }

79
        print_func(ins, names);
Paul's avatar
Paul committed
80
81
82
83
84

        count++;
    }
}

Paul's avatar
Paul committed
85
program::program() : impl(std::make_unique<program_impl>()) {}
Paul's avatar
Paul committed
86

Paul's avatar
Paul committed
87
program::program(program&&) noexcept = default;
Paul's avatar
Paul committed
88
89
program& program::operator=(program&&) noexcept = default;
program::~program() noexcept                    = default;
Paul's avatar
Paul committed
90

Paul's avatar
Paul committed
91
instruction_ref program::add_instruction(const operation& op, std::vector<instruction_ref> args)
Paul's avatar
Paul committed
92
{
Paul's avatar
Paul committed
93
    return insert_instruction(impl->instructions.end(), op, std::move(args));
Paul's avatar
Paul committed
94
}
Paul's avatar
Paul committed
95
96
97
instruction_ref program::insert_instruction(instruction_ref ins,
                                            const operation& op,
                                            std::vector<instruction_ref> args)
Paul's avatar
Paul committed
98
{
Paul's avatar
Paul committed
99
100
101
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
102
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
103
    shape r     = compute_shape(op, args);
Paul's avatar
Paul committed
104
    auto result = impl->instructions.insert(ins, {op, r, std::move(args)});
Paul's avatar
Paul committed
105
    instruction::backreference(result);
Paul's avatar
Paul committed
106
    assert(result->valid(begin()));
Paul's avatar
Paul committed
107
    return result;
Paul's avatar
Paul committed
108
109
}

Paul's avatar
Paul committed
110
111
112
instruction_ref program::replace_instruction(instruction_ref ins,
                                             const operation& op,
                                             std::vector<instruction_ref> args)
Paul's avatar
Paul committed
113
114
115
116
{
    assert(std::all_of(
               args.begin(), args.end(), [&](instruction_ref x) { return has_instruction(x); }) &&
           "Argument is not an exisiting instruction");
Paul's avatar
Paul committed
117
    assert(not starts_with(op.name(), "@"));
Paul's avatar
Paul committed
118

Paul's avatar
Paul committed
119
    shape r = compute_shape(op, args);
120
    instruction::replace(ins, op, r, std::move(args));
Paul's avatar
Paul committed
121
    assert(ins->valid(begin()));
Paul's avatar
Paul committed
122
123
124
    return ins;
}

Paul's avatar
Paul committed
125
instruction_ref program::replace_instruction(instruction_ref ins, instruction_ref rep)
Paul's avatar
Paul committed
126
{
Paul's avatar
Paul committed
127
128
129
    assert(has_instruction(ins));
    assert(has_instruction(rep));
    assert(ins != rep);
Shucai Xiao's avatar
Shucai Xiao committed
130

Shucai Xiao's avatar
Shucai Xiao committed
131
    if(ins == std::prev(this->end()))
Shucai Xiao's avatar
Shucai Xiao committed
132
    {
Shucai Xiao's avatar
Shucai Xiao committed
133
        return replace_instruction(ins, op::identity{}, rep);
Shucai Xiao's avatar
Shucai Xiao committed
134
135
    }

Paul's avatar
Paul committed
136
    // TODO: Should it be an error if the output is empty?
Paul's avatar
Paul committed
137
    if(ins->outputs().empty())
Paul's avatar
Paul committed
138
139
140
    {
        return rep;
    }
Paul's avatar
Paul committed
141
142
143
    // Make a copy of outputs which can be changed when calling replace_argument
    auto outputs = ins->outputs();
    for(auto out : outputs)
Paul's avatar
Paul committed
144
    {
Paul's avatar
Paul committed
145
146
        // TODO: Check for possible cycles
        if(out != rep)
Paul's avatar
Paul committed
147
        {
Paul's avatar
Paul committed
148
            instruction::replace_argument(out, ins, rep);
Paul's avatar
Paul committed
149
        }
Paul's avatar
Paul committed
150
        assert(out->valid(begin()));
Paul's avatar
Paul committed
151
    }
Paul's avatar
Paul committed
152
    // Replacement should not be dead code unless its the last instruction
Paul's avatar
Paul committed
153
    assert(!rep->outputs().empty() or rep == std::prev(end()));
Paul's avatar
Paul committed
154
    // Output of the original instruction should only be the replacement or empty
Paul's avatar
Paul committed
155
156
157
    assert(ins->outputs().empty() or std::all_of(ins->outputs().begin(),
                                                 ins->outputs().end(),
                                                 [&](auto i) { return i == rep; }));
Paul's avatar
Paul committed
158
    assert(ins->valid(begin()));
Paul's avatar
Paul committed
159
    assert(rep->valid(begin()));
Paul's avatar
Paul committed
160
161
162
    return rep;
}

Paul's avatar
Paul committed
163
instruction_ref program::remove_instruction(instruction_ref ins)
Paul's avatar
Paul committed
164
165
{
    assert(has_instruction(ins));
Paul's avatar
Paul committed
166
    assert(ins->outputs().empty());
Paul's avatar
Paul committed
167
168
169
170
    ins->clear_arguments();
    return impl->instructions.erase(ins);
}

171
172
instruction_ref program::remove_instructions(instruction_ref first, instruction_ref last)
{
Paul's avatar
Paul committed
173
174
    if(first == last)
        return first;
Paul's avatar
Paul committed
175
    // TODO: Check every element
176
    assert(has_instruction(first));
Paul's avatar
Paul committed
177
    std::for_each(first, last, [&](instruction& ins) { ins.clear_arguments(); });
Paul's avatar
Paul committed
178
    assert(std::all_of(first, last, [&](instruction& ins) { return ins.outputs().empty(); }));
179
180
181
182
183
184
185
186
187
    return impl->instructions.erase(first, last);
}

instruction_ref program::move_instruction(instruction_ref src, instruction_ref dst)
{
    impl->instructions.splice(dst, impl->instructions, src);
    return src;
}

Paul's avatar
Paul committed
188
instruction_ref program::add_literal(literal l)
Paul's avatar
Paul committed
189
{
Paul's avatar
Paul committed
190
191
192
193
    impl->instructions.emplace_front(std::move(l));
    return impl->instructions.begin();
}

Paul's avatar
Paul committed
194
instruction_ref program::add_outline(const shape& s)
Paul's avatar
Paul committed
195
196
197
{
    impl->instructions.push_front({builtin::outline{s}, s, {}});
    return impl->instructions.begin();
Paul's avatar
Paul committed
198
199
}

Paul's avatar
Paul committed
200
instruction_ref program::add_parameter(std::string name, shape s)
Paul's avatar
Paul committed
201
{
202
    assert(get_parameter_shape(name) == shape{});
Paul's avatar
Paul committed
203
    impl->instructions.push_front({builtin::param{std::move(name)}, std::move(s), {}});
Paul's avatar
Paul committed
204
205
206
    return impl->instructions.begin();
}

Paul's avatar
Paul committed
207
shape program::get_parameter_shape(std::string name) const
Paul's avatar
Paul committed
208
209
{
    auto ins = std::find_if(
Paul's avatar
Paul committed
210
        impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
Paul's avatar
Paul committed
211
            if(x.name() == "@param")
Paul's avatar
Paul committed
212
            {
213
                return any_cast<builtin::param>(x.get_operator()).parameter == name;
Paul's avatar
Paul committed
214
215
216
217
218
219
220
            }
            else
            {
                return false;
            }
        });
    if(ins != this->end())
Paul's avatar
Paul committed
221
        return ins->get_shape();
Paul's avatar
Paul committed
222
223
    else
        return {};
Paul's avatar
Paul committed
224
225
}

mei-ye's avatar
mei-ye committed
226
227
228
229
instruction_ref program::get_parameter(std::string name) const
{
    auto ins = std::find_if(
        impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
Paul's avatar
Paul committed
230
            if(x.name() == "@param")
mei-ye's avatar
mei-ye committed
231
            {
Paul's avatar
Paul committed
232
                return any_cast<builtin::param>(x.get_operator()).parameter == name;
mei-ye's avatar
mei-ye committed
233
234
235
236
237
238
239
240
241
            }
            else
            {
                return false;
            }
        });
    if(ins != this->end())
        return ins;
    else
mei-ye's avatar
mei-ye committed
242
        return this->end();
mei-ye's avatar
mei-ye committed
243
244
}

Paul's avatar
Paul committed
245
246
247
std::unordered_map<std::string, shape> program::get_parameter_shapes() const
{
    std::unordered_map<std::string, shape> result;
Paul's avatar
Paul committed
248
    for(auto&& ins : impl->instructions)
Paul's avatar
Paul committed
249
    {
Paul's avatar
Paul committed
250
        if(ins.name() == "@param")
Paul's avatar
Paul committed
251
        {
252
            auto&& name  = any_cast<builtin::param>(ins.get_operator()).parameter;
Paul's avatar
Paul committed
253
            result[name] = ins.get_shape();
Paul's avatar
Paul committed
254
255
256
257
258
        }
    }
    return result;
}

Paul's avatar
Paul committed
259
bool program::has_instruction(instruction_ref ins) const
Paul's avatar
Paul committed
260
{
Paul's avatar
Paul committed
261
262
263
264
    return std::find_if(
               impl->instructions.begin(), impl->instructions.end(), [&](const instruction& x) {
                   return std::addressof(*ins) == std::addressof(x);
               }) != impl->instructions.end();
Paul's avatar
Paul committed
265
266
}

Paul's avatar
Paul committed
267
std::size_t program::size() const { return impl->instructions.size(); }
Paul's avatar
Paul committed
268
269
instruction_ref program::begin() const { return impl->instructions.begin(); }
instruction_ref program::end() const { return impl->instructions.end(); }
270

Paul's avatar
Paul committed
271
shape program::get_shape() const { return impl->instructions.back().get_shape(); }
Paul's avatar
Paul committed
272

Paul's avatar
Paul committed
273
274
context& program::get_context() const { return impl->ctx; }

Paul's avatar
Paul committed
275
276
instruction_ref program::validate() const
{
Paul's avatar
Paul committed
277
278
    return std::find_if(impl->instructions.begin(),
                        impl->instructions.end(),
Paul's avatar
Paul committed
279
                        [&](const instruction& i) { return !i.valid(impl->instructions.begin()); });
Paul's avatar
Paul committed
280
281
}

mei-ye's avatar
mei-ye committed
282
void program::compile(const target& t, tracer trace)
Paul's avatar
Paul committed
283
{
Paul's avatar
Paul committed
284
    assert(this->validate() == impl->instructions.end());
mei-ye's avatar
mei-ye committed
285
    this->impl->ctx = t.get_context();
Paul's avatar
Paul committed
286
    if(enabled(MIGRAPHX_TRACE_COMPILE{}))
Paul's avatar
Paul committed
287
288
289
        trace = tracer{std::cout};
    trace(*this);
    trace();
Paul's avatar
Paul committed
290
    for(auto&& p : t.get_passes(this->impl->ctx))
Paul's avatar
Paul committed
291
    {
Paul's avatar
Paul committed
292
        trace("Pass: ", p.name());
Paul's avatar
Paul committed
293
        p.apply(*this);
Paul's avatar
Paul committed
294
        trace(*this);
Paul's avatar
Paul committed
295
#ifndef NDEBUG
Paul's avatar
Paul committed
296
        trace("Validate ...");
Paul's avatar
Paul committed
297
        auto invalid = this->validate();
Paul's avatar
Paul committed
298
299
        if(invalid != impl->instructions.end())
        {
Paul's avatar
Paul committed
300
            auto index = std::distance(impl->instructions.begin(), invalid);
Paul's avatar
Paul committed
301
            MIGRAPHX_THROW(p.name() + " pass produces invalid program at instruction " +
Paul's avatar
Paul committed
302
                           std::to_string(index) + ": " + invalid->name());
Paul's avatar
Paul committed
303
        }
Paul's avatar
Paul committed
304
        trace();
Paul's avatar
Paul committed
305
306
#endif
    }
Paul's avatar
Paul committed
307
    auto invalid = this->validate();
Paul's avatar
Paul committed
308
309
    if(invalid != impl->instructions.end())
    {
Paul's avatar
Paul committed
310
        auto index = std::distance(impl->instructions.begin(), invalid);
Paul's avatar
Paul committed
311
        MIGRAPHX_THROW("Invalid program from compilation at instruction " + std::to_string(index));
Paul's avatar
Paul committed
312
    }
Paul's avatar
Paul committed
313
314
315
316
317
318
319
320
321
    this->finalize();
}

void program::finalize()
{
    for(auto ins : iterator_for(*this))
    {
        ins->finalize(this->impl->ctx);
    }
Paul's avatar
Paul committed
322
323
}

Paul's avatar
Paul committed
324
325
326
327
328
template <class F>
argument generic_eval(const program& p,
                      context& ctx,
                      std::unordered_map<std::string, argument> params,
                      F trace)
Paul's avatar
Paul committed
329
{
Paul's avatar
Paul committed
330
    assert(p.validate() == p.end());
331
    std::unordered_map<instruction_ref, argument> results;
Paul's avatar
Paul committed
332
    results.reserve(p.size() * 2);
Paul's avatar
Paul committed
333
334
    std::vector<argument> values;
    values.reserve(16);
335
    for(auto ins : iterator_for(p))
Paul's avatar
Paul committed
336
    {
Paul's avatar
Paul committed
337
        if(ins->name() == "@literal")
Paul's avatar
Paul committed
338
        {
Paul's avatar
Paul committed
339
            results.emplace(ins, trace(ins, [&] { return ins->get_literal().get_argument(); }));
Paul's avatar
Paul committed
340
        }
Paul's avatar
Paul committed
341
        else if(ins->name() == "@param")
Paul's avatar
Paul committed
342
        {
Paul's avatar
Paul committed
343
344
345
346
347
348
349
350
351
352
353
            results.emplace(
                ins, trace(ins, [&] {
                    auto param_name = any_cast<builtin::param>(ins->get_operator()).parameter;
                    if(not contains(params, param_name))
                        MIGRAPHX_THROW("Parameter not found: " + param_name);
                    auto param = params.at(param_name);
                    if(param.get_shape() != ins->get_shape())
                        MIGRAPHX_THROW("Incorrect shape {" + to_string(param.get_shape()) +
                                       "} for parameter: " + param_name);
                    return param;
                }));
Paul's avatar
Paul committed
354
        }
Paul's avatar
Paul committed
355
        else if(ins->name() == "@outline")
Paul's avatar
Paul committed
356
        {
Paul's avatar
Paul committed
357
            results.emplace(ins, trace(ins, [&] { return argument{ins->get_shape(), nullptr}; }));
Paul's avatar
Paul committed
358
        }
Paul's avatar
Paul committed
359
360
        else
        {
Paul's avatar
Paul committed
361
            values.resize(ins->inputs().size());
Paul's avatar
Paul committed
362
363
364
365
366
            std::transform(
                ins->inputs().begin(), ins->inputs().end(), values.begin(), [&](instruction_ref i) {
                    assert(results.find(i) != results.end());
                    return results[i];
                });
Paul's avatar
Paul committed
367
368
369
            results.emplace(ins, trace(ins, [&] {
                                return ins->get_operator().compute(ctx, ins->get_shape(), values);
                            }));
Paul's avatar
Paul committed
370
        }
371
        assert(results.find(ins) != results.end());
Paul's avatar
Paul committed
372
    }
373
    return results.at(std::prev(p.end()));
Paul's avatar
Paul committed
374
375
}

Paul's avatar
Paul committed
376
377
argument program::eval(std::unordered_map<std::string, argument> params) const
{
Paul's avatar
Paul committed
378
379
    auto& ctx = this->impl->ctx;
#ifndef NDEBUG
Paul's avatar
Paul committed
380
    auto sctx          = ctx;
Paul's avatar
Paul committed
381
382
383
    auto check_context = [&](auto f) {
        assert(is_shared(ctx, sctx));
        auto x = f();
Paul's avatar
Paul committed
384
        sctx   = ctx;
Paul's avatar
Paul committed
385
386
387
        return x;
    };
#else
Paul's avatar
Paul committed
388
    auto check_context = [](auto f) { return f(); };
Paul's avatar
Paul committed
389
#endif
Paul's avatar
Paul committed
390
    if(enabled(MIGRAPHX_TRACE_EVAL{}))
Paul's avatar
Paul committed
391
    {
Paul's avatar
Paul committed
392
        return generic_eval(*this, ctx, std::move(params), [&](auto& ins, auto f) {
Paul's avatar
Paul committed
393
            ctx.finish();
Paul's avatar
Paul committed
394
395
            std::cout << "Run instruction: ";
            this->debug_print(ins);
Paul's avatar
Paul committed
396
            return check_context(f);
Paul's avatar
Paul committed
397
        });
Paul's avatar
Paul committed
398
399
400
401
    }
    else
    {
        return generic_eval(
Paul's avatar
Paul committed
402
            *this, ctx, std::move(params), [&](auto&, auto f) { return check_context(f); });
Paul's avatar
Paul committed
403
    }
Paul's avatar
Paul committed
404
405
}

Paul's avatar
Paul committed
406
407
408
double common_average(const std::vector<double>& v)
{
    std::size_t n = v.size() / 4;
Paul's avatar
Paul committed
409
410
    double total  = std::accumulate(v.begin() + n, v.end() - n, 0.0);
    return total / std::distance(v.begin() + n, v.end() - n);
Paul's avatar
Paul committed
411
412
}

Paul's avatar
Paul committed
413
414
415
void program::perf_report(std::ostream& os, std::size_t n, parameter_map params) const
{
    using milliseconds = std::chrono::duration<double, std::milli>;
Paul's avatar
Paul committed
416
    auto& ctx          = this->impl->ctx;
Paul's avatar
Paul committed
417
418
    // Run once by itself
    eval(params);
Paul's avatar
Paul committed
419
    ctx.finish();
Paul's avatar
Paul committed
420
    // Run and time entire program
Paul's avatar
Paul committed
421
422
    std::vector<double> total_vec;
    total_vec.reserve(n);
Paul's avatar
Paul committed
423
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
424
    {
Paul's avatar
Paul committed
425
426
427
428
        total_vec.push_back(time<milliseconds>([&] {
            eval(params);
            ctx.finish();
        }));
Paul's avatar
Paul committed
429
    }
Paul's avatar
Paul committed
430
431
    std::sort(total_vec.begin(), total_vec.end());
    std::unordered_map<instruction_ref, std::vector<double>> ins_vec;
Paul's avatar
Paul committed
432
    // Fill the map
Paul's avatar
Paul committed
433
    generic_eval(*this, ctx, params, [&](auto ins, auto) {
Paul's avatar
Paul committed
434
        ins_vec[ins].reserve(n);
Paul's avatar
Paul committed
435
436
        return argument{};
    });
Paul's avatar
Paul committed
437
    // Run and time each instruction
Paul's avatar
Paul committed
438
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
439
    {
Paul's avatar
Paul committed
440
        generic_eval(*this, ctx, params, [&](auto ins, auto f) {
441
            argument result;
Paul's avatar
Paul committed
442
443
444
445
            ins_vec[ins].push_back(time<milliseconds>([&] {
                result = f();
                ctx.finish();
            }));
446
            return result;
Paul's avatar
Paul committed
447
448
        });
    }
Paul's avatar
Paul committed
449
450
    for(auto&& p : ins_vec)
        std::sort(p.second.begin(), p.second.end());
Paul's avatar
Paul committed
451
    // Run and time implicit overhead
Paul's avatar
Paul committed
452
453
    std::vector<double> overhead_vec;
    overhead_vec.reserve(n);
Paul's avatar
Paul committed
454
    for(std::size_t i = 0; i < n; i++)
Paul's avatar
Paul committed
455
    {
Paul's avatar
Paul committed
456
        overhead_vec.push_back(time<milliseconds>([&] { dry_run(params); }));
Paul's avatar
Paul committed
457
458
    }

Paul's avatar
Paul committed
459
    double total_time             = common_average(total_vec);
Paul's avatar
Paul committed
460
    double rate                   = 1000.0 / total_time;
Paul's avatar
Paul committed
461
    double overhead_time          = common_average(overhead_vec);
Paul's avatar
Paul committed
462
    double overhead_percent       = overhead_time * 100.0 / total_time;
Paul's avatar
Paul committed
463
    double total_instruction_time = 0.0;
Paul's avatar
Paul committed
464
    std::unordered_map<std::string, double> op_times;
Paul's avatar
Paul committed
465
    for(auto&& p : ins_vec)
Paul's avatar
Paul committed
466
467
    {
        double avg = common_average(p.second);
Paul's avatar
Paul committed
468
        op_times[p.first->name()] += avg;
Paul's avatar
Paul committed
469
470
        total_instruction_time += avg;
    }
Paul's avatar
Paul committed
471
472
    double calculate_overhead_time    = total_time - total_instruction_time;
    double calculate_overhead_percent = calculate_overhead_time * 100.0 / total_time;
Paul's avatar
Paul committed
473

474
    print_program(*this, [&](auto ins, const auto& names) {
Khalique's avatar
Khalique committed
475
        print_instruction(std::cout, ins, names);
Paul's avatar
Paul committed
476
477
478
        double avg     = common_average(ins_vec[ins]);
        double percent = std::ceil(100.0 * avg / total_instruction_time);
        os << ": " << avg << "ms, " << percent << "%";
479
        os << std::endl;
Paul's avatar
Paul committed
480
    });
Paul's avatar
Paul committed
481
482
483

    os << std::endl;
    os << "Summary:" << std::endl;
Paul's avatar
Paul committed
484
    for(auto&& p : op_times)
Paul's avatar
Paul committed
485
    {
Paul's avatar
Paul committed
486
487
        auto&& name    = p.first;
        double avg     = p.second;
Paul's avatar
Paul committed
488
489
490
491
492
        double percent = std::ceil(100.0 * avg / total_instruction_time);
        os << name << ": " << avg << "ms, " << percent << "%" << std::endl;
    }

    os << std::endl;
Paul's avatar
Paul committed
493

Paul's avatar
Paul committed
494
    os << "Rate: " << rate << "/sec" << std::endl;
Paul's avatar
Paul committed
495
496
    os << "Total time: " << total_time << "ms" << std::endl;
    os << "Total instructions time: " << total_instruction_time << "ms" << std::endl;
Paul's avatar
Paul committed
497
498
499
500
    os << "Overhead time: " << overhead_time << "ms"
       << ", " << calculate_overhead_time << "ms" << std::endl;
    os << "Overhead: " << std::round(overhead_percent) << "%"
       << ", " << std::round(calculate_overhead_percent) << "%" << std::endl;
Paul's avatar
Paul committed
501
502
}

Paul's avatar
Paul committed
503
504
void program::debug_print() const { std::cout << *this << std::endl; }
void program::debug_print(instruction_ref ins) const
Paul's avatar
Paul committed
505
{
Paul's avatar
Paul committed
506
507
508
509
510
511
512
513
514
515
    if(ins == this->end())
    {
        std::cout << "End instruction" << std::endl;
        return;
    }
    if(not has_instruction(ins))
    {
        std::cout << "Instruction not part of program" << std::endl;
        return;
    }
Paul's avatar
Paul committed
516
    std::stringstream ss;
517
    print_program(*this, [&](auto x, const auto& names) {
Paul's avatar
Paul committed
518
        if(x == ins)
Paul's avatar
Paul committed
519
520
521
522
523
524
        {
            print_instruction(std::cout, x, names);
            std::cout << std::endl;
        }
    });
}
Paul's avatar
Paul committed
525
void program::debug_print(const std::vector<instruction_ref>& inss) const
Paul's avatar
Paul committed
526
{
Paul's avatar
Paul committed
527
    for(auto ins : inss)
Paul's avatar
Paul committed
528
529
530
531
        debug_print(ins);
    std::cout << std::endl;
}

Khalique's avatar
Khalique committed
532
static std::string enclose_name(const std::string& name)
533
{
Khalique's avatar
Khalique committed
534
535
    std::string new_name = name;
    return '"' + replace_string(new_name, "\"", "\\\"") + '"';
536
537
}

538
539
540
541
void program::print_graph(std::ostream& os) const
{
    os << "digraph {" << std::endl;
    os << "\trankdir=LR;" << std::endl;
542
    print_program(*this, [&](auto ins, const auto& names) {
Khalique's avatar
Khalique committed
543
544
        os << "\t" << enclose_name(names.at(ins))
           << "[label=" << enclose_name(to_string(ins->get_operator())) << "];";
545
546
547
548
549
550
551
552
553
554
555
        os << std::endl;
        if(!ins->inputs().empty())
        {
            for(auto&& arg : ins->inputs())
            {
                os << "\t" << enclose_name(names.at(arg)) << " -> " << enclose_name(names.at(ins));
                os << "[label=" << enclose_name(to_string(ins->get_shape())) << "];";
                os << std::endl;
            }
        }
    });
556
557
558
    os << "}" << std::endl;
}

Paul's avatar
Paul committed
559
560
void program::dry_run(std::unordered_map<std::string, argument> params) const
{
Paul's avatar
Paul committed
561
    auto& ctx = this->impl->ctx;
Paul's avatar
Paul committed
562
    generic_eval(*this, ctx, std::move(params), [](auto&&...) { return argument{}; });
Paul's avatar
Paul committed
563
564
}

Paul's avatar
Paul committed
565
566
void program::annotate(std::ostream& os, std::function<void(instruction_ref)> a) const
{
567
568
569
570
571
    print_program(*this, [&](auto ins, const auto& names) {
        print_instruction(os, ins, names);
        a(ins);
        os << std::endl;
    });
Paul's avatar
Paul committed
572
573
}

Paul's avatar
Paul committed
574
bool operator==(const program& x, const program& y) { return to_string(x) == to_string(y); }
Paul's avatar
Paul committed
575

Paul's avatar
Paul committed
576
std::ostream& operator<<(std::ostream& os, const program& p)
Paul's avatar
Paul committed
577
{
578
579
580
581
    print_program(p, [&](auto ins, const auto& names) {
        print_instruction(os, ins, names);
        os << std::endl;
    });
Paul's avatar
Paul committed
582
    return os;
Paul's avatar
Paul committed
583
}
Paul's avatar
Paul committed
584

Paul's avatar
Paul committed
585
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
586
} // namespace migraphx