profiler.cpp 1.9 KB
Newer Older
1
/**
M
Megvii Engine Team 已提交
2 3
 * \file imperative/src/impl/profiler.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
4
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6
 *
M
Megvii Engine Team 已提交
7 8 9
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 11 12 13
 */

#include "megbrain/imperative/profiler.h"

14 15
#include <chrono>

16 17 18
#include "megbrain/imperative/ops/opr_attr.h"
#include "megbrain/imperative/physical_tensor.h"

19 20
#include "megbrain/plugin/opr_footprint.h"

21
#include "./function_hook.h"
22
#include "./event_pool.h"
23 24 25 26 27 28
#include "./op_trait.h"

namespace mgb {
namespace imperative {

namespace {
29 30 31 32 33 34 35

DeviceTimer::SharedEvent alloc_recorded_event(CompNode device) {
    auto event = EventPool::with_timer().alloc_shared(device);
    event->record();
    return event;
}

36
}  // namespace
37

38 39
DeviceTimer::SharedEvent DeviceTimer::get_device_time(CompNode device) {
    return alloc_recorded_event(device);
40 41
}

42 43 44 45
SmallVector<DeviceTimer::SharedEvent> DeviceTimer::get_all(SmallVector<CompNode> device_list) {
    SmallVector<DeviceTimer::SharedEvent> results;
    for (auto&& device: device_list) {
        results.push_back(alloc_recorded_event(device));
46
    }
47
    return results;
48 49
}

50 51 52 53 54
double HostTimer::get_msecs() {
    using namespace std::chrono;
    auto finish = steady_clock::now();
    auto duration = duration_cast<microseconds>(finish - m_start);
    return (double)duration.count() / 1e3;
55 56
}

57 58
double HostTimer::get_started_at() {
    return m_started_at;
59 60
}

61 62 63 64 65
void HostTimer::reset() {
    using namespace std::chrono;
    m_start = steady_clock::now();
    auto now_us = duration_cast<microseconds>(std::chrono::system_clock::now().time_since_epoch());
    m_started_at = (double)(now_us.count()) / 1e3;
66 67
}

68 69 70
}  // namespace imperative

}  // namespace mgb