Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 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 | |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 26 | #include "src/gpu/cl/ClQueue.h" |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 27 | #include "src/gpu/cl/ClTensor.h" |
| 28 | |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 30 | |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 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 | { |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 41 | bool status = false; |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 42 | mlgo::MLGOHeuristics heuristics; |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 43 | |
| 44 | if(filename != nullptr) |
| 45 | { |
| 46 | status = heuristics.reload_from_file(filename); |
| 47 | } |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 48 | return status ? std::move(heuristics) : mlgo::MLGOHeuristics(); |
| 49 | } |
| 50 | } // namespace |
| 51 | |
| 52 | ClContext::ClContext(const AclContextOptions *options) |
| 53 | : IContext(Target::GpuOcl), |
| 54 | _mlgo_heuristics(), |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 55 | _cl_ctx(), |
| 56 | _cl_dev() |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 57 | { |
| 58 | if(options != nullptr) |
| 59 | { |
| 60 | _mlgo_heuristics = populate_mlgo(options->kernel_config_file); |
| 61 | } |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 62 | _cl_ctx = CLKernelLibrary::get().context(); |
| 63 | _cl_dev = CLKernelLibrary::get().get_device(); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | const mlgo::MLGOHeuristics &ClContext::mlgo() const |
| 67 | { |
| 68 | return _mlgo_heuristics; |
| 69 | } |
| 70 | |
| 71 | ::cl::Context ClContext::cl_ctx() |
| 72 | { |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 73 | return _cl_ctx; |
| 74 | } |
| 75 | |
| 76 | ::cl::Device ClContext::cl_dev() |
| 77 | { |
| 78 | return _cl_dev; |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool ClContext::set_cl_ctx(::cl::Context ctx) |
| 82 | { |
| 83 | if(this->refcount() == 0) |
| 84 | { |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 85 | _cl_ctx = ctx; |
| 86 | CLScheduler::get().set_context(ctx); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 91 | |
| 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 | } |
Georgios Pinitas | c3c352e | 2021-03-18 10:59:40 +0000 | [diff] [blame] | 101 | |
| 102 | IQueue *ClContext::create_queue(const AclQueueOptions *options) |
| 103 | { |
| 104 | return new ClQueue(this, options); |
| 105 | } |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 106 | } // namespace opencl |
| 107 | } // namespace gpu |
| 108 | } // namespace arm_compute |