• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_HASH_TABLE_H_
17 #define MINDSPORE_CCSRC_RUNTIME_DEVICE_HASH_TABLE_H_
18 
19 #include <vector>
20 #include <string>
21 #include <memory>
22 #include <utility>
23 #include "include/common/utils/utils.h"
24 
25 namespace mindspore {
26 using DataLenPair = std::pair<void *, size_t>;
27 
28 enum class HashTableElementStatus {
29   kUnchanged = 0,
30   kModified = 1,
31   kErased = 2,
32 };
33 
34 namespace device {
35 constexpr size_t kDefaultSliceSizeInMB = 1024;
36 // The base class of device hash table.
37 template <typename Key, typename Value>
38 class HashTable {
39  public:
40   HashTable() = default;
41   virtual ~HashTable() = default;
42 
43   // Find elements with specific keys, if a key does not exist, initialize the value for the key based on the
44   // initializer and insert the key-value pair into map. The initializer can be 'normal', 'zero' or 'one', and also
45   // could be a specific Value type scalar.
46   virtual bool Find(const Key *keys, size_t key_num, bool insert_default_value, Value *outputs, void *stream) = 0;
47 
48   // Insert elements with specific keys. If key exists, update the value of the key.
49   virtual bool Insert(const Key *keys, size_t key_num, const Value *value, void *stream) = 0;
50 
51   // Insert elements with status for specific keys. If key exists, update the value and status of the key.
Insert(const Key * keys,size_t key_num,const Value * value,HashTableElementStatus * status,void * stream)52   virtual bool Insert(const Key *keys, size_t key_num, const Value *value, HashTableElementStatus *status,
53                       void *stream) {
54     return true;
55   }
56 
57   // Erase elements with specific keys.
58   virtual bool Erase(const Key *keys, size_t key_num, void *stream) = 0;
59 
60   // Reserves space for at least the specified number of elements.
61   virtual bool Reserve(size_t new_capacity, void *stream) = 0;
62 
63   // Export all keys and values in hash map, the order of each element of keys and values is consistent.
64   // Note: Even if the elements of the hash map are unchanged, the order of the key-value pair returned by the function
65   // may be different each time it is called, because there may be multi-threaded concurrent exports inside the
66   // function.
67   virtual bool GetKeysAndValues(Key *keys, Value *values, void *stream) = 0;
68 
69   // Import keys, values into the hash map.
70   virtual bool Import(const DataLenPair &input_data) = 0;
71 
72   // Export keys, values and status.
73   // Argument `incremental` mean the flag that determine whether export hash table in incremental or full manner, true
74   // for incremental export, false for full export.
75   virtual HashTableExportData Export(bool incremental) = 0;
76 
77   // Export a slice from the hash table, the size is specified by the parameter 'slice_size_in_mega_bytes' in MB,
78   // default 1024MB.
79   // Argument `incremental` determine whether export hash table in incremental or full manner, true
80   // for incremental export, false for full export.
81   // Argument `last_slice` is a bool returned to indicate whether the slice by export is the last slice, that is,
82   // the export is complete.
83   virtual HashTableExportData ExportSlice(bool incremental, bool *last_slice,
84                                           size_t slice_size_in_mega_bytes = kDefaultSliceSizeInMB) = 0;
85 
86   // Get the max number of elements the container could hold.
87   virtual size_t capacity() const = 0;
88 
89   // Get the number of elements in the map.
90   virtual size_t size() const = 0;
91 
92   // Gets whether the elements of the hash table have changed since the last export, true means that there has been a
93   // change.
94   virtual bool is_dirty() const = 0;
95 
96   // Clear all elements of hash table.
97   virtual bool Clear() = 0;
98 };
99 }  // namespace device
100 }  // namespace mindspore
101 #endif  // MINDSPORE_CCSRC_RUNTIME_DEVICE_HASH_TABLE_H_
102