1 /** 2 * Copyright 2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_DISTRIBUTED_EMBEDDING_CACHE_CACHE_STRATEGY_CHCHE_H_ 18 #define MINDSPORE_CCSRC_DISTRIBUTED_EMBEDDING_CACHE_CACHE_STRATEGY_CHCHE_H_ 19 20 #include <utility> 21 #include <vector> 22 #include <list> 23 24 namespace mindspore { 25 namespace distributed { 26 // An abstract class of general cache strategy that provides basic APIs for cache management, such as element access and 27 // modification APIs: Get, Put, and query whether the cache hits API: Exists, etc. 28 template <typename KeyType, typename ValueType> 29 class Cache { 30 public: 31 // The elements in cache are stored as key-value pairs. 32 using Element = std::pair<KeyType, ValueType>; 33 Cache(size_t capacity)34 explicit Cache(size_t capacity) : capacity_(capacity) {} 35 virtual ~Cache() = default; 36 37 // Insert an element (key-value pair) into the cache. 38 // Inserting an element into the cache generally affects the location or order of the elements in the cache, depending 39 // on different cache strategies. 40 virtual void Put(const KeyType &key, const ValueType &value) = 0; 41 42 // Query the corresponding Value from the cache according to the Key. If the element exists, the corresponding Value 43 // is assigned to parameter value and return true. If the element does not exist, return false. 44 // Access an element of the cache generally affects the location or order of the elements in the cache, depending 45 // on different cache strategies. 46 virtual bool Get(const KeyType &key, ValueType *value) = 0; 47 48 // Get the most recently used element. 49 virtual const Element &Front() const = 0; 50 51 // Get the least recently used element. 52 virtual const Element &Back() const = 0; 53 54 // Query whether the element corresponding to a particular key exists in the cache. 55 virtual bool Exists(const KeyType &key) const = 0; 56 57 // When the size of the cache is close to capacity, you can use this interface to evict some non-hot data to reserve 58 // space for new elements to be inserted into the cache. If the current cache has enough free space, this function 59 // does nothing. 60 // The input parameter 'reserve_size' indicates the number of element slots that are expected to be reserved. If the 61 // reserve_size is less than or equal to the number of slots remaining in the cache, the function does nothing. 62 // The output parameter 'evicted_elements' is used to hold the evicted element. 63 virtual void TryEvict(size_t reserve_size, std::vector<Element> *evicted_elements) = 0; 64 65 // Check whether the number of elements in cache reaches capacity. 66 virtual bool IsFull() const = 0; 67 68 // Get the current number of elements in the cache. 69 virtual size_t size() const = 0; 70 71 // Dump all elements in the cache. 72 virtual const std::list<Element> &Export() const = 0; 73 74 // Get the maximum number of elements that the cache can hold. capacity()75 size_t capacity() const { return capacity_; } 76 77 protected: 78 // The maximum number of elements that the cache can hold. 79 size_t capacity_; 80 }; 81 } // namespace distributed 82 } // namespace mindspore 83 #endif // MINDSPORE_CCSRC_DISTRIBUTED_EMBEDDING_CACHE_CACHE_STRATEGY_CHCHE_H_ 84