aboutsummaryrefslogtreecommitdiff
path: root/src/backends/cl/ClBackendContext.cpp
blob: 7789415f89cb2d68061dc4b73a084fa5e2abb18b (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ClBackendContext.hpp"
#include "ClBackendId.hpp"
#include "ClContextControl.hpp"

#include <backendsCommon/BackendContextRegistry.hpp>
#include <boost/log/trivial.hpp>

#include <mutex>

#ifdef ARMCOMPUTECL_ENABLED
// Needed for the CL scheduler calls
#include <arm_compute/core/CL/OpenCL.h>
#include <arm_compute/core/CL/CLKernelLibrary.h>
#include <arm_compute/runtime/CL/CLScheduler.h>
#endif

namespace armnn
{

namespace
{

static StaticRegistryInitializer<BackendContextRegistry> g_RegisterHelper
{
    BackendContextRegistryInstance(),
    ClBackendId(),
    [](const IRuntime::CreationOptions& options)
    {
        return IBackendContextUniquePtr(new ClBackendContext{options});
    }
};

static std::mutex g_ContextControlMutex;

std::shared_ptr<ClBackendContext::ContextControlWrapper>
GetContextControlWrapper(const IRuntime::CreationOptions& options)
{
    static std::weak_ptr<ClBackendContext::ContextControlWrapper> contextControlWrapper;

    std::lock_guard<std::mutex> lockGuard(g_ContextControlMutex);
    std::shared_ptr<ClBackendContext::ContextControlWrapper> result;

    if (contextControlWrapper.expired())
    {
        result = std::make_shared<ClBackendContext::ContextControlWrapper>(options);
        contextControlWrapper = result;
    }
    else
    {
        result = contextControlWrapper.lock();
    }

    return result;
}

} // anonymous namespace


#ifdef ARMCOMPUTECL_ENABLED
struct ClBackendContext::ContextControlWrapper
{
    ContextControlWrapper(const IRuntime::CreationOptions& options)
    : m_ClContextControl{options.m_GpuAccTunedParameters.get(),
                         options.m_EnableGpuProfiling}
    {
    }

    ~ContextControlWrapper()
    {
        if (arm_compute::CLScheduler::get().context()() != NULL)
        {
            // Waits for all queued CL requests to finish before unloading the network they may be using.
            try
            {
                // Coverity fix: arm_compute::CLScheduler::sync() may throw an exception of type cl::Error.
                arm_compute::CLScheduler::get().sync();
                m_ClContextControl.ClearClCache();
            }
            catch (const cl::Error&)
            {
                BOOST_LOG_TRIVIAL(warning) << "WARNING: Runtime::UnloadNetwork(): an error occurred while waiting for "
                                            "the queued CL requests to finish";
            }
        }
    }

    ClContextControl m_ClContextControl;
};
#else //ARMCOMPUTECL_ENABLED
struct ClBackendContext::ContextControlWrapper
{
    ContextControlWrapper(const IRuntime::CreationOptions&){}
};
#endif //ARMCOMPUTECL_ENABLED

ClBackendContext::ClBackendContext(const IRuntime::CreationOptions& options)
: IBackendContext{options}
, m_ContextControl{GetContextControlWrapper(options)}
{
}

ClBackendContext::~ClBackendContext()
{
}

} // namespace armnn