• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
26 #include "src/gpu/cl/ClQueue.h"
27 #include "src/gpu/cl/ClTensor.h"
28 
29 #include "arm_compute/core/CL/CLKernelLibrary.h"
30 
31 namespace arm_compute
32 {
33 namespace gpu
34 {
35 namespace opencl
36 {
37 namespace
38 {
populate_mlgo(const char * filename)39 mlgo::MLGOHeuristics populate_mlgo(const char *filename)
40 {
41     bool                 status = false;
42     mlgo::MLGOHeuristics heuristics;
43 
44     if(filename != nullptr)
45     {
46         status = heuristics.reload_from_file(filename);
47     }
48     return status ? std::move(heuristics) : mlgo::MLGOHeuristics();
49 }
50 } // namespace
51 
ClContext(const AclContextOptions * options)52 ClContext::ClContext(const AclContextOptions *options)
53     : IContext(Target::GpuOcl),
54       _mlgo_heuristics(),
55       _cl_ctx(),
56       _cl_dev()
57 {
58     if(options != nullptr)
59     {
60         _mlgo_heuristics = populate_mlgo(options->kernel_config_file);
61     }
62     _cl_ctx = CLKernelLibrary::get().context();
63     _cl_dev = CLKernelLibrary::get().get_device();
64 }
65 
mlgo() const66 const mlgo::MLGOHeuristics &ClContext::mlgo() const
67 {
68     return _mlgo_heuristics;
69 }
70 
cl_ctx()71 ::cl::Context ClContext::cl_ctx()
72 {
73     return _cl_ctx;
74 }
75 
cl_dev()76 ::cl::Device ClContext::cl_dev()
77 {
78     return _cl_dev;
79 }
80 
set_cl_ctx(::cl::Context ctx)81 bool ClContext::set_cl_ctx(::cl::Context ctx)
82 {
83     if(this->refcount() == 0)
84     {
85         _cl_ctx = ctx;
86         CLScheduler::get().set_context(ctx);
87         return true;
88     }
89     return false;
90 }
91 
create_tensor(const AclTensorDescriptor & desc,bool allocate)92 ITensorV2 *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 }
101 
create_queue(const AclQueueOptions * options)102 IQueue *ClContext::create_queue(const AclQueueOptions *options)
103 {
104     return new ClQueue(this, options);
105 }
106 } // namespace opencl
107 } // namespace gpu
108 } // namespace arm_compute
109