aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/AsyncExecutionCallback.cpp
blob: 2973e2d8917ffad8b2cf6dc4e73b2314f43c6423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include <AsyncExecutionCallback.hpp>

namespace armnn
{

namespace experimental
{

void AsyncExecutionCallback::Notify(armnn::Status status, InferenceTimingPair timeTaken)
{
    {
        std::lock_guard<std::mutex> hold(m_Mutex);
        // store results and mark as notified
        m_Status    = status;
        m_StartTime = timeTaken.first;
        m_EndTime   = timeTaken.second;
        m_NotificationQueue.push(m_InferenceId);
    }
    m_Condition.notify_all();
}

armnn::Status AsyncExecutionCallback::GetStatus() const
{
    return m_Status;
}

HighResolutionClock AsyncExecutionCallback::GetStartTime() const
{
    return m_StartTime;
}

HighResolutionClock AsyncExecutionCallback::GetEndTime() const
{
    return m_EndTime;
}

std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNewCallback()
{
    auto cb = std::make_unique<AsyncExecutionCallback>(m_NotificationQueue, m_Mutex, m_Condition);
    InferenceId id = cb->GetInferenceId();
    m_Callbacks.insert({id, std::move(cb)});

    return m_Callbacks.at(id);
}

std::shared_ptr<AsyncExecutionCallback> AsyncCallbackManager::GetNotifiedCallback()
{
    std::unique_lock<std::mutex> lock(m_Mutex);

    m_Condition.wait(lock, [this] { return !m_NotificationQueue.empty(); });

    InferenceId id = m_NotificationQueue.front();
    m_NotificationQueue.pop();

    std::shared_ptr<AsyncExecutionCallback> callback = m_Callbacks.at(id);
    m_Callbacks.erase(id);
    return callback;
}

} // namespace experimental

} // namespace armnn