Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | #ifndef __ARM_COMPUTE_TEST_TENSOR_CACHE_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_CACHE_H__ |
| 26 | |
| 27 | #include "RawTensor.h" |
| 28 | |
| 29 | #include <map> |
| 30 | #include <mutex> |
| 31 | #include <utility> |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace test |
| 36 | { |
| 37 | /** Stores @ref RawTensor categorised by the image they are created from |
| 38 | * including name, format and channel. |
| 39 | */ |
| 40 | class TensorCache |
| 41 | { |
| 42 | public: |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 43 | /** Search the cache for a tensor of created from the specified image and |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | * format. |
| 45 | * |
| 46 | * @param[in] key Key to look up the tensor. Consists of image name and format. |
| 47 | * |
| 48 | * @return The cached tensor matching the image name and format if found. A |
| 49 | * nullptr otherwise. |
| 50 | */ |
| 51 | RawTensor *find(std::tuple<const std::string &, Format> key); |
| 52 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 53 | /** Search the cache for a tensor of created from the specified image, |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | * format and channel. |
| 55 | * |
| 56 | * @param[in] key Key to look up the tensor. Consists of image name, format and channel. |
| 57 | * |
| 58 | * @return The cached tensor matching the image name and format if found. A |
| 59 | * nullptr otherwise. |
| 60 | */ |
| 61 | RawTensor *find(std::tuple<const std::string &, Format, Channel> key); |
| 62 | |
| 63 | /** Add the given tensor to the cache. Can later be found under the given |
| 64 | * image name and format. |
| 65 | * |
| 66 | * @param[in] key Key under which to store the tensor. Consists of image name and format. |
| 67 | * @param[in] raw Raw tensor to be stored. |
| 68 | * |
| 69 | * @return A reference to the cached tensor. |
| 70 | */ |
| 71 | RawTensor &add(std::tuple<const std::string &, Format> key, RawTensor raw); |
| 72 | |
| 73 | /** Add the given tensor to the cache. Can later be found under the given |
| 74 | * image name, format and channel. |
| 75 | * |
| 76 | * @param[in] key Key under which to store the tensor. Consists of image name, format and channel. |
| 77 | * @param[in] raw Raw tensor to be stored. |
| 78 | * |
| 79 | * @return A reference to the cached tensor. |
| 80 | */ |
| 81 | RawTensor &add(std::tuple<const std::string &, Format, Channel> key, RawTensor raw); |
| 82 | |
| 83 | private: |
| 84 | using FormatMap = std::map<std::tuple<std::string, Format>, RawTensor>; |
| 85 | using ChannelMap = std::map<std::tuple<std::string, Format, Channel>, RawTensor>; |
| 86 | |
| 87 | FormatMap _raw_tensor_cache{}; |
| 88 | ChannelMap _raw_tensor_channel_cache{}; |
| 89 | std::mutex _raw_tensor_cache_mutex{}; |
| 90 | std::mutex _raw_tensor_channel_cache_mutex{}; |
| 91 | }; |
| 92 | |
| 93 | inline RawTensor *TensorCache::find(std::tuple<const std::string &, Format> key) |
| 94 | { |
| 95 | const auto it = _raw_tensor_cache.find(key); |
| 96 | return it == _raw_tensor_cache.end() ? nullptr : &it->second; |
| 97 | } |
| 98 | |
| 99 | inline RawTensor *TensorCache::find(std::tuple<const std::string &, Format, Channel> key) |
| 100 | { |
| 101 | const auto it = _raw_tensor_channel_cache.find(key); |
| 102 | return it == _raw_tensor_channel_cache.end() ? nullptr : &it->second; |
| 103 | } |
| 104 | |
| 105 | inline RawTensor &TensorCache::add(std::tuple<const std::string &, Format> key, RawTensor raw) |
| 106 | { |
| 107 | std::lock_guard<std::mutex> lock(_raw_tensor_channel_cache_mutex); |
| 108 | return std::get<0>(_raw_tensor_cache.emplace(std::move(key), std::move(raw)))->second; |
| 109 | } |
| 110 | |
| 111 | inline RawTensor &TensorCache::add(std::tuple<const std::string &, Format, Channel> key, RawTensor raw) |
| 112 | { |
| 113 | std::lock_guard<std::mutex> lock(_raw_tensor_channel_cache_mutex); |
| 114 | return std::get<0>(_raw_tensor_channel_cache.emplace(std::move(key), std::move(raw)))->second; |
| 115 | } |
| 116 | } // namespace test |
| 117 | } // namespace arm_compute |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 118 | #endif /* __ARM_COMPUTE_TEST_TENSOR_CACHE_H__ */ |