Compute Library
 22.05
ClContext.cpp
Go to the documentation of this file.
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 
30 
31 namespace arm_compute
32 {
33 namespace gpu
34 {
35 namespace opencl
36 {
37 namespace
38 {
39 mlgo::MLGOHeuristics populate_mlgo(const char *filename)
40 {
41  bool status = false;
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 
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 
67 {
68  return _mlgo_heuristics;
69 }
70 
71 ::cl::Context ClContext::cl_ctx()
72 {
73  return _cl_ctx;
74 }
75 
76 ::cl::Device ClContext::cl_dev()
77 {
78  return _cl_dev;
79 }
80 
81 bool ClContext::set_cl_ctx(::cl::Context ctx)
82 {
83  if(this->refcount() == 0)
84  {
85  _cl_ctx = ctx;
87  return true;
88  }
89  return false;
90 }
91 
93 {
94  ClTensor *tensor = new ClTensor(this, desc);
95  if(tensor != nullptr && allocate)
96  {
97  tensor->allocate();
98  }
99  return tensor;
100 }
101 
103 {
104  return new ClQueue(this, options);
105 }
106 } // namespace opencl
107 } // namespace gpu
108 } // namespace arm_compute
ITensorV2 * create_tensor(const AclTensorDescriptor &desc, bool allocate) override
Create a tensor object.
Definition: ClContext.cpp:92
Base class specifying the queue interface.
Definition: IQueue.h:41
bool reload_from_file(const std::string &filename)
(Re)Load the heuristics from reading a dotmlgo file
static CLScheduler & get()
Access the scheduler singleton.
Base class specifying the tensor interface.
Definition: ITensorV2.h:45
::cl::Context cl_ctx()
Underlying cl context accessor.
Definition: ClContext.cpp:71
void set_context(cl::Context context)
Accessor to set the CL context to be used by the scheduler.
OpenCL queue implementation class.
Definition: ClQueue.h:43
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
const char * kernel_config_file
Kernel cofiguration file.
Definition: AclTypes.h:139
StatusCode allocate()
Allocates tensor.
Definition: ClTensor.cpp:70
Context interface.
Definition: IContext.h:50
::cl::Device cl_dev()
Underlying cl device accessor.
Definition: ClContext.cpp:76
bool set_cl_ctx(::cl::Context ctx)
Update/inject an underlying cl context object.
Definition: ClContext.cpp:81
Copyright (c) 2017-2022 Arm Limited.
const mlgo::MLGOHeuristics & mlgo() const
Extract MLGO heuristics.
Definition: ClContext.cpp:66
int refcount() const
Reference counter accessor.
Definition: IContext.h:84
MLGOHeuristics for configuring GEMM kernels.
OpenCL tensor implementation class.
Definition: ClTensor.h:38
IQueue * create_queue(const AclQueueOptions *options) override
Create a queue object.
Definition: ClContext.cpp:102
ClContext(const AclContextOptions *options)
Default Constructor.
Definition: ClContext.cpp:52
const cl::Device & get_device()
Gets the CL device for which the programs are created.
cl::Context & context()
Accessor for the associated CL context.