blob: d8ef18e62e3acb80719b35c30e9779c7f75df4d0 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "src/gpu/cl/ClContext.h"
25
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000026#include "src/gpu/cl/ClQueue.h"
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000027#include "src/gpu/cl/ClTensor.h"
28
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000029#include "arm_compute/core/CL/CLKernelLibrary.h"
30
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000031namespace arm_compute
32{
33namespace gpu
34{
35namespace opencl
36{
37namespace
38{
39mlgo::MLGOHeuristics populate_mlgo(const char *filename)
40{
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000041 bool status = false;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000042 mlgo::MLGOHeuristics heuristics;
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000043
44 if(filename != nullptr)
45 {
46 status = heuristics.reload_from_file(filename);
47 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000048 return status ? std::move(heuristics) : mlgo::MLGOHeuristics();
49}
50} // namespace
51
52ClContext::ClContext(const AclContextOptions *options)
53 : IContext(Target::GpuOcl),
54 _mlgo_heuristics(),
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000055 _cl_ctx(),
56 _cl_dev()
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000057{
58 if(options != nullptr)
59 {
60 _mlgo_heuristics = populate_mlgo(options->kernel_config_file);
61 }
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000062 _cl_ctx = CLKernelLibrary::get().context();
63 _cl_dev = CLKernelLibrary::get().get_device();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000064}
65
66const mlgo::MLGOHeuristics &ClContext::mlgo() const
67{
68 return _mlgo_heuristics;
69}
70
71::cl::Context ClContext::cl_ctx()
72{
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000073 return _cl_ctx;
74}
75
76::cl::Device ClContext::cl_dev()
77{
78 return _cl_dev;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000079}
80
81bool ClContext::set_cl_ctx(::cl::Context ctx)
82{
83 if(this->refcount() == 0)
84 {
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000085 _cl_ctx = ctx;
86 CLScheduler::get().set_context(ctx);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000087 return true;
88 }
89 return false;
90}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000091
92ITensorV2 *ClContext::create_tensor(const AclTensorDescriptor &desc, bool allocate)
93{
94 ClTensor *tensor = new ClTensor(this, desc);
95 if(tensor != nullptr && allocate)
96 {
97 tensor->allocate();
98 }
99 return tensor;
100}
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000101
102IQueue *ClContext::create_queue(const AclQueueOptions *options)
103{
104 return new ClQueue(this, options);
105}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000106} // namespace opencl
107} // namespace gpu
108} // namespace arm_compute