• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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_PS_PS_CACHE_PS_DATA_PS_DATA_PREFETCH_H_
17 #define MINDSPORE_CCSRC_PS_PS_CACHE_PS_DATA_PS_DATA_PREFETCH_H_
18 
19 #include <map>
20 #include <string>
21 #include <memory>
22 #include <atomic>
23 #include <condition_variable>
24 #include "include/backend/distributed/ps/ps_cache/ps_data_channel.h"
25 
26 #define EXPORT __attribute__((visibility("default")))
27 
28 namespace mindspore {
29 namespace ps {
30 class EXPORT PsDataPrefetch {
31  public:
32   EXPORT static PsDataPrefetch &GetInstance();
33 
cache_enable()34   EXPORT bool cache_enable() const { return cache_enable_; }
set_cache_enable(bool cache_enable)35   EXPORT void set_cache_enable(bool cache_enable) { cache_enable_ = cache_enable; }
36   EXPORT void CreateDataChannel(const std::string &channel_name, size_t step_num);
37   EXPORT bool PrefetchData(const std::string &channel_name, void *data, const size_t data_size,
38                            const std::string &data_type);
39   EXPORT bool FinalizeData(const std::string &channel_name);
40   EXPORT void NotifyFinalize();
41   EXPORT bool QueryData(const std::string &channel_name, void **data_ptr) const;
42   EXPORT size_t data_size(const std::string &channel_name) const;
43   EXPORT bool TryWakeChannel(const std::string &channel_name) const;
44 
45  private:
PsDataPrefetch()46   PsDataPrefetch() : cache_enable_(false), data_ready_(false) {}
47   virtual ~PsDataPrefetch() = default;
48   PsDataPrefetch(const PsDataPrefetch &) = delete;
49   PsDataPrefetch &operator=(const PsDataPrefetch &) = delete;
50   std::shared_ptr<PsDataChannel> ps_data_channel(const std::string &channel_name) const;
51   void WakeAllChannel() const;
52   std::map<std::string, std::shared_ptr<PsDataChannel>> ps_data_channel_map_;
53   bool cache_enable_;
54   bool data_ready_;
55   std::mutex data_mutex_;
56   std::condition_variable data_prefetch_;
57   std::condition_variable data_process_;
58   std::atomic_bool need_wait_{true};
59   std::atomic_bool invalid_data_type_{false};
60   // Ensure that the Finalize function is multithreaded safe.
61   std::mutex finalize_mutex_;
62 };
63 }  // namespace ps
64 }  // namespace mindspore
65 #endif  // MINDSPORE_CCSRC_PS_PS_CACHE_PS_DATA_PS_DATA_PREFETCH_H_
66