blob: 4bb059e2039b6613154d99cc996ec08de506d8fe [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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#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
33namespace arm_compute
34{
35namespace test
36{
37/** Stores @ref RawTensor categorised by the image they are created from
38 * including name, format and channel.
39 */
40class TensorCache
41{
42public:
43 /* Search the cache for a tensor of created from the specified image and
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
53 /* Search the cache for a tensor of created from the specified image,
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
83private:
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
93inline 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
99inline 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
105inline 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
111inline 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 Barbierac69aa12017-07-03 17:39:37 +0100118#endif /* __ARM_COMPUTE_TEST_TENSOR_CACHE_H__ */