• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 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_DATA_QUEUE_MANAGER_H_
18 #define MINDSPORE_CCSRC_DISTRIBUTED_EMBEDDING_CACHE_DATA_QUEUE_MANAGER_H_
19 
20 #include <memory>
21 #include <utility>
22 #include <string>
23 #include "include/backend/distributed/embedding_cache/blocking_queue.h"
24 #include "include/backend/distributed/embedding_cache/embedding_cache_utils.h"
25 #include "include/backend/visible.h"
26 
27 namespace mindspore {
28 namespace distributed {
29 using IdsDataQueue = BlockingQueue<IdDataInfo>;
30 using IndicesDataQueue = BlockingQueue<IndexDataInfo>;
31 using IdsAndIndicesDataQueuePair = std::pair<std::shared_ptr<IdsDataQueue>, std::shared_ptr<IndicesDataQueue>>;
32 
33 // This class is used to manage the Cache prefetch queue in the Embedding Cache mode.
34 class BACKEND_EXPORT DataQueueManager {
35  public:
36   static DataQueueManager &GetInstance();
37 
38   void CreateDataQueue(const std::string &channel_name, size_t sink_size, size_t capacity);
39 
40   const IdsAndIndicesDataQueuePair &GetDataQueue(const std::string &channel_name) const;
41 
42   size_t GetSinkSize(const std::string &channel_name) const;
43 
44   void CloseAllQueues();
45 
IsClosed()46   bool IsClosed() { return closed_.load(); }
47 
48  private:
49   DataQueueManager() = default;
50   ~DataQueueManager() = default;
51   DISABLE_COPY_AND_ASSIGN(DataQueueManager);
52 
53   mindspore::HashMap<std::string, IdsAndIndicesDataQueuePair> channels_to_queues_;
54 
55   mindspore::HashMap<std::string, size_t> channels_to_sink_sizes_;
56 
57   std::atomic_bool closed_{false};
58 
59   std::mutex mtx_;
60 };
61 }  // namespace distributed
62 }  // namespace mindspore
63 #endif  // MINDSPORE_CCSRC_DISTRIBUTED_EMBEDDING_CACHE_DATA_QUEUE_MANAGER_H_
64