blob: 611c1cb5018bceffe4f53bd28c038eaf20827289 [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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
27
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000028#include "src/gpu/cl/ClQueue.h"
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000029#include "src/gpu/cl/ClTensor.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
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 if (filename != nullptr)
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000045 {
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)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 : IContext(Target::GpuOcl), _mlgo_heuristics(), _cl_ctx(), _cl_dev()
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000054{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010055 if (options != nullptr)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000056 {
57 _mlgo_heuristics = populate_mlgo(options->kernel_config_file);
58 }
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000059 _cl_ctx = CLKernelLibrary::get().context();
60 _cl_dev = CLKernelLibrary::get().get_device();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000061}
62
63const mlgo::MLGOHeuristics &ClContext::mlgo() const
64{
65 return _mlgo_heuristics;
66}
67
68::cl::Context ClContext::cl_ctx()
69{
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000070 return _cl_ctx;
71}
72
73::cl::Device ClContext::cl_dev()
74{
75 return _cl_dev;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000076}
77
78bool ClContext::set_cl_ctx(::cl::Context ctx)
79{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 if (this->refcount() == 0)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000081 {
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000082 _cl_ctx = ctx;
83 CLScheduler::get().set_context(ctx);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000084 return true;
85 }
86 return false;
87}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000088
89ITensorV2 *ClContext::create_tensor(const AclTensorDescriptor &desc, bool allocate)
90{
91 ClTensor *tensor = new ClTensor(this, desc);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010092 if (tensor != nullptr && allocate)
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000093 {
94 tensor->allocate();
95 }
96 return tensor;
97}
Georgios Pinitasc3c352e2021-03-18 10:59:40 +000098
99IQueue *ClContext::create_queue(const AclQueueOptions *options)
100{
101 return new ClQueue(this, options);
102}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000103} // namespace opencl
104} // namespace gpu
105} // namespace arm_compute